The fact that you can generate bitcoins yourself using a computer is often one of the first things that people hear about Bitcoin, at least in the early days when it was realistically possible to do so on a home computer. Bitcoin mining serves two very important purposes. It gets the initial coins into circulation and it records all the transactions that take place. It does both these things without requiring a central authority to oversee the process.
When someone sends bitcoins from one address to another, they are publishing a transaction publicly over the internet that has been signed by their private key. Other users of the network can verify that the transaction is valid by checking the sending address actually has the coins available to send and that the transaction signature is correct. The transaction then needs to be included in a ledger somewhere, so that everybody knows that the receiving address has the coins instead of the sender. Bitcoin uses a public ledger called the blockchain.
Someone needs to decide which transactions should be included in the blockchain. A transaction is not considered completed until it is included in the blockchain, otherwise someone could attempt to spend the same coins twice and others would not know which one to accept as genuine. Bitcoin “mining” is the method used to decide who gets to decide which transactions are included.
Bitcoin mining involves using a computer to solve a mathematical puzzle that can only realistically be solved by trial and error. Of course, once somebody has found the solution, it is easy to verify that they are correct. Every time someone solves the puzzle they get to create a “block” within the blockchain and include transactions in it. There is an arbitrary limit on the size, in terms of data, that can be included in a block (1 MB), which limits the actual number of transactions that can be included. Once a miner has published the block with the transactions included, every one starts working on the next puzzle.
The puzzle the miners are trying to solve was agreed way back before the first ever block was mined, they are basically taking a set of inputs, adding their guess as to the solution and calculating the SHA256 value. The aim is to come up with an answer with a certain number of leading zeros. The number of leading zeros is known as the difficulty, this is adjusted every two weeks to try and even out the blocks found to one every ten minutes. One of the inputs to the puzzle is the hash of the previous block, this ensures that one block always builds on the previous and that miners cannot start working on solving a later block until the next one is found.
But why would miners be interested in finding a block? They need an incentive. They can refuse to include transactions in the blocks they find unless the sender includes a fee. The block size limit means there will come a time when people have to increase the fees in order to have their transaction included. In the meantime the network takes the opportunity to put new bitcoins into circulation by rewarding the miner with new bitcoins, this is known as the “block reward”.
The block reward was set at 50 bitcoins per block to start, it then halves every 210,000 blocks, approximately every four years, meaning that as time goes by the miners will become more reliant on transaction fees. The BitcoinClock.com website is a good place to keep track of when the next block halving is due. It is estimated that the last bitcoins will be mined around the year 2140. There will never be more than 21 million bitcoins mined.