Merkle tree
One hash for many blocks
A Merkle tree turns a list of data blocks into a single hash — the root. Every block is a leaf: SHA-256 of the byte 0x00 and the block. Every node above is SHA-256 of the byte 0x01 and the hashes of its two children. Anyone who knows the root (signed, say) can check that a block belongs to the tree with just the hashes of the neighbours on the way to the root — one per level: 4 hashes for 16 blocks, 20 for a million. The toy computes exactly as RFC 9162 §2.1 says. The number of blocks need not be a power of two: the list splits after k blocks, where k is the largest power of two smaller than n, so the shape of the tree depends only on n and nothing duplicates the last block. Change a block and every hash on its way to the root changes. Pick a block and walk through the verification of its proof step by step.
A tree of your blocks
An empty line is an empty block. A leaf is SHA-256(0x00 ‖ block), a node SHA-256(0x01 ‖ left ‖ right) — as in RFC 9162.
-937 B too long. Shorten the text — the toy works on small data.
A leaf that is a node
A swapped block cannot be hidden — try it in the toy’s “Break it” mode above. The changed leaf changes every hash on the way to the root, and for the old proof to still fit, the new data would have to give the same hash as the old: a second preimage or a collision of SHA-256 (see the Hash functions station).
The tree falls to a design mistake. If a leaf is H(d) and a node is H(l ‖ r), every node is also the leaf of a 64-byte “block” l ‖ r. The attacker takes the nodes right above the leaves and hands in their children as the blocks of a shorter list — that list has the same root, although nobody added it. RFC 9162 (like RFC 6962 before it) prepends the byte 0x00 to a leaf and 0x01 to a node, so a leaf hash and a node hash are never computed from the same bytes; the RFC states that this domain separation is required for second preimage resistance. Bitcoin’s tree has no prefixes, which is why a transaction of exactly 64 bytes (without witness data) can pose as a node: a light client (SPV) then accepts an inclusion proof for a transaction that is not in the block — the consensus change proposal BIP 54 makes such transactions invalid. The same tree duplicates the last hash of a level with an odd number of nodes, so the lists [a, b, c] and [a, b, c, c] share one root (comment in the Bitcoin Core code). Bitcoin-Qt and bitcoind before 0.6.1 (fixed in May 2012) remembered a block with such duplicated transactions as invalid and then rejected the real block with the same hash, which allowed splitting the chain (CVE-2012-2459).
A node passed off as a leaf
Deliberately broken implementationLeaf H(d) and node H(l ‖ r) without prefixes — only to show the attack.
- 0 Alice → Bob: $20
- 1 Bob → Carol: $5
- 2 Carol → Dave: $12
- 3 Dave → Eve: $7
Where trees guard data
- Certificate TransparencyCertificate authorities add the TLS certificates they issue to public logs, and a log signs the root of a tree over all its entries — the same tree as in the toy. An inclusion proof shows that a certificate is in the log; a consistency proof (§2.1.4) shows that the new tree only appended entries to the old one, changing or removing nothing.Source (Certificate Transparency)
- GitA tree object (a directory) holds the hashes of its files and subdirectories, and a commit holds the hash of the project tree and the hashes of its parent commits. It is a graph of hashes rather than a binary tree, but it works the same way: changing any file in history changes the hashes of every later commit.Source (Git)
- BitcoinThe transactions of a block form a Merkle tree, and only its root goes into the block’s hash (§7). A light client (SPV, §8) keeps only block headers and, for its own transaction, fetches the branch of the tree that links it to the block.Source (Bitcoin)
- BitTorrent v2 (BEP 52)Every file has its own SHA-256 tree over 16 KiB blocks; the .torrent file holds its root (
pieces root) and, for files larger than a piece, one layer of hashes — one per piece. Identical files always have the same root.Source (BitTorrent v2 (BEP 52)) - Sigstore RekorRekor is a transparency log in which software maintainers and build systems record signed metadata. It can be asked for an inclusion proof of an entry, and auditors check its consistency: that the log only grows and no entry is changed or removed.Source (Sigstore Rekor)
- SLH-DSA signatures (FIPS 205)NIST’s post-quantum signature standard of August 2024 relies on hash functions alone. One-time WOTS+ keys are the leaves of Merkle trees (XMSS), the trees are stacked into a hypertree, and the public key contains the root of the top one; a signature carries authentication paths.Source (SLH-DSA signatures (FIPS 205))
Domain separation and ready-made libraries
For a log with proofs, take a ready-made CT tree instead of your own — in Go, transparency-dev/merkle computes and verifies proofs and Tessera runs a whole log. If you must write the tree yourself, follow RFC 9162: prefix 0x00 for leaves and 0x01 for nodes, the split after k with no duplicated last block, verification with the leaf index and the tree size, and a root that is signed or comes from a trusted source. When a leaf is made of several fields, encode their lengths (CT encodes entries as TLS structures with lengths), because “ab” ‖ “c” and “a” ‖ “bc” are the same bytes.
const sha256 = async (...parts) => new Uint8Array(
await crypto.subtle.digest('SHA-256', await new Blob(parts).arrayBuffer()),
);
// RFC 9162 §2.1.1: leaves and nodes in separate domains
const leafHash = (data) => sha256(Uint8Array.of(0x00), data);
const nodeHash = (left, right) => sha256(Uint8Array.of(0x01), left, right);static byte[] LeafHash(byte[] data) => SHA256.HashData([0x00, .. data]);
static byte[] NodeHash(byte[] left, byte[] right) =>
SHA256.HashData([0x01, .. left, .. right]);Do not hash leaves and nodes with the same H(d) and H(l ‖ r), and do not duplicate the last hash of an odd level — both mistakes give the same root for different lists of blocks. Do not accept a proof without checking the leaf index and the tree size, and do not compare it with a root supplied by the same sender as the proof.
// ✕ no prefixes: the node H(l ‖ r) is also the leaf of block l ‖ r
const leafHash = (data) => sha256(data);
const nodeHash = (left, right) => sha256(left, right);
// ✕ duplicating the last hash as in Bitcoin:
// [a, b, c] and [a, b, c, c] have the same root
if (level.length % 2 === 1) level.push(level.at(-1));