Merkle trees
Merkle trees are binary hash trees that allow efficient and secure summary and verification of large data sets by storing one short root hash and providing logarithmic-size proofs.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Merkle trees let an engineer summarise many data items (transactions, file chunks) into a single short value (the Merkle root) so nodes can verify membership or integrity with a small proof. They are used in blockchains: Bitcoin uses a binary Merkle tree over transaction IDs (txids); Ethereum uses Merkle–Patricia tries (a different authenticated structure) for state, transactions, and receipts; both enable compact block headers, light clients, and efficient sync.
Key ideas
Internal node: an internal node's value is the hash of the concatenation of its two child node values. If left child has value L and right child R, then internal node value = H(L || R). We write || for concatenation. State clearly how you concatenate (byte order) in an implementation; differing conventions change the result. Use an unambiguous encoding (fixed-size child hashes, or length-prefix/domain separation) to avoid ambiguity attacks.
Merkle root: the value at the root node after hashing all internal nodes up to the root. This single value summarises all leaves.
Merkle proof (authentication path): to prove leaf i is in the tree you provide the leaf's hash plus the list of sibling node hashes needed to recompute the root. The verifier recomputes parent hashes up the tree and checks equality with the known Merkle root.
Exact definitions (university style):
Definition (Binary Merkle tree): Let L = [d1, d2, ..., dn] be n data blocks. Define leaf hashes li = H(di). Build a binary tree by pairing adjacent li and computing parent nodes p = H(l_left || l_right). Repeat until a single root r is obtained. The tuple (L, H) produces Merkle root r.
Assumptions behind security: H is collision- and second-preimage-resistant. Under these, if r matches, it is computationally infeasible for an adversary to produce a different leaf sequence containing a forged di that still yields the same root.
Common construction details you will be asked about in exams:
If n is not a power of two, two standard methods: 1) duplicate the last hash at each level when that level has an odd number of nodes (as in Bitcoin), or 2) build an unbalanced tree where a node may have one child; in that case, explicitly define the single-child rule (e.g., parent = H(child) or a domain-separated variant), rather than implicitly using H(child || child). Always state which method you assume.
Proof verification algorithm (verifier's steps): 1) start with li = H(di) (or the provided leaf hash), 2) iteratively combine with sibling hash s using H(left || right) according to the sibling position, 3) after O(log n) steps compare computed value to Merkle root r.
What to draw for an exam diagram: draw a full binary tree with leaves at bottom labelled d1..d4. Next to each leaf write its leaf hash l1=H(d1), etc. For each internal node, label p12=H(l1||l2), p34=H(l3||l4), and root r=H(p12||p34). For a proof diagram for d3, highlight path d3 -> l3 -> p34 -> r and show the sibling hashes l4 and p12 that the prover sends.
Complexity facts (state them as theorems you can cite):
Theorem: Building the Merkle tree for n leaves requires computing exactly n-1 internal hashes (plus n leaf hashes) if the tree is full; total nodes = 2n-1. Proof: each internal node except leaves arises from pairing two child nodes; a full binary tree with n leaves has n-1 internal nodes.
Theorem: A Merkle proof for one leaf requires at most ceil(log2 n) sibling hashes (O(log n)) to verify. Proof: tree height is ceil(log2 n); each level contributes at most one sibling.
Be explicit about edge cases (n not power of two) and state which tree-shape assumption you use when counting.
Formulas
height = ceil(log2(n)) — height of a full binary Merkle tree (levels of hashing from leaves to root); n = number of leaves; units: levels (dimensionless). Applies for n >= 1.
total_nodes = 2*n - 1 — total number of nodes in a full binary tree with n leaves; n dimensionless. Applies when tree is full (each internal node has two children).
internal_hashes = n - 1 — number of internal hash computations to produce the root from n leaves; n dimensionless. Applies for full tree.
proof_size_hashes ≤ ceil(log2(n)) — number of sibling hashes in an authentication path; each hash has fixed byte length (digest_length bytes). digest_length is implementation-dependent (e.g., 32 bytes for SHA-256).
proof_bytes ≈ proof_size_hashes * digest_length — approximate byte-size of a Merkle proof (plus small overhead for leaf index and flags).
build_time = O(n) — time complexity to compute tree (assuming hash is O(1) per digest).
verify_time = O(log n) — time to verify a single Merkle proof (number of hash computations proportional to tree height).
Note: digest_length should be specified for the hash used; e.g. for SHA-256 digest_length = 32 bytes.
Worked examples
Example 1 — full tree with 4 leaves (toy hash function):
We use a simple toy hash H(s) = sum of ASCII codes of characters in string s, modulo 1000. This is only to illustrate arithmetic; do not use in real systems.
Leaves (as strings): '10', '20', '30', '40'.
Step 1: compute leaf hashes:
- l1 = H('10') = ASCII('1') + ASCII('0') = 49 + 48 = 97
- l2 = H('20') = 50 + 48 = 98
- l3 = H('30') = 51 + 48 = 99
- l4 = H('40') = 52 + 48 = 100
Step 2: compute internal hashes (pair concatenation is text 'l_left|l_right'):
- p12 = H('97|98') = ASCII('9')+ASCII('7')+ASCII('|')+ASCII('9')+ASCII('8') = 57+55+124+57+56 = 349
- p34 = H('99|100') = 57+57+124+49+48+48 = 383
Step 3: compute root:
- r = H('349|383') = characters '3','4','9','|','3','8','3' ascii sum = 51+52+57+124+51+56+51 = 442
Final answer: Merkle root = 442 (toy example). All arithmetic shown.
Proof example for leaf '30' (l3):
- Prover sends l3 = 99 and sibling l4 = 100 and sibling at next level p12 = 349.
- Verifier: compute p34' = H('99|100') = 383, then r' = H('349|383') = 442. Compare r' to known root 442 — matches, so membership verified.
Example 2 — odd number of leaves (3 leaves) and duplication rule:
Leaves: 'A','B','C'. We choose rule: duplicate last leaf to make leaves count power-of-two.
Leaf hashes (toy H using ASCII sum):
- l1 = H('A') = 65
- l2 = H('B') = 66
- l3 = H('C') = 67
- duplicate l4 = l3 = 67
Pair and root:
- p12 = H('65|66') = ASCII('6')+ASCII('5')+ASCII('|')+ASCII('6')+ASCII('6') = 54+53+124+54+54 = 339
- p34 = H('67|67') = 54+55+124+54+55 = 342
- root r = H('339|342') = compute ascii sums => (characters '3','3','9','|','3','4','2') 51+51+57+124+51+52+50 = 436
Result: Merkle root = 436.
Show in exam: explicitly state the duplication policy and show every ASCII sum or hash computation step.
Common mistakes
Thinking the proof length is O(n) instead of O(log n) — proofs include sibling hashes per level, not all leaves.
Forgetting that leaf hashes are H(data), not data itself; proofs normally pass the leaf hash or recomputed leaf hash from data.
Using an insecure toy hash in production (toy hash only for class examples). Always mention the real system uses cryptographic hashes like SHA-256.
Arithmetic mistakes in worked examples: verify each hash arithmetic step and mention modulo/digest length rules if used.
For your exam
Kinds of questions you will see:
- Definitions: define Merkle tree, Merkle root, Merkle proof, properties (complexity and security assumptions).
- Short notes: compare Merkle vs. simple hash lists; explain why Merkle proofs are useful for light clients.
- Derivations: show number of nodes/internal hashes and prove O(log n) proof size; draw the tree and label nodes.
- Numericals: compute a Merkle root for small data using a given toy hash; show all steps.
- Programs/algorithms: write pseudocode to build tree or verify a proof; trace the pseudocode on small input.
- Diagrams: draw a tree, show authentication path for a leaf and list the sibling hashes sent.
What a full-marks answer includes:
- Clear definitions with bold naming (Merkle root, leaf hash, authentication path).
- Statement of assumptions about the hash function and how you handle non-power-of-two leaves.
- A correct diagram with labeled hashes for a tree question.
- For numericals: show each hash computation step, mention the toy hash rule used, and present the final root in bold.
- For proofs/algorithms: give pseudocode, explain time complexity, and show a short trace.
Quick check
- What is the Merkle proof size (in number of hashes) for n = 1024 leaves?
- Give two security assumptions required for a Merkle tree to prevent forgery.
- How many internal nodes are there in a full binary Merkle tree with 7 leaves?
Answers: 1) ceil(log2(1024)) = 10 hashes. 2) Collision resistance and second-preimage resistance of the hash function. 3) 7 - 1 = 6 internal nodes.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?