TeachMeBitcoin

VarInt: The Anchor Guide to Variable-Length Integers

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

VarInt: The Anchor Guide to Variable-Length Integers

IMPORTANT

Executive Summary: A VarInt (Variable-Length Integer), technically known as CompactSize, is a serialization format used by Bitcoin to store integers with maximum space efficiency. Because most "counts" in Bitcoin (like the number of inputs or script lengths) are small numbers, VarInt allows them to be stored in just a single byte. For larger numbers, it uses a "Sentinel Byte" system to signal that more bytes follow. This optimization is the primary reason the Bitcoin blockchain remains manageable in size.


🔍 Why This Module Matters

Every byte on the Bitcoin blockchain costs money (fees) and storage (node hardware). If Bitcoin used a standard 8-byte integer for every "count" field, the blockchain would be nearly double its current size today. VarInt is the "Compression" engine of the protocol. This module will deconstruct the "Sentinel Byte" logic, explain the four tiers of integer magnitude, and show you how to spot a VarInt inside a raw hex transaction dump.


🏛️ The Sentinel Logic: Scaling on Demand

The brilliance of VarInt is that the First Byte acts as a traffic controller.

Tier 1: The "Lite" Mode (Value < 253)

Tier 2: The "Extended" Modes (Sentinels)

If the first byte is 0xFD, 0xFE, or 0xFF, it is a Sentinel. It means: "Stop! Don't use my value. Look at the bytes that follow me."

Sentinel Followed By Max Value Typical Use Case
0xFD 2 Bytes 65,535 Long Output Scripts (Multisig).
0xFE 4 Bytes 4,294,967,295 Large Mempool counts.
0xFF 8 Bytes $1.8 \times 10^{19}$ Theoretical max (Total Satoshis).

⚙️ Visualizing VarInt in Raw Hex

Let's look at how the number 500 is stored in a Bitcoin transaction.

  1. 500 is greater than 252, so we need a sentinel.

  2. 500 fits in 2 bytes, so we use the 0xFD sentinel.

  3. The Hex: FD F4 01 (Little-Endian representation of 500).

graph TD
 A[Read First Byte] --> B{Byte < 0xFD?}
 B -- YES --> C[Value = Byte]
 B -- NO --> D{Is it 0xFD?}
 D -- YES --> E[Read next 2 bytes]
 D -- NO --> F{Is it 0xFE?}
 F -- YES --> G[Read next 4 bytes]
 F -- NO --> H[Read next 8 bytes]
 style C fill:#9f9,stroke:#333,stroke-width:2px
 style E fill:#f96,stroke:#333,stroke-width:2px

🛠️ Where are VarInts Used?

You will encounter VarInts every time you parse a transaction:

  1. TX_IN Count: The number of inputs in the transaction.

  2. TX_OUT Count: The number of outputs.

  3. Script Length: The number of bytes in the scriptSig or scriptPubKey.

  4. Message Size: The length of P2P network messages.

Note: VarInt is not used for the block size itself (which is a fixed 4-byte uint32) or for the transaction version. It is only used for "Variable-Length" fields.


🛡️ VarInt vs. Fixed-Length: The Performance Trade-off

Feature Fixed-Length (uint32) VarInt (CompactSize)
Size Always 4 Bytes 1, 3, 5, or 9 Bytes
Complexity Low (Easy to parse) High (Conditional logic)
Efficiency Poor (Wastes space) Excellent (Saves space)
Protocol Block Headers, Version Transactions, P2P Traffic

🎯 Learning Objectives for this Module

By the end of this module, you will be able to:

  1. Define a VarInt and its primary purpose in the Bitcoin protocol.

  2. Identify the four sentinel bytes and the magnitude of data they represent.

  3. Explain why the first byte is the "Key" to decoding a VarInt.

  4. Describe the cumulative impact of VarInt on blockchain storage size.

  5. Differentiate between fields that use VarInt and those that use fixed-length integers.


🗺️ Module Roadmap: What's Next?

Now that we've seen the "Space-Saving" logic, we will look at the implementation:

  1. VarInt Logic: A deep dive into the CompactSize specification.

  2. Fixed vs. VarInt: Why some fields were chosen to be variable.

  3. VarInt History: How the format evolved from Satoshi's original code.

  4. Python VarInt Auditor: Writing a script to read and write VarInt bytes.


🎓 Summary

VarInt is the "Micro-Optimizer" of the Bitcoin network. It is a simple yet powerful example of how every bit and byte of the protocol is tuned for long-term sustainability. By mastering the logic of variable-length integers, you are understanding the engineering discipline required to build a global financial system that can survive for centuries on limited resources.

☕ 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!