⛓ Blockchain & Applied Crypto

Merkle Trees Explained

A Merkle tree, also called a hash tree, is a data structure that lets a system commit to a large collection of data with a single hash and later prove that any individual item belongs to that collection without revealing or transmitting all of it. Named after Ralph Merkle, it is one of the most important building blocks in blockchains, powering everything from block construction to lightweight clients. Understanding Merkle trees clarifies how blockchains stay both compact and verifiable.

What a Merkle Tree Is

A Merkle tree is a binary tree of hashes. The raw data items, such as transactions, are placed at the bottom as leaves. Each leaf is hashed, then pairs of hashes are concatenated and hashed together to form parent nodes. This pairing repeats level by level until a single hash remains at the top: the Merkle root.

The essential property is that the root depends on every leaf. Because the tree is built from a cryptographic hash function, changing even one bit of one leaf changes that leaf's hash, which changes its parent, and so on up to the root. The root is therefore a compact, tamper-evident commitment to the entire data set.

How a Merkle Tree Is Built

Constructing a Merkle root follows a simple bottom-up procedure:

  1. Hash each data item to produce the leaf nodes.
  2. Group the leaves into pairs and hash each pair together to produce the next level.
  3. If a level has an odd number of nodes, duplicate the last node so it can be paired (this is Bitcoin's convention).
  4. Repeat until only one hash remains, which is the Merkle root.
def merkle_root(leaves):
    level = [H(x) for x in leaves]
    while len(level) > 1:
        if len(level) % 2: level.append(level[-1])   # duplicate last
        level = [H(level[i] + level[i+1]) for i in range(0, len(level), 2)]
    return level[0]

The result is a single hash, typically 32 bytes, that fixes both the contents and the ordering of an arbitrarily large set of items.

Merkle Proofs and Proof of Inclusion

The reason Merkle trees are so useful is the Merkle proof, sometimes called a proof of inclusion. To prove that a particular transaction is in a block, you do not need the whole block. You only need the sibling hash at each level along the path from that leaf up to the root.

A verifier starts with the transaction, hashes it, then repeatedly combines the running hash with each supplied sibling until they arrive at a computed root. If that computed root matches the trusted Merkle root, the transaction is proven to be part of the set. For a set of n items the proof contains only about log2(n) hashes, so proving inclusion in a block of a million transactions takes roughly twenty hashes.

This is exactly how Simplified Payment Verification (SPV) clients work: they store only block headers, which contain the Merkle root, and request compact proofs instead of downloading full blocks.

Merkle Trees in Blockchains

Blockchains use Merkle structures in several ways:

  • In Bitcoin, every block header stores the Merkle root of that block's transactions. This binds all transactions to the proof-of-work-secured header and enables SPV wallets.
  • In Ethereum, a variant called a Merkle-Patricia trie commits not just to transactions but to the entire account state and to receipts, letting nodes prove a specific account balance or storage value against a single state root.
  • Light clients, cross-chain bridges, and rollups all rely on Merkle proofs to verify data against a compact root they already trust.

For the bigger picture of how these roots fit into blocks, see how blockchain works.

Security Properties and Pitfalls

A Merkle tree is only as strong as its hash function. Its guarantees rest on collision resistance and second-preimage resistance: an attacker must not be able to find a different data set that produces the same root. If the underlying hash were broken, an attacker could substitute forged data under an unchanged root.

Implementation details matter too. Bitcoin's habit of duplicating the final node when a level has an odd count once enabled a subtle vulnerability in which distinct transaction lists could be crafted to yield the same Merkle root, allowing a form of block malleability. Robust designs defend against this by domain-separating leaf and internal nodes, hashing them with different prefixes, so that a leaf can never be reinterpreted as an internal node, and by rejecting the ambiguous duplicate structure. These defenses close off second-preimage attacks that exploit the tree's shape rather than the hash function itself.

Key Takeaways

  • A Merkle tree hashes data in pairs up to a single Merkle root that commits to every leaf and its position.
  • Any change to any item cascades up and changes the root, making the structure tamper evident.
  • A Merkle proof verifies inclusion using only about log2(n) sibling hashes, enabling lightweight SPV clients.
  • Blockchains embed Merkle roots in block headers and, in some designs, use Merkle-Patricia tries to commit to full state.
  • Security depends on the collision resistance of the hash function and on careful construction to avoid duplicate-node and leaf/internal confusion attacks.
merkle-treehashingdata-structuresmerkle-proofblockchain

Frequently asked questions

What is a Merkle tree?

A Merkle tree, also called a hash tree, is a binary tree of hashes that lets a system commit to a large collection of data with a single hash. Data items sit at the bottom as leaves, each is hashed, then pairs of hashes are concatenated and hashed together level by level until one hash remains, the Merkle root. Because it is built from a cryptographic hash function, the root depends on every leaf and its position.

What is a Merkle root used for in blockchain?

A Merkle root is a single hash, typically 32 bytes, that commits to every transaction in a block and their ordering, and it is stored in the block header. Because any change to any transaction cascades up the tree and changes the root, it ties transaction integrity directly to the header that consensus secures. This makes the block a compact, tamper-evident commitment to all of its transactions.

How does a Merkle proof work?

A Merkle proof, or proof of inclusion, shows that one item belongs to the committed set without needing the whole set. The verifier starts with the item, hashes it, then repeatedly combines the running hash with the supplied sibling hash at each level until reaching a computed root, and checks that it matches the trusted root. For a set of n items the proof contains only about log2(n) hashes, so proving inclusion in a block of a million transactions takes roughly twenty hashes.

What is Simplified Payment Verification (SPV)?

Simplified Payment Verification is how lightweight blockchain clients confirm a transaction without downloading full blocks. An SPV client stores only block headers, each of which contains the Merkle root, and requests a compact Merkle proof to check that a transaction is included. This lets constrained devices verify inclusion against a header secured by proof of work rather than storing the entire chain.

Are Merkle trees secure?

A Merkle tree is only as strong as its underlying hash function, and its guarantees rest on collision resistance and second-preimage resistance so that no different data set can produce the same root. Implementation details also matter: Bitcoin's habit of duplicating the final node on odd levels once enabled a subtle malleability flaw. Robust designs domain-separate leaf and internal nodes by hashing them with different prefixes to prevent such attacks.

How does Ethereum use Merkle trees?

Ethereum uses a variant called a Merkle-Patricia trie that commits not just to transactions but to the entire account state and to transaction receipts. This lets a node prove a specific account balance or storage value against a single state root. More broadly, light clients, cross-chain bridges, and rollups all rely on Merkle proofs to verify data against a compact root they already trust.

Try it hands-on

K0G is an open toolkit of browser-based security utilities — hashing, encoding, JWT, certificates, crypto and more, all running locally in your browser.

Explore the tools →