Section 5.43

Merkle trees and authenticated data structures

Definition

A Merkle tree is a data structure that summarizes a list of data blocks in one short value, the root hash. Each leaf holds the hash of one block, computed by a cryptographic hash function §5.1, which maps any input to a short fixed-length value. Each higher node holds the hash of its children. An authenticated data structure is a data structure kept by an untrusted party that answers each query with a proof, which the asker checks against a root it already trusts. A Merkle tree is the basic example.

The idea

A device downloads a 4 GiB disk image from servers it does not trust. It trusts one 32-byte value that the publisher signed. If that value is the hash of the whole image, the device can check nothing until all 4 GiB have arrived, and a mismatch does not say which part was bad. A signed list holding the hash of every 4 KiB block lets the device check each block on arrival, but the image has 220=1,048,5762^{20} = 1{,}048{,}576 blocks, so the list is 32 MiB, and the device needs all of it before it can check the first block. A Merkle tree gives the device both: a 32-byte trusted value, and a check of any single block with a proof of 640 bytes (the arithmetic is in the worked example).

The intuition rests on one property of a cryptographic hash function: once a hash value is fixed, no one can find a different input that produces it. A Merkle tree stacks this property. The root fixes the two hashes below it, each of those fixes the two below it, and so on down to the blocks. To check one block, the verifier needs only the hashes that sit beside the path from that block up to the root, one per level. A tournament bracket is a fair picture: to trace one player’s route to the final, you look at one opponent per round, not at every match. The picture stops being accurate in one respect. In a bracket, the winner of a match is one of the two players. In a Merkle tree, a parent is a new value computed from both children, so a change in any block changes every node on its path, including the root.

The tree is named after Ralph Merkle, who described it in 1979 [353], and is also called a hash tree. An authenticated data structure applies the same idea to other questions, such as “is this item in the set?” or “what value is stored under this key?”: an untrusted server holds the data and answers with proofs, and the client keeps only the trusted root. Certificate Transparency, a set of public logs of the certificates that web browsers rely on, uses the Merkle trees of RFC 9162. The version-control system Git and the Linux disk-checking feature dm-verity also rely on hash trees.

How it works

The rules below are those of RFC 9162. Let HH be the cryptographic hash function, for example SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), whose output is 32 bytes. The blocks are d0,d1,…,dn−1d_0, d_1, \dots, d_{n-1}, numbered from 0; the number of a block is its index, and nn is the tree size. Write a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb.

  1. Leaf hashes. For each block did_i, compute H(𝟶𝚡𝟶𝟶‖di)H(\mathtt{0x00} \mathbin{\Vert} d_i): the hash of the byte 0x00 followed by the block.

  2. Interior nodes. Combine neighboring hashes in pairs, left to right. The parent of a left hash ℓ\ell and a right hash rr is H(𝟶𝚡𝟶𝟷‖ℓ‖r)H(\mathtt{0x01} \mathbin{\Vert} \ell \mathbin{\Vert} r). Repeat level by level until one hash remains: the root. When nn is not a power of two, RFC 9162 fixes the shape: the left subtree holds the first kk blocks, where kk is the largest power of two smaller than nn, and the right subtree holds the rest. With 5 blocks, k=4k = 4.

  3. Trusted root. The root reaches the verifier through a channel it trusts. Usually the publisher signs the root and the tree size together with a digital signature §5.14, which anyone holding the publisher’s public key can check.

  4. Inclusion proof. To show that block did_i is in the tree, the prover sends the block, its index ii, the tree size nn and the sibling hashes: the hash beside the path at each level, from the leaf upwards. The proof holds at most ⌈log2n⌉\lceil \log_2 n \rceil hashes. Here log2n\log_2 n is the power to which 2 must be raised to give nn, so log21,048,576=20\log_2 1{,}048{,}576 = 20, and ⌈x⌉\lceil x \rceil is xx rounded up to a whole number.

  5. Verification. The verifier computes the leaf hash of the block. At each level it combines the current hash with the next sibling hash, putting the current hash on the left when the node on the path is a left child and on the right when it is a right child. The index and the tree size decide which. The verifier accepts when every sibling hash has been used and the final hash equals the trusted root.

In a tree whose size is a power of two, the left-or-right rule is the binary form of the index. Number the levels from j=0j = 0 at the leaves. The node on the path at level jj is a left child when bit jj of ii is 0 and a right child when that bit is 1, counting bit 0 as the lowest bit. For other sizes, RFC 9162 gives a verification algorithm §14.1 that follows the split rule of step 2.

The prefix bytes 0x00 and 0x01 keep leaves and interior nodes apart. This is an instance of domain separation §5.22: inputs of different kinds carry different tags, so that one kind can never be mistaken for the other. Without them, the input of an interior node, two 32-byte hashes side by side, is a 64-byte string that could be presented as a block. Its leaf hash would equal the interior node, so a verifier that does not fix the tree size could be made to accept a “block” made of two hashes. With the prefixes, a leaf input and an interior input differ in their first byte.

Forgery is hard for the following reason. Suppose a prover presents a block different from did_i, with sibling hashes that make the verifier reach the trusted root. Compare that computation with the honest one, starting at the root and walking down the path. Both end in the same root. At the first node on the way down where the two computations hashed different inputs, they produced the same output, so the prover has found two different inputs with the same hash: a collision. A collision-resistant §5.5 hash function makes that infeasible. If an honest party built the tree from fixed data, one of the two colliding inputs is the honest one, fixed in advance, so the prover has found a second preimage: a different input with the same hash as a given input. A second-preimage-resistant §5.4 hash function makes that infeasible too.

Worked example

This example uses a toy hash so that every step can be checked by hand. The toy is deliberately insecure. For a byte string b1,b2,…,bkb_1, b_2, \dots, b_k, let T(b1,…,bk)=(1⋅b1+2⋅b2+…+k⋅bk)mod97T(b_1, \dots, b_k) = (1 \cdot b_1 + 2 \cdot b_2 + \dots + k \cdot b_k) \bmod 97, where xmod97x \bmod 97 is the remainder after dividing xx by 97. Multiplying each byte by its position makes the result depend on the order of the bytes, as a real hash does. The four blocks are single bytes: A = 10, B = 20, C = 30 and D = 40, at indexes 0, 1, 2 and 3.

A leaf input is the byte 0 followed by the block byte vv, so its toy hash is 1⋅0+2⋅v=2v1 \cdot 0 + 2 \cdot v = 2v, reduced modulo 97. The leaf hashes are 20 for A, 40 for B, 60 for C and 80 for D. An interior input is the byte 1, then the left hash ℓ\ell, then the right hash rr, so its toy hash is (1+2ℓ+3r)mod97(1 + 2\ell + 3r) \bmod 97.

The verifier trusts the root 48.

The inclusion proof for C, index 2, holds two sibling hashes, bottom first: 80 (the leaf hash of D) and 64 (the parent of A and B). Index 2 is 10 in binary. The verifier computes the leaf hash of C, 2⋅30=602 \cdot 30 = 60. Bit 0 of the index is 0, so C is a left child: the parent is 1+2⋅60+3⋅80=3611 + 2 \cdot 60 + 3 \cdot 80 = 361, which reduces to 70. Bit 1 is 1, so that parent is a right child: the root is 1+2⋅64+3⋅70=3391 + 2 \cdot 64 + 3 \cdot 70 = 339, which reduces to 48. The result equals the trusted root, so the verifier accepts. Four leaves needed log24=2\log_2 4 = 2 sibling hashes.

Now suppose a server sends C’ = 31 with the same proof. The leaf hash is 2⋅31=622 \cdot 31 = 62. The parent is 1+2⋅62+3⋅80=1+124+240=3651 + 2 \cdot 62 + 3 \cdot 80 = 1 + 124 + 240 = 365, and 365−291=74365 - 291 = 74. The root is 1+2⋅64+3⋅74=1+128+222=3511 + 2 \cdot 64 + 3 \cdot 74 = 1 + 128 + 222 = 351, and 351−291=60351 - 291 = 60. Since 60≠4860 \ne 48, the verifier rejects. Order matters as well: a verifier that wrongly put 70 on the left at the top would compute 1+2⋅70+3⋅64=3331 + 2 \cdot 70 + 3 \cdot 64 = 333, which reduces to 333−291=42333 - 291 = 42, and would reject the genuine block.

The toy fails where a real hash must not. The block 127 has leaf hash 2⋅127=2542 \cdot 127 = 254, and 254−2⋅97=60254 - 2 \cdot 97 = 60, the same leaf hash as C. So block 127 passes with C’s proof. Finding such a collision is trivial for the toy and infeasible for SHA-256, for which no collision is known. The whole guarantee rests on the hash function.

Real sizes: the 4 GiB image of the first example, in 4 KiB blocks, has 232/212=220=1,048,5762^{32} / 2^{12} = 2^{20} = 1{,}048{,}576 blocks, so the tree has 20 levels above the leaves. An inclusion proof holds 20 SHA-256 hashes, 20×32=64020 \times 32 = 640 bytes. A flat list of all leaf hashes would hold 220×32=33,554,4322^{20} \times 32 = 33{,}554{,}432 bytes, which is 32 MiB.

Stated precisely

Let HH be the hash function and d0,…,dn−1d_0, \dots, d_{n-1} the blocks, n≥1n \ge 1. RFC 9162 calls the root the Merkle Tree Hash (MTH) of the list and defines it recursively. For one block, and for n>1n > 1 with kk the largest power of two smaller than nn: MTH(d0)=H(𝟶𝚡𝟶𝟶‖d0),MTH(d0,…,dn−1)=H(𝟶𝚡𝟶𝟷‖MTH(d0,…,dk−1)‖MTH(dk,…,dn−1)).\begin{aligned} \mathrm{MTH}(d_0) &= H(\mathtt{0x00} \mathbin{\Vert} d_0), \\ \mathrm{MTH}(d_0, \dots, d_{n-1}) &= H(\mathtt{0x01} \mathbin{\Vert} \mathrm{MTH}(d_0, \dots, d_{k-1}) \mathbin{\Vert} \mathrm{MTH}(d_k, \dots, d_{n-1})). \end{aligned} RFC 9162 defines the MTH of an empty list as the hash of the empty string. An inclusion proof for index ii in a tree of size nn is a list of at most ⌈log2n⌉\lceil \log_2 n \rceil hashes, and the verifier accepts when the recomputation described above yields the trusted root RR.

The guarantee: if HH is collision-resistant, no feasible computation produces a root RR, a tree size nn, an index ii and two different blocks, each with an inclusion proof that verifies against RR at index ii. For a tree that an honest party built from fixed data, second-preimage resistance of HH suffices to stop a forged block. The guarantee depends on four assumptions.

  1. HH is collision-resistant (or, for an honest tree, second-preimage-resistant).

  2. Leaf inputs and interior inputs are encoded differently, and the verifier fixes the tree size.

  3. The verifier received RR through an authenticated channel, such as a signature it checked.

  4. The verifier checks that the index in the proof is the position it asked about.

A verified inclusion proof establishes exactly this: the block is at position ii in the list of nn blocks that RR summarizes. It establishes nothing about who produced RR, when, or whether the data in the list is true.

Limits and common mistakes

The root needs an authority. A proof connects a block only to the root the verifier holds. If an attacker can supply the root, every block of the attacker’s choosing verifies.

A correct proof against an old root is still correct. An attacker who replays an old signed root and its old data passes every inclusion check. Freshness needs a separate mechanism, such as a version number that the verifier never lets decrease, called a version high-water mark §8.27.

An inclusion proof cannot show that an item is absent, or that a list of query results is complete. Proving absence needs a tree whose leaves are sorted by key, so that a proof can show the two neighboring leaves between which the missing key would sit. Showing that a newer tree only extends an older one, as an append-only log §8.13 must, needs a consistency proof, which RFC 9162 also defines. Nor does one proof show that every user sees the same root: a log that shows different roots to different users is detected only when users compare roots.

Membership says nothing about content. A malicious or false record in the tree verifies as well as a true one.

Shape rules matter. The digital currency Bitcoin builds a Merkle tree over each batch of payment records that it adds to its ledger, and it pairs the last hash of a level with a copy of itself when the level has an odd number of hashes. As a result, a list with an odd number of payment records and the same list with its last record repeated have the same root. The resulting flaw is recorded as CVE-2012-2459 (Common Vulnerabilities and Exposures entry 2012-2459). A tree definition must give every distinct list a distinct root.

Merkle trees are not the Merkle–Damgård construction §15.8. The second is a way of building a hash function from a fixed-size compression step; it shares only the inventor’s name.

Questions answered

How does an inclusion proof connect one block to a trusted root, and what does it not prove?

An inclusion proof connects a block to a trusted root by letting the verifier recompute that root from the block and a few hashes from the Merkle tree. A Merkle tree summarizes a list of blocks in one root hash: each leaf is the hash of a block, and each higher node is the hash of its two children. The proof carries the block’s index, the tree size and the sibling hashes, meaning the hash beside the path at each level from the block up to the root. The verifier hashes the block, then combines the result with each sibling in turn, placing it left or right as the index dictates, and compares the final value with the root it trusts. A match means that this exact block sits at this position in the list the root summarizes, unless someone has found a collision in the hash function, two inputs with the same hash. The proof needs only about log2n\log_2 n hashes for nn blocks: 20 hashes for about a million blocks.

The proof does not prove that the root is authentic; that needs a signature or another trusted channel. It does not prove that the root is current, because an old signed root verifies old data. It does not prove that an item is absent, that a set of results is complete, or that other users see the same root; those need non-membership proofs, consistency proofs and comparison of roots between users. And it does not prove that the block’s content is true or harmless.

Related entries: Cryptographic hash function (§5.1), Integrity (§1.9), Downgrade attack (§1.27). Further reading: [97, 353].