Compact Size Encoding
Bitcoin's variable-length integer format for lengths and counts inside serialised data.
Basically
Compact size encoding is Bitcoin's way of writing counts and lengths in transaction data. Small numbers use fewer bytes than large numbers.
Key Takeaways
- Compact size encoding is Bitcoin's variable-length integer format for counts and lengths in raw transactions. It uses 1 byte for values 0 to 252, 3 bytes for values up to 65,535, 5 bytes for values up to roughly 4.3 billion, and 9 bytes for larger values.
- Every Bitcoin transaction uses compact size integers to specify the number of inputs and outputs, the length of each script, and the count and size of witness items.
- Compact size encoding is distinct from Bitcoin Core's internal VarInt format. Both are variable-length integer encodings, but they use different schemes and appear in different contexts.
What Is Compact Size Encoding?
Compact size encoding, sometimes called CompactSize or "varint" in older documentation, is Bitcoin's variable-length unsigned integer format for transaction serialisation and peer-to-peer protocol messages. The format adapts its byte width to the value instead of using a fixed-width integer for every count or length: small numbers take 1 byte, while larger numbers use a prefix byte followed by 2, 4, or 8 additional bytes.
Satoshi Nakamoto's original Bitcoin implementation introduced this encoding. Its rules are part of Bitcoin's consensus because they are embedded in the raw transaction format: every node must parse and validate these integers identically or risk chain splits. Bitcoin Core's reference implementation in serialize.h, specifically the ReadCompactSize and WriteCompactSize functions, defines the format rather than a BIP.
How It Works
The first byte of a compact size integer signals how many bytes follow. Compact size encoding has four ranges:
Encoding Rules
The byte values 0xFD (253), 0xFE (254), and 0xFF (255) are reserved prefix markers and never represent a value directly. A first byte of 0xFD tells the parser to read the next 2 bytes as a little-endian unsigned integer. Single-byte encoding therefore stops at 252, not 255.
Encoding Examples
Different values serialise as follows in compact size format. All multi-byte values use little-endian byte order:
Canonical Encoding
Bitcoin Core enforces canonical encoding during deserialisation because the format allows redundant representations. The value 100, for example, could technically be encoded as 3 bytes using the 0xFD prefix, but ReadCompactSize rejects non-canonical forms when a shorter encoding would suffice. These checks prevent malleability issues and ensure deterministic serialisation.
The value 100 encoded as [0xFD, 0x64, 0x00] instead of the canonical [0x64] would be rejected. Any transaction containing a non-canonical compact size integer is treated as invalid.
Where Compact Size Appears in Transactions
Compact size integers appear throughout the raw Bitcoin transaction format. Every transaction includes multiple compact size fields:
Transaction Structure
A serialised Bitcoin transaction post-SegWit contains compact size fields. These are the fields:
SegWit transactions include additional compact size fields in the witness structure. The witness structure contains these fields:
Block serialisation also uses compact size encoding for the transaction count at the start of a block's transaction list. Peer-to-peer protocol messages such as inv, getdata, and addr use it as well.
Practical Size Impact
Typical transactions gain minimal overhead from compact size fields, and a transaction with fewer than 253 inputs or outputs uses just 1 byte for each count field. Script lengths under 253 bytes also use 1 byte, which covers virtually all standard scripts. Bitcoin Script imposes a 10,000-byte limit on script sizes, so a maximum-length script needs a 3-byte compact size prefix, which is still very compact.
Compact size fields contribute to overall transaction weight and therefore affect the fee a transaction must pay. Witness compact size fields benefit from the witness discount, counting as only 1 weight unit per byte rather than 4.
CompactSize vs. Bitcoin Core VarInt
Bitcoin Core's two different variable-length integer encodings are both sometimes called "varint", which often causes confusion. The encodings are not interchangeable.
Bitcoin Core's VarInt uses a 7-bit encoding scheme in which the most significant bit of each byte indicates whether more bytes follow. The scheme is more space-efficient for certain value ranges but was designed for internal storage, not the consensus-critical wire format. You should check which encoding Bitcoin documentation or source code references: transaction data always uses CompactSize, while the chainstate database uses VarInt.
Transaction Parsing and Construction
Software that creates or reads raw Bitcoin transactions must implement compact size encoding correctly. Wallet software, block explorers, and node implementations rely on it to determine where one field ends and the next begins in the byte stream. Incorrect parsing would misinterpret the entire transaction structure.
Script and Witness Serialization
Witness data for SegWit or Taproot transactions prefixes each witness element with a compact size integer. A standard P2WPKH input has two witness items, a signature and a public key, and each is preceded by its compact size length:
Fee Estimation and Size Calculation
Accurate fee estimation requires the exact serialised size of a transaction, including all compact size fields. Tools like the Bitcoin Optech transaction size calculator account for compact size overhead when computing virtual bytes and transaction weight. Most standard transactions use 1 byte for each compact size field, but transactions with more than 252 inputs or outputs use 3-byte count fields.
Protocol Message Framing
The Bitcoin peer-to-peer protocol uses compact size integers extensively in messages that carry lists. Inventory vectors in inv messages, address lists in addr messages, and header lists in headers messages all begin with a compact size count.
Implementation Correctness
Missing canonical encoding checks can create subtle bugs. A parser that accepts non-canonical compact size integers may produce valid-seeming results but disagree with Bitcoin Core about transaction validity. Alternative node implementations could then accept invalid transactions or reject valid ones, potentially causing chain forks.
Denial-of-Service Vectors
Compact size integers can theoretically represent values up to 2^64 − 1, so a malicious peer could send a message claiming to contain billions of items. Bitcoin Core guards against this by capping ReadCompactSize at a maximum value of 0x02000000, or about 33 million, and by imposing message-level size limits. Implementations parsing compact size integers must include similar bounds checks to avoid memory exhaustion.
Byte-Order Sensitivity
Multi-byte compact size values use little-endian byte order, matching Bitcoin's general serialisation convention. Developers working on big-endian systems or porting code between languages must handle byte-order conversion carefully. Swapped byte order would produce wildly incorrect values and corrupt the entire transaction parse.
Confusion with Other Formats
The naming overlap between CompactSize and Bitcoin Core's internal VarInt encoding has caused bugs in third-party software. Other systems, including Protocol Buffers and LEB128, use the term "varint" for entirely different encodings. You should always verify which specific format a given context expects when working with Bitcoin serialisation.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.