1 of 4

Your RightSignal

Cryptography from zero · 10 of 111 / 3

Merkle trees

The concept

How one 32-byte hash can vouch for a million files

A Merkle tree hashes data in pairs, level by level, until one hash is left: the root. If you trust the root, you can check any single piece using only a handful of hashes, not the whole dataset.

Take four chunks A, B, C, D. Hash each one to get hA, hB, hC, hD (the leaves). Then hash pairs together: hAB = H(hA ‖ hB) and hCD = H(hC ‖ hD), where ‖ means 'stuck end to end'. Finally root = H(hAB ‖ hCD).

To check chunk C, you need only hD and hAB, called the proof (or audit path). Hash C to get hC, combine it with hD to get hCD, then combine that with hAB to get a root. If it matches the trusted root, C is genuine.

What it’s forTells you how many hashes someone must send to prove one piece belongs to the tree.

proof size = ⌈log₂ n⌉ hashes

n
number of pieces (leaves) in the tree
log₂ n
how many times you can halve n before reaching 1, i.e. the tree's height
⌈ ⌉
round up to a whole number

What it givesA proof that grows by just one hash each time the data doubles.

  1. 8 chunks: log₂ 8 = 3 hashes in the proof
  2. 1,048,576 chunks: log₂ 2²⁰ = 20 hashes
  3. 20 × 32-byte SHA-256 hashes = 640 bytes of proof