Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: tobasco on November 13, 2019, 02:44:59 PM



Title: Why do we reverse the byte order transaction hashes and block hashes?
Post by: tobasco on November 13, 2019, 02:44:59 PM
Why do we reverse the byte order hashes when searching for transactions and blocks from bitcoin-cli and blockchain explorers?

When you hash transaction data or a block header, you get a hash digest. This hash digest is used internally in bitcoin for referring to previous TXIDs in serialized transaction data, and for previous block hashes in block headers.

However, when we come to search for a specific transaction or block from the command line, the byte order is reversed. Why is this? Wouldn't it be much easier to use the same byte order as the one that comes out of the hash function and is used internally in bitcoin?


Title: Re: Why do we reverse the byte order transaction hashes and block hashes?
Post by: DannyHamilton on November 13, 2019, 03:16:45 PM
https://en.wikipedia.org/wiki/Endianness

Quote
big-endianness is the dominant ordering in networking protocols (IP, TCP, UDP). Conversely, little-endianness is the dominant ordering for processor architectures (x86, most ARM implementations) and their associated memory. File formats can use either ordering; some formats use a mixture of both


Title: Re: Why do we reverse the byte order transaction hashes and block hashes?
Post by: tobasco on November 13, 2019, 04:30:39 PM
https://en.wikipedia.org/wiki/Endianness

Quote
big-endianness is the dominant ordering in networking protocols (IP, TCP, UDP). Conversely, little-endianness is the dominant ordering for processor architectures (x86, most ARM implementations) and their associated memory. File formats can use either ordering; some formats use a mixture of both

Thanks for the Wiki link, but I'm still unsure.

Everything inside serialized transaction data can be considered to be in little-endian, where the txids used are in the default byte order that comes out of the hash function. These are sometimes referred to being in little-endian too.

So why do we convert hashes to big-endian when searching for transactions and blocks, when internally everything else is in little-endian?


Title: Re: Why do we reverse the byte order transaction hashes and block hashes?
Post by: pooya87 on November 14, 2019, 04:44:41 AM
nobody knows why because this is a decision that the creator (Satoshi) made and to my knowledge there is no comment from him saying why.
however, it probably has something to do with the fact that hashes are interpreted as "numbers" (uint256), for example a block header hash is interpreted as a number to compare it with the target for the difficulty calculation purpose. and in writing we write numbers in big endian notation hence the endian-swap for representing them in GUI and using the non-reversed in code under the hood.
probably since uint256 is used elsewhere too (such as representation of a transaction ID) that reversing effect also happens there.


Title: Re: Why do we reverse the byte order transaction hashes and block hashes?
Post by: tobasco on November 14, 2019, 11:56:40 AM
nobody knows why because this is a decision that the creator (Satoshi) made and to my knowledge there is no comment from him saying why.
however, it probably has something to do with the fact that hashes are interpreted as "numbers" (uint256), for example a block header hash is interpreted as a number to compare it with the target for the difficulty calculation purpose. and in writing we write numbers in big endian notation hence the endian-swap for representing them in GUI and using the non-reversed in code under the hood.
probably since uint256 is used elsewhere too (such as representation of a transaction ID) that reversing effect also happens there.

Thank you.