Bitcoin Forum
May 21, 2024, 03:58:42 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Why do we reverse the byte order transaction hashes and block hashes?  (Read 182 times)
tobasco (OP)
Member
**
Offline Offline

Activity: 61
Merit: 12


View Profile WWW
November 13, 2019, 02:44:59 PM
 #1

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?

DannyHamilton
Legendary
*
Offline Offline

Activity: 3388
Merit: 4653



View Profile
November 13, 2019, 03:16:45 PM
Merited by gmaxwell (1), hugeblack (1)
 #2

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
tobasco (OP)
Member
**
Offline Offline

Activity: 61
Merit: 12


View Profile WWW
November 13, 2019, 04:30:39 PM
 #3

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?

pooya87
Legendary
*
Offline Offline

Activity: 3458
Merit: 10572



View Profile
November 14, 2019, 04:44:41 AM
Merited by fillippone (1)
 #4

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
tobasco (OP)
Member
**
Offline Offline

Activity: 61
Merit: 12


View Profile WWW
November 14, 2019, 11:56:40 AM
 #5

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.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!