Post
Topic
Board Development & Technical Discussion
Re: C or C++ code for validating Bitcoin addresses
by
DannyHamilton
on 08/07/2016, 18:02:15 UTC
What confuses me the most is that hex 0000000000000000000000000000000000000000 is 1111111111111111111114oLvT2 (27 characters) but hex 0000000000000000000000000000000000000001 is 11111111111111111111BZbvjr (26 characters).

20 zeroes in the beginning of the BTC address payoad results in an address of length 27.
19 zeroes in the beginning of the BTC address payload and then byte with decimal value of 1 results in an address of length 26.

But now all of sudden 17 zeroes in the beginning followed by the byte with value 1 results in an address of length 27 again.

What logic is there? It is said that the number of 1s in the beginning of the address depends on the number of zeroes in the beginning of the address payload but this dependency is no way trivial.

Lets walk through the steps and see what happens:

Starting with an RIPEMD-160 hash of 20 bytes that are all 0's...
0000000000000000000000000000000000000000

Add a version byte in front (in the case of a P2PKH address, that would be a byte with value 0).

000000000000000000000000000000000000000000

Now we have 21 bytes that are all 0's.

Calculate a checksum on this value:

Code:
SHA256(SHA256(000000000000000000000000000000000000000000)) =
94a00911c4da27f9271727ffa7a14d8d5588fc0fff9964340ec4065f387e622b

Append the first 4 bytes (8 characters) of the checksum to the RIPEMD-160 hash with version byte:
00000000000000000000000000000000000000000094a00911

Temporarily ignore leading zero bytes:
94a00911

Convert the value from hex to base58:
0x94a00911 =
4oLvT2 (base 58)

Each LEADING 00 BYTE is replaced with a single 1:
Code:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1

Concatenate 21 ones with 6 base58 digits:
1111111111111111111114oLvT2

21 "ones" plus 6 base58 digits = 27 characters



Now lets try the same with 19 zeros and a bytes with value 1...

0000000000000000000000000000000000000001

Add a version byte in front (in the case of a P2PKH address, that would be a byte with value 0).

000000000000000000000000000000000000000001

Now we have 20 bytes that are all 0's, followed by a byte that is represented in hex as "01"

Calculate a checksum on this value:

Code:
SHA256(SHA256(000000000000000000000000000000000000000001)) =
9d35b5b9d5befcf2d6b89994f7f64279b0645d5d4a5f1a6fa2dcc615bbed04ef

Append the first 4 bytes (8 characters) of the checksum to the RIPEMD-160 hash with version byte:
0000000000000000000000000000000000000000019d35b5b9

Temporarily ignore leading zero bytes:
019d35b5b9

Convert the value from hex to base58:
0x019d35b5b9 =
BZbvjr (base 58)

Each LEADING 00 BYTE is replaced with a single 1:
Code:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1

Concatenate 20 ones with 6 base58 digits:
111111111111111111111BZbvjr

20 "ones" plus 6 base58 digits = 26 characters

(notice that the number of leading 0 bytes was decreased by 1 because the last byte was now a 01, however the number of base58 digits didn't increase since both 0x94a00911 and 0x019d35b5b9 can be represented with 6 base58 digits (4oLvT2 and BZbvjr respectively).