shadowy-pycoder / bitcoin_message_tool

Bitcoin message signing/verification tool
MIT License
4 stars 3 forks source link

[signature header] Fix array of possible header bytes #1

Closed satsie closed 1 year ago

satsie commented 1 year ago

Hi! Thank you for making such a fantastic tool. It's really helped me understand how message signing works.

This PR fixes the array of possible bytes that the signature header can be. Some of the hex values were missing and some were (mistakenly?) repeated. I've also added comments for the ranges that each set of bytes correspond to, however I didn't know what the 43 - 46 range was.

shadowy-pycoder commented 1 year ago

First of all, thank you for your time reviewing the source code. I am very glad that it helped you to better understand how bitcoin works, I also learned a lot of things working on it. As for repeating headers, it was clearly a mistake because I used as a reference the same wiki page you linked, and the following lines caused a confusion:

Compute the header byte. If r < p-n and is_even(y), set HeaderByte to 34. If r < p-n and >is_odd(y), set HeaderByte to 31. If r ≥ p-n and is_even(y), set HeaderByte to 32. If r ≥ p-n >and is_odd(y), set HeaderByte to 31.

As you can see, "31" is repeated twice, which doesn't make sense. The order of headers is not that important in the current implementation (it was in the previous one when I tried to calculate headers in the "sign" function instead of "verify" function), but also should be corrected for consistency. So, I approve your pull request.

satsie commented 1 year ago

Thanks for taking a look and merging! That bit about the header byte in the bitcoin wiki is very confusing to me :sweat_smile: Admittedly, I've been using this table from Bitcoin Stack Exchange more: https://bitcoin.stackexchange.com/a/111691/85287 Sharing in case it helps :smile_cat:

Screenshot from 2023-05-04 11-03-50

Side thought - do you know what the 43-46 range is?

shadowy-pycoder commented 1 year ago

Side thought - do you know what the 43-46 range is?

This header range is reserved for Taproot addresses.