In the previous articles, we discussed :
So, we know the overview of what does it mean by mining of bitcoins. Let's look into a bit more details in this article on how mining of bitcoin is done.
In What Is Mining of Bitcoins? We came to know that, bitcoin miners have to perform a cryptographic hash taking into account the current block of transactions during a certain period of time, the previous block in the chain and something called 'nonce' and find a hashed value less than the difficulty target.
But what is this 'nonce' actually? And how does it work? Let's understand.
Bitcoin uses the hashcash function as the mining core. This hashcash algorithm requires mainly three parameters:
A service string
A nonce and
What is 'service string' in mining of Bitcoin ?
In bitcoin, a service string is encoded in the block header data structure. It includes the following fields:
A version field
The hash of the previous block in the block chain
The root hash of the merkle tree of all the transactions in the current block
The current time
What is 'nonce' in mining of Bitcoin ?
The 'nonce' in a bitcoin block is a 32 bit field, whose value is set so that, the hash of the block will contain a run of zeroes. As we have seen, service string for a block cannot be changed, so by changing this field we can get a different hash value of the block. The property of a good hash function is, if we change the key even if little bit, the resultant value will be completely different. So, if for a given nonce, we do not get the resultant hash to be less than the difficulty target, we change the nonce and hash again. And thus repeat the process.
Why do we need a counter while mining Bitcoin ?
The counter is a 32 bit parameter. Each time we try with a different nonce field, we increment the value of this counter, so that we do not repeat the work.
What is 'hashcash function' ?
Hashcash function is the core of bitcoin mining. Like any other cryptographic hash functions, it uses a hash function as a building block. Hashcash function can be instantiated with different hash functions like hashcash-SHA1, hashcash-SHA256 etc.
The hashcash algorithm is relatively easier to understand.
Let's assume for a pre-image x, we get a hash value y.
So, y = H(x)
The task is to find another pre-image x' so that,
y = H(x')
So, the overall idea is, you have computed a fixed value of service string, now use hashcash algorithm to get a nonce value so that, the resultant hash is less than the difficulty target.