Skip to content
CryptoAcademy logo CryptoAcademy
Menu
Lesson Beginner · 14 min

Merkle trees

A Merkle tree pairs up transaction hashes repeatedly until one root remains — 32 bytes that commit to every transaction in the block.

In this lesson

  • Construct a Merkle root from four transactions
  • Verify a Merkle proof step by step
  • Explain how light clients use proofs

Prerequisites

Pairs all the way up

From leaves to root
A Merkle tree hashes transactions in pairs until one root hash remains MERKLE ROOT a3f9…7c2e H(AB) hash of A + B H(CD) hash of C + D A tx 1 hash B tx 2 hash C tx 3 hash D tx 4 hash Proof of inclusion: tx 3 + hashes H(AB), a3f9… → 3 hashes are enough to prove tx 3 is in the block. Used by Simplified Payment Verification (SPV) wallets that do not download every transaction.
Editing any leaf (red) changes every hash on the path to the root — nothing hides.

Transactions are hashed (leaves), then hashes are hashed in pairs, layer after layer, until a single Merkle root remains. The root goes in the block header; the full list stays in the block body.

A Merkle proof is the set of sibling hashes needed to recompute the root from one leaf — logarithmic in the number of transactions: ~10 hashes instead of 4,000.

merkle.py python
import hashlib\n\ndef h(x): return hashlib.sha256(x).digest()\n\ndef root(leaves):\n    level = [h(l) for l in leaves]\n    while len(level) > 1:\n        if len(level) % 2: level.append(level[-1])   # odd node promoted\n        level = [h(level[i] + level[i+1]) for i in range(0, len(level), 2)]\n    return level[0].hex()

Example · SPV wallets on your phone

A phone wallet cannot store 600 GB of blocks. It downloads only headers, then asks a peer for the Merkle proof of its transaction. Recomputing the root against the header proves inclusion — with no full node required.

Layer Nodes Contents
Leaves N One hash per transaction
Middle N/2, N/4, … Hashes of pairs below
Root 1 Commits to everything

One changed leaf, one changed root

That is the tamper-evidence property in a nutshell: altering any transaction — even flipping one bit — forces a different root, which forces a different block hash, which breaks the chain above it.

Build and break a tree

Hash four transactions into a root, edit one leaf, and watch the proof for that leaf fail.

Try it yourself

Order the steps

Practice · server-graded
  • Order the steps of building a Merkle tree for four transactions A, B, C, D.

    1 pt

    Enter the correct position (1 = first) for each item.

    Hash pairs AB and CD
    Obtain the root hash
    Hash the two pair hashes
    Hash each transaction

Match the parts

Practice · server-graded
  • Match each Merkle tree component with its role.

    1 pt
    Leaf
    Branch
    Root

Practice exercise

Practice: blockchain basics

Start exercise
Why duplicate the last hash on odd levels?

So every pair is defined. It is a convention, not a security hole — attackers still cannot forge a path.

Can a proof lie?

No — recomputing the root must byte-for-byte match the header hash, which is itself chained to the tip.

Practice

  • Blockchain basics Start

Share this page

Send this link to a friend, or scan the QR code to open it on your phone.