Lessons 1/4 · 20 min
Building a Merkle tree
The starting question
A shopkeeper receives a batch of four payments. They want a single short value that changes if any of the four is altered, and that later lets them prove a specific payment belongs to the batch. Hashing the concatenation of the whole batch gives the first property but not the second: to prove one payment, you would have to hand over the other three. The Merkle tree gives both.
The concept (this workshop's toy convention)
Rules that every tool in the course applies:
- Leaf = SHA-256 of the data's text (for example a transaction).
- Parent node = SHA-256 of the two child digests concatenated as raw bytes, left then right.
- If a level has an odd number of nodes, the last one is duplicated.
- Repeat until a single node remains: the Merkle root.
A worked example: four transactions
Texts: tx0:Amina->Bello:20, tx1:Bello->Chantal:5, tx2:Chantal->Didier:12, tx3:Didier->Amina:7. Digests computed with SHA-256, truncated to 8 hex characters for readability:
| Node | Computation | Start of digest |
|---|---|---|
| L0 | SHA-256(tx0) | 801c17a0… |
| L1 | SHA-256(tx1) | 251f1575… |
| L2 | SHA-256(tx2) | b25a5829… |
| L3 | SHA-256(tx3) | f62a1b13… |
| N1 | SHA-256(L0 ‖ L1) | 82bcaf46… |
| N2 | SHA-256(L2 ‖ L3) | 024b1837… |
| Root | SHA-256(N1 ‖ N2) | 803bff37… |
The symbol ‖ means concatenation. You can recompute every row with the tools: sha256 for leaves, hexhash for nodes (entering the two full digests one after the other).
What the root guarantees
- If one leaf changes by a single bit, its leaf digest changes, then every node on the path up to the root: the root changes (avalanche effect, workshop 104).
- The root also depends on the order of the leaves: swapping tx0 and tx1 gives another root, because L0 ‖ L1 ≠ L1 ‖ L0.
- This assumes SHA-256 is collision resistant: no one currently knows how to produce two different inputs with the same digest.
A case with an odd number of leaves
With three leaves a, b, c: compute N1 = H(La ‖ Lb), then N2 = H(Lc ‖ Lc) (c is duplicated), then the root H(N1 ‖ N2). With five leaves the duplication occurs at two levels. The merkle-explorer tool shows it: enter 5 leaves and watch the copied nodes.
What this does NOT guarantee
The root says nothing about the validity of the data (a transaction can be fraudulent and still sit prettily in the tree), nor about its origin: it is a digest, not a signature. And the duplication rule has a downside, seen in lesson 3.
Recap
- Root = a 32-byte summary of the whole set, sensitive to every bit and to order.
- Leaf = H(data); node = H(left ‖ right); odd level: duplicate the last node.
- The root proves a set's consistency, not its validity.
Interactive tool
SHA-256 digest
Type some text: the digest updates on every keystroke. Compare with a second text to see the avalanche effect.
Digest (64 hex characters)
…Digest of the second text
…Differing characters are underlined and bold. Text is UTF-8 encoded with no space or newline added.
Interactive tool
SHA-256 of hex bytes
Paste bytes as hex: the hash covers the bytes, not the text of the digits. A second field is concatenated after the first.
Digest
…Spaces, newlines and 0x prefixes are ignored.
Interactive tool
Merkle tree explorer (toy)
One leaf per line (2 to 8). Pick a leaf to see its inclusion proof; edit a text to see which nodes change.
5 leaves
Nodes changed since the last valid input are underlined and marked "changed". Nodes on the chosen leaf's path are outlined. Last node of an odd level: it is duplicated as its own sibling.
Convention: leaf = SHA-256(UTF-8 text); parent = SHA-256(left ‖ right, raw bytes); odd node duplicated. Toy tree: Bitcoin double-hashes and serialises differently.
