Learn › Blockchain & Access Control › Module 1 · Foundations and hashing
Cryptographic hash functions, MACs and the birthday bound
Symmetric, asymmetric and hash primitives, integrity checks and the avalanche effect, MACs, the hash-family definition, preimage, second-preimage and collision resistance, the random oracle model and birthday attacks.
Three cryptographic primitives
| Symmetric | Asymmetric | Hash | |
|---|---|---|---|
| Keys | One shared secret | Public/private pair | None (unkeyed) |
| Reversible? | Yes, with the key | Yes, with the private key | No — one-way |
| Blockchain role | Rare in consensus | Wallets, addresses, signatures | Block links, Merkle roots, PoW |
A hash maps input of any length to a fixed-length digest: “Hi” and a 10 GB file both give 256 bits under SHA-256.
Integrity and the avalanche effect
Alice sends x over an insecure channel and y = h(x) over an authentic channel; Bob accepts if h(x′) = y. This detects modification (integrity), not eavesdropping (confidentiality). If Mallory controls both channels she can change x and recompute h — the unkeyed hash alone is not enough.
Avalanche effect (SHA-256)
“The quick brown fox jumps over the lazy dog” → d7a8fbb3…37c9e592
Same sentence with a full stop → ef537f25…8635fb6c
One extra character flips about half of the 256 output bits.
A toy function h(x) = (x + 1) mod 10 fails at once: h(1) = h(11) = 2, and any target is trivially inverted.
Message authentication codes
y = hK(x), where K is a secret shared by Alice and Bob.
Both x and the tag y travel on the same insecure channel; without K, Mallory cannot produce a valid tag for x′. A MAC gives integrity and data-origin authentication but needs prior key distribution. Real use: HMAC-SHA256 signatures on API requests and payment webhooks.
Formal hash family
A hash family is a 4-tuple (X, Y, K, H): messages X, digests Y (finite), keys K, and for each k a function hk: X → Y. (x, y) is a valid pair under k if y = hk(x). With |X| = N and |Y| = M there are MN possible functions (N = 4, M = 2 gives 16). |K| = 1 is an unkeyed hash (SHA-256); |K| > 1 is a keyed hash (MAC). Compression requires |X| ≥ 2|Y|.
Four security requirements
| Property | Given | Must be infeasible to find | Attack it stops |
|---|---|---|---|
| Preimage resistance (one-way) | y | any x with h(x) = y | Recovering passwords from stored hashes |
| Second-preimage resistance | x | x′ ≠ x with h(x′) = h(x) | Swapping a signed contract for a forged one |
| Collision resistance | nothing | any pair x ≠ x′ with equal hashes | Rogue CA certificate (MD5, 2008) |
| No shortcuts (pseudorandom) | many pairs (mi, h(mi)) | h(mnew) without evaluating h | Algebraic prediction |
- By the pigeonhole principle collisions always exist; resistance means they are infeasible to find.
- Collision resistance implies second-preimage resistance (a second preimage is a collision).
- Counter-example to “no shortcuts”: h(x) = ax mod n gives h(x1 + x2) = h(x1) + h(x2) mod n without knowing a.
- Git commit IDs rely on these properties: altering an old commit changes every later ID.
Random oracle model (Bellare–Rogaway, 1993)
An oracle secretly picks a uniformly random function h from all functions X → Y. The only way to learn h(x) is to query it; a repeated query returns the same answer, a new query returns a fresh uniform value. Hence for x1 ≠ x2, Pr[h(x1) = h(x2)] = 1/M, and knowing one output says nothing about another.
Attacks are measured as (ε, Q) Las Vegas algorithms: at most Q oracle queries, success probability at least ε, and never a wrong answer (only success or failure).
Analogy: someone born today
Ask Q random people. Pr[success] = 1 − (1 − 1/365)Q. Replace 365 by M = 2n and this is exactly the preimage attack.
Attack complexity and the birthday paradox
| Attack | Success with Q queries | Work |
|---|---|---|
| Preimage | 1 − (1 − 1/M)Q ≈ Q/M | O(2n) |
| Second preimage | 1 − (1 − 1/M)Q−1 | O(2n) |
| Collision | 1 − ∏i=1Q−1(1 − i/M) | O(√M) = O(2n/2) |
Collisions are cheaper because Q queries form Q(Q − 1)/2 candidate pairs.
Q ≈ √(2M ln(1/(1 − ε))); for ε = 0.5, Q ≈ 1.17√M.
Worked examples
- Birthdays: 23 people give 50.7% chance of a shared birthday; 50 give 97%. 1.17√365 ≈ 22.4.
- A 40-bit digest falls to about 220 ≈ one million queries.
- 128-bit collision security needs n/2 = 128, so a digest of at least 256 bits (SHA-256).
- MD5’s 128-bit output gives only a 264 birthday bound, and cryptanalysis makes it far weaker still.