Wait a Minute… How DOES Bitcoin Work?

Bitcoinheader

Over the past few years I’ve developed a continuing interest in Bitcoin. The cryptocurrency boom that we are currently experiencing is nothing short of mind-boggling, especially when you compare one of the first Bitcoin transactions (someone buying 2 pizzas for 10,000 Bitcoin in 2010), with the current going-rate of a single Bitcoin (over $10,000). Because of my interest, I’m often asked how Bitcoin works, why it isn’t a scam, and why people shouldn’t just go out and buy a bunch of graphics cards in hopes to get rich. I think it’s high time to sit down and write a quick primer on just how all the underlying technologies work.

Bitcoin is based around three technologies that have existed for years but are put into novel uses. These are the following:

  1. Distributed Databases
  2. Public Key Cryptography
  3. Hashing

These technologies, when combined, create what is known as a Blockchain. Don’t worry, I’m going to explain everything; and hopefully in a way that’s easy to understand!

Distributed Databases

The idea of distributed databases and non-centralized networks has been around for as long as the internet. If you’ve ever used Bittorrent, you’ve participated in a decentralized database which shares information about what parts of a specific file you have or need. The internet itself can be thought of as a bunch of networks with no center, where data can find its way to any spot there’s a connection.

In Bitcoin, every person who wants it may install wallet software. This allows you to create a wallet address (described below) and track how much Bitcoin you have. The way this works is that every person with a wallet also has the entire history of every transaction ever made in Bitcoin. Considering how tightly most people keep their own banking details, this might sound scary, but it’s necessary. In the physical world there are numerous local, state, federal, and international organizations monitoring and auditing banks to keep them from adding zeros to accounts and then cashing out. In Bitcoin, no one is regulating transactions, so keeping them private means they could be easily manipulated. A wallet isn’t directly tied to a person, and there’s no link that you have a specific wallet or are associated directly with a transaction. In this regard, Bitcoin is like cash: if they don’t see you holding the money, they can’t really prove it’s yours.

Public Key Cryptography

Most people are familiar with symmetric encryption, where you use a password to encrypt data, and then you use that same password to decrypt that data. If you need someone else to decrypt it, you can give them the password, but you need to find a separate secure way to do this. Asymmetric encryption (or public key-encryption) uses two keys. Each key can encrypt data, but each key can only decrypt data from the other key. Often, one key will be kept secret and only used by one person/computer/entity. The other is then able to be publicly disclosed (you could put it on a billboard or broadcast it on TV). Using the public key, anyone can encrypt a message that only the owner of the private key can decrypt. Conversely, if the owner of the private key wants to prove he’s the owner of the public key, he can encrypt a message with his private key and then anyone with the public key can decrypt and verify it.

In Bitcoin, you don’t have a bank account or a login. You generate a “wallet” which is a private and public key. Whenever you send Bitcoins to someone, you send it using their public key. The process is literally encrypting a message using your private key basically stating “I own this public key, and I’m sending this many Bitcoin to this address. I can prove I have this much by referencing the following transactions others made to me…” There is no limit on how many wallets a person or group can have, and some people keep money in multiple accounts so that if they lost one they don’t lose all their Bitcoin.

Hashing

Hashing is a complex mathematical function performed on data to make quick comparisons on large datasets. A good way to think of it is the remainder in a division problem, only this problem is much more complex. The output of a hash always has the same length. For instance, a sha256 function always outputs a 256-bit remainder, displayed in hexadecimal (the numbers 0-9 and the letters A-F). It will be this long no matter if you feed it one bit or 100 terabytes (though it will take much longer to compute). Additionally, hashes also have the feature of being very hard to predict the output. If you perform the sha256 of the following two sentences, the results are very different:

“My favorite flavor of ice cream is rocky road” e1fece8874ad0d3c4b8ecb9546cc42ddbe42b4d3198ba7fe19281e1cc6ad380e

“My favorite flavor of ice cream is rocky road!”  e8e55a047efe505a282212e5d3c6e4d60d0e9867b4c881cdea4b594243f64ec9

Hashes are used all over to hide data that shouldn’t be directly saved. A good website won’t store your password, they’ll store a hash of your password, and then when you log in they’ll compare a hash of what you’ve entered with what they have stored. If those match, then what you entered was the same as what you originally set as your password.

Putting It All Together

So, everyone who has the Bitcoin wallet software sees all the transactions, and they can make new transactions by pointing to old ones that prove they have money. So how do I let everyone know I’ve just made a transaction? And what stops someone from creating two transactions using the same money?

Bitcoin implements a process called Proof of Work. Every wallet program has all the up to date transactions, but some can also have what’s known as “miners,” and if you’ve heard of Bitcoin mining, they are talking about this process:

  1. The miner takes a bunch of recently submitted transactions (called a block).
  2. The miner takes a hash of the last block of transactions it knows about and adds it to the proposed block.
  3. The miner then adds a random bunch of gibberish to the mess.
  4. The miner then hashes everything together and checks if it meets an accepted solution.

Currently, the accepted solution is any hash that starts with eight 0’s (ex. 0000000074ad0d3c4b8ecb9546cc42ddbe42b4d3198ba7fe19281e1cc6ad380e). The Bitcoin network regulates itself at known intervals such that as time goes on and the total computing power of the network (meaning that they solve these problems faster), the number of 0’s required at the beginning to be an “accepted solution” goes up at roughly the same rate, making the problem harder to solve.

Why add random gibberish, though? Because it’s very difficult (currently mathematically impossible, google P=NP to make your head spin) to predict what the hash of a given input will be, it’s very, very, very unlikely that a hash of the previous block of transactions plus the pending ones will begin with eight 0’s (less likely than winning the lottery twice in one day while being struck by lightning and hit by a piece of space debris). By adding a bit of random gibberish, the resulting hash is completely different. If the hash it comes up with doesn’t start with eight 0’s, it throws out the random gibberish, picks a new blob of random gibberish, and hashes the whole thing again.

Miners around the world are doing this all the time, trying to guess what bit of gibberish will grant the right result. On average, this process takes between 10 and 30 minutes. Once a solution that contains the correct amount of 0’s is found, that miner announces to those around it that it has a solution. They check it and confirm and spread that around to their peers, and so it gets to all the wallets. The miner who finds this is rewarded an amount of Bitcoin (current 12.5). This is the only way Bitcoin are created. In addition to the difficulty changing over time in response to how quickly miners solve hashes, the reward decreases over time. Because these variables are accounted for, the rate of new Bitcoin being created is stable and known. There will only ever be roughly 21 million Bitcoin, and they will all be mined around May 7th, 2140.

Naturally it’s possible for two miners to find the solution to a block. If this is the case, it’s basically a race to see who finds the next block. When the next block is added to the chain, the longest chain is the winner, and people discard any abandoned blocks that weren’t further built upon. If you deal with merchants, they often don’t consider a payment fully “paid” until it is in a confirmed block and then another 1-3 blocks are confirmed after it. This means it’s certain to be locked in and won’t be changing.

One final part of a transaction is an optional “fee” that the person making the transaction gives as a reward to the miner who processes the transaction. Miners are usually configured to seek out the blocks with the highest fees offered, meaning that miners are essentially being bribed to include transactions. After all Bitcoin are mined, this will be the only method of payment that miners receive (and it’s already becoming a thing as the network skyrockets in popularity).

Bitcoin is a network of people sharing a book of IOUs treated as money, and amazingly it works. Deals reference previous deals to fund future ones, and the global current nature of the ledger everyone shares enables this to function at immense scale and speed. Because Bitcoin is a new technology, there are many people who are quick to call it a scam. But it isn’t a scam or a Ponzi scheme. The current value of Bitcoin is based on trading, just like people trade stocks, bonds, and pork belly futures. That isn’t to say there aren’t problems, but unlike our current banking system which has remained largely unchanged in the last 40 years (why can’t I deposit a check on the weekend? And why do transactions take DAYS?), Bitcoin can be updated and fixed if there’s a problem found. I don’t think Bitcoin will replace money, but I do think that money will soon start looking a lot more like Bitcoin.