Intermediate Lesson 1 4 min read

Hash: the function that only goes one way

SHA-256 takes anything and returns 256 bits. It is not possible to reverse it, and this impossibility underpins half of Bitcoin.

Search the internet and you'll find sites that promise to decrypt a hash. They don't decrypt anything because there's nothing to decrypt. What these sites have is a giant table of pre-calculated results: when they find yours there, they haven't undone the calculation, they've just recognized a result someone already stored.

A hash function takes anything — a word, a contract, a two-gigabyte video — and returns a fixed-size number. Bitcoin uses SHA-256, designed by the NSA and published in 2001 as a U.S. federal standard. The number it returns is 256 bits, which amounts to sixty-four characters written in the way programmers write — sixty-four for the video and sixty-four for the word.

The first property is being deterministic. The same input produces the same output today, it did in 2009, and it will on a computer on the other side of the world, without coordinating with anyone. That's why thousands of us verify the same block and arrive at the same result, as the beginner's trail showed: they don't need to agree, they need to calculate.

Whatever goes in, always comes out the same size — and there's no way back from flour to grain.

The second is fixed size, and it's what turns the function into a summary. This short output is what travels, gets compared, and is stored instead of the entire document. A block header is eighty bytes and speaks for thousands of transactions precisely this way.

The third is being one-way, which is where the phrase that names the lesson comes from. Calculating a file's summary takes microseconds. Finding a file that produces a summary chosen by you requires trying input by input, and the space of attempts is the same 2^256 that Lesson 5 will measure. No one knows how to reverse it. It's not that it's difficult: there's no known shortcut.

Burning the letter takes a second. Reading the letter from the ashes is what no one knows how to do.

The fourth is the avalanche effect. Changing a period to a comma doesn't change the summary a little: it changes entirely. On average, half of the bits flip, and the result bears no resemblance to the previous one. There's no almost right, no being close. It's what makes the hash useful for verification: a single altered character in a thirty-page contract produces an unrecognizable summary, and the recipient notices without reading the contract.

The fifth is collision resistance. Since possible inputs are infinite and outputs are 2^256, there are infinite different inputs with the same summary — that's mathematics, not a flaw. The requirement is that no one can find a pair. And this requirement has been broken before: MD5 fell in 2004, at the hands of Chinese researcher Wang Xiaoyun, and SHA-1 fell on February 23, 2017, when Google and the CWI institute in Amsterdam published two different PDF files with the same summary, at the cost of over nine quintillion operations. Both were retired, and it's good to know that hash functions age.

In 2017, two different documents began to open the same lock — and the function that did this was retired.

With these five properties in hand, you can see SHA-256 supporting Bitcoin in four places at once. It chains the blocks because each header carries the summary of the previous one, and altering an old block ruins all the following ones. It summarizes the transactions of a block into a tree. It's the calculation the miner repeats billions of times per second in search of a result small enough. And it enters the recipe for the address, alongside a second function called RIPEMD-160. Satoshi even applied it twice in most of these uses, as a precaution against a known attack.

A vocabulary detail closes the lesson, as it avoids the wrong question later on. Hash is not cryptography. Encrypting assumes there is someone who can decrypt, with the right key; summarizing assumes there is no one, with any key.

The operation in the next lesson is also easy to do and infeasible to undo, but what it keeps on the other side isn't a file: it's the ownership of your money. In the next lesson, the private key, the public key, and the secp256k1 curve.