On Bitcoin 2: The One Way Function
For those of you who have never heard of “white paper”, it is an article written by a creator of the project where the creator talks about what the creator will do and how to accomplish the goal. In the case of Bitcoin, the creator is Satoshi Nakamoto. The project was to make a peer-to-peer electronic cash system and proposed a way to do it. Obviously, Satoshi succeeded on this mission, and this 9-page white paper became something that’s worth more than $1T in market cap by a little over 10 years.
At least to me, the paper was tough to read. There were just so many sophisticated ingredients developed through years of research in computer science and cryptography. Satoshi’s innovation was connecting those ingredients and making it into one. Hence, to understand the paper, I had to learn about those ingredients first. Perhaps you have heard of hash function, proof of work, and digital signatures which are used in Bitcoin. I am talking about those. They were not invented by Satoshi but they play crucial roles in the Bitcoin white paper. I would like to try to explain some of the key ideas in Bitcoin for average people who did not necessarily come from mathematical backgrounds.
The main contribution of Satoshi Nakamoto is a solution to the double spending problem as we talked about in the last note. The Bitcoin network is a peer to peer network whose members do not trust each other, but somehow they need to agree on how much each member owns by establishing a public ledger. The double spending problem was solved by establishing the public ledger which is extremely hard to hack. Hence, the question is how we can get such a ledger. If you have heard of the Byzantine Tolerance Problem, that’s exactly what we have here.
The below is a 1 page version of the Bitcoin white paper.
Everything is done by cryptography
The main part of the white paper is dedicated to making a public ledger which records every transaction encrypted by cryptography. The blockchain technology is a tool which enables peers to maintain such a ledger. Each block can contain about 2000 transactions per a block. [data]
New transactions get put to a new block and this new block gets connected to the previous block; Thus, blockchain. This connecting behavior is called mining and people who do mining are called miners. They solve a mathematical question. I will explain this question later on, but the point is that this question is so easy for everyone to see if you get your answer right, but extremely hard to get your answer. It is like playing archery which you cannot cheat. You have to keep shooting arrows until you hit the target, which means you waste all of your arrows till you hit the target. It is very easy to see if you got your target. We just need to see the target and see if there is an arrow on the target. We will go over what Bitcoin mining is exactly, but it is like archery in which you use electricity every time you shoot an arrow. Of course this leads to a concern about the energy use, and the Bitcoin network uses a lot of energy, yet at the same time this method does provide a strong security which results in the value of the Bitcoin.
Miners compete to hit the given targets and whoever wins the race gets awarded by bitcoins called block rewards. These rewards get half every four years. Originally, it started with 50 bitcoins. Then in 2012 the reward became 25 bitcoins, then in 2016 it became 12.5 bitcoins, since 2020 it’s been 6.25. This is called halving. Even Though rewards get less, the bitcoin price in terms of fiat is skyrocketing so there are enough incentives, thus a big race. The maximum number of halvings allowed is 64 times, so the code imposes a zero reward (returns only the fees) if the 64 halvings is exceeded. One reason that the market regards bitcoin as a digital gold is because they both have a finite supply. Open source code of bitcoin says there will be 21 million bitcoin. There are roughly 19 million bitcoins available on the earth.
Speaking about the finite supply, there are precious metals including gold in asteroids, which means Elon musk could change the supply of gold. The supply of bitcoin is fixed unless the code gets changed. [source] Plus, gold supply is going up, which is easy to imagine because gold hardware miners get more and more efficient. [source]
I wanna dive into how bitcoin mining works in detail, but before that we need to know 1-way functions.
Bitcoin gets more and more secure as time goes
There are rules which miners need to follow, one of which is that when you win the mining race, you have to connect to the longest chain to get the reward. You could connect your block to shorter chains, but you wouldn’t get rewarded. There are also things called forking, which could happen when two parties hit the target at the same time, but this would settle eventually. For the sake of simplicity, let’s assume this does not happen for now.
By the above rule, once a new block gets mined, it gets chained up with the longest chain. The record of this block gets more and more difficult to be hacked as more and more new blocks get connected starting with this block. In the block chain, the number of blocks connected starting with the first block called is called block height. The current height of Bitcoin is over 73000 for the first block created by Satoshi Nakamoto. [data]
The above idea is summarized in chapter 3-5 in the bitcoin white paper. The key is 1-way function.
Lex Friedman’ podcast with Silcio Micali
One day I was listening to the podcast of Lex Friedman with the guest Silcio Micali who is the Turing Award winning computer scientist, and also a founder of a blockchain project called Algorand. There, Lex Friedman asked Silcio Micali about the most beautiful idea in cryptography, computer science and mathematics.
Micali said one powerful idea is a 1-way function, he added a 1-way function empowers good guys and hinders bad guys. This feature is a key in the Bitcoin network. The rest of the note is dedicated to explain a 1-way function and how it is used in bitcoin mining. I wanna avoid math but this is fundamental, so let me explain.
Let’s get into the definition of a function. If you have seen that if f(x)=2x then f(3)=6, then you already got it. If you haven't, you can think of a function as a smoothie blender. A smoothie blender takes various things such as milk, bananas, strawberries, sugar, and so on. Then you press the button, and you get your smoothie. You can think of a function like this; it takes numbers and letters and you get an output. A 1-way function is a special kind of function. “1-way” means that it is easy to get an output, but by having some output, there is no way to guess what was inputted. So, a blender is exactly like a 1-way function because making a smoothie is easy by pushing a button, but having an already made smoothie by someone, it is not easy to guess the ingredients used and nearly impossible to guess how much of every ingredient was put in.
There are many 1-way functions. One of the most famous ones is SHA-256. SHA-256 is used in Bitcoin. It’s the abbreviation of Secure Hash Algorithm 256 bits, you can input pretty much anything; letters, numbers, and special characters in any length, theoretically up to 2^64 bit. The output of SHA-256 is 64 digits letters from A to F and numbers from 0 to 8. You can play around it. [SHA-256 calculator]
<example>
Input=test
Output=837CCB607E312B170FAC7383D7CCFD61FA5072793F19A25E75FBACB56539B86B
SHA-256 is a 1-way function; you can get an output easily once you decide your input, but if you have some output given, you can never guess the original input. In other words, to find the input, all you can do is randomly pick a candidate and try. This feature will play a key role in the security of Bitcoin mining. An output via SHA-256 function is often called hash for simplicity. In the above example, the hash of test was 837CCB607E312B170FAC7383D7CCFD61FA5072793F19A25E75FBACB56539B86.
Now, let’s understand the relation to mining.
Mining and SHA-256
Earlier in this note, we talked about connecting a new block to the previous block, and they form a chain of blocks. SHA-256 is used when you try to connect. First, we will look into what exactly blocks are. A block just contains data, mainly transactions and the summary of the block called block header, which contains the following.
Version: Bitcoin is all open sourced, hence there are versions for members to download. [version history]
Previous Block Hash: This is a hash of the previous block. So, say you are working on block #100, then it is the hash of the block header of the block #99. This information contains everything about the block #99. Hashing is great because you can summarize all block information into a 256 bits.
Merkle Root: This is a technical part, but it is just a compression of all transactions.
Timestamp: The time when the block gets made.
Difficulty Target: Satoshi programmed so that a block gets mined every 10 minutes. Currently in 2022, a winning miner gets 6.25 Bitcoin. So, every 10 minutes new 6.25 bitcoins get issued. However, more and more miners are coming in to play, so in the archery example, more and more arrows get shot. If we keep using the same target, the game gets easier. Because we are firm on 10 minutes, we need to make the target smaller. This is the idea of a difficult target. If each block tends to get mined in less than 10 minutes, which means less competition, we make it harder to mine. Conversely, if each block tends to get mined in more than 10 minutes, which means more competition, we make it easier to mine. This adjustment is done every two week. We will see how difficulty gets adjusted in the context of Bitcoin mining soon.
Nonce: the counter for the number of arrows shot to the target. In other words, the number of trials.
Mining
Now, we are ready to understand what mining is and how SHA-256 comes in. SHA-256 function takes pretty much everything as an input and outputs 64 digits letters from A to F and numbers from 0 to 8. In particular, you can input the block header. Everything in the block header is fixed except for nonce. Nonce is important because you keep changing in the mining process. Going back to the characteristics of SHA-256, you have no idea what you get as an output unless you try. In bitcoin mining, you wanna get consecutive 0’s from start to as much as possible. The more 0’s you need, the more trials you need to make because a such case is very rare. A baby example is throwing a dice. What is the possibility of getting 4? A dice has six faces, so, it’s 1/6. Now what is the probability that you throw a dice twice and get 4 both times? It is 1/6 times 1/6, so 1/36. And, the the probability of throwing dice three times and getting 4 every time is 1/6 times 1/6 times 1/6, which is 1/216. Probability is getting to zero. In other words, if you wanna see that happen, you have to try so many times, like 216 times.
The Bitcoin mining race is pretty much similar. You wanna get many consecutive 0’s from the first digit by hashing the block header changing nonce. The target difficulty is measured by how many consecutive 0’s are required. Let’s say “blockinfo” represents the block header except for a nonce. You hash “blockinfo” adding a nonce in the following way. Let’s assume the target difficulty is low and you only need to get the first digit to be 0. Again with SHA-256 calculator, we have
blockinfo0 => 1803DC6EC2778842A1E81B624AD61250E84B809B6943AED2C98A1A808DFD8C4B
blockinfo1 => 95C7C35D1B94EF2837B860C2574DA2E400FF62092698C227B77AD1225A596EA7
blockinfo2 => A589CEA5EB5588A2D78467048C1B97DC10B91D8D11796BE88CF6D52C5C697F2E
blockinfo3 => C804FF30C9AE80A389A18100634DBFF35ED823A6DE251E805FC6BF5B4E643D0B
blockinfo4 => 28BC12919FC9611D878C229A17B952684415250E55E8D33382A2CBCEA2850614
Keep going, and we finally get
blockinfo11 =>
02D6319D50B3D8A0D5C7DDC8B4E13BB87C73C56CA4E32742037865E47444A113
So, it took us 12 trials to meet the difficulty target. If we were to aim from 2 leading 0’s, we have to try more. Now, the current difficulty target is over 17 leading 0’s, which requires so many trials; the current hash rate is round 200 million TH/S. [source] One tera hash(TH) is 1 trillion (1,000,000,000) hashes, and to get one block, it takes 10 minutes which is 3600 seconds. A lot of trials, thus a lot of electricity is used.
Proof of Work
It is so easy for everyone to see that if a miner got the nonce right because everyone can just hash and check. Also miners cannot cheat by the virtue of SHA-256. Therefore, this correct nonce shows the Proof of Work. As we saw in the example, this mining competition is all about how fast you can hash. It changed a lot from 13 years ago when Satoshi said “1 vote for 1 cpu” in the white paper. There has been some progress in hardware development, and now almost all miners use ASIC (Application-Specific Integrated Circuit) which are specifically designed to only hash, but super fast.
Mining is a well established business. Because it uses electricity when you hash, it is better to mine in a place where electricity is cheap. Also, because ASIC machines get heated, it’s better to do it in a cold place. Just to get the feel for it, take a look at the following.
Mining is distributed around the world. Here is the distribution with respect to countries.
[source]
Notice, China disappeared due to countrywide ban of mining.
How to participate
There are few ways to mine as an individual. First, you could go get a miner and complete solely. Solo mining has a very small chance of winning. Large mining companies who own thousands of ASIC miners have huge hash rates and there is no chance for a solo miner. Yet sometimes you get lucky.
If you are not a company, a great way is to join a mining pool. They aggregate independent hashing powers, and you get a piece of reward depending on how much you contributed in terms of hash rate.
Energy
Lastly, we have to talk about Bitcoin energy use; hopefully this guides to the next note. It is true that bitcoin uses a lot of energy, more than some countries. It uses roughly 0.6% of global electricity consumption. [source]
Bitcoin mining takes 125.13 Twh per year while Gold mining takes 131 TWh per year. [source] Unlike gold mining where you need serious mining machines, bitcoin mining kits are more portable. Hence it could incentivize bitcoin miners to move to the place with cheap electricity including a place where waste of energy such as gas flaring are happening. For instance, global gas flaring is worth 668TWh of energy, which is 5.5 times the bitcoin energy network. In the US, Texas has a great opportunity for this because of its oil production. Take a look at this article, this 23-year-old Texans made $4 million last year mining bitcoin off flare gas from oil drilling.
Bitcoin mining is not perfect like other precious metal mining, but we are seeing it’s becoming greener. Currently in Q4 of 2021, it is estimated that 58.5% of bitcoin mining is done by renewable energy. [source]
Cost of renewable energy is rapidly decreasing, which is good news for Bitcoin. The cheaper it is, the better for bitcoin miners. [source][source][source]
Next notes
In next note, we will go over Bitcoin energy use. While some critics are right and needed, it would be good to see the current state of Bitcoin energy use comparing with other precious metal minings, daily necessities, and other crypto currencies. These comparisons would tell us efficiency of Bitcoin as a medium of exchange and store of value.
The Bitcoin community was aware of the energy use of Bitcoin from very early days. Here is a tweet from Hal Finny, one of legendary figures in Bitcoin, on Jan/28 2009. Recall Bitcoin blockchain started on Jan/3 2009. Also, he only tweeted 41 times, and this tweet is one of them.
Thanks for reading.