By Noah Newfield
You have 3 Bitcoins, and decide to send one to your friend Doug. This exchange is called a transaction. The purpose of this article is to explain everything that happens between when you hit the send button and your friend receives his coins.
We will break down the transaction into three parts: Hashing, Merkle Trees, and Proof of Work. Each element helps create a system of transactions that is both elegant and ingenious.
- Hashing — encrypting the transactions being sent
- Merkle Trees — how the encrypted transaction are organized to be more secure
- Proof of Work — how bitcoin miners process the organized transactions.
Lets begin with hashing.
After you press send on your transaction to Doug, the network enters your transaction into something called the hashing function, official name SHA-256.
This function has important properties that we’ll go over, but conceptually its only slightly different from the function x + 1.
Let’s use x + 1 as an example. Your transaction is the input. Let’s call it 1.
It enters our makeshift hashing function (x+1) and we get an output of 2.
The Hashing function works the exact same way (with an input and output) but with a far more complicated function that produces very specific numbers.
Each number entered into the Hashing function will return a 256 bit number, 67+ digits long. This number is mathematically proven to be deterministic, unique, and irreversible.
Let’s break that down.
- Deterministic — The functions input determines its output. Entering the same input will alway yield the same output
- Unique — Every input to the function will produce a unique output. No two different inputs yield the same output
- Irreversible — You are unable to reverse engineer the function. You are unable to use the output to find the input
Once you press send, your transaction goes into the hash function, and the output, called your hash, which has the properties listed above, is sent to the network for processing.
Let’s follow it.
Right now it is in a chronologically ordered pile of other transactions waiting to be processed.
A mining computer assembles them into a 1 megabyte block, which as of Sep 2018 holds around 1600 transactions (but changes depending on a multitude of factors).
For security, those transactions are assembled into a Merkle Tree.
Let’s assume that our transaction is only being processed with three others. A total of four transactions in the block.
We’ll call our transaction TxA, like in the diagram below. Keep in mind TxA is actually our hash, a unique number that represents our transaction.
In the diagram we see that the hashes for TxA and TxB are added together and that number is put back into the hashing algorithm.
The output is TxAB. A single hash that is unique representation of both transactions.
This is repeated up the tree until we come to the end of the tree, the Merkle Root or Root Hash.
The Root Hash of our four transaction block would be TxABCD.
A 67+ digit number that uniquely represents every transaction in the block, whether it be 4 or 1000.
How does this protect the Bitcoin Network?
If you were attacking the network and your goal was to to add a fake transaction to the block or change an existing one, you wouldn’t be able to.
Since each branch of the tree is unique, a change to any of them will affect the Root Hash they lead to. If the Root Hash is changed in any way the block will be not be accepted to the network and no harm can be done.
Next, the transactions will actually be confirmed.
The process is called PoW, proof of work.
It is at the core of Bitcoin philosophy and technology.
So far, you’ve sent your transaction, it has gone through the Hash function to encrypt it, the miners have found the unique Root Hash of your transaction and 1000+ others, and they have assembled those transactions into a block.
Each assembled block has a heading, called a block header.
Each heading holds the following information about the block: Bitcoin software version, a time stamp, the Root Hash of the block’s transactions, the Root Hash of the previous block (to keep them in order), and two variables called a target and nonce.
Targets and nonces, are the two numbers in the problem that miners must solve in order to validate the block and confirm its transactions.
Solving this problem proves that the amount of computational work i.e. Proof of Work, the miner has done and allows the network to accept the block.
The problem looks like this:
nonce + Root Hash -> SHA-256 Hashing Function -> output < target
Input -> Function -> Output
The network decides a target for the miners. The miners are tasked with finding a nonce, that when added to the Root Hash and put into the Hashing Function, is less than the given target.
The nonce starts at 0, but could be one of billions of numbers.
The complex rooms of computers in Bitcoin mines spend the vast majority of their energy adding 1 to the nonce and trying over and over again to solve the problem.
There is competition among miners making it a race to see who can solve the problem first and receive the 12.5 Bitcoin reward for successfully mining the block.
In order to keep mining competitive, as more miners compete for the same blocks the target is lowered to make finding a working nonce more difficult. Controlling the target ensures that each miner is doing a certain degree of computational work to validate a block. That is how the Bitcoin blockchain mechanism Proof of Work operates.