###### This is a continuation of a 3 part series describing Bitcoin mining in three different difficulty levels. You are now on **Veteran**

To fully understand mining, we first need to understand cryptographic hash functions.

More specifically, we need to understand the 256 bit secure hash algorithm also known as SHA256 developed by the NSA. SHA256 (Secure Hash Algorithm) is used in security applications and protocols like TLS, SSL, PGP, SSH and Bitcoin.

What SHA256 does is encrypt any message, file or piece of information into a unique combination of ones and zeros.

The important part is that the result looks completely random. If you change just one bit of information you end up with a completely different hash. The hash you get is unpredictable.

This plays an important role in more than one aspect for Bitcoin, but we are focusing on mining for this article.

Miners need to create a "block header" before starting the guessing game, let's go through some of the necessary fields.

Size |
Field |
Description |

4 bytes | Version | The Bitcoin Version Number |

32 bytes | Previous Block Hash | The previous block header hash |

32 bytes | Merkle Root | A hash of the root of the merkle tree of this block's transactions |

4 bytes | Timestamp | The timestamp of the block in UNIX |

4 bytes | Difficulty Target | The difficulty target for the block |

4 bytes | Nonce | The counter used by miners to generate a correct hash |

The "nonce" is a number used once and the number that needs to be guessed by the miner.

The resulting hash of the block needs to be lower than the target. The target (difficulty target) is calculated by the network.

For instance, the target for Block 694,288 was

000000000000000000136aa20000000000000000000000000000000000000000

The number starts with 18 zeros followed by a one. The number is in hexadecimal format.

The hash of the block needs to be lower than this number. And this is what gives you the difficulty of the guessing problem.

What the miners do is add the nonce to the block and change it so often until the hash for the whole block has more zeros than the target. For this specific block this was the hash that the miner found.

It starts with 19 zeros

00000000000000000008bf6a32c7bb2d24b06a4ca419228b2e8056a25d750eaf

You can try finding a working nonce yourself to get a feeling of how many tries are necessary with this online SHA256 generator.

In this generator, try putting your name as an example and look for a hash that starts with two zeros. You should be able to get one zero after only a couple of tries. To get a hash with two zeroes, it could take upwards of 50 tries.

The probability to get a hash that starts with 3 zeros is already 1 in 4096. So don't bother wasting your time to look for a third zero. The exponential growth picks up fast.

You can calculate the probability of finding a number that when added to a message generates a hash with 19 starting zeros when using a hexadecimal format. It's 1/16^19 which is an insanely large number.

What Proof of Work does is make it extremely hard to find this number which results in a lot of computing work in the form of many guesses.

But once the number is found, everyone can easily verify that it is the correct number.

All you need is to fetch a block in its raw representation encoded in hexadecimal, run SHA256 once, and see that the resulting hash starts with the given amount of zeros.

The unpredictability of incoming transactions and Proof of Work also make sure that the order of blocks stays the same over time.

Because every new block includes the hash of the previous block, by adding the hash of the previous block you cannot easily reorganize blocks.

The older the block is, the more certain it becomes that it will stand exactly like that for all eternity, because changing one single element (like one transaction) would mean all the Proof of Work of all the following blocks would need to be redone.

## 51% attack!

A threat to the network is a 51% attack in which case an attacker has more computing power than the rest of the Bitcoin network combined.

The higher the growing hashrate goes, the less likely this case becomes as it's simply insanely expensive to attack the network in such a way. The consequences are also often misunderstood.

Nakamoto explained in the Bitcoin Whitepaper the longest chain protocol, and its security, by analyzing the private double-spend attack, a race between the adversary and the honest nodes to grow a longer chain.

Nakamoto writes, "The majority decision is represented by the longest chain, which has the greatest proof-of-work effort invested in it. If a majority of CPU power is controlled by honest nodes, the honest chain will grow the fastest and outpace any competing chains. To modify a past block, an attacker would have to redo the proof-of-work of the block and all blocks after it and then catch up with and surpass the work of the honest nodes. We will show later that the probability of a slower attacker catching up diminishes exponentially as subsequent blocks are added."

The probability of this attack is also formally described in the white paper and can be characterized as a Binomial Random Walk.

One example, let's say the honest chain has 55% of the computing power and the attacker chain has 45%. The probability that the attacker catches up from ten blocks behind is (45/55)^10 which equals 13%.

The probability the attacker catches up from 100 blocks behind is (45/55)^100 is extremely low = 0.000000192744693%

Another fraudulent attack could be a miner trying to add a block with a higher target thereby a lower difficulty or starting the hash with less zeros. But other network participants will easily reject the block.

Everyone is incentivized to play by the rules, which is beautiful.

## The block reward

So where do new bitcoins come from and how do they get added to the miners addresses?

The "block reward" is added as the first transaction in a block. It has no sender or signature and is also known as the "coinbase transaction".

The Coinbase transaction is also set by each miner individually. The current maximal block reward is 6.25 bitcoins, but miners could potentially only want 1 bitcoin from this transaction.

In that case the other 5.25 will never enter the supply. In some blocks, some miners have actually done that. It makes absolutely no sense from an economic perspective, just another fun fact.

This is the case of block 501,726 from December 2017. The miner of this block forgot to claim the 12.5 BTC reward and the resulting output is 0.

Want another fun fact? Because Bitcoin mining is solving an algorithmic math problem you could theoretically do it by hand by calculating the SHA256 values. But as you can guess, computers are infinitely faster that humans at mining Bitcoin, and sitting with pen and paper looks hilarious when you break down the numbers.

If you would have started mining manually at the start of the universe 13.8 billion years ago, you would have: $0.000887 worth of bitcoins right now. Now that sounds profitable!

For more insight on Bitcoin Mining you can calculate mining profitability using a calculator and see what it costs to mine a bitcoin depending on electricity cost, mining power, hashrate, and more.

This is a continuation of a 3 part series describing Bitcoin mining in three different difficulty levels. You finished reading **Veteran**. Start again!