Knowledge Base

Browse the concepts behind Truestamp. Follow the links between concepts, or search across everything.

Truestamp's Merkle Tree

Truestamp's binary Merkle hash tree at the block and epoch levels, the RFC 9162 Merkle Tree Hash over leaves sorted by key with no padding, its 0x00 leaf and 0x01 node prefixes over SHA-256, how the root is computed, and the leaf count committed beside every root.

Overview

A Merkle tree reduces a whole set of entries down to a single fingerprint, called the root, in a way that lets anyone later prove that one particular entry was part of the set without revealing or re-hashing the rest. An entry is a key plus a SHA-256 digest already computed over whatever that key names, so a single tree can hold entries of more than one kind: a Truestamp block’s tree holds both submitted items and the external entropy observations captured for that block, each by its record hash (an item’s item_hash, an observation’s observation_hash). Truestamp builds the same tree one level up too: an epoch’s tree holds the block_hash of each block the epoch covers, and the epoch’s hash is what is written to Stellar and Bitcoin. Every entry’s digest becomes a leaf, pairs of hashes are combined into node hashes level by level, and the single hash left at the top is the root. The tree is exactly the Merkle Tree Hash of RFC 9162 section 2.1.1, computed over the entries in key order. Truestamp’s implementation is pure Elixir with no dependencies beyond the standard cryptographic and hex-encoding libraries, which makes it small enough to audit line by line and straightforward to reimplement in any other language.

Leaf, node, and root

Three words describe the whole structure.

  • A leaf is the bottom of the tree: one input entry, represented by its pre-computed SHA-256 hash. Truestamp does not hash your original document. You (or Truestamp) compute a SHA-256 hash of the data first, and that 32-byte hash is what enters the tree as a leaf.
  • A node (also called an internal node) sits above the leaves. Each node is the hash of the two hashes directly beneath it, its left child and its right child. Nodes combine two hashes into one, so each level up the tree has half as many hashes as the level below, rounded up.
  • The root is the single hash at the very top, the one value that summarizes every leaf. Change any leaf, add a leaf, remove a leaf, or reorder the leaves, and the root changes. In Truestamp the root of a block’s tree is recorded as that block’s Merkle root and folds into the block’s overall fingerprint.

The tree is a binary tree: every node has exactly two children. When the number of leaves is not a power of two the tree is unbalanced, with a shorter right-hand side, and nothing is added to even it out.

How the root is computed

Building the tree is a repeated pairing-and-hashing process that starts at the leaves and works upward.

  1. Order the entries. The input entries are always sorted by their key, comparing raw key bytes ascending, so the same set of entries always produces the same root regardless of the order they were supplied in. Each entry’s key must also be unique within one tree, because the key is how a proof is later looked up. Two entries carrying the same key are refused rather than quietly collapsed into one leaf, so a tree either holds every entry it was handed or does not build at all.
  2. Hash each leaf. Each leaf’s 32 digest bytes are prefixed with a single 0x00 byte and run through SHA-256. In shorthand: leaf = SHA-256(0x00 || entry_hash).
  3. Hash pairs up the levels. Adjacent hashes are paired left and right, joined with a 0x01 prefix, and hashed: node = SHA-256(0x01 || left || right). This produces the next level up. When a level has an odd number of hashes, the last one has no partner and is carried up to the next level unchanged: it is not duplicated, and no padding hash is invented for it. The same pairing repeats on each new level until a single hash remains.
  4. That last hash is the root.

Level by level, this builds the same tree as RFC 9162’s recursive definition: for more than one leaf, take k as the largest power of two smaller than the leaf count, build the left subtree from the first k leaves and the right subtree from the rest, and hash the two subtree roots together as a node. Five leaves, for example, split into a balanced tree of four on the left and the fifth leaf alone on the right, so the root is SHA-256(0x01 || root_of_first_four || leaf_5).

An empty tree (no entries at all) has a special defined root: the SHA-256 hash of the empty input, e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. That root is not the leaf hash of anything, so no inclusion proof can ever check out against it. A tree with a single entry has that one entry’s leaf hash (SHA-256(0x00 || entry_hash)) as its root; there are no siblings to pair, so no further hashing happens.

The tree refuses to build past 40 levels, which already allows for roughly a trillion leaves. This is an arithmetic guard against a nonsensical depth, not a load limit: Truestamp never commits a tree of more than 2^32 leaves, so every inclusion proof into one of its trees has at most 32 steps.

Domain separation: the 0x00 and 0x01 prefixes

The 0x00 byte in front of every leaf and the 0x01 byte in front of every node are not decoration. They are domain separation, and they are what make the tree secure against a class of forgery called a second preimage attack.

Without a prefix, a leaf hash and a node hash would be computed the same way, so an attacker could take an internal node’s hash and pass it off as if it were a leaf, presenting a fake but valid-looking proof. By hashing leaves with a 0x00 prefix and nodes with a 0x01 prefix, a leaf can never be mistaken for a node or vice versa: the two are drawn from separate, non-overlapping input spaces. It is worth being clear about what does not do this: Bitcoin’s original Merkle tree hashes leaves and internal nodes identically, which is exactly the confusion described above, and it separately duplicates the final hash on an odd level, a rule with its own documented root-collision weakness (CVE-2012-2459). Truestamp carries an odd hash up unchanged instead, so no two different leaf sets share a root that way. Ethereum’s Merkle Patricia trie and its SSZ trees are different constructions again, and neither is this one. Truestamp applies this same single-byte-prefix discipline across all of its hashing, which is covered in hashing and domain separation.

Reproducing a root

Because the tree is the standard one, any RFC 6962 or RFC 9162 Merkle Tree Hash implementation reproduces a Truestamp root for every leaf count, once it is handed the leaves in Truestamp’s key order. A verifier written from scratch in any language needs five rules:

  • 0x00 in front of every leaf input.
  • 0x01 in front of every joined pair of child hashes.
  • SHA-256 throughout.
  • Leaves ordered by raw key bytes ascending, never a locale-aware comparison.
  • For more than one leaf, split at the largest power of two below the leaf count (equivalently, carry an unpaired last hash up a level unchanged), with no padding and no duplicated leaves.

Every leaf input must also be exactly 64 lowercase hex characters, a 32-byte digest. Uppercase input, a short or long string, or anything that is not a hex digest is rejected rather than coerced.

The leaf count is committed with the root

A root commits to its tree’s size, but a verifier cannot read the size out of it, and the inclusion walk does not check it: the same path can reach the same root under another size, and the leaf’s index can move with the size. So every tree whose root Truestamp commits also commits its leaf count, as a Certificate Transparency signed tree head carries the tree size beside the root. A block record holds its tree’s merkle_root and leaf_count, and is hashed into the block_hash; an epoch record is exactly {leaf_count, merkle_root}, and is hashed into the epoch_hash written to the chains. A verifier checks, for every inclusion proof, both that its walk reaches the record’s merkle_root and that its tree_size equals the record’s leaf_count. In every block record, leaf_count is 0 exactly when merkle_root is the empty root, as in every genesis and key-event block, which include nothing.

What this tree does not do

The tree proves that a leaf belonged to a specific root. It is not an append-only transparency log, and it deliberately leaves out the machinery that would make it one: there are no consistency proofs between trees and no third-party log monitoring. Tamper evidence over time comes from somewhere else entirely, namely the block hash chain, the signatures over each block record, and the commitment of each epoch’s hash to public blockchains.

What the tree is used for

Reducing a set of entries to one root hash is only useful if you can later prove a single entry belonged to that set. That is the job of an inclusion proof: a short list of sibling hashes that lets anyone recompute the path from one leaf back up to the root, confirming the entry was in the tree, without needing every other entry. Truestamp uses this to prove that a specific submitted item was part of a specific block. The inclusion proof walk, and how it is verified, are covered in the inclusion proofs concept.

The root is also compact and stable, which is what lets Truestamp fold an entire block’s worth of entries, the submitted items and the block’s external entropy observations alike, into a single value that the block record holds, and then fold many block hashes into the one Merkle root that an epoch record holds. This concept covers the tree and its root only; the proof mechanics and the commitment steps that build on it live in their own concepts.

Citations

  1. RFC 9162: Certificate Transparency Version 2.0. Section 2.1.1 defines the Merkle Tree Hash this tree computes: the 0x00 leaf and 0x01 node prefixes over SHA-256, the empty-tree root as the hash of the empty input, the single-leaf root, and the split at the largest power of two below the leaf count. Truestamp’s only addition is the byte-wise key sort that fixes the leaf order.
  2. RFC 6962: Certificate Transparency. The original definition of the same Merkle Tree Hash, which RFC 9162 obsoletes without changing it, so a verifier written strictly to either RFC reproduces a Truestamp root for every leaf count.
  3. CVE-2012-2459. The Bitcoin Merkle root collision named above, which comes from duplicating the last hash on an odd level rather than from the missing leaf and node prefixes.
Referenced by