5

I've noticed just from studying the raw hex values of transactions that many components are formatted in hexidecimal little endian.

For example, the amount specified in an output will be formatted as b6f5050000000000 instead of 0005F5B6, which represents 390,582 satoshi.

To be honest, I have a basic general understanding of big-endian vs little-endian but why is the little-endian format used? And why not just use the normal hex string format like 0005F5B6 in the example above?

2
  • 1
    Does this answer your question? Why does the Bitcoin protocol use the little-endian notation? Commented May 24, 2023 at 8:50
  • The replies on this thread and from the link you sent me both give me a better understanding of it. And it does seem like it has more to deal with lower level programming which I am not an expert in. Nonetheless, good education for myself.
    – jiamijiang
    Commented May 25, 2023 at 3:06

4 Answers 4

5

To add onto RedGrittyBrick's answer, little endianness is a result of using little endian CPUs, and then sending data as it is stored in memory.

On little endian architectures, integers in memory are stored as little endian. When preparing messages to be sent over the network, Satoshi's serialization functions would simply copy the memory directly, thus inheriting the way that such data is stored in memory.

I believe the only exception to this is the uint256 implementation which is used for storing all 256 bit hashes. This appears to be deliberately written to interpret these integers as little endian.

Otherwise, the 0.1.0 client was written in a way that would not have been compatible with big endian architectures. If it were compiled for and ran on a big endian CPU, I think it would incorrectly deserialize integers. However the endianness used in the vast majority of computers is little endian, so this was unlikely to have caused an issue, not to mention that Windows isn't available on any big endian architectures anyways.

4

It is probably because Satoshi Nakamoto was sometimes rather careless.

It is a deviation from established standards that I would say has no sufficiently good reason in this particular case.

Just as interoperability among different Bitcoin node implementations is guided by Bitcoin Improvement Proposals (BIPs), interoperability among computers on the internet is guided by a system of Requests For Comments (RFCs). It seems likely that the BIP process was loosely modeled on the earlier RFC process.

RFC1700 by Reynolds and Postel in 1994 says

When a multi-octet quantity is transmitted the most significant octet is transmitted first.

That is to say that "Network Order" is big-endian.

Computer hardware (the CPU etc) can be little-endian or big-endian. An application running on a little-endian machine is expected to translate to and from big-endian when communicating over the Internet. networking libraries provide for this. Most network protocols and applications do this.

Satashi Nakamoto failed to do this properly.

2
  • 2
    I should probably add that clumsy mortal imperfect heroes are still heroes. Commented May 23, 2023 at 15:23
  • 2
    There is a very obvious good reason. Byte order conversions take time and the vast majority of computers impelementing this protocol are little-endian. Commented May 24, 2023 at 7:53
2

There isn't any other reason for use of little-endian format than just the agreement made among programmers who implemented it. Bitcoin would have worked equally well with big-endian format.

1

As far as I know Satoshi never explained why both big endian and little endian are used in the Bitcoin protocol. Some of the reasons for using little endian generally are discussed here but I don't know to what extent they apply to its usage in Bitcoin.

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.