A nonce is a number that can only be used once in cryptography. In Bitcoin’s mining process, miners aim to find a hash below a target number which is often difficult to calculate. The Proof-of-work process in Bitcoin’s mining involves taking an input which contains Merkle Root, timestamp, previous block hash and a nonce in order to produce an output.
If a miner generates an output from a given input that results in a smaller hash than the target hash, such miner wins the block and the consensus is reached. Such a miner needs to brute force all possible nonces in order to luckily find a hash smaller than the target hash.
The “nonce” in a bitcoin block is a 32-bit (4-byte) field whose value is set so that the hash of the block will include a run of leading zeros. The rest of the fields may not be changed, because they have a defined meaning.
Any change made to the block data such as the nonce will completely change the block hash. Because it is impossible to predict which combination of bits will result in the right hash, many different nonce values are tried. Then the hash is recomputed until a hash containing the required number of zero bits is found. The number of zero bits required is set by the difficulty. It must have a certain number of leading zero bits to be less than the value.
A nonce is the number that Bitcoin miners are solving for. When the solution is found, Bitcoin miners are rewarded with Bitcoin.
How to Find the Nonce
Defining which string to use as the nonce requires a lot of trial-and-error, as it is a random string. A miner must guess a nonce, add it to the hash of the current header, rehash the value, and compare it to the target hash. If the resulting hash value meets the conditions, the miner has solved the difficulty and is awarded the block rewards.
It is quite impossible for a miner to successfully guess the nonce on the first attempt. Hence, the miner may likely test a lot of nonce options before getting it right. The greater the difficulty—a measure of how hard it is to create a hash that is less than the target—the longer it is likely to arrive at the solution for the difficulty.
How Are Nonces Used?
Nonce is primarily used in the authentication process. During an authentication process, nonce is used to ensure that old communications are not reused. For instance, when a supplier gets the repetition of the same order under one name and with the same purchase information, a nonce is used to to give originality to the message such that if the supplier receives any other order from the same person with the same nonce, the supplier could discard the order