Scroll to turn the page
Blockchain · College Blockchain
a fingerprint for any piece of data
A cryptographic hash function takes an input of any length and returns a short, fixed-length output, its digest. Every blockchain is built on one.
Bitcoin uses SHA-256, published by the US National Institute of Standards and Technology (NIST) in 2001 and standardized in FIPS 180-2 in 2002. Its digest is always 256 bits, written as 64 hexadecimal digits (each digit is 4 bits).
SHA-256("Atlas") = 9dee1c0ed39ef4e2686f974b5d574531fa5091be91e7ea72875e763278293ad4 SHA-256("atlas") = 7c82602500857aa6ed0cf38c4c3e4ec645bdcaa82c00b9155eb08be100c778a9
The two inputs differ only in one capital letter, and the digests share nothing visible. A five-byte word and a 67-byte payment note both come out as 256 bits.
This chapter is about what makes a hash function useful: what it must make hard (1.1), how hard (1.2), how SHA-256 works inside (1.3), and the three structures every blockchain builds from it: hash pointers (1.4), Merkle trees (1.5) and commitments (1.6).
deterministic, fast, and hard to run backward
A hash function is deterministic (the same input always gives the same digest) and fast to compute. A cryptographic one adds three things that must be hard.
| property | hard to find… |
|---|---|
| preimage resistance | any input with a given digest h |
| second-preimage resistance | a different input with the same digest as a given input |
| collision resistance | any two different inputs with the same digest |
Collisions must exist: there are infinitely many inputs and only 2256 digests. The guarantee is that nobody can find one.
A good hash also shows the avalanche effect: change one bit of the input and each output bit flips with probability one half. In the figure, “Pay Bob 5 coins” and “Pay Bob 6 coins” differ in one character, and 127 of the 256 bits of their digests differ, close to the 128 you would expect.
That is why a digest works as a fingerprint: it reveals nothing about which part of the input changed, only that something did.
brute force and the birthday bound
For an ideal n-bit hash, the best attack is to try inputs at random.
To hit one given digest (a preimage) takes about 2n tries: 2256 ≈ 1.16 × 1077 for SHA-256. To find any two inputs that collide takes only about 2n/2, because every new digest can match any of the earlier ones. That is the birthday bound:
With N = 365 birthdays, 23 people give a better-than-even chance that two share one. The figure uses a toy hash, the first 16 bits of SHA-256 (N = 65,536). The chance passes one half at k = 302, about 1.18√N. Hashing “tx 0”, “tx 1”, … found the first real collision at try 244: “tx 24” and “tx 243” both start e0eb.
For SHA-256, √N = 2128 ≈ 3.4 × 1038 tries, far beyond any computer. Weaker hashes have fallen: the first SHA-1 collision was published in 2017, and SHA-1 is no longer trusted for signatures.
padding, blocks and a chaining value
SHA-256 reads its input in 512-bit blocks. First the message is padded: a single 1 bit, then zeros, then the message length as a 64-bit number, so the total is a whole number of blocks.
The figure’s message is 95 bytes long. With the padding (33 bytes) it fills 2 blocks of 64 bytes.
A chaining value of eight 32-bit words starts at fixed constants H0. The compression function mixes one block into it over 64 rounds of additions, rotations and bitwise logic, and passes the result on. The last chaining value is the digest.
H0 6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19 H1 1bd0107bc07ad0547be0de2aeb42d4689f2c74e94fee6c4a382ab547ed310130 digest a7722ec7c6d2f7aa1318370ffe047c79e2a851e5ef1e389a85f844347be6ba26
This design (Merkle–Damgård) means a hash of a huge file needs memory for only one block and one chaining value at a time. Bitcoin hashes twice, SHA-256 of SHA-256, for block headers and transaction IDs.
a chain that shows any edit
A hash pointer is a reference to data together with the hash of that data. Follow it, and you can check the data has not changed since the pointer was made.
Link records with hash pointers and you get a hash chain: each record stores the hash of the record before it, and its own hash covers that stored value too. The figure chains 5 records.
1 prev 00000000 Alice pays Bob 5 hash 36b3d1df 2 prev 36b3d1df Bob pays Carol 2 hash 0147fcbb 3 prev 0147fcbb Carol pays Dave 1 hash a4f37dac 4 prev a4f37dac Dave pays Erin 3 hash 7db82a82 5 prev 7db82a82 Erin pays Alice 4 hash eeed4d7e
Now someone edits record 2 to “Bob pays Carol 20”. Its hash changes, so record 3’s stored pointer no longer matches. To hide the edit, the forger must rewrite every later record as well, all the way to the newest one.
So whoever remembers just the latest hash can detect a change anywhere in the history. Stuart Haber and W. Scott Stornetta used this to timestamp documents in 1991; a blockchain is a hash chain of blocks.
one hash for many transactions, and short proofs
A Merkle tree (Ralph Merkle, 1979) hashes many items into one. Hash each item to get the leaves, then hash the leaves in pairs, then those hashes in pairs, up to a single Merkle root.
The figure’s tree holds 8 transactions in 3 levels; its root starts d52d4a6ed526. Change any transaction and the root changes. (When a level has an odd number of hashes, Bitcoin pairs the last one with itself.)
To prove “Fay → Gus 6” is in the tree, you send only the sibling hash at each level: 3 hashes. The checker hashes the transaction, combines it with each sibling in turn, and compares the result with the root it already trusts. Here it matches.
| transactions | hashes in a proof |
|---|---|
| 8 | 3 |
| 1,024 | 10 |
| 1,000,000 | 20 |
| 1,000,000,000 | 30 |
A proof grows with log2 of the number of items. A light wallet keeps only block headers (each with its Merkle root) and asks for these short proofs: simplified payment verification, from the Bitcoin white paper.
seal a value now, open it later
A commitment lets you fix a value now without showing it, and prove later what it was. Publish c = H(salt ‖ value); later reveal the salt and the value, and anyone can rehash and compare.
It must be hiding (c reveals nothing about the value) and binding (you cannot open it to a different value). Binding comes from collision resistance. Hiding needs the random salt: without it, anyone could hash “heads” and “tails” and see which matches.
H("heads") = 5c0bc2423c1af18c…414e H("tails") = ae7a4a6cba2f9fb1…8d62 H(salt+"heads") = a234145e9864c91e…6f39 published H(salt+"tails") = 5049d288f2dd72cc…e44a In the figure the committer seals “heads” with the salt 7f3a91c2e05b, publishes the hash, and later reveals both; the verifier’s rehash matches.
A related property, puzzle-friendliness, says there is no shortcut to an input whose hash lands in a target range other than trying many: exactly what proof of work relies on (Chapter 4).