#041: Let's Build a Bitcoin
You might have heard about this cryptocurrency thing. Bitcoin is soaring, and it seems like there’s a new cryptocoin offering every other day. You might even own a few. But what exactly is a Bitcoin, and by extension, cryptocurrency?
Whenever you want to exchange money, you need a trusted third party to guarantee the transaction. That third party can take the form of “hard” currency (ie. coins and banknotes, and by extension, whatever sovereign nation issued them), or a bank which keeps track of all the transactions. Another important function both fulfill is making sure you can’t spend your money more than once. Banks do so by keeping track of your balance, and banknotes have serial numbers on them, to ensure you can’t use the “same” money again.
But would it be possible to keep a record of all transactions (and therefore balances) without having a third party that everyone needs to trust? This is where cryptocurrencies come in. To explain how they work, we’ll try and build one for ourselves.
The first feature we’re going to need is a ledger. This is simple, as it’s only a list of all the transactions that have happened so far in the system. Each line is a transaction in the form of “Alice pays Bob $100”. This ledger is public and accessible to everyone who wants to participate in your cryptocurrency, and everyone can add new transactions to it. Whenever you want to know who owns how much, you just tally up all transactions, and you’ll have everyone’s balance.
And here’s the first problem: If everyone can add whatever transactions they like, what’s to stop an attacker called Eve from adding “Alice pays Eve $1.000.000”? Alice never paid anything to Eve in the first place, but right now, there’s no way for us to know which transactions are real, and which aren’t.
Here, the first piece of crypto comes in: digital signatures. I’ll go into the details of how they work in one of the upcoming issues of this newsletter, so for now the short version: By generating a public and secret key pair, you can add a digital signature using your secret key to any message you’d like. Anyone else can use your public key to verify that it was indeed you who signed this message. Changing any bit in the signed message makes the signature invalid, and it’s extremely hard for someone to compute your private key if all they have is your public key.
So, you establish a new rule: Whenever someone adds a new signature, the sender has to digitally sign it. So if Alice wants to pay Bob $100, she’ll add the transaction “Alice pays Bob $100”, and to prove it was indeed her, signs it using her secret key. Now Eve cannot add a transaction claiming Alice paid her anything, since she can’t sign the message with Alice’s secret key.
However, there’s still a wrinkle: Bob can now keep adding the same, already signed, transaction to the ledger. The signature itself won’t change, since the content is still the same, and yet Bob can claim to have received the money over and over again.
To prevent this, we’ll add a unique number to each transaction, which we could do by numbering them starting at 1. Signing a transaction has to include this number. So, now Bob can’t re-add the same transaction, since the number already exists in the system, and Bob can’t change the number, because this would invalidate Alice’s signature, and he can’t re-sign the transaction, because Bob doesn’t know Alice’s secret key.
Great! Now we can add transactions to the public ledger, we know that each transaction is actually valid thanks to digital signatures, and we’ve put in some safeguards to prevent people from exploiting the transaction system.
There’s still one problem though: we’ve treated the ledger as a centralized entity, so up until now, all we’ve done is to rebuild a bank. Bitcoin et al. are decentralized, so how can we achieve that? That’s what I’ll write about next week.
Other interesting links from around the web:
-
How the industry-breaking Spectre bug stayed secret for seven months
-
The Strange History of One of the Internet’s First Viral Videos
-
How a TV Works in Slow Motion (video) — Everything from CRTs up to modern OLED TVs.