24

Why does the Bitcoin use the litte-endian notation? It is less common than big-endian notation, thus when programming low-level applications working with Bitcoin protocol, one quite often has to create custom operations for handling the endianness switching. Is there some specific reason why this notation was chosen?

0

4 Answers 4

13

Most likely it is a small design error, but there is big argument about it at bitcointalk.org:What would you change about the Bitcoin protocol?.

17

Modern computers almost always use little-endian internally, so this choice improves speed. If Bitcoin used network byte order, then order conversions would be necessary for every message sent or received.

Hashes are defined by the standards as being big-endian, and crypto libraries deal with them in that form, so hashes are transmitted in big-endian. Bitcoin displays hashes in little-endian because Bitcoin sometimes considers hashes to be little-endian integers instead of strings.

1
  • 2
    When you say Bitcoin displays hashes, do you mean how it's commonly displayed on Bitcoin software, tools, and websites? Or are you talking about something actually in the bitcoin core code for displays hashes a certain way?
    – Jacob Ford
    May 15, 2018 at 17:03
9

Bitcoin transaction and block hashes are considered little-endian when treating them as integers. On the network they are just 32-byte sequences in the order they are generated by the hash function.

In both transaction and block hashes bytes are reversed when displayed by Bitcoin software and websites in hexadecimal form. This makes blocks show up in big-endian form, which is the way we humans write numbers down.

I would suppose that the idea of reversing bytes comes from the fact that block hashes in Bitcoin are treated as numbers for the purposes of difficulty calculations, meaning the hash must be below certain threshold in order to pass. It is natural for a human to expect such a number to be displayed on screen with leading zeroes. However this is not a very solid reason, since block hashes could have been interpreted as big-endian when converting to integer, while paying a tiny performance penalty.

As for transaction hashes, I have no good explanation as to why they are also reversed. Perhaps it is just for consistency.

6

Modern CPUs also typically have byte-swapping load and store instructions, or at least a byte swap register instruction, so that effective programmers (and optimizing compilers) may eliminate, or nearly eliminate, the overhead of endianness compensation.

This "small design error" may be a simple case of "premature optimization".

1
  • 8
    Well, a premature optimization is a design error ^^
    – o0'.
    Nov 27, 2011 at 19:28

Your Answer

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

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