How do you check one leaf in a Merkle tree?

A short proof lets you check one item against a trusted root hash without hashing the entire collection again.

The worked example below traces the proof for leaf B.

Where left and right matter

A Merkle tree hashes items at the leaves and combines pairs of hashes up to a single root. In this tree the items are Oak (A), Elm (B), Ash (C) and Yew (D). A and B form branch AB on the left. C and D form branch CD on the right.

Left and right tell you how to assemble the input before calling SHA-256. Start with the hex byte 0x01, append the left child’s 32 hash bytes, then append the right child’s 32 hash bytes. Hash that one 65-byte sequence. The hex strings on screen are a way to display the bytes.

Two steps from leaf B to the root

First compute B’s own leaf hash by hashing 0x00 followed by the bytes for Elm. Left and right have no role in this leaf calculation. They come into play when you have two child hashes to combine.

  1. Rebuild parent AB. The proof supplies A’s hash. A is the left child and B is the right child, so hash 0x01 followed by A’s hash bytes and then B’s hash bytes.
  2. Rebuild the root. The proof supplies the CD branch hash. AB is the left child of the root, so hash 0x01 followed by the rebuilt AB hash bytes and then the supplied CD hash bytes.

Try Swap the first pair with B selected. Now the parent input has B’s hash bytes before A’s. That changes the parent hash and the rebuilt root fails the comparison. A’s hash stands in for item A, and the CD hash already summarizes C and D, so the proof can supply those hashes without the original items.

The inputs here use SHA-256 as in Lesson 4.1. The different prefixes distinguish hashing a leaf from hashing a pair, as specified in RFC 9162’s tree definition.

Expected hashes for leaf B (hex prefixes only)
ValueHash prefix
Supplied A hash77457e949e72…
Supplied CD hashc1546c119aec…
Rebuilt AB hash7155dc08a674…
Rebuilt and trusted root90f041447aaa…

The check uses all 64 hex digits of each digest. The table shows only the first 12 to make the steps easier to scan.

Compare with a root you already trust

The root commits to the items and their order. A match gives strong evidence that the item is included at the selected position in the tree behind that root. As in Lesson 4.4, the reference needs a basis for trust. When you have no independent reason to trust the root, a match only checks consistency with the root you were given. See the RFC’s inclusion proof definition.

Check your understanding

To check leaf C, which two sibling hashes do you need? Where does each go?

Show the answer

Put leaf D’s hash to the right of C’s hash to rebuild the right branch. Then put the supplied AB branch hash to the left of the rebuilt right branch.