Skip to the workspace
Pebellslearn by building
Hashes, Keys and Signatures  ›  Lesson

Merkle Trees — One Hash for Many Transactions

Cryptography & Hashing 12 minA Merkle tree hashes transactions in pairs, level by level, into a single root kept in the block header, so one transaction can be proved present with only a handful of hashes.

blockchain / Merkle Trees — One Hash for Many Transactions

Explore each scene at your own pace.

01 · The rule

The rule

Merkle Trees — One Hash for Many Transactions

Meera runs a light client on her phone. She wants to be sure that Asha's payment really is inside block 812, but that block holds 3,000 transactions and she does not want to download them all. A Merkle tree lets a full node send her a proof of just twelve hashes, and lets her check that proof herself.

The rule

Hash in pairs, level by level, until one root remains.

Here is the tree for four transactions, A, B, C and D. H means "hash of", and + means the two hashes are joined end to end before hashing.

                Root = H(H_AB + H_CD)
               /                     \
      H_AB = H(H_A + H_B)      H_CD = H(H_C + H_D)
        /          \             /          \
      H_A          H_B         H_C          H_D
       |            |           |            |
       A            B           C            D
  • Hash each transaction to get the leaves: H_A, H_B, H_C and H_D.
  • Join neighbouring pairs and hash each pair: H_AB and H_CD.
  • Repeat until a single hash is left. That is the Merkle root, and it goes in the block header.

When a level has an odd number of hashes, Bitcoin pairs the last one with a copy of itself; other chains use their own rule. Change any transaction and its leaf changes, then its parent, and so on up to the root. This is why the block hash, computed from the header, commits to every single transaction.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.