Crypto & Security

Chapter 4
Hash Functions, Merkle Structures and Data Integrity

Diploma in Blockchain
Crypto & Security   •   Diploma

Hash Functions, Merkle Structures and Data Integrity

Introduction

Blockchains are often described as immutable ledgers, but immutability is not a magical property of distributed databases. It is achieved through several interacting mechanisms: cryptographic hashing, digital signatures, consensus, replicated storage and economic incentives. This chapter examines hashing and Merkle structures, which provide the basic evidence that data has not been altered and allow large collections of transactions to be represented compactly.

A cryptographic hash function converts an input of arbitrary length into a fixed-length output called a digest, hash value or message digest. The function is deterministic: the same input always produces the same output. However, a secure hash should make it computationally infeasible to reverse the process, find a different input with the same output, or deliberately construct two inputs that collide.

Merkle trees extend the usefulness of hashes. They organize many records into a tree whose single root hash commits to the entire collection. A Merkle proof can then demonstrate that one record belongs to the committed collection without transmitting every other record. In blockchain systems, these ideas support block linking, transaction verification, mining, light-client operation and tamper evidence.

Learning Objectives

By the end of this chapter, students should be able to:

  • explain the operation and security properties of cryptographic hash functions;
  • distinguish preimage, second-preimage and collision resistance;
  • calculate basic hash-chain and Merkle-tree relationships;
  • construct and verify Merkle proofs;
  • explain how hashes support block linking, transaction verification and mining;
  • evaluate the limitations of hashes, commitments and Merkle structures in real systems; and
  • identify professional, governance and security practices for using these mechanisms.

1. Cryptographic Hash Functions

1.1 Definition and desired properties

A cryptographic hash function is a deterministic function

H: {0,1}* → {0,1}n

where {0,1}* denotes all finite-length binary strings and n is the fixed digest length in bits. For an input message m, the digest is written as h = H(m). A function such as SHA-256 produces a 256-bit digest, normally displayed as 64 hexadecimal characters.

Important properties include:

Determinism. Identical inputs produce identical outputs.

Fixed-length output. A short input and a very large input both produce an output of the same configured length.

Efficient computation. Honest users should be able to calculate a digest rapidly, even for large data.

Avalanche effect. A small change in the input should alter many output bits in an apparently unpredictable way.

One-way behaviour. Given a digest, finding a corresponding input should be computationally infeasible when the hash is properly designed and used.

A hash function is not encryption. Encryption is designed to be reversible by someone possessing a key; a hash is normally intended to be non-reversible. A hash is also not a digital signature: it does not by itself identify who created a message or prove authorization.

1.2 Security properties

The security of a hash function is commonly discussed through three related resistance properties.

Preimage resistance. Given a target digest y, it should be infeasible to find any message m such that:

H(m) = y

For an ideal n-bit hash, a generic brute-force search requires approximately 2^n trials on average up to constant-factor variation. This property supports commitment schemes and prevents an observer from recovering a secret value from its digest.

Second-preimage resistance. Given a particular message m₁, it should be infeasible to find a different message m₂ such that:

m₂ ≠ m₁ and H(m₂) = H(m₁)

This protects an already published document or transaction from being replaced by another document with the same digest.

Collision resistance. It should be infeasible to find any two distinct messages m₁ and m₂ satisfying:

m₁ ≠ m₂ and H(m₁) = H(m₂)

The attacker is free to choose both messages. Because of the birthday effect, a generic collision search for an ideal n-bit hash requires approximately 2^(n/2) trials, not 2^n.

For example, an ideal 256-bit hash has a generic preimage work factor near 2^256 and a generic collision work factor near 2^128. These are estimates, not guarantees. Cryptanalytic weaknesses, implementation errors, poor input encoding or advances in computing may reduce actual security.

PropertyAttacker knowsAttacker seeksIdeal generic work factor
Preimage resistanceDigest yAny m with H(m)=yApproximately 2^n
Second-preimage resistanceMessage m₁Different m₂ with same digestApproximately 2^n
Collision resistanceHash function onlyAny pair with same digestApproximately 2^(n/2)

The work factors assume an ideal n-bit hash, no shortcut attack and independent trials. They should not be interpreted as a complete security assessment.

1.3 The birthday effect

There are 2^n possible digests. If many independently selected messages are hashed, two may share a digest even though no target digest was specified. This is the birthday phenomenon. A rough probability that at least one collision occurs after hashing q random messages is:

P(collision) ≈ 1 − e^(−q(q−1)/(2·2n))

Here q is the number of hashed messages, n is the digest length in bits and e is the base of the natural logarithm. The approximation is most useful when q is much smaller than 2^n. It explains why collision resistance is approximately 2^(n/2).

This does not mean that a 256-bit hash becomes unsafe after 2^128 ordinary files. It means that a sufficiently powerful adversary seeking any collision has a generic strategy with that approximate scale. Security design must also consider the value of the protected assets, available hardware and possible cryptanalytic advances.

1.4 Domain separation and encoding

Hashing is only as unambiguous as the data representation supplied to the hash. Concatenating variable-length fields without lengths or tags can create ambiguity. For example, the sequence of fields ("ab", "c") may be indistinguishable from ("a", "bc") if both are encoded simply as the byte string abc.

Robust systems use canonical serialization, explicit field lengths, type tags or domain-separation prefixes. A conceptual design may use:

Hleaf = H(tagleaf ∥ encode(record))

and

Hnode = H(tagnode ∥ Hleft ∥ Hright)

where ∥ denotes concatenation. The tags distinguish leaf hashing from internal-node hashing and reduce structural ambiguity. Exact constructions depend on the protocol specification.

2. Hashes as Commitments and Tamper Evidence

A commitment allows one party to commit to a value while withholding the value until a later reveal. A basic hash commitment is constructed by selecting a random secret nonce r and publishing:

C = H(r ∥ x)

where x is the value being committed to and r is a sufficiently unpredictable random salt. Later, the committer reveals x and r; a verifier recalculates the hash and checks that it equals C.

The scheme has two important goals:

  • Hiding: without x and r, the commitment should not reveal the committed value. Random salting is especially important when x comes from a small or guessable set.
  • Binding: after publishing C, the committer should not be able to open it as two different values. This relies on collision or second-preimage resistance, depending on the exact protocol and security argument.

A hash commitment is not automatically confidential. If the possible values are few and no random salt is used, an observer can hash every candidate and compare the results. Hashes also do not prove that the committer possessed legal authority or that the committed data was accurate.

For data integrity, a published digest acts as a fingerprint. If a later copy produces a different digest, at least one copy or processing step differs. However, tamper evidence is not the same as tamper prevention. An attacker who can alter both the data and the published digest may avoid detection unless the digest is itself protected by a trusted publication channel, a digital signature, a blockchain record or another independent control.

3. Hash Chains and Block Linking

A hash chain links records by including the previous record's digest in the next record. A simplified chain is:

h₀ = H(data₀)
hᵢ = H(hᵢ₋₁ ∥ dataᵢ), for i ≥ 1

Changing data₁ changes h₁, which changes h₂ and every subsequent digest. In a blockchain, a block header normally contains a previous-block hash. The resulting chain gives later blocks a dependency on earlier blocks.

A block hash does not by itself make history immutable. An attacker with sufficient control might rewrite a block and recompute later hashes. Blockchain consensus makes such rewriting costly or difficult by requiring the attacker to satisfy the relevant consensus rules and, in proof-of-work systems, redo computational work. Thus, hash linking supplies tamper evidence and historical dependency; consensus supplies the distributed acceptance rule.

A typical block header may contain a version, previous-block hash, Merkle root, timestamp, difficulty-related field and nonce. Exact fields differ among protocols. The block hash commits to the header, while the Merkle root commits to the transactions represented by that block.

4. Merkle Trees

4.1 Structure

A Merkle tree is a binary hash tree. Leaves represent records, commonly transactions. Internal nodes hash the hashes of their children, and the top node is the Merkle root.

Binary Merkle tree from four transactions to a Merkle root Merkle root R H(L₁ ∥ L₂) H(L₃ ∥ L₄) L₁ = H(T₁) L₂ = H(T₂) L₃ = H(T₃) L₄ = H(T₄)
Binary Merkle tree showing four transaction leaves and a root hash
Figure: Binary Merkle tree showing four transaction leaves and a root hash

Figure: A Merkle tree commits four transactions to one root hash.

For leaves L₁, L₂, L₃, L₄, the internal nodes are calculated as:

P₁ = H(L₁ ∥ L₂), P₂ = H(L₃ ∥ L₄), R = H(P₁ ∥ P₂)

The root changes if a transaction changes, assuming the hash function's relevant security properties hold. A root therefore acts as a compact commitment to the ordered set or sequence represented by the tree. The protocol must specify ordering, duplicate handling and the treatment of an odd number of leaves.

4.2 Odd numbers of leaves

When a level has an odd number of nodes, protocols may duplicate the final node, promote it unchanged, use a special unary rule or construct a non-binary tree. These choices are not interchangeable. Verification software must implement the exact protocol rule. Ambiguous handling can create inconsistent roots or verification vulnerabilities.

4.3 Merkle proof

To prove that transaction T₂ is included, a prover supplies T₂ and the sibling hashes needed to reconstruct the root. In the four-leaf tree above, the proof includes L₁ and P₂, together with the left/right position of each sibling:

  1. Compute L₂ = H(T₂).
  2. Combine L₁ and L₂ in the correct order to obtain P₁.
  3. Combine P₁ and P₂ in the correct order to obtain R.
  4. Accept if the reconstructed root equals the trusted root.

For N = 2^k leaves, a balanced binary Merkle proof normally contains k = log₂N sibling hashes. More generally, the proof size is proportional to log₂N, while constructing the complete tree requires work proportional to N. This enables a verifier to check membership without downloading all transactions.

A proof is meaningful only relative to a trusted root and a specified tree construction algorithm. A Merkle proof demonstrates inclusion in a committed tree; it does not by itself prove that the transaction is valid, authorized, unspent or final. Those properties require signatures, consensus rules and state verification.

4.4 Worked example with symbolic hashes

Suppose a protocol uses four transaction leaves and produces the following symbolic values:

  • L₁ = aa, L₂ = bb, L₃ = cc, L₄ = dd;
  • P₁ = H(aa ∥ bb);
  • P₂ = H(cc ∥ dd);
  • R = H(P₁ ∥ P₂).

A proof for transaction T₂ consists of the sibling aa, the indication that it is on the left, and the sibling P₂, also on the right. The verifier computes H(T₂), then H(aa ∥ H(T₂)), then H(previous ∥ P₂). If the result equals R, the transaction is included under the assumed ordering and encoding rules. The symbolic example intentionally does not invent numerical digest values; actual values require a specified hash algorithm and exact byte encoding.

5. Blockchain Applications

5.1 Transaction verification

The Merkle root in a block header commits to the block's transaction list. A full node can recalculate the root from all transactions and compare it with the header. If the values differ, the block is invalid under the protocol's rules.

A light client may receive a transaction and a Merkle proof instead of the complete transaction list. It can verify that the transaction leads to the root in a header it considers authentic. This reduces bandwidth and storage requirements, but the client still needs a method for selecting trustworthy headers and must understand whether the proof establishes inclusion in a confirmed, valid and sufficiently deep block.

5.2 Mining

In proof-of-work systems, miners repeatedly vary a nonce or other permitted header field and calculate the block-header hash. A block is acceptable when the numerical interpretation of the hash is below the network target:

Hash(header) < target

The target is a protocol parameter; lower targets require more expected trials. Mining therefore uses hashing as a computational resource. The Merkle root allows a miner to commit the transaction set in the header while changing a nonce to search for a qualifying block hash.

The probability that one independent trial succeeds is approximately:

p = target / 2n

when the hash is uniformly distributed over 2^n values and the target is interpreted as an integer in the permitted range. The expected number of trials is approximately 1/p = 2^n / target. This is a probabilistic model, not a guarantee about the exact number of trials before success.

5.3 Tamper evidence and block confirmation

If a transaction is altered, its leaf hash changes, then its parent hashes and the Merkle root change. The block header changes, and the hash links from later blocks no longer match. An attacker must therefore produce a consistent replacement history and persuade the consensus system to accept it. In proof-of-work, this may require substantial additional work; in other systems, it may require control of stake, validators or governance processes.

Hashing does not establish truth. A blockchain can immutably preserve a false, fraudulent or poorly verified statement. Integrity means that the recorded bytes remain consistent with the commitment, not that the underlying event was legitimate.

6. Commitments, Merkle Proofs and Related Structures

Merkle trees are one form of authenticated data structure. Other structures include sparse Merkle trees, Patricia or radix trees, vector commitments and accumulator schemes. Their trade-offs differ.

StructureTypical strengthTypical trade-off
Ordinary Merkle treeSimple membership proofsProof length grows logarithmically with collection size
Sparse Merkle treeEfficient proofs over a large key spaceProof paths can be relatively large and require defined empty nodes
Patricia/radix treeEfficient keyed-state representationMore complex update and encoding rules
Vector commitmentCompact commitments and potentially short proofsOften requires stronger assumptions or specialized constructions

A state tree may commit not only to transactions but also to account balances, contract storage or unspent outputs. The root can be placed in a block header, allowing a verifier to check claims about a particular state. Non-membership proofs require additional structure and cannot always be obtained from a basic list Merkle tree without protocol-specific design.

7. Limitations and Security Risks

Weak or obsolete algorithms

MD5 and SHA-1 have well-known collision weaknesses and should not be selected for new integrity-critical systems. A migration plan is necessary when legacy systems use them. SHA-256, SHA-3 and other algorithms may be appropriate depending on protocol requirements, but algorithm choice must follow current standards and threat analysis.

Length-extension and construction errors

Some hash constructions used in naive keyed designs may be vulnerable to length-extension attacks. A secret-key message authentication code such as HMAC should be used where authentication is required, rather than assuming that H(secret ∥ message) is universally safe.

Ambiguous serialization

Different software implementations may hash different byte sequences while displaying the same human-readable fields. Canonical serialization, test vectors, explicit endianness and strict parsing are essential.

Confusing integrity with authenticity

Anyone can calculate a public hash. A matching digest shows consistency with the committed bytes, not who produced them. Digital signatures, authenticated channels and access controls address authenticity and authorization.

Trusting the wrong root

A perfect Merkle proof against an attacker-controlled root proves little. The verifier must authenticate the block header or commitment and assess its consensus status.

Replay and context confusion

The same transaction or message may be valid in one network, contract or domain but invalid in another. Domain identifiers, chain identifiers, nonces and context-specific encoding help prevent replay and cross-protocol confusion.

Quantum considerations

Large-scale quantum computing could reduce the effective generic security of some hash searches through quantum algorithms. The exact impact depends on the attack and security property; a commonly cited generic collision-search reduction is associated with the birthday-bound setting. Quantum risk is a reason for long-lived systems to monitor standards and plan cryptographic agility, not a reason to claim that present-day hashes are automatically broken.

8. Professional Practice and Governance

A professional implementation should:

  1. select a current, standardized hash function appropriate to the threat model;
  2. specify exact input encoding, field ordering, length prefixes and domain tags;
  3. document tree balancing and odd-node rules;
  4. use independent test vectors and cross-implementation tests;
  5. authenticate the root or block header before accepting a proof;
  6. separate membership verification from transaction validity and finality;
  7. protect random salts and secret material in commitment schemes;
  8. plan algorithm migration and cryptographic agility; and
  9. log verification failures and investigate them as potential data-integrity incidents.

Governance is also important. A system should define who may publish a root, how disputes are handled, how corrected data is represented and whether privacy obligations restrict the publication of information that can be guessed from a digest. Storing a hash on a blockchain may still create a durable record that links to personal information. Hashing pseudonymizes data in some circumstances but does not automatically anonymize it.

Artificial intelligence systems can use hash-based data lineage to identify whether a model, dataset or prompt has changed. A digest can support provenance records, while a Merkle tree can commit to many training artifacts. Nevertheless, a hash does not validate the quality, legality, fairness or representativeness of the data. Human governance and audit remain necessary.

9. Illustrative Case: Supply-Chain Document Verification

Background

A logistics consortium maintains digital records for batches of temperature-sensitive medicine. Each shipment generates documents such as packing records, sensor summaries and inspection certificates. The consortium wants distributors to verify that a document belongs to a published shipment record without downloading the entire archive.

Problem

Documents are exchanged among manufacturers, carriers and distributors. A document may be accidentally altered during conversion or deliberately replaced. Storing every document directly on a shared blockchain would be expensive and could expose commercially sensitive information.

Analysis

The consortium creates a canonical byte representation for each document, including a document type, shipment identifier, version and content digest. It hashes each record as a leaf in a Merkle tree. The Merkle root is included in a consortium block and signed or otherwise accepted by the consortium's governance process. A distributor receives a document, its Merkle proof and the relevant authenticated block header.

Approach and solution

The distributor:

  1. reconstructs the exact canonical bytes;
  2. computes the document leaf hash;
  3. combines it with each sibling hash according to the proof's left/right directions;
  4. compares the reconstructed root with the authenticated root; and
  5. separately checks signatures, shipment permissions, timestamps and business rules.

The system does not place the full confidential documents on-chain. It stores a commitment and maintains an access-controlled off-chain archive.

Outcome

A changed document produces a different leaf and fails the proof. A valid proof demonstrates that the document was included in the committed collection at a specified block. It does not demonstrate that the temperature sensor was calibrated, that the document was truthful or that the shipment complied with pharmaceutical law. Those claims require independent controls and evidence.

Learning points

The case illustrates the separation between integrity, inclusion, authenticity and substantive correctness. It also shows why canonical encoding, authenticated roots, privacy design and off-chain governance are as important as the hash function itself.

Chapter Summary

Cryptographic hash functions map arbitrary-length inputs to fixed-length digests and provide efficient fingerprints for data. Their security is described through preimage, second-preimage and collision resistance. For an ideal n-bit hash, generic preimage and second-preimage work are approximately 2^n, whereas generic collision work is approximately 2^(n/2) because of the birthday effect.

Hash commitments allow a party to publish a value derived from secret data and reveal the data later. Random salting supports hiding, while collision-related resistance supports binding. Hash chains and block-header links create historical dependencies, but consensus is required to make rewriting difficult in a distributed network.

Merkle trees compress a collection of records into a root hash. Merkle proofs use logarithmically many sibling hashes to demonstrate membership relative to a trusted root. In blockchain systems, Merkle roots support transaction commitment, light-client verification and tamper evidence; proof-of-work mining uses repeated block-header hashing to search for a hash below a target. None of these mechanisms alone proves authenticity, legal validity, truth, confidentiality or finality. Correct serialization, domain separation, authenticated roots, secure algorithms and appropriate governance are essential.

Key Terms

Cryptographic hash function: A deterministic function that maps arbitrary-length input to a fixed-length digest.

Digest: The fixed-length output produced by a hash function.

Preimage resistance: Difficulty of finding an input that produces a specified digest.

Second-preimage resistance: Difficulty of finding a different input with the same digest as a specified input.

Collision resistance: Difficulty of finding any two distinct inputs with the same digest.

Birthday effect: The phenomenon that makes generic collision searches require about 2^(n/2) work for an n-bit hash.

Avalanche effect: The strong change in output caused by a small input change.

Hash commitment: A published hash that binds a party to a value while allowing later revelation.

Salt: Random data added to a value before hashing, often to prevent guessing attacks.

Hash chain: A sequence in which each hash depends on the previous hash and new data.

Merkle tree: A hash tree whose root commits to a collection of records.

Merkle root: The top hash of a Merkle tree.

Merkle proof: Sibling hashes and positional information used to verify membership in a committed tree.

Canonical serialization: An unambiguous method for converting structured data into bytes.

Domain separation: Use of distinct tags or contexts to prevent unintended reuse across protocols.

Authenticated data structure: A structure that enables efficient verification of data against a commitment.

Tamper evidence: The ability to detect inconsistency after data or a commitment has been altered.

Block linking: Connecting a block to its predecessor through the predecessor's hash.

Proof of work: A consensus mechanism requiring a qualifying computational hash result.

Cryptographic agility: The ability to replace cryptographic algorithms as requirements change.

Review Questions

Short Answer Questions

  1. What is the difference between a cryptographic hash function and encryption?
  2. Define preimage resistance and second-preimage resistance.
  3. Why is generic collision resistance for an n-bit hash approximately 2^(n/2)?
  4. What information is normally included in a Merkle proof?
  5. Why does a Merkle proof require a trusted or authenticated root?

Long Answer Questions

  1. Explain how hash functions, block-header links and consensus interact to provide blockchain tamper evidence.
  2. Compare preimage, second-preimage and collision resistance, including the attacker's knowledge and objective in each case.
  3. Describe how to construct and verify a Merkle tree for four transactions, including the role of left/right ordering.
  4. Explain the security goals and limitations of a salted hash commitment.
  5. Discuss why canonical serialization and domain separation are necessary in professional blockchain implementations.

Analytical / Application Questions

  1. A light client receives a transaction, a Merkle proof and a block header. Identify what the client can establish and what additional checks are needed before treating the transaction as final and valid.
  2. A company publishes unsalted hashes of employee salary categories, where only twelve categories are possible. Analyze the privacy weakness and propose a safer commitment design.
  3. A legacy blockchain uses an ambiguous rule for handling an odd number of Merkle leaves. Analyze the interoperability and security risks and recommend an engineering response.

Multiple Choice Questions with Answers

Question 1

Which property makes it difficult to find any two distinct messages with the same hash?

A. Preimage resistance

B. Second-preimage resistance

C. Collision resistance

D. Key confidentiality

Correct Answer: C. Collision resistance

Question 2

For an ideal n-bit hash function, the approximate generic work factor for finding a collision is:

A. 2^n

B. 2^(n/2)

C. n^2

D. log₂(n)

Correct Answer: B. 2^(n/2)

Question 3

What does a Merkle proof primarily demonstrate?

A. That a transaction was legally authorized

B. That a transaction belongs to a collection committed by a specified root

C. That a block has achieved finality

D. That a transaction signature is valid

Correct Answer: B. That a transaction belongs to a collection committed by a specified root

Question 4

In a binary Merkle tree with 1,024 leaves, how many sibling hashes are normally required for a balanced membership proof?

A. 10

B. 32

C. 512

D. 1,024

Correct Answer: A. 10

Question 5

Why is a random salt useful in a hash commitment?

A. It makes the hash reversible

B. It prevents all possible collisions

C. It makes guessing small candidate values more difficult

D. It replaces the need for authentication

Correct Answer: C. It makes guessing small candidate values more difficult

Question 6

What happens to a blockchain block's Merkle root when one transaction is modified, assuming a secure hash and correct recomputation?

A. It necessarily remains unchanged

B. Only the block timestamp changes

C. The root changes because the modified leaf affects its ancestors

D. The previous block hash automatically repairs it

Correct Answer: C. The root changes because the modified leaf affects its ancestors

Question 7

Which statement best describes the role of a previous-block hash?

A. It encrypts all transactions in the previous block

B. It creates a dependency linking the current block to its predecessor

C. It proves that every transaction is legally valid

D. It generates users' private keys

Correct Answer: B. It creates a dependency linking the current block to its predecessor

Question 8

In proof-of-work mining, a block is generally acceptable when:

A. The header hash is numerically below the target

B. The Merkle root is longer than the block hash

C. The transaction count is prime

D. The nonce is equal to zero

Correct Answer: A. The header hash is numerically below the target

Question 9

Why is canonical serialization important when constructing a Merkle tree?

A. It ensures different implementations hash the same logical data into the same bytes

B. It guarantees that data is confidential

C. It eliminates the need for consensus

D. It allows hashes to be decrypted

Correct Answer: A. It ensures different implementations hash the same logical data into the same bytes

Question 10

What does a matching hash or Merkle proof not establish by itself?

A. Consistency with the committed bytes

B. Inclusion under the specified root

C. The truth, authorization or legal validity of the underlying data

D. The integrity of the proof path

Correct Answer: C. The truth, authorization or legal validity of the underlying data

Crypto & Security
Chapter 4 of 14