Learn / Math / Merkle Trees & Vector Commitments
16. Merkle Trees & Vector Commitments
Merkle Trees · Lesson 17 of 26 · 12:43
How to lock in a whole list — millions of items — with one short root, prove a single one belongs with a handful of hashes, and let a hidden member prove membership exactly once.

The last lesson locked in one value, and then one polynomial, with a commitment. Now the problem gets bigger: how do you lock in a whole list — millions of names, accounts, or transactions — and later prove that one item is on it, without handing over the list? The tool for that is the Merkle tree. By the end of this lesson the goal is to see how it’s built, why nobody can quietly edit it, how a handful of hashes can prove one item belongs, and how a ZK proof hides which item you’re proving while still stopping you from using it twice.

Building a Merkle tree

A fingerprint for a whole pile sounds easy: just hash everything together. But then proving one document is in the pile would mean handing over the entire pile. A Merkle tree builds the fingerprint in layers instead, a bit like a tournament bracket where a hash moves up each round.

building the tree, level by level ROOT = H(h12, h34) h12 = H(h1, h2) h34 = H(h3, h4) h1 = H(L1) h2 = H(L2) h3 = H(L3) h4 = H(L4) L1 L2 L3 L4 ROOT PAIRS HASHES LEAVES MANY LEAVES, ONE ROOT Hash the pairs, then the pairs of pairs, until one hash remains.
Fig. 1 — many leaves, one root. Hash the pairs, then the pairs of pairs, until one hash remains.
A Merkle tree

Hash each leaf on its own: h1=H(L1)h_1 = H(L_1), and so on. Pair them up and hash the pair: h12=H(h1,h2)h_{12} = H(h_1, h_2) — write h1h_1 and h2h_2 next to each other, then hash the result. Repeat one level up: ROOT=H(h12,h34)\text{ROOT} = H(h_{12}, h_{34}).

The root commits to everything below it, and everyone only has to remember that one short value, not the whole list.

Nobody edits it quietly

This is the property that makes the root worth trusting: no leaf can be edited without the root changing, and there’s no way to patch the root to match without finding a hash collision — which the last lesson but one already showed is infeasible.

change one leaf, the root changes h12 = H(h1, h2) h1 = H(L1) h2 = H(L2) L1 L2 ROOT′ = H(h12, h34′) ROOT 7e3f9a2c… ROOT′ c81d5f3b… h34′ = H(h3′, h4) h3′ = H(L3′) h4 = H(L4) L3′ tampered L4 NO QUIET EDITS Every hash on the path to the root is recomputed — one bit differs, and the root no longer matches.
Fig. 2 — change one leaf, and the root changes. Every hash on the path to the root is recomputed.

Say someone swaps one leaf for something else. That leaf’s hash comes out different, which changes the pair it belongs to, which changes the root. The left half of the tree — everything not on that leaf’s path — never has to be touched, and never changes.

Proving membership

Here’s the harder question: how does a verifier, holding only the published root, get convinced that one specific leaf is really in the tree — without being handed the whole tree? The trick is that the verifier doesn’t need most of it. They only need the hashes sitting right next to the leaf’s path, because those are exactly what’s missing to rebuild the root.

prove one leaf with three hashes L1L2L4L5L6L7L8 h1h2h5h6h7h8 h12h56h78 h5678 L3 h3 h34 h1234 ROOT h4 P h12 P h5678 P VERIFIER RECOMPUTES 1. h3 = H(L3) 2. h34 = H(h3, h4) 3. h1234 = H(h12, h34) 4. root′ = H(h1234, h5678) ✓ root′ = published root P = sibling hash sent as the proof PROVE ONE LEAF WITH THREE HASHES The verifier never sees the other leaves — only three sibling hashes and the root.
Fig. 3 — prove one leaf with three hashes. The verifier never sees the other leaves, only three sibling hashes and the root.
A membership proof

To prove L3L_3 belongs, send the three sibling hashes on its path — h4h_4, h12h_{12}, h5678h_{5678} — each marked with which side it sits on, since H(h3,h4)≠H(h4,h3)H(h_3, h_4) \ne H(h_4, h_3). The verifier recomputes h3h_3, then h34h_{34}, then h1234h_{1234}, then root′\text{root}', and checks root′=root\text{root}' = \text{root}.

One caveat worth flagging: this proof on its own is not private. The verifier sees the leaf itself and exactly where it sits in the tree. That’s fine for public data, but it isn’t zero-knowledge — the last section of this lesson fixes that.

Proof size grows slowly

A Merkle proof needs one sibling hash per level of the tree, and each level is one halving of the pile — so the proof length is log⁡2(n)\log_2(n). That grows remarkably slowly.

proof size grows slowly ITEMS IN THE TREE HASHES IN THE PROOF = log₂(n) 8 3 1,024 10 1,048,576 20 4,294,967,296 32 DOUBLE THE DATA, ADD ONE HASH A tree with 4 billion leaves needs only 32 hashes to prove any single one.
Fig. 4 — double the data, add one hash. A tree with 4 billion leaves needs only 32 hashes to prove any single one.

Eight items need three hashes. Grow the tree to over four billion leaves — thirty-two doublings — and the proof grows by exactly thirty-two hashes. At roughly 32 bytes per hash, that’s about one kilobyte to prove one name out of four billion.

Vector commitments

A Merkle tree commits to an ordered list with one short value, and lets you open any single position — but it’s one way to do that job, not the only way. The general idea has a name: a vector commitment.

same promise, different trade-offs THE IDEA v1 v2 v3 vn C open position i (here i = 3) v3 + proof π ✓ checks against C TWO WAYS TO BUILD ONE MERKLE TREE proof = log n hashes no trusted setup needs only a hash function KZG proof = ONE point needs a trusted setup needs pairings (Topic 13) Vector Polynomial (Lagrange) KZG commit Topic 9 Topic 15 A VECTOR COMMITMENT LOCKS IN A WHOLE LIST, ONE SHORT VALUE
Fig. 5 — same promise, different trade-offs. A vector commitment locks in a whole list and lets you open any single position.

A Merkle tree needs nothing but a hash function, and no trusted setup, at the cost of a log⁡n\log n-sized proof. KZG, from the previous topic, compresses the proof down to a single point, at the cost of a trusted setup and the pairings built two lessons back. To use KZG on a list: turn the vector into a polynomial through Lagrange interpolation, then commit to that polynomial with KZG. Opening position ii then means proving the polynomial’s value at ii.

Nullifiers

Here’s the problem a private membership proof creates: it lets any member prove they belong without revealing which member it was — but nothing stops that same member from proving it again, say to vote twice. The fix is a second value, derived from the member’s secret, that comes out the same every time for the same member and means nothing to anyone else: a nullifier.

leaf = H(nf, s) → published N = H(nf) — N doesn't reveal which leaf FIRST SPEND nf, s, Merkle path (private) ZK proof public root N N not in used list ✓ accepted SECOND SPEND nf, s, Merkle path (private) ZK proof public root N N already used ✕ rejected USED LIST N same note → same N, always PROVE YOU BELONG, BUT ONLY ONCE Nullifiers stop double-use without revealing which member you are.
Fig. 6 — prove you belong, but only once. Nullifiers stop double-use without revealing which member you are.
Nullifiers

Each member’s leaf is H(nf,s)H(nf, s) for two secrets nfnf and ss. Spending publishes N=H(nf)N = H(nf). Because a hash is one-way, NN doesn’t reveal which leaf it came from — but the same nfnf always produces the same NN, so a second spend publishes the same NN again, and gets rejected against the used list.

This is a simplified version of the idea — real protocols like Tornado Cash and Semaphore use different exact formulas, but they all follow this same pattern.

Why this matters for ZK

The membership proof from earlier in this lesson wasn’t private: the verifier saw the leaf and its whole path. Move that exact check inside a ZK circuit, and the secrets never leave the prover at all.

member of the set, identity unknown PRIVATE ZK CIRCUIT PUBLIC leaf secrets (nf, s) Merkle path (siblings) never leaves the prover 1. rebuild the leaf 2. hash up the path, must equal root 3. compute N = H(nf) root N proof π hashing inside a circuit is costly — ZK systems use circuit-friendly hashes, like Poseidon THE VERIFIER SEES ONLY THE ROOT, N, AND THE PROOF The core of anonymous membership: private voting, anonymous credentials, mixers.
Fig. 7 — member of the set, identity unknown. The verifier sees only the root, N, and the proof.

The leaf secrets and the Merkle path stay private, inside the circuit. The circuit rebuilds the leaf, hashes up the path and checks it equals the root, and computes N=H(nf)N = H(nf) — all without ever showing its work. Out come only the root the verifier already knew, the nullifier NN, and the proof itself. One practical note: hashing inside a circuit is expensive, so real systems use hash functions designed for circuits, like Poseidon, playing the same role as HH throughout this lesson.

That’s the core of anonymous membership — private voting, anonymous credentials, mixers — and it’s built from nothing more than the Merkle tree this lesson opened with.

Key takeaways
  • A Merkle tree commits to a whole list with one short root, built by hashing leaves in pairs up to a single value.
  • Changing any leaf changes the root, and producing a different leaf that keeps the same root means finding a hash collision — infeasible.
  • A membership proof needs only the sibling hashes on a leaf’s path, so proof size grows as log⁡2(n)\log_2(n) — about 32 hashes proves one item among 4 billion.
  • A vector commitment generalizes this same promise; KZG is an alternative that compresses the proof to one point, at the cost of a trusted setup and pairings.
  • Nullifiers (N=H(nf)N = H(nf)) let a member prove membership exactly once without revealing which member they are — the basis of anonymous voting, credentials, and mixers.