TeachMeBitcoin

The 'Bits' Compact Format

From TeachMeBitcoin, the free encyclopedia ⏱️ 3 min read

The 'Bits' Field: Unpacking Compact Target Serialization in Block Headers

To keep the block header compact and save valuable storage space, the 32-byte (256-bit) target is not written to the header in its full layout. Instead, it is compressed into a highly efficient 4-byte (32-bit) floating-point-like representation stored in the bits field.

This guide details the structure of the bits field, the decompression formula used to rebuild the full 256-bit target, and a step-by-step parsing walkthrough.


📐 1. The Structure of the 4-Byte bits Field

The 4-byte bits field represents a floating-point number using a base-256 scientific notation. It is split into two components: an Exponent (1 byte) and a Coefficient (3 bytes, sometimes called the mantissa).

                            THE 4-BYTE 'BITS' LAYOUT

                        Bits: 0x1703a3d5 (4 Bytes)

                  1 Byte: Exponent          3 Bytes: Coefficient
                     ┌──────────┐         ┌──────────────────────┐
                     │   0x17   │         │       0x03a3d5       │
                     └──────────┘         └──────────────────────┘

🧮 2. The Decompression Formula

To decompress bits into the full 256-bit target integer, nodes apply this mathematical equation:

$$\text{Target} = \text{Coefficient} \times 256^{(\text{Exponent} - 3)}$$

Edge Cases and Sign Flags:


🛠️ 3. Step-by-Step Decompression Walkthrough

Let's decompress the real block header bits value: 0x1703a3d5.

Step A: Extract the Exponent and Coefficient

Step B: Plug the Values into the Formula

$$\text{Target} = \texttt{0x03a3d5} \times 256^{(23 - 3)}$$ $$\text{Target} = \texttt{0x03a3d5} \times 256^{20}$$

Step C: Execute the Multiplication

In hexadecimal, multiplying by $256$ is equivalent to shifting the value left by two hex places (or adding two trailing 00 characters): * $256^1 = \text{shift by } 1 \text{ byte} \rightarrow \text{adds } \texttt{00}$ * $256^{20} = \text{shift by } 20 \text{ bytes} \rightarrow \text{adds } 20 \text{ trailing } \texttt{00}$ bytes (40 hex characters)

  1. Take the coefficient: 03a3d5
  2. Pad left with zeroes to make it 32 bytes (64 hex characters) total: $$\texttt{000000000000000000000000000000000000000000000000000000000003a3d5}$$
  3. Shift left by 20 bytes (by moving the digits left and appending 20 trailing null bytes \x00): $$\text{Target} = \texttt{00000000000000000003a3d50000000000000000000000000000000000000000}$$

This decompressed 256-bit target is used by the node's verification engine to evaluate incoming block header hashes.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!