<p>Recall that vanitygen allows you to list several patterns and search for addresses that match any of them.  It looks to me like for each prefix, we compute the range of hash160 values that, after base58 encoding, would match the desired prefix.  (Conveniently, the checksum only affects the end of the address, so we don't have to worry about it.) These ranges are put into an <a href="https://en.wikipedia.org/wiki/AVL_tree" rel="nofollow noreferrer">AVL tree</a>.  Then <a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L979" rel="nofollow noreferrer"><code>vg_prefix_avl_search</code></a> takes a generated hash160 and searches the tree to see whether it matches any of the desired ranges.  Note this means that base58 encoding doesn't actually have to be done at all for addresses that won't match.</p>

<p>If we are in regex matching mode instead, then the relevant code is in <a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L1712" rel="nofollow noreferrer"><code>vg_regex_test</code></a> which is pretty straightforward: compute the base58 encoding and match against the given regexes.</p>

CC BY-SA 3.0
2018-01-26T14:05:54.650Z
2018-01-26T14:05:54.650Z
Nate Eldredge
<p>I've been trying the find the code in Vanitygen that does checks whether the generated address starts with the expected pattern.</p>

<p>Vanitygen repo: <a href="https://github.com/samr7/vanitygen" rel="nofollow noreferrer">https://github.com/samr7/vanitygen</a></p>

<p>What I currently know:</p>

<ul>
<li><p><a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L505" rel="nofollow noreferrer">vg_output_match_console</a> is called when the generated address starts with the expected pattern.</p></li>
<li><p><a href="https://github.com/samr7/vanitygen/blob/master/pattern.c#L239" rel="nofollow noreferrer">vg_exec_context_calc_address</a> generates the address as a byte array, which is probably used.</p></li>
<li><p><a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L686" rel="nofollow noreferrer">get_prefix_ranges</a> finds the range of the bignum of the address we want to generate, such that all bignums in that range start with the expected prefix.</p></li>
<li><p><a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/util.c#L239" rel="nofollow noreferrer">vg_encode_address</a> generates the address in Base58, however is run only at the end.</p></li>
<li><p><a href="https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L979" rel="nofollow noreferrer">vg_prefix_avl_search</a> seems to be an important part (that does what?)</p></li>
</ul>

<vanitygen><source-code>
(Shouldn't be too hard) Where vanitygen checks if the random address starts with the expected pattern?
MCCCS
# (Shouldn't be too hard) Where vanitygen checks if the random address starts with the expected pattern?

(Asked by: username_0 on 2018-01-26)

I've been trying the find the code in Vanitygen that does checks whether the generated address starts with the expected pattern.

Vanitygen repo: <https://github.com/samr7/vanitygen>

What I currently know:

* [vg\_output\_match\_console](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L505) is called when the generated address starts with the expected pattern.
* [vg\_exec\_context\_calc\_address](https://github.com/samr7/vanitygen/blob/master/pattern.c#L239) generates the address as a byte array, which is probably used.
* [get\_prefix\_ranges](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L686) finds the range of the bignum of the address we want to generate, such that all bignums in that range start with the expected prefix.
* [vg\_encode\_address](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/util.c#L239) generates the address in Base58, however is run only at the end.
* [vg\_prefix\_avl\_search](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L979) seems to be an important part (that does what?)

**Tags:** vanitygen, source-code


---

## 1 answers

### Answer by username_1 on 2018-01-26. Score: 4 | Accepted answer

Recall that vanitygen allows you to list several patterns and search for addresses that match any of them. It looks to me like for each prefix, we compute the range of hash160 values that, after base58 encoding, would match the desired prefix. (Conveniently, the checksum only affects the end of the address, so we don't have to worry about it.) These ranges are put into an [AVL tree](https://en.wikipedia.org/wiki/AVL_tree). Then [`vg_prefix_avl_search`](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L979) takes a generated hash160 and searches the tree to see whether it matches any of the desired ranges. Note this means that base58 encoding doesn't actually have to be done at all for addresses that won't match.

If we are in regex matching mode instead, then the relevant code is in [`vg_regex_test`](https://github.com/samr7/vanitygen/blob/cd1a7282431dcf7e522777976aa18728ee5bb7be/pattern.c#L1712) which is pretty straightforward: compute the base58 encoding and match against the given regexes.


---

