Mathwords logoMathwords

Merkle Tree — Definition, Formula & Example

A Merkle tree, also called a hash tree, is a binary tree where each leaf holds the hash of one data block and each parent holds the hash of its two children. The single hash at the top, the Merkle root, acts as a fingerprint for the whole data set. Changing any block changes the root.

Let HH be a cryptographic hash function and let d1,…,dnd_1, \dots, d_n be data blocks. A Merkle tree is a rooted binary tree whose ii-th leaf is labeled H(di)H(d_i). Each internal node with left child LL and right child RR is labeled H(L ∥ R)H(L \,\|\, R), where ∥\| denotes concatenation. The root's label is the Merkle root. A membership proof (audit path) for a leaf is the ordered list of sibling labels on the path from that leaf to the root, each marked left or right. The proof's length equals the leaf's depth. In a perfect binary tree with n=2kn = 2^k leaves, every proof has exactly k=log⁡2nk = \log_2 n hashes. When nn is not a power of 2, the count depends on how the tree is built. If each level duplicates its last node whenever it has an odd count, as Bitcoin does, every proof has ⌈log⁡2n⌉\lceil \log_2 n \rceil hashes for n≥2n \ge 2. In an unbalanced tree, different leaves can have proofs of different lengths.

Key Formula

P=H(L ∥ R),∣π∣=log⁡2n(perfect tree, n=2k leaves)P = H(L \,\|\, R), \qquad |\pi| = \log_2 n \quad \text{(perfect tree, } n = 2^k \text{ leaves)}
Where:
  • PP = Hash stored at a parent node
  • L,RL, R = Hashes stored at the left and right children
  • HH = The hash function. Real systems use a function such as SHA-256.
  • ∥\| = Concatenation. The order matters, so the left child always goes first.
  • ∣π∣|\pi| = Number of sibling hashes in a membership proof
  • nn = Number of leaves (data blocks), assumed here to be a power of 2

Worked Example

Problem: Four records have leaf hashes h1=4h_1 = 4, h2=9h_2 = 9, h3=2h_3 = 2, h4=7h_4 = 7. Use the toy hash H(x,y)=(2x+y) mod 23H(x, y) = (2x + y) \bmod 23, which is not secure and is used only for practice. Build the Merkle root. Then verify that leaf 3 belongs to the tree using its proof: h4=7h_4 = 7 (right sibling), then h12h_{12} (left sibling). Finally, show what happens if leaf 3 is altered to 5.
Step 1: Hash each pair of leaves, then hash the two results to get the root.
h12=(2⋅4+9) mod 23=17,h34=(2⋅2+7) mod 23=11,root=(2⋅17+11) mod 23=45 mod 23=22h_{12} = (2\cdot 4 + 9) \bmod 23 = 17,\quad h_{34} = (2\cdot 2 + 7) \bmod 23 = 11,\quad \text{root} = (2\cdot 17 + 11) \bmod 23 = 45 \bmod 23 = 22
Step 2: The verifier knows only the leaf value 2, the proof [7,17][7, 17], and the trusted root 22. The verifier combines the leaf with each proof hash in order and puts each sibling on its marked side. With n=4n = 4 leaves, this takes log⁡24=2\log_2 4 = 2 hashes.
H(2,7)=(4+7) mod 23=11,H(17,11)=(34+11) mod 23=22  ✓H(2, 7) = (4 + 7) \bmod 23 = 11,\qquad H(17, 11) = (34 + 11) \bmod 23 = 22 \;\checkmark
Step 3: Now repeat the check with the altered leaf value 5. The recomputed root no longer matches the trusted root.
H(5,7)=(10+7) mod 23=17,H(17,17)=51 mod 23=5≠22H(5, 7) = (10 + 7) \bmod 23 = 17,\qquad H(17, 17) = 51 \bmod 23 = 5 \ne 22
Answer: The Merkle root is 22. The proof for leaf 3 recomputes 22, so the leaf is verified. The altered leaf produces 5 instead of 22, so the tampering is detected. The verifier never needed h1h_1 or h2h_2 individually.

Why It Matters

Bitcoin block headers store only a Merkle root of the block's transactions. A lightweight wallet can confirm that a payment is in a block of 1,024 transactions by checking just log⁡21024=10\log_2 1024 = 10 sibling hashes instead of downloading every transaction. Certificate Transparency logs use the same idea, and Git uses a related hash-linked structure. Merkle trees appear in college courses on cryptography, distributed systems, and data structures.

Common Mistakes

Mistake: Treating the combining step as order-free, so that H(L ∥ R)H(L \,\|\, R) is assumed to equal H(R ∥ L)H(R \,\|\, L).
Correction: Concatenation is not commutative, and neither are real hash functions. In the example, H(2,7)=11H(2, 7) = 11 but H(7,2)=16H(7, 2) = 16. A proof must record whether each sibling sits on the left or the right, and the verifier must follow that order.

Related Terms

  • Complete Binary Tree — Shape that keeps Merkle proofs near log base 2 length
  • Rooted Tree — Defines the parent, child, and depth used in proofs
  • Tree (Graph Theory) — Underlying graph structure that a Merkle tree labels with hashes
  • Coding Theory — Detects random errors, whereas Merkle trees detect deliberate tampering