So Bitcoin is cool and all, but how does it actually work? Like what is happening under the hood?
You’ve probably seen people talking about mining, immutability, hash rates, algorithms but for us tech noobs, we usually just gloss over that stuff.
But you can’t do that forever. Eventually you need to read about this stuff if you want to be educated about this space. So that’s exactly what I did.
This article is everything I’ve learned about “how Bitcoin works”. I’ve collected all my notes on the topic below, and since you’re probably smarter than me, hopefully you’ll understand them all too!
The Basic Concept
Bitcoin is digital currency. It’s a bit like email for money.
WHAT IS BITCOIN? A FREE BEGINNER'S GUIDE FOR KIWIS.
ENTER YOUR EMAIL BELOW AND I'LL SEND IT STRAIGHT TO YOUR INBOX.
Everybody has a unique address, just like a bank account number or an email address.
If you want to pay someone, you can send Bitcoin to them over the internet. Usually it arrives in just a few minutes, and only costs a few cents.
Bitcoin is different from a bank in that it’s universal. Anybody can have a Bitcoin address in just a few seconds, whether they’re in Japan, Iraq, Australia, even Antarctica. It doesn’t matter how old you are, your race, religion, credit history, how rich or poor you are. All you need is an internet connection.
The problem: Trust
People have tried to make digital currencies before. However they never succeeded due to various problems of creating digital value. The main problem with digital currencies in the past has been trust.
In fact, the main problem with all currencies is trust.
How do we know this currency is worth anything? How can we trust the government behind it? If there is no government behind it, how do we know it won’t just disappear? How do we know someone won’t just create billions of dollars of it out of thin air? How can you have monetary controls without a Federal Reserve, without a government?
The reason Bitcoin has lasted so long is, it finally found a way to solve these problems.
The first way it solves trust is through decentralisation. No one entity controls Bitcoin. It is governed by consensus of the people who use it and hold it. Therefore, it’s self serving in that the people who use it are naturally motivated to do what’s best for it. Decentralisation also means it is immune to censorship or shutdown.
The second way it solves trust is through transparency. The Bitcoin blockchain is a public ledger. Everyone who has an internet connection can see it. If Fred tells you he sent you $10, the entire world can check if he really did. By being total transparent, it’s difficult for anyone to be fraudulent.
The third way it solves trust is through its peer to peer protocol. By removing intermediaries such as banks and governments, senders and receivers now only need to deal with each other. While in the past banks have had to provide guarantees on top of governments also providing guarantees and commissions overseeing the banks and so on, now all that intermediary trust is removed. People simply deal directly with each other.
How does it do all of this?
Let’s take a look below.
How It Works (For The Consumer)
For the consumer, Bitcoin works just like internet banking.
You deposit Bitcoins into your account. When you want to pay someone, you enter their address and click Send.
The transaction gets sent to the network, processed, confirmed, and gets spit out the other end.
It’s just like money.
The difference is, it runs peer to peer. This means there are no third parties involved.
Normally when you send money to someone, you must send it through an institution. You set up an account at Kiwibank, you give money to Kiwibank to put in your account, and then you tell Kiwibank to send it to Fred for you.
If Fred banks with BNZ, then Kiwibank contacts BNZ and tells them to put that money into Fred’s account.
Once it arrives, BNZ alerts Fred that he’s got money, and he goes to the bank or an ATM to retrieve it.
As you can see, there are four parties involved here – you, Kiwibank, BNZ and Fred.
If you were sending this money overseas to Japan or Europe, there would probably be five or six entities, plus another hundred dollars in fees and a few phone calls and signatures.
Bitcoin removes all those intermediaries. When you send Bitcoin, nobody is involved except you, Fred and the internet.
However, as we said, the problem is trust. Without banks and governments, how can people trust this money? How do we know it’s real money?
How Bitcoin Creates Trust And Value
Bitcoin is not created out of thin air.
It is mined using computer power.
Let’s say Fred wants to mine Bitcoin. He fires up his computer and connects to the Bitcoin network.
Fred’s computer does two things for Bitcoin.
First, he’s maintaining the network. By running the Bitcoin software on his computer, he serves as another node securing the Bitcoin database. This contributes to the decentralisation of Bitcoin, like we explained before. The more Bitcoin nodes that are online, the stronger the network becomes.
However, Bitcoin understands Fred needs an incentive to do this. Humans are greedy by nature, so Fred won’t just leave his computer validating Bitcoin transactions 24/7 just to help the network out. He needs a reward.
This is where the mining side of it comes in.
While the Bitcoin software is running, there is also a reward protocol running in the background. This is a very complicated mathematical puzzle that your computer tries to solve, a bit like a lottery (I’ll explain it more below). Whichever lucky miner solves the puzzle gets rewarded with some Bitcoin.
This incentivises people to maintain the Bitcoin database and contribute to securing the network.
This provides value to Bitcoin because it means Bitcoin can’t just be printed out of nowhere, the same way dollars can by a government or central bank.
With Bitcoin, the inflation rate is fixed by mathematics. The supply is fixed by mathematics. This makes the value of Bitcoin very difficult to manipulate.
Many people compare these characteristics to gold – it is scarce, it must be mined, it is universal, divisible, fungible. The only difference is gold lives in big vaults under the ground, and Bitcoin lives on the internet.
That’s the first part of the trust equation.
The second part to this equation is a genius piece of technology known as the blockchain.
How The Blockchain works
A blockchain is nothing more than a database.
The Bitcoin blockchain stores data, such as how much Bitcoin is in each people’s wallet, and what transactions have been sent.
The difference between a blockchain and a regular database is, instead of being stored on just one computer, it is stored on thousands, maybe millions of computers around the world.
As an example, let’s go back to Fred’s account with Kiwibank.
If Kiwibank wants to charge Fred a fee, they simply charge it to his account. The only people who have access to Kiwibank’s database is Kiwibank themselves. Therefore they can do whatever they like, whenever they want.
This is known as centralisation, where full control is given to a single source.
Bitcoin works a bit differently. Because it runs on a blockchain, its database is stored on many thousands of computers around the world, such as Fred’s.
If anything is to change in that database, it needs to be updated on all those databases at the same time. This is known as consensus. No one entity can charge you a fee or remove funds from your account. For that to happen, everyone running the database needs to agree that this is the correct action, and update their records at the same time. If people can clearly see something not right is being done, such as trying to send money they don’t have, they’ll reject it.
Similarly, if someone like Fred tries to tweak the software on his computer and give himself a million Bitcoin, it will be noticed very quickly. If there are 1,000 computers running the Bitcoin software, the other 999 will very easily see he is being malicious and reject it from the consensus. For Fred to rejoin the network, he will need to edit his database to be in line with all the others before he’s allowed to reconnect.
As you might have guessed, this is also what makes the Bitcoin blockchain immune to censorship. If the US government tries to shut Bitcoin down, and disconnects all the nodes in America, there are still thousands of computers maintaining the network in 197 other countries. There may be a node in the jungle somewhere in Brazil, on a yacht in the Indian Sea, maybe a few in the Nepalese mountains. As long as one of these nodes is online at any one time, the network will survive.
This is why decentralisation is such a key issue to creating trust and validating Bitcoin as a currency.
Now that we have a broad idea of what Bitcoin is, let’s dig a little deeper and see how the blockchain actually works under the surface.
First you need to understand hashing. Once you understand that, you’ll be able to understand the mining algorithm. Once you understand the mining algorithm, you can understand how blocks are formed. And once you understand blocks you’ll be able to understand how they connect into a blockchain.
We’re gonna get a little technical from here on out, so sit up and pay attention!
To understand Bitcoin you need to understand hashing.
Hashing is almost like a form of encryption.
For example, you enter an input into an algorithm, and it spits out an output known as a hash.
A hash is nothing complicated – just a string of numbers and letters.
It’s important to see this in action so you have a mental image of what is actually happening.
To do this, you can try out the SHA-256 hashing algorithm (the one that Bitcoin uses) using this generator.
Take a look: I entered the input “bmoney.co.nz” and here’s what it gave me:
So from that we know the “hash” of “bmoney.co.nz” is:
Just to test, I tried this again on a different hashing calculator site and it gave me the same hash.
You can try this yourself. Just enter anything, like your name, and it will give you a unique hash that corresponds to whatever you entered.
Now here’s another important thing to know about the SHA-256 algorithm: No matter how long or short your input is, the algorithm will always produce an output that is 256-bit (64 characters).
Meaning if your input is 1 word, the hash is 64 characters and if your input is a million words, the hash is 64 characters.
As an example, I pasted my entire Investor Guide To Bitcoin article into the generator (2,000ish words) and the output was this:
Still 64 characters.
So no matter how much data you want to store, you can do it in 64 characters with a SHA256 hash algorithm. Instead of you (or a computer program) remembering thousands of lines of data, you can just remember the hash. Pretty cool eh?
But not all hashing is created equal. Some are better than others.
For a hashing algorithm to work for Bitcoin, it needs pre-image resistance.
Pre-image resistance means it is infeasible for you to find the hash input just by looking at the hash output.
For example, the SHA 256 hash of “one” is:
Is it possible for you to figure out that the string above is the hash of “one” just by looking at it?
Of course not.
But let’s take another scenario.
Let’s say I tell you the string above is the hash of a number between one and ten.
Can you figure that out?
All you need to do is enter every number between one and ten into the algorithm and find out which one matches.
This is known as the brute force method. You can’t crack the algorithm itself, but you can simply try every possibility until you find a match.
In this scenario, the brute force method would be very effective. You would probably find the answer in just a couple of minutes.
However, let’s say we decide to change the range of inputs. Instead of one to ten, let’s say it’s one to five billion.
Can you still crack the input via brute force? You can, but it’s going to take you so long that it doesn’t really matter.
Why is this important? It’s a key factor in the process of Bitcoin mining.
Now that we understand hashing, pre-image resistance and the brute force method, we are able to understand how Bitcoin mining works.
The idea is that miners compete to solve a very complicated puzzle. The miner who solves the puzzle “wins” the block and gets rewarded with some Bitcoin. Then the next block arrives and they do it all over again.
That’s the simple explanation.
For those of you who are interested in the complicated explanation (all of you, obviously, because this is so cool) here’s the long version:
Finding the nonce
Bitcoin mining is all about finding a number known as the nonce.
If you think of Bitcoin mining as opening up a treasure chest full of Bitcoins, the nonce (short for “number only used once”) is the key to the chest.
Bitcoin miners are simply searching for this key.
Let me explain further (pay attention here because it gets nerdy):
With Bitcoin, approximately every ten minutes a new batch of transactions arrive. This information is known as a block.
When it arrives, the first thing miners do is hash the entire contents of the block.
Then, miners add an arbitrary number to the end of this hash. It’s basically just a random number the miner has guessed.
This number is known as the nonce.
Then the whole string (the block hash + the nonce) is hashed again.
If this final hash falls within a target range, the miner has solved the puzzle and they get rewarded with Bitcoin.
Therefore, the aim of the Bitcoin miner is simply to find the correct nonce (sometimes known as the golden nonce).
Find the golden nonce, win the Bitcoin.
Confused? An example:
Let’s do it in a very simplified example.
Let’s say the target set on the Bitcoin software puzzle is to find a number less than 100.
And let’s say we know the nonce is between 5 and 10.
The block comes in. We hash it. The hash of the block comes out as abcdef.
So, the next step is to simply add all the possible nonces to the hash.
The first option will be abcdef5 (abcdef [the hash] +5 [the nonce]).
Then we hash it again.
Let’s say the hash of abcdef5 comes out as 150.
Because 150 is not less than 100, 5 is not the nonce.
So, we try again with abcdef6, abcdef7 and so on, until we find the correct nonce.
Of course with Bitcoin, it is not that simple. There are not only 5 possible nonces. There are millions.
Therefore Bitcoin miners need to operate on brute force, going through millions of nonces, until they find the correct one.
When a miner finds the correct nonce they notify the rest of the network. The rest of the miners validate the nonce to ensure it is actually correct. Then the winning miner receives a Bitcoin reward.
Then all miners receive the next block and the process repeats.
The next thing to understand is the concept of difficulty.
It is not impossible for a miner to find the nonce on the first try. However, he would literally need to be the luckiest man alive to do that. It would be the equivalent of me asking you to guess a number between 1 and 10 million, and you getting it right on the first try.
In the same way, it would be just as unlikely for you to guess the nonce on the very last try.
Just by laws of probability we know that the usual outcome will be somewhere in between the two extremes.
And of course, we can make it easier or harder by simply changing the range. Instead of me asking you to guess a number between 1 and 10 million, I could just make it between 1 and 2 million.
The Bitcoin software’s goal is to keep average block times to ten minutes. To do this, it simply changes the target range.
If finding the nonce is taking longer than ten minutes, the target range is increased so the nonce is found faster.
If finding the nonce is happening too fast, the target range is decreased so finding the nonce takes longer.
Through adjusting the difficulty this way, the Bitcoin software is able to keep block times to an average of ten minutes.
Hash rate refers to the amount of hashing activity on the network.
If the hash rate is high, it means there are many miners on the network trying to solve the nonce puzzle. As a result, the Bitcoin software will start to increase the difficulty, so the nonce isn’t found too fast.
If the hash rate is low, it means miners on the network are few, possibly having left to mine other coins, or simply shut off because mining is no longer profitable. As a result, the Bitcoin software will reduce the difficulty to try and attract miners back to the network.
This is one of the impressive aspects of Bitcoin. The software encourages miners to act in their best interests – leaving the network when the difficulty is too high, and joining the network when the difficulty is low. It just so happens that what is best for the miners is also best for the network.
When miners accept the new block of transactions, they need to verify the transactions first.
That involves checking whether the people who sent the transactions actually have enough Bitcoin to spend the amount they are requesting.
If someone tries to send an invalid transaction, it will be rejected by the majority (if not all) of the miners and be excluded from the block.
When the miners have verified which transactions can be included in the block, they can then hash it and begin searching for the nonce.
The concept of blocks (in a chain)
Now that we understand hashing, mining and the nonce we can understand blocks.
Every second, people are sending transactions to the Bitcoin network.
For example, if I want to send 1 Bitcoin to Fred, I open my Bitcoin wallet and submit that transaction to the network.
Every ten minutes, miners then receive these transactions in a bundle of data. As we know, this bundle of information is known as a block.
But blocks are more than just transactions. A block also contains:
- Difficulty target
- Hash of the previous block
It’s that last item that is essential to understanding blockchains in action. Let me explain:
Each block contains lots of data (like, a lot).
This data can’t be carried forward into each block, because it’s just too big.
So how do we keep a record of all the transactions in Bitcoin’s history?
Let’s say we have a starting block, we will call it Block A.
With Block A, everything gets verified by miners, the nonce is found, it gets timestamped within the ten minute window, everything is perfect, boom, block is confirmed.
Then miners move onto Block B.
Now with Block B, everything happens exactly the same. Miners verify the transactions, nonce gets found, block is confirmed. However, when the Bitcoin software adds Block B to the chain, it includes one special thing – the hash of Block A.
Remember, including the hash is easy. It’s just a 64 character string. So even if Block A is enormous, it can still be hashed into a small 64 character string.
This will continue down the line, so the hash of Block B will be included in Block C, and the hash of Block C will be included in Block D and so on.
This means every block is connected in a chain, hence the term blockchain.
If someone tried to “hack” the chain and modify Block A to say, give themselves a million Bitcoins, then the hash of Block A would change.
And that would mean it would no longer match the hash of Block A that’s recorded in Block B.
Therefore they would also need to hack Block B.
However, after hacking Block B, the hash of Block B would change also, and it wouldn’t match the hash that’s been recorded in Block C.
So they would also need to hack Block C, and Block D, and so on.
Now in this example, it might not seem that impossible, because there are only four blocks.
But what if there are 10 million blocks? And a new block being formed every 10 minutes?
Therefore, the blockchain cannot be modified because modifying one block requires modifying every other block as well. Because all those blocks have already been confirmed, this task is very close to impossible.
This is the underlying concept of the so-called immutable blockchain.
So what did we learn?
That the Bitcoin network is secured by miners.
Mining is the process of finding a secret key known as a nonce, through a process known as hashing.
Hashing is performed on groups of transactions known as blocks.
Blocks are validated by miners during the mining process and then are added to the blockchain.
Each new block contains the hash of the previous block.
Therefore a block cannot be modified without modifying every other block, making the blockchain immutable.
Do you now understand the Bitcoin blockchain? What did I miss? Let me know in the comments 😉