Bitcoin has been at the top of the headlines from the beginning of 2021. But this article is not written to predict whether bitcoin will rise or plummet on the charts. It is about understanding blockchains – The technology which enables bitcoin to securely operate without any central authority like banks. If you want to send money to your family residing in another part of the world, simply initiate a transaction, wait for a few minutes, and your family gets the money. Compare that to banks who take multiple days to verify the transaction. Ironically, bitcoin promises to be more secured even though your transactions are validated by random people with computers. All thanks to math and cryptography that you don’t need to trust anyone and still sleep peacefully.

The Bitcoin Network

The bitcoin blockchain network consists of all the computers in the world that are sending, receiving, and mining (generating) bitcoins. Everyone maintains an individual copy of the record of all the approved transactions. When you want to send bitcoins to someone, you broadcast that message to all the computers in the network. A miner validates and confirms your transaction by solving a “puzzle”. Once confirmed, the miner broadcasts this message to the rest of the network. Then, everyone verifies the message and adds your transaction to their personal copy. 

To understand blockchains, let’s go through all the steps of a bitcoin transaction in detail.

Announcing Transactions

When I want to send money to my friend, Omar, I simply broadcast a message to everyone in the network saying, “Transfer 1.5 BTC from Safin to Omar”. But how can everyone be sure that I am the one who initiated the transaction? What if Omar is the one who broadcasted this message so that money is transferred from Safin’s wallet to his wallet without Safin’s permission? 

Here is the role of Digital Signatures. Every user in the bitcoin network has a Private key and a Public key. As the names suggest, everyone in the network can see your Public Key, but no one will ever know your Private Key and you shouldn’t tell it to anyone. These keys are actually really long alpha-numeric characters. Your Public Key is unique because it is not something you set, rather, it is generated from your Private Key

Every message you broadcast to the network must be digitally signed. This signature is a 256-digit number of 0s and 1s. What if someone copies your signature from one transaction to sign another transaction? That is not possible because you cannot use the same signature for all the messages. A digital signature depends on the message and the sender’s private key. Changing the message slightly generates a whole new signature. Mathematically, a digital signature is a function of the message and the Private Key

f (Message, Private Key) = Digital Signature

Still, this doesn’t stop Omar from broadcasting, “Transfer 1.5 BTC from Safin to Omar”. He can write the message and sign it digitally. But, there is another mathematical function used to verify the validity of the signature. Inputting the Message, Signature, and Public Key (of the sender) to the function spits out True or False. So if someone checks the validity of the message using Safin’s public key, the function will spit out False because Omar generated the Signature using his private key, not Safin’s. 

Verify (Message, Signature, Public Key) = T/F

Okay, so now that we know that no one except Safin can initiate the message, “Transfer 1.5 BTC from Safin to Omar”, how do we know that Safin actually has 1.5 BTC? 

In this transaction, Safin must show reference to some previous transactions where he had received at least 1.5 bitcoins. Let’s say 2 months ago, Safin received money from his friends. He had received 1 BTC from Rinith and 0.5 BTC from Leen. So today, when he wants to send 1.5 BTC to Omar, he has to show references to the transactions from Rinith and Leen. Basically, you must show reference to previous transactions that collectively amount to at least the amount you are trying to send. Once a transaction has been already referred to, it cannot be referenced again. So tomorrow if Safin wants to send 1 BTC to another friend, he can’t refer to Rinith’s transaction because it is already referred by the 1.5 BTC transfer to Omar. 

So, a message may look something like the one shown in the figure. Your name does not appear anywhere in the network, only your Public Key does. 

AMOUNTFROM TO
1.5 BTC1NDyJtNTjmwk5xPNhjgAMu4HDHigtobu1sbc1q3kqxrs82r8gjfymvtj54wxzr2fcl9a5434cgct

Confirming Transactions

Once your transaction is validated, it is placed in a pool of unconfirmed transactions. Now we introduce a user in the bitcoin network whose name is Hadi. His job is to confirm the transactions from this pool. So first, he picks a bunch of unconfirmed transactions and groups them together to form a block. Then, he has to solve a cryptographic puzzle! Here is the fun, or rather the head scratching part. 

Let’s take a moment to understand the cryptographic function called the “SHA256” (Pronounced shaa 256). Basically, every single piece of data in the digital world is associated with a “digital fingerprint” called its Hash. For a given piece of data, the SHA256 generates a Hash which is apha-numeric and has 256 digits. The interesting property of SHA256 is that it is irreversible. You can easily obtain a hash for a data, but given a hash, you can never figure out its original data. To understand better, take a look at the demonstration below. 

Example of a hash for a given data
Screenshot taken from https://andersbrownworth.com/blockchain/hash
Inserting a full-stop at the end completely changes the hash
Screenshot taken from https://andersbrownworth.com/blockchain/hash

Now, a block consists of a list of transactions, hash, and a number called Nonce. Hadi’s job is to find a nonce for this block that generates a hash starting with four zeros (We will discuss more about the four zeros later). There is no formula or equation you can solve to find this number. Like discussed earlier, you can easily find a hash for a given data, but not the other way around. The only way for Hadi is to keep guessing numbers till he finds a hash starting with four zeros. Sometimes, the first number he guesses could give a hash starting with four zeros, but this occurrence has very low probability. 

Example of a solved block
Screenshot taken from https://andersbrownworth.com/blockchain/block

Once Hadi finds the correct nonce, the block is said to have a “Proof of Work”. Then, he broadcasts it to the rest of the network who simply verify the nonce by passing it through the SHA256 function. All the transactions in the block are now confirmed. Because Hadi did so much work to find the special number, he is rewarded with some bitcoins – No one transfers those bitcoins, instead the system generates the bitcoins out of thin air for Hadi. 

This is how new bitcoins are added to the network. Hadi is called a miner in the sense that he goes through so many numbers to find one special number for a reward; like mining deep under the earth for rewards like gold. 

Like Hadi, there are many miners in the bitcoin network. Basically, miners listen for transactions made by the public. Then they combine a group of transactions in a block and compete with other miners to solve a cryptographic puzzle to create Proof of Work. The first one to solve the puzzle is rewarded with some bitcoins, and in some cases, transaction fees. 

The cryptographic puzzle cannot be solved humanly by guessing numbers every time. It requires immense computational power. Miners use powerful processors to keep guessing as many numbers they can in a given amount of time. Back in 2009 when bitcoin just started, I could have mined them with my laptop. But now, there are dedicated GPUs for bitcoin mining. 

Creating a blockchain

Once a block is said to have a Proof of Work and verified by others in the network, it is placed on the bitcoin blockchain network. Every block in the blockchain consists of its own hash and the previous block’s hash for the sake of ordering the transactions. Thus, a block is linked to its previous block. Because blocks are linked in such a way, it is called a blockchain (Chain of blocks). 

The bitcoin blockchain is a record of all the transactions ever made since 2009. Everyone in the network can see all the transactions. If you want to check all the transactions dating back to 2009, you can check them at https://blockexplorer.com/

Example of a blockchain

Since a block consists of the hash of its previous block, it is very difficult to modify a block in the middle of the chain. Let’s say there is a blockchain of currently 10 blocks. You want to maliciously modify the contents of the 5th block by removing one zero from one of its transactions. As you know, modifying the data, even a little bit, completely changes the hash. Now you have to find a nonce that makes the hash of this block starting with four zeros. But that changes the “Previous Hash” in block 6. So you have to find a nonce for block 6 such that its hash starts with four zeros. But that will change the hash of block 7. And you will have to repeat this till block 10. 

You have to “re-mine” 5 blocks by yourself and broadcast it to the network to make it seem as if nothing has changed. By the time you mined the 5 blocks, other miners must have already added and verified new blocks to the original blockchain. Let’s say that by the time you mined 5 blocks, other miners have added 5 more blocks to the end of the original blockchain. All computers in the network can now see two block chains; one with 10 blocks and another with 15 blocks. The rule of thumb in these situations is to accept the longer chain. No one will replace their copy with your copy and your blockchain will be rejected. 

Moreover, you will never have enough computational power to match the collective mining rate of the rest of the network. So, the above statement of you solving 5 blocks and other miners adding 5 more blocks within the same time frame is already an overstatement. Therefore, it is said that blockchains are immutable. 

Some Bitcoin Facts

In the example above, we saw that miners have to find a number that gives a hash starting with four zeros. In bitcoin, this number changes periodically to increase its difficulty so that mining one block requires 10 minutes on average. This also means, it takes only 10 minutes to transfer money between accounts. Compare that to the 2-3 day transaction time through banks. Interesting!

Earlier, with less computational power, it was reasonable to mine one block in 10 minutes. Now that miners use very powerful processors, the puzzle has to become progressively difficult. But it can be solved in 10 minutes regardless because there are many miners in the network now compared to 2009.  

This is how other cryptocurrencies differ from bitcoin and each other. For example, the average block time in Ethereum is 15 seconds while in Litecoin, it is 2.5 minutes. 

New bitcoins are added to the network when a block is mined. This means, every 10 minutes, new bitcoins are added. Back in 2009, the block reward was 50 BTC. Based on the math, this amount is halved every 4 years. Today, miners receive 6.25 BTC as block rewards. Given this depletion of block rewards, it is estimated that the last bitcoin will be mined in 2140. Will miners stop validating blocks after that? No, they can still be incentivized with transaction fees. 

What if two blocks are mined at the same time?

Although the math makes the situation unlikely, it is possible that two miners might have solved two blocks, A & B, at the same time. When miners broadcast this information, it doesn’t reach every computer at the same time. In blockchains, it is not like everyone is connected to a platform and whenever users want to broadcast any information, people can see it right away. Rather, information travels from computer to computer based on where the message is broadcasted from. So it is possible that some computers receive A first and others receive B before A. 

If Hadi sees A first, he will add his next block to it. If Zin (another miner) sees B first, she will add her next block to it. Then computers in the network may see two versions of the blockchain. But eventually, one chain will get longer than the other one. Then, we know the rule of thumb is to accept the longer chain, and the shorter chain will get cancelled. All the transactions in the shorter chain will be sent back to the pool of unconfirmed transactions. That’s why it is recommended that you wait for at least 6 more blocks (equivalent to one hour) to be added after your block to actually confirm your transaction. 

Ending Remarks

Blockchain is a remarkable technology that has paved the way for a revolutionary invention like bitcoin (and other cryptocurrencies). Regardless of what happens in the future, coming up with a concept of completely removing intermediaries (banks) to send and receive money quickly and securely is fascinating. Ironically, the system depends on thousands of random people, whom you may never see, to operate and prevent frauds – Thanks to the math and cryptography. 

At the time of writing this article, many companies have already started accepting bitcoins as payment. One of the first big companies to accept bitcoin is Microsoft. Other companies include PayPal, Home Depot, Starbucks, and many more. In fact, in June 2021, ElSalvador became the first country to give bitcoin the status of legal tender. Meaning – you can pay your debts in bitcoins in this country. 

Cryptocurrency is not the only application of blockchains. They have also shown promises in the healthcare industry to maintain patients’ records. Officials are also considering using blockchain for presidential elections. Other industries with blockchain use-case include legal, media & entertainment, Internet of Things (IoT), and many more. 

Would you consider implementing blockchain in your business? Let’s see what the future holds. 

5 comments

  1. Good Stuff bro! Interesting, I finally learned how miners make bitcoin. Plus, I always thought there was a finite amount of bitcoin but since the reward keeps getting halved every 4 years that may be true!

    1. Yeah it’s interesting. Some coins like dogecoins are infinite. I wonder how that actually works. Perhaps we must look at the math behind bitcoin’s depletion more closely.

Leave a comment

Your email address will not be published.