# CompactSize

> How Pearl writes counts and lengths: one byte below 253, then 0xfd, 0xfe or 0xff prefixes. With every CompactSize in a real transaction.

Source: https://pearlcodex.com/technical/general/compact-size/ · Last updated: 2026-11-02 · Checked against Pearl's source code at v1.5.5

A transaction is full of lists: a list of inputs, a list of outputs, scripts of different lengths. A reader needs to know how long each one is before reading it.

So each list or script starts with a small number saying how many items or bytes follow. That number is a [CompactSize](https://pearlcodex.com/glossary/#compact-size), sometimes called a varint.

The trick is that it’s compact. Most counts are tiny, so most CompactSizes are one byte. Only a big number pays for more bytes.

## Try it

Type a number to see its CompactSize bytes, or type bytes to decode them.

_Interactive: CompactSize converter. Try it on the page: <https://pearlcodex.com/technical/general/compact-size/>_

## How does it work?

The first byte decides everything. If it’s below `0xfd` (253), it _is_ the number. If it’s one of the three marker bytes, it tells you how many bytes to read next.

| First byte   | Then                   | Range                   | Total size |
| ------------ | ---------------------- | ----------------------- | ---------- |
| `00` to `fc` | nothing                | 0 to 252                | 1 byte     |
| `fd`         | 2 bytes, little-endian | 253 to 65,535           | 3 bytes    |
| `fe`         | 4 bytes, little-endian | 65,536 to 4,294,967,295 | 5 bytes    |
| `ff`         | 8 bytes, little-endian | 4,294,967,296 and up    | 9 bytes    |

- **Size:** 1, 3, 5 or 9 bytes
- **Type:** Unsigned integer
- **Format:** Marker byte, then little-endian
- **Range:** 0 to 2⁶⁴ − 1

A few worked examples, all computed with the codex’s own encoder:

| Number | CompactSize      | Why                                 |
| ------ | ---------------- | ----------------------------------- |
| 2      | `02`             | below 253: one byte                 |
| 252    | `fc`             | the largest one-byte value          |
| 253    | `fd fd 00`       | the first value that needs a marker |
| 1,000  | `fd e8 03`       | 0x03e8, little-endian               |
| 70,000 | `fe 70 11 01 00` | 0x011170 needs four bytes           |

The bytes after the marker are [little-endian](https://pearlcodex.com/technical/general/byte-order/), smallest byte first, like every other integer in Pearl.

## Where are the CompactSizes in a real transaction?

Here is a real Pearl payment from block 130,825: one input, two outputs.

**A typical payment: 1 input, 2 outputs · 205 bytes**

```
01000000 00 01 01 5939557091bae792be66f26cbd83fc78f16a690a854109e8a1d115e253812133 ab020000 00 ffffffff 02 b0359f1100000000 22 5120d7ab02c96a342bdddb5d07b9f3ebd6c04e077ff41418110b9a1e5997439416b2 00c2eb0b00000000 22 512062e9da3682aab288392cf7218bafc36287288abb3ce200c5d1a50aa234073b2b 01 40 4686c7014749649b25555874c7772ecb7a44e85454893ed5888db11ebcdc82a22d74ce1a1671d02810f5ba1dca3277409e95a653ba9644099f7da26be7a86ba2 00000000
```

| Field                                             | Bytes          | Value                                                                                                                                                                                                                  |
| ------------------------------------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Version (`version`)                               | 4, offset 0    | 1 · int32 little-endian. Pearl wallets and pearld coinbases use version 1; relay policy accepts 1-2.                                                                                                                   |
| Marker (`marker`)                                 | 1, offset 4    | 0x00 · Segwit marker (BIP-144): says witness data follows the outputs.                                                                                                                                                 |
| Flag (`flag`)                                     | 1, offset 5    | 0x01 · Witness flag; 0x01 is the only value Pearl accepts.                                                                                                                                                             |
| Input count (`vin.length`)                        | 1, offset 6    | 1 · CompactSize number of inputs.                                                                                                                                                                                      |
| Previous txid (`vin[0].txid`)                     | 32, offset 7   | 33218153e215d1a1e80941850a696af178fc83bd6cf266be92e7ba9170553959 · The transaction being spent, stored byte-reversed (internal order); shown here in normal display order.                                             |
| Output index (`vin[0].vout`)                      | 4, offset 39   | 683 · Which output of that transaction is spent (uint32 LE).                                                                                                                                                           |
| scriptSig length (`vin[0].scriptSig.length`)      | 1, offset 43   | 0 · Always 0 for non-coinbase inputs in Pearl: every spend uses the witness instead.                                                                                                                                   |
| Sequence (`vin[0].sequence`)                      | 4, offset 44   | 0xffffffff · Final (0xffffffff): no RBF signal, no relative locktime, and locktime is ignored for this input. Pearl wallets use this.                                                                                  |
| Output count (`vout.length`)                      | 1, offset 48   | 2 · CompactSize number of outputs.                                                                                                                                                                                     |
| Amount (`vout[0].value`)                          | 8, offset 49   | 295,646,640 grains (2.95646640 PRL) · int64 little-endian, in grains (1 PRL = 100,000,000 grains).                                                                                                                     |
| Script length (`vout[0].scriptPubKey.length`)     | 1, offset 57   | 34 · CompactSize length of the locking script.                                                                                                                                                                         |
| Locking script (`vout[0].scriptPubKey`)           | 34, offset 58  | prl1p674s9jt2xs4amk6aq7ul867kcp8qwll5zsvpzzu6revewsu5z6eqv9d5z4 · OP_1 OP_PUSHBYTES_32 \<output key Q>: a Taproot output.                                                                                              |
| Amount (`vout[1].value`)                          | 8, offset 92   | 200,000,000 grains (2.00000000 PRL) · int64 little-endian, in grains (1 PRL = 100,000,000 grains).                                                                                                                     |
| Script length (`vout[1].scriptPubKey.length`)     | 1, offset 100  | 34 · CompactSize length of the locking script.                                                                                                                                                                         |
| Locking script (`vout[1].scriptPubKey`)           | 34, offset 101 | prl1pvt5a5d5z42egswfv7uscht7rv2rj3z4m8n3qp3w35592ydq88v4seuhn70 · OP_1 OP_PUSHBYTES_32 \<output key Q>: a Taproot output.                                                                                              |
| Witness items (`witness[0].length`)               | 1, offset 135  | 1 · Number of witness stack items for input 0.                                                                                                                                                                         |
| Schnorr signature length (`witness[0][0].length`) | 1, offset 136  | 64 · CompactSize length of this witness item.                                                                                                                                                                          |
| Schnorr signature (`witness[0][0]`)               | 64, offset 137 | 4686c7014749649b25555874c7772ecb7a44e85454893ed5888db11ebcdc82a22d74ce1a1671d02810f5ba1dca3277409e95a653ba9644099f7da26be7a86ba2 · 64-byte BIP-340 signature (R.x ‖ s) with SIGHASH_DEFAULT: a Taproot key-path spend. |
| Locktime (`locktime`)                             | 4, offset 201  | 0 · 0: no time lock.                                                                                                                                                                                                   |

- **txid:** d919e02a4adfd739ceef31d98803f0ddd24e1b1fa67b0fe572cdec42c1b384c9
- **Size:** 205 bytes · 154 vbytes
- **Block:** 130,825
- **Fee:** 7,915 grains

It contains seven CompactSizes, and every one of them fits in a single byte:

| Offset | Byte | Means                                                     |
| ------ | ---- | --------------------------------------------------------- |
| 6      | `01` | 1 input                                                   |
| 43     | `00` | the input’s scriptSig is empty (always, for Pearl spends) |
| 48     | `02` | 2 outputs                                                 |
| 57     | `22` | the first output’s script is 34 bytes                     |
| 100    | `22` | the second output’s script is 34 bytes                    |
| 135    | `01` | the input’s witness has 1 item                            |
| 136    | `40` | that item is 64 bytes: a Schnorr signature                |

The 34-byte scripts are Taproot locks (`51 20` followed by a 32-byte key), so `22` appears in almost every Pearl output.

**Make a guess:** The coinbase of the genesis block carries a 234-byte message in its script. Is its length prefix one byte or three?

**Answer:** One byte: ea.

234 is below 253, so it fits in the single byte `0xea`. Only a script of 253 bytes or more needs the `fd` marker. The coinbase of block 130,825 is shorter: its script length is `27`, 39 bytes.

## Why not just use a fixed size?

Because most counts are tiny, and every byte is stored by every full node and paid for in fees.

If every count and length in the payment above were a fixed 4-byte number, its seven CompactSizes would take 28 bytes instead of 7, and the 205-byte transaction would grow to 226 bytes, about 10% bigger, to carry no extra information. CompactSize spends the extra bytes only on the rare big number.

## Where else does Pearl use them?

Anywhere something has a variable length:

- **The transaction count in a block.** After the certificate and the 108-byte header, block 130,825 has the byte `73`: 115 transactions.
- **Witness data.** Each input’s witness starts with an item count, and each item with its length (BIP-144, same as Bitcoin).
- **Taproot and P2MR script trees.** A leaf hash covers the leaf version, then a CompactSize of the script length, then the script. A 34-byte leaf script gets the prefix `22`.

> **Note: Not every length is a CompactSize**
>
> Pearl’s [certificate](https://pearlcodex.com/technical/block/certificate/) uses plain 4-byte little-endian lengths. In block 130,825 the public data length is `a4000000` (164 bytes) and the proof length is `a8e80000` (59,560 bytes). Scripts, for their part, push data with their own opcodes, such as `4c` (OP_PUSHDATA1). Know which format a field uses before decoding it.

## What does “non-canonical” mean?

Nothing stops you writing 5 as `fd 05 00`. It decodes to 5. But it’s wasteful, and worse, it means the same data could be written two ways, giving two different transaction IDs.

So Pearl’s decoder, like Bitcoin’s, rejects any CompactSize that could have been shorter. Each number has exactly one valid encoding:

- after `fd`, the value must be at least 253;
- after `fe`, at least 65,536;
- after `ff`, at least 4,294,967,296.

**Code: Pearl's encoder, and a JavaScript one**

pearld writes a CompactSize in [`WriteVarIntBuf`](https://github.com/pearl-research-labs/pearl/blob/v1.5.5/node/wire/common.go#L571-L612). Since v1.5.5 it builds the bytes in a small helper, `putVarInt`; the bytes are the same as btcd’s and Bitcoin’s:

`node/wire/common.go`

```go
switch {
case val < 0xfd:
	b[0] = uint8(val)
	return 1
case val <= math.MaxUint16:
	b[0] = 0xfd
	littleEndian.PutUint16(b[1:3], uint16(val))
	return 3
case val <= math.MaxUint32:
	b[0] = 0xfe
	littleEndian.PutUint32(b[1:5], uint32(val))
	return 5
default:
	b[0] = 0xff
	littleEndian.PutUint64(b[1:9], val)
	return 9
}
```

The reader, [`ReadVarIntBuf`](https://github.com/pearl-research-labs/pearl/blob/v1.5.5/node/wire/common.go#L500-L548), returns an error whenever the value could have been written in fewer bytes, which is the same as being smaller than the minimum for its marker.

```js
function compactSize(n) {
  if (n < 0xfd) return [n];
  if (n <= 0xffff) return [0xfd, n & 0xff, n >> 8];
  if (n <= 0xffffffff) return [0xfe, n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, n >>> 24];
  throw new Error('use BigInt for 9-byte values');
}
compactSize(1000); // [253, 232, 3] = fd e8 03
```

## In short

- A CompactSize is a variable-length number used for counts and lengths inside transactions and blocks.
- Values 0 to 252 take one byte. Bigger values start with a marker byte, 0xfd, 0xfe or 0xff, followed by 2, 4 or 8 little-endian bytes.
- Pearl only accepts the shortest possible encoding; anything longer is rejected as non-canonical.
- A real one-input, two-output payment contains seven CompactSizes, all one byte long.
- Not every length is a CompactSize: Pearl’s certificate uses fixed 4-byte lengths, and scripts use their own push opcodes.

## Read the code

- [ReadVarInt, with the non-canonical check](https://github.com/pearl-research-labs/pearl/blob/v1.5.5/node/wire/common.go#L500-L548) `node/wire/common.go:500-548`
- [WriteVarInt and putVarInt](https://github.com/pearl-research-labs/pearl/blob/v1.5.5/node/wire/common.go#L571-L612) `node/wire/common.go:571-612`
- [Decoding a transaction's counts and lengths](https://github.com/pearl-research-labs/pearl/blob/v1.5.5/node/wire/msgtx.go#L570-L702) `node/wire/msgtx.go:570-702`

## Sources

### Bitcoin standards

- [BIP-144: segregated witness serialization (witness counts and lengths)](https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki)

### Other

- [learnmeabitcoin: Compact Size](https://learnmeabitcoin.com/technical/general/compact-size/)
- [Bitcoin protocol documentation: variable length integer](https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer)
