Bitcoin has been around for more than 15 years now. When it comes to Bitcoin, another term is often used: the blockchain. The reason? Bitcoin needs a blockchain to exist. This is not the case the other way around: Bitcoin is only an application of the blockchain. And besides Bitcoin, there are many other cryptocurrencies. You can compare the blockchain with the operating system of your smartphone and Bitcoin with an app that runs on it. In this blog, I will go deeper into what exactly a blockchain is and how it works. I use the blockchain as it is used by Bitcoin.
Ledger
The blockchain can be seen as a digital ledger in which transactions are recorded. This ledger contains all transactions that have ever been made. The ledger has an open character, nobody is the ‘boss’. This is useful because it means anyone can add a transaction, just like anyone can do with regular currency via bank transfer. But on the other hand, it might also be inconvenient. Because if everyone is allowed to place transactions, how can we be sure that they have actually taken place and can (therefore) be trusted?
Cryptography
Every transaction is signed by the owner. In the digital world, we have known the possibility to sign data for some time. Just consider the concept of public and private keys. This digital signature is more secure than an analogue signature because an analogue signature can easily be copied. And although a digital signature is ultimately just a series of zeros and ones (and can therefore also be copied), the digital signature has an important property that prevents that.
The content of the data being signed – the Bitcoin transaction – is used as input along with the user’s private key to generate the digital signature. In fact, the signature consists of a series of 256 ones and zeros. When the input changes, even just one bit, a completely different series comes out of the function. Because the transaction data always has a unique ID, it is not possible to copy an ‘interesting’ transaction, in which you receive Bitcoins, for example, including the signature. After all, the unique ID is no longer unique.
Sign(Transaction Data, Private Key)= Signature
In addition to a function for signing, there is also a function with which a signed message can be checked. This function uses the transaction data as input, the signature that is already present with that transaction and the public key of the person who signed the transaction. The result of this formula is shown as either true or false. From where you can be sure that the signature was made by someone who owns the private key associated with the public key. Due to the enormous amount of 2256 possible signatures, it is inconceivable that anyone could have made these up by chance.
Moreover, because the function that generates the signature also uses the transaction data with a unique ID as input, you can be sure that this transaction data is original, unaltered and has only happened once. When changing this data, a completely different signature would appear as a result and the control function would therefore give a false result.
Check(Transaction Data, Signature, Private Key)= true or false
Blocks of transactions
The blockchain works with blocks of data, with the bulk of that data being the signed transactions, as just described. Each block has a sequence number. The first block has sequence number 0 and block number 701421 was created during the writing of this blog. In addition, a block has a special number called the nonce. The nonce has a special purpose, about which more later in this blog. Furthermore, there are two hashes in a block.
Without going into detail about the underlying mathematics, there are so-called hash functions in cryptography that can generate a hash based on certain inputs, sometimes also called digest. The input is generally a piece of text or a file. The hash seems random, but it isn’t. In the case of the Bitcoin blockchain, the SHA256 function is used, where the number refers to the number of bits in the function’s result. If the input is the same, the hash function always produces the same result. If the input changes, even just one bit changed from 0 to 1, the result will be completely different. Conversely, it is not possible to determine what the input was based on a known hash. That is, the only method you have is to try an input and then see if you arrive at the correct hash.
SHA256(“Hello world”)= | SHA256(“Hello worlt”)= |
01000010110001111111110001001010
01111101011110111111110111001011 10010110000100000010010110111110 11110011100011100010110000010001 11110001001010111110000001011111 11001000111101001100001101111011 11010000011100100011010000011011 01101111001100110011101100101011 |
01001101111000101110100010001100
11011001001100100101100110100100 10111110011111110110101100000000 11111000010001000100001110111110 00011011000101101111100000010001 01111100001110011111000110100111 01001011001101101010001011000010 00111110010110001001101010010011 |
One different character leads to a completely different result.
The entire contents of a block are given as input to the SHA256 function. The hash result is added to the block, so it can be seen by everyone. Anyone can check whether the hash is correct by submitting the contents of the block as input to the SHA256 function. Do you get the same hash as a result? Then the content of the block is correct. If not, it has been modified, intentionally or unintentionally, and is therefore invalid.
However, the hash of a block still has to meet a special condition before the block is really considered a valid block by the Bitcoin network. The hash must start with a certain number of zeros when we write out the hash as a binary number. The number of zeros lately varies between 68 and 76. This basically means that the contents of the block should be such that when used as input to the hash function, the result starts with that number of zeros. The chance of this is small, in the case of 72 desired zeros, for example, it is with a random block 12721272.
The previously mentioned nonce in the block is meant to make the hash satisfy the said condition. A nonce is actually a number, so it must be chosen so that the hash of the entire block, including that nonce, starts with the desired number of zeros. It takes a lot of math to find the right nonce because the only method is to try one by one. This is intentionally designed that way. So, you cannot ‘quickly’ come up with a block with a valid hash. In the blockchain world, finding a valid hash is called Proof-of-Work. It is inevitable that a lot of computation was done to generate a block that has a hash that starts with the desired amount of zeros. On the other hand, everyone can quickly check whether the content of the block leads to the hash present and therefore that the block is valid.
Chain of Blocks
Because a block in the blockchain cannot contain an infinite number of transactions, new blocks must be continuously generated. As mentioned before, a block contains two hashes, but we’ve only used one so far. The second hash is the hash of the previous block. Each block therefore also contains the hash of the previous block. As a result, a block is actually linked to the previous block. And that previous block is linked in the same way to the block that comes before it. This creates a chain of blocks: the blockchain!
Since the hash of the previous block is a piece of content of the current block, it also counts in the calculation of the hash of the current block. This makes it virtually impossible to make a change anywhere in the blockchain. First, for the modified block, a hash must be recalculated that satisfies the conditions of the number of zeros. That new hash should be included in the next block. That means that the content of that block has changed, so a hash must also be calculated for that block again and that new hash must be included in the next block et cetera, et cetera.
As a result, it is mathematically impossible to make a change in the blockchain ‘valid’. And even if you had enough computing power to realize this, there are still other “obstacles” to overcome.
Distributed Blockchain
The blockchain is distributed. This means that there is not one copy of the blockchain, but many identical copies. Each copy is maintained by a group of servers. These servers are called miners. A miner actually does the following:
- listen to new transactions and collect them in a block
- calculate the correct nonce/hash for this block (Proof-of-Work)
- make this block known on the Bitcoin network for the other miners
As soon as a new valid block has been announced, all miners will start generating the next block. The miners also get something in return for all their work: Bitcoins! In each new block, a miner is allowed to allocate a certain amount of Bitcoins to himself: the block reward. This is a special transaction that is added to all regular transactions. So, these Bitcoins are created out of nowhere and this is where they come from in the first place! However, this special transaction is not valid until the entire block is valid. Therefor it is a miner’s job to be the first to generate a new valid block. Incidentally, a miner often also receives a small transaction fee for each transaction in the block. These fees are paid by you and me when we send a Bitcoin transaction across the network.
Making a new block known on the network takes some time. As a result, it can happen that individual miners have calculated a new block (more or less) simultaneously. You then temporarily have a branch of the blockchain. So, there are two new blocks that are valid according to the rules that can be added to the blockchain. Which block should we now see as the truth? The answer to that is: the blockchain that contains the most work. In practice, this is the longest blockchain, the one with the most blocks. By simply waiting a number of blocks, a longest blockchain is automatically created and it is automatically recognized as the valid blockchain by the global network. That is why, in general, a number of blocks are also waited before a transaction is considered valid. The chance that it is a fraudulent / fabricated transaction becomes a lot smaller with each new block.
The fact that the blockchain is distributed makes it even less prone to fraud. Suppose that someone managed to make a change to one of the blockchains or calculate a new block and that this person therefore has sufficient processing power to get all the necessary hashes ‘correct’. Then it might still be realistic to be able to present one valid block to the Bitcoin network (as mentioned earlier), but after that it will be competing against the joint processing power of all miners worldwide. The chance of being the first to continuously calculate a valid next block is therefore virtually zero.
Particularities
The number of zeros required for a valid hash is not static. The number is chosen in such a way that it takes on average about 10 minutes before a block is calculated with the computing power present in the Bitcoin network. The number of zeros required is also known as the difficulty. This difficulty is recalculated every 2600 blocks. If these blocks were calculated faster than 2600 * 10 minutes, the difficulty goes up. If it takes longer, the difficulty goes down. How much up or down depends on how much faster or slower the 2600 blocks finished.
The block reward that miners receive is currently 6.25 BTC. And yes, with the current exchange rate that is more than 250,000 euros! In general, special (expensive) hardware is used for mining with so-called ASICs (Application Specific Integrated Circuit), processors that are designed for a special purpose. This hardware runs 24×7 and therefore consumes the same amount of power that must also be paid for. The block reward was 50 BTC in the beginning and is halved every 210,000 blocks. Due to the halving process, no more than 21,000,000 Bitcoins end up in circulation.
The first block in the Bitcoin blockchain is also known as the Genesis Block. This block was created on January 3, 2009 at 6.15 pm. The block, with the hashes contained in it, is the basis for the rest of the blockchain. It is a special case because there is no ‘previous’ block. Also included in this block is a block reward transaction of 50 BTC. Due to the distinct way the Bitcoin software handles this block, these 50 Bitcoins cannot be spent. It is unclear whether the inventor of the Bitcoin intended it that way or whether it happened by accident.
The inventor of the Bitcoin blockchain is Satoshi Nakamoto. At least that is the name he has identified himself under in the paper on Bitcoin. No one really knows who is behind this name.
Used sources:
https://anders.com/blockchain/
Ever wonder how Bitcoin (and other cryptocurrencies) actually work?
https://en.bitcoin.it/wiki/Genesis_block
https://www.bitcoinmining.com/what-is-the-bitcoin-block-reward/