Under the Loom
Contents

§ 1.4 · Foundations · 4 min

Merkle Trees and Why Blockchains Love Them

One hash that commits to thousands of transactions, and a proof that fits in a few hundred bytes. How Merkle trees work, and why every major blockchain relies on them.

The Loom Desk ·

SHEET 42 · FOUNDATIONSUNDER THE LOOM
Fig. 1.4 — schematic. Watch the real thing in wevr.ai’s live tables.

Every block header on Bitcoin is roughly 80 bytes. Inside that tiny header is a single 32-byte value that commits to every transaction in the block — sometimes thousands of them. Change one bit of one transaction and that value changes completely. The structure that makes this possible is the Merkle tree, and once you understand it, a surprising amount of blockchain design falls into place.

This chapter explains how Merkle trees are built, how they let you prove a transaction is in a block without downloading the block, and how different chains use variations of the same idea.

Hashes, briefly

A cryptographic hash function takes any input and produces a fixed-size fingerprint. Three properties matter here:

  • Deterministic: the same input always gives the same hash.
  • Avalanche effect: a tiny change in the input changes the output unpredictably.
  • Collision resistance: it is computationally infeasible to find two different inputs with the same hash.

Bitcoin uses SHA-256, applied twice. Ethereum uses Keccak-256. The details differ; the role is the same — a short value that stands in for a large one and cannot be forged.

Building the tree

Start with the transactions in a block. Hash each one: these hashes are the leaves of the tree.

Now pair the leaves up, concatenate each pair, and hash the result. That gives a new layer with half as many hashes. Repeat — pair, concatenate, hash — until one hash remains. That final value is the Merkle root, and it goes into the block header.

If a layer has an odd number of hashes, Bitcoin duplicates the last one so every hash has a partner. Other designs handle odd counts differently, but the principle is identical: each layer summarises the one below it until everything is summarised by one value.

The result is a commitment. Anyone who has the root and the full list of transactions can rebuild the tree and confirm the root matches. And because of the avalanche effect, no one can swap, add, remove or reorder a transaction without changing the root — which would change the block header, which would invalidate the block's proof of work or signatures.

Merkle proofs: the real superpower

Committing to data is useful. Proving a single item is included, cheaply, is what makes Merkle trees indispensable.

Suppose you want to prove that transaction T is in a block with 4,096 transactions. You do not need all 4,096. You need:

  1. The hash of T itself.
  2. The hash of T's sibling at the bottom layer.
  3. The sibling hash at each layer above that, up to the root.

With those, anyone can recompute the path from T to the root. For 4,096 transactions, that path has only 12 steps, because the number of layers grows with the logarithm of the number of leaves. Twelve 32-byte hashes is under 400 bytes — a proof a phone can download and check in milliseconds.

This is called a Merkle proof or inclusion proof, and it is the basis of lightweight verification. A wallet that only downloads block headers can still verify that a payment was included in a block by requesting a Merkle proof for it. Bitcoin's original paper described exactly this, under the name simplified payment verification.

Variations across chains

Bitcoin uses a straightforward binary Merkle tree over the block's transactions. A second commitment, introduced with SegWit, covers witness data separately.

Ethereum goes further. Each block header contains three roots:

  • a transactions root, committing to the block's transactions;
  • a receipts root, committing to the outcome of each transaction, including the event logs it emitted;
  • a state root, committing to the entire state of the chain — every account balance, every contract's code and storage — after the block.

These use a more elaborate structure called a Merkle Patricia trie, which supports efficient lookups by key as well as proofs. The state root in particular is remarkable: one 32-byte value commits to the state of every account on the network, and a proof can show any single account's balance against it.

You can see these roots in the block headers on the Wevr.ai live tables; every block carries them.

Why blockchains love them

Merkle trees solve several problems at once:

  • Integrity: a block header commits to its full contents without containing them.
  • Efficient verification: light clients and other chains can check inclusion with tiny proofs.
  • Data availability checks: rollups and bridges can prove facts about one chain to another using roots and proofs rather than full data.
  • Synchronisation: nodes can compare roots to find exactly where their data differs, rather than comparing everything.

That last point is easy to overlook. Two nodes that disagree about state can walk down their trees together, comparing hashes layer by layer, and find the differing items quickly.

Limits and what comes next

Merkle proofs grow with the depth of the tree and the number of siblings at each level. For Ethereum's state, with its wide branching, proofs can be several kilobytes — fine for occasional checks, heavy for applications that need many of them.

That is why Ethereum researchers have long worked on alternative structures, such as Verkle trees, which use different cryptography to make proofs much smaller. The goal is the same as Merkle's original idea: let anyone verify a small fact about a large dataset without holding the whole thing.

Checking your understanding

Try this: open any recent block in the live tables and find its Merkle root, then count its transactions. Work out how many hashes an inclusion proof for one of those transactions would need — the answer is the number of times you can halve the transaction count before reaching one. It is always smaller than you expect, and that smallness is the whole point.

Index terms: merkle-trees · hashing · data-structures · light-clients

Next · § 1.5 UTXO vs Account Models, Compared →