BITCOIN MAINNETLive explorer status
THE SHARED RECORD

Merkle trees and the merkle root

A merkle tree hashes a block's transactions in pairs until one hash remains: the merkle root. Learn how it works and how merkle proofs save space.

Pair, hash, repeat

  1. Take the txid of every transaction in the block.
  2. Pair them up and hash each pair together.
  3. Pair up those hashes and hash again.
  4. Repeat until a single hash is left: the merkle root.
Transactions are hashed in pairs up to a single rootTx ATx BTx CTx DH(AB)H(CD)Merkle root
Transactions are hashed in pairs up to a single root

If there's an odd number at any level, the last hash is paired with a copy of itself.

With five transactions, the last one is duplicated to make a pairTx ATx BTx CTx DTx ETx E (copy)H(AB)H(CD)H(EE′)H(EE′) (copy)HashHashMerkle root
With five transactions, the last one is duplicated to make a pair

Why bother?

  • Tamper evidence: change any transaction and the root changes, which changes the header hash, which breaks the Proof of work.
  • Small proofs: to prove a transaction is in a block you only need the hashes along its path to the root: about 12 hashes for a block of 4,000 transactions. This is how light clients work.
A merkle proof: to prove Tx C is included, supply only the green hashesTx ATx BTx CTx DTx ETx FTx GTx HH(AB)H(CD)H(EF)H(GH)HashHashMerkle rootorange: path being proven · green: hashes supplied as the proof
A merkle proof: to prove Tx C is included, supply only the green hashes

Why proofs stay tiny

A merkle proof needs one hash per level of the tree:

Transactions in blockHashes in proof
83
1,00010
4,00012
1,000,00020

That's why a phone wallet can check that a payment is in a block without downloading the whole block. See SPV and light clients.

A known weakness, already handled

Duplicating the last hash at odd levels once allowed two different transaction lists to share a merkle root (CVE-2012-2459). Bitcoin Core detects and rejects such blocks.

Share this guide
Share on X ↗

Key takeaways

  • The merkle root is a single hash built by hashing transactions in pairs.
  • It lets an 80-byte header commit to every transaction.
  • Merkle proofs show inclusion with only a handful of hashes.

Common questions

What if a block has one transaction?

The merkle root is simply that transaction's txid, as in the genesis block.

Why is the merkle tree needed if every node downloads full blocks?

It makes the header commit to the transactions and enables compact proofs for light clients.

What is a merkle proof?

The list of sibling hashes needed to rebuild the path from one transaction up to the merkle root.

Who invented merkle trees?

Ralph Merkle, in 1979, long before Bitcoin.