(This post is an extract from another post written a year ago on my other, more personal blog.)

I finally got around to studying the math behind bitcoin.

If you more or less know what a hash is (the hash is as a short string, e.g. 32 characters, than can be calculated from a file of arbitrary size; if even one byte in the file changes, the hash will be completely different; read more on wikipedia, or ask me in the comments) and you more or less know how the public and private keys in asymmetric cryptography work (you can encrypt (encode) something with the public key, ONLY its matching secret private key can decode it; you can SIGN any file with a secret private key, the authenticity of that signature can be proven by anyone with matching public key; read more on wikipedia, or ask in the comments!) you can more or less understand bitcoin in particular and cryptocurrency in general.

Let’s say you were to generate a completely random private key, you can then use a well-known procedure to derive its matching public key. By applying two successive hash functions to that public key, you have a bitcoin address!

If I were to owe you money, you could then give me that bitcoin address.

I could then pay you back by writing a specially crafted message called a bitcoin transaction, in which I describe that I am transferring some bitcoins TO the address that you gave me FROM another bitcoin address (henceforth the source address), of which I have the matching secret private key.

In that message, I cryptographically sign the input part, a modified version of the whole transaction, including source and destination address, with the (secret) private key matching that source address. The signature mathematically proves that I own the bitcoins I am about to transfer, and it mathematically locks in the whole transaction, so that the destination addresses also can’t be changed. I generally also allocate a very small amount (by leaving money unaccounted for) as a transaction fee. We’ll see why in a minute.

I broadcast the signed transaction to the bitcoin network, where it eventually gets picked up by one or more of the bitcoin miners. Miners batch together a number of transactions into a block, together with a hash of the last successfully mined block, and a piece of random data called the nonce. They then proceed to continuously hash the block, changing the nonce every time so that the hash changes, until the first few digits of the hash are zeroes.

Based on the nature of cryptographic hashes, this will statistically take a very long time. One could get lucky and get the correct hash early, but generally it requires a whole lot of number crunching, which means kilowatts, which means actual money. The special hash resulting from this number crunching is called the proof of work.

When a miner has hit the jackpot, they broadcast the block to the network, which recognises that it’s the next valid block by checking the hash, and then, in a peer to peer fashion, irreversibly records this as the next block in the globally shared block chain. The successful miner currently receives 12.5 bitcoins (on 2017-06-03 worth about 27500 EURO) as well as all of the included per-transaction fees. This reward is set to halve again sometime in the future.

Now you probably understand why so many people are mining so enthusiastically. (No, you can’t really participate anymore with your home PC like you could in the early days; you have to acquire a large room full of bitcoin mining ASICs, circuitry that has been purpose-designed for one thing: bitcoin mining, to make any kind of impact. On the other hand, if you play the lottery, you might as well fire up your PC.)

You could now go and print out your private key (or its QR code) and the matching bitcoin address (actually you only need the private key, the public key and address can be derived from it) and then destroy all of your computers. Whenever you need to send that bitcoin somewhere, you simply type in the private key or rather scan the QR code, and then repeat the process of creating a bitcoin transaction, using your private key.

The money is never actually stored anywhere, only transactions encoding the movement of money from one random virtual address to another are. The block-chain is mathematically unbreakable and unforgeable.

I find the relative simplicity of the whole thing utter genius: A usable and versatile currency backed by hard math.

Further reading

The two sources that helped me the most were Bitcoin transactions, metaphorically (Part 1) and Bitcoin transactions, technically (Part 2), both on the What does the quant say? blog.