sipa / bech32

Code snippets and analysis of the Bech32 format
191 stars 107 forks source link

bech32 decoder looks for the first '1' as the HRP separator, not the last '1' as specified #44

Closed daira closed 5 years ago

daira commented 5 years ago

https://github.com/sipa/bech32/blob/60848c2e9f214910d98636773fae103ea948be6f/ref/c/segwit_addr.c#L95-L99

https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32 says that the last '1' in the string is the HRP separator. The reference code is inconsistent with that and with Bitcoin Core.

sipa commented 5 years ago

I don't think so; (*data_len) increments, but is used as a negative in the index to input: input[(input_len - 1) - *data_len], so it counts backwards.

There is also a unit test for an HRP that includes a "1", which both the C and C++ code pass.

daira commented 5 years ago

You're right, sorry for the invalid report.