What is Nostr?
npub10p3…n2l2
2025-01-30 15:27:45

npub10p…mn2l2 on Nostr: We’ve got a good #DecodingBitcoin post for you today. The topic? ...

We’ve got a good #DecodingBitcoin post for you today. The topic? π„π§ππ’πšπ§π§πžπ¬π¬, AKA the order of bytes in a computer’s memory.

When we put it like that it sounds a little boring, but there’s something interesting, and dare we say π‘Žπ‘›π‘›π‘œπ‘¦π‘–π‘›π‘”, about how bitcoin handles endianness…

Before we get to that, let’s better understand what endianness is.

Imagine reading directions in different languages: while English is written and read from left to right, Arabic text flows from right to left.



Similarly, computers have two ways to store data:

1. 𝐁𝐒𝐠-𝐞𝐧𝐝𝐒𝐚𝐧 (BE): Most significant byte first
2. 𝐋𝐒𝐭𝐭π₯𝐞-𝐞𝐧𝐝𝐒𝐚𝐧 (LE): Least significant byte first



When computers with different byte orders try to communicate, they can misread each other. It’s like two people reading numbers from opposite directions.



𝐁𝐒𝐠-𝐞𝐧𝐝𝐒𝐚𝐧 𝐬𝐭𝐨𝐫𝐞𝐬 𝐭𝐑𝐞 𝐦𝐨𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž 𝐟𝐒𝐫𝐬𝐭. This is similar to how humans read numbers and Hex in most cases: starting with the most important information.

Suppose we want to store the number 12345678 (hexadecimal: 0x00BC614E) in memory. In big-endian, the bytes are stored in this order:

00 BC 61 4E



Observe that:

- The 𝐦𝐨𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž (00) is stored at the π₯𝐨𝐰𝐞𝐬𝐭 𝐦𝐞𝐦𝐨𝐫𝐲 𝐚𝐝𝐝𝐫𝐞𝐬𝐬 (00).
- The π₯𝐞𝐚𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž (4E) is stored at the 𝐑𝐒𝐠𝐑𝐞𝐬𝐭 𝐚𝐝𝐝𝐫𝐞𝐬𝐬 (03).

Big-endian is considered more "human-readable" because the data is stored in the order we naturally read it.

𝐋𝐒𝐭𝐭π₯𝐞-𝐞𝐧𝐝𝐒𝐚𝐧 𝐬𝐭𝐨𝐫𝐞𝐬 𝐭𝐑𝐞 π₯𝐞𝐚𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž 𝐟𝐒𝐫𝐬𝐭. This might feel counter intuitive to humans but is more efficient for modern processors.

Using the same number 12345678 (0x00BC614E), here's how it looks in little-endian:

4E 61 BC 00



This time, the π₯𝐞𝐚𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž (4E) is stored at the π₯𝐨𝐰𝐞𝐬𝐭 𝐦𝐞𝐦𝐨𝐫𝐲 𝐚𝐝𝐝𝐫𝐞𝐬𝐬 (00).

The 𝐦𝐨𝐬𝐭 𝐬𝐒𝐠𝐧𝐒𝐟𝐒𝐜𝐚𝐧𝐭 π›π²π­πž (00) is stored at the 𝐑𝐒𝐠𝐑𝐞𝐬𝐭 𝐚𝐝𝐝𝐫𝐞𝐬𝐬 (03).

This "reversal" of bytes is common in the Bitcoin Core codebase.

In bitcoin, most data like transaction IDs, block headers, and amounts are all in little-endian format or with the bytes reversed.



π‘π‘œπ‘‘π‘’: πΈπ‘›π‘‘π‘–π‘Žπ‘›π‘›π‘’π‘ π‘  π‘œπ‘›π‘™π‘¦ π‘Žπ‘π‘π‘™π‘–π‘’π‘  π‘‘π‘œ π‘–π‘›π‘‘π‘’π‘”π‘’π‘Ÿπ‘ . 𝐼𝑑 𝑖𝑠 π‘›π‘œπ‘‘ π‘π‘œπ‘Ÿπ‘Ÿπ‘’π‘π‘‘ π‘‘π‘œ π‘ π‘Žπ‘¦ π‘Ž β„Žπ‘Žπ‘ β„Ž 𝑖𝑠 𝑙𝑖𝑑𝑑𝑙𝑒-π‘’π‘›π‘‘π‘–π‘Žπ‘›. 𝑆𝑖𝑛𝑐𝑒 π‘‘β„Žπ‘’π‘Ÿπ‘’β€™π‘  π‘›π‘œ π‘œπ‘“π‘“π‘–π‘π‘–π‘Žπ‘™ π‘‘π‘’π‘Ÿπ‘š π‘“π‘œπ‘Ÿ π‘‘β„Žπ‘–π‘  𝑀𝑒 π‘ π‘Žπ‘¦ β€œπ‘π‘¦π‘‘π‘’ π‘ π‘€π‘Žπ‘π‘π‘’π‘‘β€ π‘œπ‘Ÿ β€œπ‘Ÿπ‘’π‘£π‘’π‘Ÿπ‘ π‘’ 𝑏𝑦𝑑𝑒𝑠”.

For readability, the bytes are swapped back to the style of big-endian when this data is displayed to humans. A block explorer is one example of where you can see this.

Bitcoin Core’s JSON-RPC interface was the first time block hashes were printed for human consumption. That was when someone decided to reverse the ordering of hash so that it looked like a human readable integer.



It turns out the real block hash, the actual sha256 value you get if you compute the hash yourself, is:

e4b1d56439d46d9070e58c4368cccc97596fa908daf101000000000000000000

The zeros are actually on the right! At first glance it looks like this is a very large number, but we know the integer value of a block hash actually gets smaller as the difficulty increases.

It's clear that the bytes are reversed and in the style of little-endian. But why? We can thank Satoshi for that. Satoshi decided to interpret the block hash as a little-endian integer. The more zeroes there are on the right side, the smaller the (little-endian) integer.

Since most modern CPUs are little-endian, bitcoin uses it to optimize performance.

However, network protocols typically use big-endian, creating a mismatch πŸ™€

Big-endian is used for network communication (network byte order). Little-endian is used for bitcoin’s internal storage.

This duality requires developers to frequently, and sometimes frustratingly, convert between the two formats when working with bitcoin data.

Have you been the victim of an endianness oversight when writing bitcoin code? It's a common source of pain for developers new to bitcoin (and even the seasoned ones!)

As covered by the transaction ID example earlier, byte order confusion can be common.



Another gotcha is length specification. When converting to little-endian, always specify the correct byte length:



Hope you learned something new about endianness today. If you enjoyed this, share it with a friend and don’t forget to follow us, nprofile1qyt8wumn8ghj7etyv4hzumn0wd68ytnvv9hxgtcppemhxue69uhkummn9ekx7mp0qqs8sccnw8c4ns0xjen9mgdgks25d46k2kcssk60uxs635zmngxh5ns3quvtq (nprofile…uvtq) !

This material is from Decoding Bitcoin, your go-to resource for understanding #bitcoin, privacy, and decentralization.

Visit https://bitcoindevs.xyz/decoding for the full lesson, and more free, interactive content.

Thanks for reading!






Author Public Key
npub10p33xu03t8q7d9nxtks63dq4gmt4v4d3ppd5lcdp4rg9hxsd0f8q7mn2l2