Domain-Separated Hashing (SHA-256 Byte Prefixes)
How Truestamp separates SHA-256 hash contexts with single-byte domain prefixes, keeping leaf hashes, node hashes, and each kind of hashed object in distinct hash spaces to prevent ambiguity and second-preimage confusion.
Overview
A hash function like SHA-256 takes any input and produces a fixed 32-byte fingerprint. Truestamp uses SHA-256 everywhere, for many different kinds of data: user submissions, blocks, entropy observations, the nodes of a Merkle tree, and more. If every one of those used plain SHA-256, a fingerprint computed for one purpose could be reinterpreted as a fingerprint for another purpose. Domain separation fixes this by putting a single distinguishing byte (a “domain prefix”) at the front of the input before hashing. The same bytes hashed under two different prefixes produce two unrelated fingerprints, so each context gets its own private hash space that cannot be confused with any other. Not every large system does it: Bitcoin’s original Merkle tree hashes leaves and internal nodes the same way and carries documented second-preimage weaknesses as a result, and Ethereum’s trees use a different hash function and a different structure entirely.
Why hashing needs domain separation
A hash on its own answers “here is a 32-byte fingerprint,” but it does not say what the fingerprint is a fingerprint of. That missing context is where attacks live.
Consider two completely different things that both get hashed:
- a piece of user-submitted data, and
- an internal record the system generated.
If both are hashed with plain SHA-256(data), then a fingerprint the system produced for one is byte-for-byte a valid fingerprint for the other. An attacker who can get a value accepted in the “cheap” context can try to pass it off in the “trusted” context, because nothing in the fingerprint distinguishes the two. This is a form of second-preimage confusion: the attacker does not need to break SHA-256 itself, they just need two contexts to accept the same fingerprint.
Domain separation removes that shared surface. Each context prepends its own reserved byte, so:
SHA-256(0x10 || data)(one context), andSHA-256(0x20 || data)(a different context)
are different fingerprints even when data is identical. A value that is legitimate in the first context is meaningless in the second, because recomputing it there would require the second context’s prefix, which it does not have. The contexts are cryptographically walled off from one another.
How a byte prefix works
A domain prefix is exactly one byte placed immediately before the data, and then the whole thing is hashed as a unit. In Truestamp the core operation is simply SHA-256(prefix_byte || data), where || means “followed by.” Every kind of record Truestamp hashes is a JSON object, and the data after the prefix is its JSON Canonicalization Scheme (JCS, RFC 8785) encoding: sorted member names, no whitespace, one spelling for every value.
For example, an item’s claims (the statements the submitter made) are hashed under the claims prefix 0x10:
claims_hash = SHA-256(0x10 || JCS(claims))
You can reproduce this in any language. In JavaScript:
const PREFIX_CLAIMS_HASH = 0x10;
const data = new TextEncoder().encode(claimsJcs);
const preimage = new Uint8Array([PREFIX_CLAIMS_HASH, ...data]);
const hash = await crypto.subtle.digest("SHA-256", preimage);
In Python:
import hashlib
PREFIX_CLAIMS_HASH = b"\x10"
digest = hashlib.sha256(PREFIX_CLAIMS_HASH + claims_jcs).hexdigest()
Both produce the same 64-character hex fingerprint that Truestamp stores. The claims {"name":"Q3 report"}, already in their JCS form, hashed under 0x10 give e6a5d22776ffd78467732f4c7dc976cc702fe5a962d3b20fb76f7ddfb01a9207, and the same bytes hashed under any other prefix give a completely unrelated value. That is the whole mechanism: one byte, prepended, changes which hash space the fingerprint belongs to.
Why a single byte is enough
A prefix only needs to be unique per context, not secret or large. A single byte gives 256 possible domains, which is far more than Truestamp needs, and it costs just one byte of overhead per hash. An alternative design uses a human-readable string tag (for example "items-claims:" in front of the data), but that adds tens of bytes of overhead per hash and requires more care to implement identically across languages. The single reserved byte is the minimal, unambiguous way to say “this fingerprint belongs to this domain.”
Separating leaf hashes from node hashes
The most important use of domain separation is inside a Merkle tree, the structure that lets Truestamp prove one item was included in a batch. A Merkle tree has two kinds of hashes:
- leaf hashes, computed from the 32-byte hash of the data being included, and
- internal node hashes, computed by combining two child hashes.
Each gets its own reserved prefix:
- a leaf is hashed as
SHA-256(0x00 || item_hash), and - an internal node is hashed as
SHA-256(0x01 || left_child || right_child).
Without those prefixes, an attacker could take an internal node (which is a combination of two hashes) and present it to a verifier as if it were a leaf, or take a leaf and claim it is an internal node. Because leaves and nodes would live in the same hash space, a single 32-byte value could be valid in both roles, opening a second-preimage substitution attack against the proof. The 0x00 / 0x01 split makes a leaf hash and a node hash structurally different pre-images, so a value that is a legitimate leaf can never be reinterpreted as a legitimate node, and vice versa. Truestamp adds a second, independent guardrail here: every hash fed into the tree must be exactly 32 bytes, so an internal node (which is built from 64 bytes of input) can never masquerade as a leaf input on length alone.
Separating different kinds of hashed objects
Beyond the tree’s own leaf and node prefixes, Truestamp reserves a distinct prefix for every different kind of object it hashes. Each domain gets its own reserved value so that, for instance, a fingerprint of user claims can never be confused with a fingerprint of an entropy observation or of a block, even if the underlying bytes happened to match. The prefixes are grouped into families by their first hex digit, in the order Truestamp builds the values from the leaves up: 0x1X for items, 0x2X for entropy, 0x3X for blocks, 0x4X for epochs and 0x5X for proofs, with 0x6X for keys beside that path. The values are defined once in a single canonical registry and reused everywhere, so the same prefix always means the same thing across the whole system and across independent re-implementations. Within each family the last digit distinguishes the kinds, so a single reserved byte identifies both the family and the exact object it covers. Three kinds are signed with Ed25519: a block record under 0x31, a key handover under 0x62 and a proof under 0x50. Each signature is made over the 32 bytes of that kind’s hash, so a signature made for one kind can never verify as a signature for another.
When a new use does not need a new prefix
A prefix separates hash contexts, not hash purposes. Getting that distinction right is what keeps the registry small and stable, and the witnesses an item commits to are the clearest example.
An item’s record names each witness the item commits to in its witness_hashes and stores
that witness’s hash.
A verifier recomputes any witness whose details the proof bundle
carries and compares the result against the committed hash. None of those recomputations
introduces a prefix:
- the
blockwitness is the head block’s ownblock_hash, recomputed under the block-hash prefix0x31, and - every entropy witness is that observation’s own
entropy_hash, recomputed under the entropy prefix0x20.
The same holds for the block a proof carries for its signing key event: it is a block node like any other, so its hash is recomputed under that same block-hash prefix.
The reason is that the witness hash is not a new kind of fingerprint. It is the block’s hash, or the observation’s hash, quoted in another place. Minting a separate prefix for “the same hash, used as a witness” would produce a value that does not equal the object’s real hash, so a verifier could not compare the two, and the registry would grow an entry that names a role rather than an object.
The beacon tools are a second example. The coin toss, the dice roll, the drawing, the lottery and the random integer all read the block’s own hash, its 64 hex digits taken as one 256-bit number, and hash nothing. That hash is already a full SHA-256 output in the block’s own hash space, so hashing it again under a prefix for each tool would give a verifier nothing more to check, and the registry would grow entries that name uses of the beacon rather than objects.
The rule this illustrates: reserve a new prefix when a genuinely different kind of object, or a differently-shaped preimage, enters the system. Do not reserve one when an existing fingerprint is merely being referenced somewhere new. Adding a new witness type is therefore a registry-of-names change and not a registry-of-prefixes change, which is why new witness types can be introduced without disturbing any existing hash.
What domain separation does and does not protect
Domain separation is a targeted defense. It is worth being precise about its scope.
It provides:
- Context isolation. A fingerprint computed in one domain cannot be reused as a valid fingerprint in another, because the reserved prefix byte differs. This blocks cross-context and second-preimage substitution attacks without relying on the strength of the surrounding protocol.
- Structural distinctness in the tree. Leaf hashes and internal-node hashes occupy separate hash spaces, so proof paths cannot be forged by swapping one role for the other.
It does not, by itself, provide:
- Collision resistance or preimage resistance. Those come from SHA-256 itself, not from the prefix. Domain separation ensures collisions and preimages found in one domain stay confined to that domain.
- Unambiguous field boundaries. When a record holds several members (as the item record holds its claims hash, its id and its witness hashes), the prefix alone does not keep the members apart. The encoding does: every record is hashed as the JCS of a JSON object, and JCS gives exactly one byte string for each value Truestamp hashes, so two different records never share a preimage and no length framing is needed. Domain separation and an unambiguous encoding are complementary defenses, not substitutes for each other.
A record never contains its own hash or its own signature, so nothing in a preimage depends on the value being computed from it.
Citations
- RFC 6962: Certificate Transparency. Source of the
0x00leaf and0x01internal-node prefixes that Truestamp’s Merkle leaf and node hashing follows. - RFC 8785: JSON Canonicalization Scheme (JCS). The canonical encoding every record is hashed in, which is what keeps the members of a record unambiguous without length framing.
- Byte prefix
- Byte-Prefix Registry
- Claims hash
- Domain separation
- Ed25519 Signatures
- Entropy Verification Levels
- Improving the Efficiency and Reliability of Digital Time-Stamping
- Item Ownership vs Authorship
- Merkle leaf
- RFC 6962
- RFC 6962: Certificate Transparency
- RFC 8785: JSON Canonicalization Scheme (JCS)
- Secure Hash Standard (SHS)
- Some Aspects of Merkle Tree - DEV Community
- The Item Record and Its Hash
- The Proof Lifecycle
- Truestamp's Merkle Tree
- UUIDv7 Identifiers
- Verify a Proof