When learning about bitcoin and the mining process, you are bound to come across various new terms, one of which is nonce. To understand what a nonce is, we need to first learn what a hash is.
Hash is an alphanumeric code used to represent a word, message, or data. In a proof-of-work, or bitcoin mining process, a valid hash must meet certain criteria. Each hash must begin with a series of zeros, or the hash has a low value, according to the proof-of-work criteria. As a result, proof-of-work is difficult to accomplish.
Let’s take the example of the word ‘pintu’. If it hashed with SHA-256 hash algorithm, the result will always be: 24d52030496d5935c5929564269fae1194db553c0ca3e2ede7a4ecc836f134d3
So how do you get the word ‘pintu’ to have a hash starting with the number 0?
To get a hash with a small number, the miner must enter data known as nonce. Then, what is nonce, how does nonce work and the use of nonce in cryptocurrency? Learn more about it in this article.
What is Nonce?
Nonce is the only data that a miner can add. A nonce is an additional number entered by the miner to change the hash output so that it has a low value or equal to the mining target. Miners can only guess the nonce numbers to produce a hash target that starts with the number 0. So, you can say nonce is a brute force way to achieve mining targets.
How does nonce work?
Let’s take an example of how nonce works by looking at the following block simulation.
As a requirement for a valid proof-of-work block, the resulting hash must have a small number or start with a few digits of 0 according to the difficulty target. For example, let’s say the difficulty target is the block must have 4-digit number 0 in the beginning.
- Block #1 has data that includes bitcoin transactions from A to B as much as 5 BTC. The hash of this data is bc97ee..5052. based on the hexadecimal format. Because in the hexadecimal letter b is equal to 11 and c is equal to o12, then the hash can be interpreted as 111297…5052 in a decimal number. In essence, this hash is started with number 1.
- In order for the hash to start with the number 0 in the beginning, the miner must enter a data that will change the hash value. In this case is a nonce number, which is a random number so that the hash can have a different value. After the miner enters the nonce, he has to run the SHA-256 hashing algorithm in order to generate a new hash.
With Nonce number 1 (Figure B), the resulting hash with the same data will be 306b…ba6a. This hash is still considered invalid because it does not meet the difficulty level (starting with 0000) . The miner will try to change the nonce numbers one by one from 1,2,3… , and so on (Figure C), running the SHA 256 hashing algorithm until it gets a small valid hash that matches the mining difficulty level.
After various attempts, a nonce with the number 16,663 produces a hash with a small value that starts with a 4-digit zero number in front of it.
This process of using hashing algorithms and adding nonces is what we call mining. Miners compete with each other to guess the nonce and perform hashing according to the target. The fastest miner who can create a valid block (i.e. find a nonce that makes a block hash smaller than the difficulty target) will get a block reward or reward in the form of Bitcoin.
What is the purpose of mining difficulty?
Bitcoins are mined every 10 minutes on average and this should be constant in the long run. With the increasing computer power, it is certain that miners will find it easier to find hashes. To ensure that Bitcoin is mined constantly every 10 minutes, the mining difficulty must be adjusted as well, so that if the number of computer hardware and the miners increase, it will not affect the time to find new blocks. The higher the mining difficulty, the smaller the value of the valid hash (or the more zeros in front of the hash).
Mining difficulty is adjusted every 2016 block, or approximately every 2 weeks. If the miner finds the hash faster than 10 minutes then the difficulty level will be added, but if the miner finds the hash slower than 10 minutes then the difficulty level will be reduced.
For example if we have to find a hash that is less than or equal to 0xxx then we have 65,536 possible hashes. But if we have to find a hash less than or equal to 000xx, then we only have 256 possible hashes. So the less the target difficulty, the less likely the miner can find the appropriate hash. The more leading zeros (the smaller) the more difficult it will be to find a suitable hash.
With the current difficulty target, miners have to try to guess the nonce and run the hashing algorithm quadrillion times to find a hash that matches the difficulty target. If the target difficulty increases then the effort required to find the appropriate hash will be twice as difficult. This makes the Bitcoin network even more secure.
In addition to guessing the nonce and running hashing algorithms, miners must also validate blocks. In the next explanation, we will see how the relationship between hashing in a blockchain.
Conclusion
- Nonce is an additional number entered by the miner during the bitcoin mining process, to change the hash output, so that it has the same value as the mining target.
- Mining difficulty is the level of difficulty in mining bitcoin, which serves to keep new bitcoins being mined every 10 minutes, to maintain the number of bitcoins in circulation so that the price continues to increase.
- No matter how many miners participate, they will not be able to mine bitcoins any faster. The difficulty level of mining will increase as more miners engage, while the difficulty level of mining will drop as fewer miners participate.
- What is the relationship between nonce and mining difficulty? With the target difficulty increasing, miners must try to guess the nonce and run the hashing algorithm quadrillion times to find a hash that matches the difficulty target. If the target difficulty increases, then the effort required to find the appropriate hash will be twice as difficult. This makes the Bitcoin network even more secure.