arthrop0d

Brief articles on varied topics.

Merkle Proofs and Root Hash Verification

cryptocurrencyalgorithmscryptography

A Merkle proof is a small bundle of hashes and position information that lets a verifier show that one data item belongs to a larger, hash-committed dataset. The verifier hashes the item, combines that result with the proof’s sibling hashes, and checks whether the resulting value matches a known Merkle root.

The problem Merkle proofs solve

Suppose a blockchain node, storage service, or database claims that a particular transaction or record is part of a dataset. Sending the entire dataset would let you check the claim, but that becomes impractical when the dataset contains millions of entries.

A single hash of the dataset is compact, but it is not enough to check one item. A verifier needs some evidence connecting the item to that hash. A Merkle tree provides that evidence in a logarithmic-size proof: for n leaves, the proof normally contains about log2(n) sibling hashes rather than all n leaves.

The process works only if the verifier already has an authentic root hash. That root might be signed, included in a trusted block header, or obtained from another authenticated source. A proof by itself does not establish that the root is genuine.

Building the tree

A Merkle tree is a binary tree whose bottom-level values, called leaves, represent the dataset items. Each leaf is hashed. Then pairs of neighboring hashes are combined and hashed again to form the next level. This repeats until one hash remains: the root.

For four items, the structure looks like this:

                 root
                /    \
             H01      H23
             /  \     /  \
           H0   H1   H2   H3
           |    |    |    |
          item0 ...      item3

Here, H0 is the hash of item0, H01 is the hash of the ordered concatenation of H0 and H1, and root is the hash of H01 followed by H23.

Real protocols must define details such as whether leaves and internal nodes use different prefixes, how an odd number of leaves is handled, and how item ordering is determined. These rules are part of the tree’s format; two implementations using different rules will produce different roots.

A common defensive convention is domain separation, which means hashing leaves and internal nodes with different prefixes so their inputs cannot be confused:

import hashlib


def leaf_hash(data):
    return hashlib.sha256(b"\x00" + data).digest()


def parent_hash(left, right):
    return hashlib.sha256(b"\x01" + left + right).digest()

The prefixes are not universal Merkle-tree requirements. They illustrate that the verifier must use exactly the same construction as the producer.

What a proof contains

For one target item, a proof commonly contains:

Consider a proof for item1 in the four-item tree. The verifier has item1, the sibling hash H0, and the sibling hash H23. It does not need item0, item2, or item3 individually. H0 summarizes the first missing item, while H23 summarizes the entire other half of the tree.

The position information matters because hash inputs are ordered. In general,

hash(A || B) != hash(B || A)

where || means concatenation. Knowing the sibling hash but not which side it occupies would leave the verifier unable to reconstruct the correct parent.

How verification reconstructs the root

Verification starts at the target leaf:

  1. Hash the supplied item to produce its leaf hash.
  2. Take the first sibling hash from the proof.
  3. Put the two hashes in their specified left-to-right order and hash them to produce the parent hash.
  4. Repeat with the next sibling, moving one level upward each time.
  5. Compare the final computed hash with the expected root.

A compact verifier might look like this:

def verify(item, proof, expected_root):
    current = leaf_hash(item)

    for sibling, sibling_is_left in proof:
        if sibling_is_left:
            current = parent_hash(sibling, current)
        else:
            current = parent_hash(current, sibling)

    return current == expected_root

In this example, each proof entry contains a sibling hash and a Boolean saying whether that sibling is on the left. A real wire format might encode the positions as bits alongside a list of hashes, but the information is the same.

For item1, the first step combines H0 and H1 as parent_hash(H0, H1), producing H01. The next combines H01 with H23, producing the root. If the result equals the trusted root, the item is included in the tree defined by that root.

What the result proves

A successful proof says: given this root and this tree-construction rule, this item occupies this position in the committed dataset. It does not prove that the dataset is current, that the item is semantically valid, or that the root came from an honest source. Those properties require additional protocol rules and authentication.

The security argument relies on the hash function’s collision resistance: it should be computationally infeasible to find two different inputs with the same hash. Otherwise, an attacker might substitute data while preserving the root. The root acts as a compact commitment, and the proof supplies the missing path needed to test one inclusion claim.

That is why Merkle proofs often appear in blockchain RPC responses, light-client protocols, content-addressed storage, and error messages describing a “proof path” or “invalid root.” The verifier exchanges one item and a handful of hashes instead of downloading the entire dataset. As the dataset grows, the dataset itself may grow linearly while the proof grows only logarithmically.

← All articles