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 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 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 , numbered from 0; the number of a block is its index, and is the tree size. Write for the byte string followed by the byte string .
Leaf hashes. For each block , compute : the hash of the byte
0x00followed by the block.Interior nodes. Combine neighboring hashes in pairs, left to right. The parent of a left hash and a right hash is . Repeat level by level until one hash remains: the root. When is not a power of two, RFC 9162 fixes the shape: the left subtree holds the first blocks, where is the largest power of two smaller than , and the right subtree holds the rest. With 5 blocks, .
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.
Inclusion proof. To show that block is in the tree, the prover sends the block, its index , the tree size and the sibling hashes: the hash beside the path at each level, from the leaf upwards. The proof holds at most hashes. Here is the power to which 2 must be raised to give , so , and is rounded up to a whole number.
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 at the leaves. The node on the path at level is a left child when bit of 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 , 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 , let , where is the remainder after dividing 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 , so its toy hash is , 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 , then the right hash , so its toy hash is .
Parent of A and B: , and .
Parent of C and D: , and .
Root: , and .
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, . Bit 0 of the index is 0, so C is a left child: the parent is , which reduces to 70. Bit 1 is 1, so that parent is a right child: the root is , which reduces to 48. The result equals the trusted root, so the verifier accepts. Four leaves needed sibling hashes.
Now suppose a server sends C’ = 31 with the same proof. The leaf hash is . The parent is , and . The root is , and . Since , the verifier rejects. Order matters as well: a verifier that wrongly put 70 on the left at the top would compute , which reduces to , and would reject the genuine block.
The toy fails where a real hash must not. The block 127 has leaf hash , and , 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 blocks, so the tree has 20 levels above the leaves. An inclusion proof holds 20 SHA-256 hashes, bytes. A flat list of all leaf hashes would hold bytes, which is 32 MiB.
Stated precisely
Let be the hash function and the blocks, . RFC 9162 calls the root the Merkle Tree Hash (MTH) of the list and defines it recursively. For one block, and for with the largest power of two smaller than : RFC 9162 defines the MTH of an empty list as the hash of the empty string. An inclusion proof for index in a tree of size is a list of at most hashes, and the verifier accepts when the recomputation described above yields the trusted root .
The guarantee: if is collision-resistant, no feasible computation produces a root , a tree size , an index and two different blocks, each with an inclusion proof that verifies against at index . For a tree that an honest party built from fixed data, second-preimage resistance of suffices to stop a forged block. The guarantee depends on four assumptions.
is collision-resistant (or, for an honest tree, second-preimage-resistant).
Leaf inputs and interior inputs are encoded differently, and the verifier fixes the tree size.
The verifier received through an authenticated channel, such as a signature it checked.
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 in the list of blocks that summarizes. It establishes nothing about who produced , 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 hashes for 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].