12

When converting uint256 (the datatype used to represent hashes) to string in Bitcoin Core, the bytes are reversed. When debugging functional tests, this can create potential confusion when comparing hashes passed to script on the Python side and hashes naively printed from within Bitcoin Core's cpp.

Why are the bytes reversed when getting HexStr() for hashes in Bitcoin Core?

2

1 Answer 1

14

From sipa on IRC (2023-01-06):

jamesob: Probably dumb question, but why does uint256::ToString() (-> HexStr()) print out the blob in reversed byte order?

sipa: Because that's what it has always done, really.

sipa: And people expect txids and block hashes in hex notation to be reserved.

sipa: Historically, that was because txids/block hashes were 256-bit numbers constructed by interpreting the double-sha256 output as a little-endian integer, and then printing it out for human consumption in hex (where humans expect numbers in big endian).

sipa: But now uint256 is just a byte array wrapper, the behavior is maintained because block hashes and txids are always printed in reverse.

sipa: (block hashes are still interpreted a little-endian numbers for the purpose of comparing PoW to the target, but that's handled by the arith_uint256 class now)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.