What is Bitcoin mining, and why is it necessary?

Why does Bitcoin have “Miners?”

Gold has miners because people want gold and it just so happens, unfortunately, that most gold is deep in the earth. Bitcoin has miners because people want bitcoins, but something here seems silly: how did a bunch of bitcoins, the tokens of a man made invention, end up locked up in circumstances demanding mining? What’s the point of that?

Not the Best Name

The truth is, bitcoin “mining” is a misnomer. When gold is mined, nothing is achieved beyond the discovery of new gold. When bitcoins are mined, however, a valuable service is provided to the Bitcoin network: decentralized transaction recordation and validation.

Double Spending

Bitcoin relies on miners to record and validate transactions because of a particular problem inherent in any system of digital currency: double spending. Double spending is the high-tech incarnation of counterfeiting. Say, for example, that a currency user, Alice, has a $5 note and she gives it to Bob. Can Bob be sure that he’s received $5 rather than a forgery? In the physical world, probably. In the digital world, probably not.

In the physical world, Alice would have to find paper, ink, and machines capable of making a convincing duplicate of her $5. The cost of that activity, alongside moral scruples and the threat of arrest, keeps counterfeiting in check.

In the digital world, however, a computer file version of a $5 note, like an MP3 file or an MS Word document, can be copied perfectly at effectively no cost. There’s no way to tell which file is the original, and the ease of copying means counterfeit currency could rapidly overrun the economy.

To fix this, the inventors of Bitcoin designed a system of network interactions, a protocol, that checks each putative Bitcoin transfer against a public ledger called the blockchain. A crook can try and resend already spent bitcoins until they’re blue in the face; if those transactions don’t check out, however, miners won’t record them and the community ignores the attempt at fraud.

How Does Mining Work?

Listening for Transactions

Bitcoin miners connect to the Bitcoin network like telephone operators. Miners use their computers to listen for transaction requests across the entire network and assemble a list of valid transactions.

Bitcoins are not sent and received like file attachments in an email. There are no files at all, only assignments of bitcoins made to various public addresses. Each public address has a matching private key and only the holder of that key is capable of digitally signing a new transaction request. Additionally, the request must have inputs. Inputs are the previous transactions that the sender is using to fund the new transaction. If you previously received five bitcoins from Alice and four from Bob, you can list these inputs to fund a new transaction to Cynthia of up to nine bitcoins in value.

Miners check two things when they hear your request. First they check to make sure that your digital signature proves that you were actually the recipient of those inputs. Second, they check to make sure that you’ve not already spent those inputs. To perform this second check, miners peak at a public database of all valid past transactions, called the blockchain, to see if those inputs were already used in a transaction or if they are still available. Copies of this blockchain are stored on the computers of all Bitcoin users that connect to the network.

Thus, miners are playing the role of bank tellers: inspecting checks, making sure all the appropriate signatures and account numbers are there, checking the customer’s ID, and looking for proof that the customer has enough cash-on-hand to fund the transaction.

Completing a “Block”

If everything checks out, the miner will add the transaction to their personal list of all valid transactions over the last few minutes. Every few minutes, one miner will be selected to add their personal list, a block, to the official blockchain, thus keeping the public record up to date.

To prevent miner’s from fraudulently corrupting the blockchain, the Bitcoin protocol makes miners compete. A different miner is empowered to write each block, roughly every 10 minutes, and only valid blocks will be accepted by the rest of the mining community. Here’s how that works:

Guess and Check the “Nonce”

A miner’s block will become a part of the chain whenever a majority of the community of miners agree (A) that the transactions listed by the miner are valid—no signatures from impersonators and no double spending—and (B) that the miner correctly guessed a special number, the nonce, that solves a particular math problem. Miners perform this check by looking at the proposed block’s particular digital signature. This signature is a computer generated product of three inputs, (1) the signature of the predecessor block, (2) a list of valid transactions since that predecessor, and (3) a particular random number, called a nonce.

To understand it all, we need a bit more information about digital signatures. Signatures operate by using “hash” functions. At their simplest, hash functions are math equations that take any given input and create a seemingly random output that will always correspond to that particular input.

The hash function used by Bitcoin is called SHA256. Using that function, the input text:

“This is a hash!”

will always output this string of characters:

“dcc67309a9c5c4a6d5434de87dbd4162f745f32b2a6aedf89c89d31d863b022b”

You can try it yourself by visiting an online hash calculator; if you type in “This is a hash!” without the quotes you’ll get the same specific string of numbers and letters.

If a hash function is well written, any change to the inputs will drastically change the output string, and different inputs would never output the same string. By that standard, SHA256 is very well written. For example, changing our input “This is a hash!” even slightly results in entirely different outputs:

“This is a hash!” = “dcc67309a9c5c4a6d5434de87dbd4162f745f32b2a6aedf89c89d31d863b022b” “This is a hash?” = “d43edbde4b15a97e780c1a9e1392b2c4601750fe03db543b3c4c44624d277641” “This is a hash brown.” = “5692e888b50c526f7eb95342a6fd56760b2ff95a766414562daa4083bab8bcfc”

Therefore, if the inputs for a new block’s signature are the signature of the predecessor block and a list of recent transactions, the output will be a unique string that could only have been made from that exact data. Because it is the unique product of those inputs, that signature can be used to prove that the transactions therein described happened in a given order: within the current block or some previous block. Try and change the order by making up phony past blocks and the signature will no longer match. This allows the particular beneficiary of a transfer to prove that they were the first to receive the coins; any subsequent double spending of those coins is fraud.

All miners, however, are capable of writing a signature composed of the previous block’s signature and the new transaction list very quickly using their powerful computers. How do we pick a winner at regular intervals to make them compete? The solution is to ask for a string that will be difficult to generate quickly, a specific sort of output string, one that starts with a certain number of zeros, like this:

“0000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

That long line of zeros at the start of the hash is statistically improbable, like flipping a coin and getting heads thirteen times in a row. Nonetheless, there is a particular combination of inputs that will result in a hash output that starts with all those zeros. The combination will involve a particular random number called the “nonce” that miners will have to guess.

The miners repeatedly hash their two known inputs (the previous block signature and the list of new transactions), along with guesses at the random nonce. Eventually, one miner will happen upon a nonce that will give them a signature with the requested number of zeros at the start.

Miners that use more powerful computers can make guesses faster, and, like buying more lottery tickets, these miners will be more likely to win the race to find a particular hash. This is why miners can compete with each other by investing in more powerful computers. More tries at the hash equals more blocks written to the blockchain over time. To prevent blocks from being written too quickly or too slowly as more or less computing power is used by miners, the protocol is adjusted every two weeks to demand a longer, harder to guess, or shorter, easier to guess, string of zeros at the front of the hash. The target for those adjustments is generation of a new block every ten minutes.

Whenever a miner solves a block by writing a signature with enough zeros, they broadcast it and the other miners validate the solution and check to make sure that the transactions listed are all valid. If it all checks out, miners will begin competing to solve a new block using the last block’s signature as an input.

Rewards

That brings us, at last, to the question of why miners mine. This answer is actually simple, miners mine because the writer of a new block in the blockchain has permission from the protocol to give herself a reward of brand new bitcoins, called a coinbase transaction. That reward started at 50 bitcoins per block. Every four years the protocol is adjusted, reducing the reward by half. One day the reward will be very small, but miners can also be rewarded by collecting fees volunteered by users that request transactions.

Summary

We’ve discussed why mining is necessary: to stop double spending by creating a ledger of all transactions, the blockchain. We’ve also learned, in simplified terms, how mining actually works. In future, we’ll discuss what happens when miners collaborate to mine blocks, forming mining pools, and discuss the cryptography involved more thoroughly.

Peter Van Valkenburgh is Director of Research at Coin Center.