What is a Nonce?
A nonce is a unique number that is used in cryptographic hash puzzles. The term “nonce” is derived from the phrase “number used only once.”
The Long Definition
Nonce, or “number used only once,” is a crucial piece of cryptographic hash functions. In blockchain and crypto, it is a unique number that is needed to solve a required hash during mining. Miners are tasked with discovering this number to open a new block.
The first miner to correctly guess the nonce gets the right to write the next block to the blockchain and is rewarded for it with block rewards. \
Understanding the Nonce
There are blockchains that use the Proof of Work (PoW) consensus mechanism. PoW uses cryptography to validate blocks before they are added to the protocol. This involves hashing.
During hashing, data is run through an algorithm. The algorithm converts the data into a unique string of text (known as a hash) based on its contents. Therefore, two exact copies of data will always return the same hash values. Any changes to the data, no matter how small, will result in different hash values.
This is how blockchains determine that a particular block hasn’t been altered. The protocol runs the following information through a hashing algorithm and generates a target hash;
- The block number: The position of the block in the blockchain. For example, block #13.
- The block’s data: The information stored in the block. This includes transaction data.
- The previous block’s hash: The hash value from the previous block. In this case, it will be the hash value from block #12.
On the other hand, miners compete to generate the target hash. This involves running the same data through the mining program. The first miner to get the target hash wins the right to append the new block to the blockchain.
Given that all this information is easily available, mining crypto should be the easiest of things. But it isn’t. Miners have to do a lot of work (hence the name Proof of Work) to validate blocks. So, where does this ‘work’ come from?
Where the Nonce Comes in
This is where the nonce comes in. In addition to the block number, data, and the previous block’s hash, a “number used only once” passes through the hashing algorithm to generate the target hash.
But this number isn’t readily apparent to the miner. Instead, miners have to guess it, or in this case, work for it. Think of it as the missing part of the puzzle that participants have to look for. Only the correct nonce will return the required hash value.
How Does it All Work?
Basically, the protocol uses the nonce, block number, block data, and the previous block’s hash value to generate a target hash.
On the other hand, miners compete to generate a hash value similar to the target value. They have all the data except the nonce, which they have to guess. The first miner to get it writes the next block to the blockchain and receives the block reward.
But the nonce is a number used only once for any particular block. Therefore, every time a miner guesses the wrong number, the nonce changes. In some blockchains, it increases by one. In others, a new nonce is generated randomly. This will happen with each attempt until one miner gets it right.
Think of it as a puzzle that changes every time someone gets it wrong so that the required solution is different after every failed attempt. To solve this puzzle, miners typically have to test a large number of nonce options.
Over time, this process has gotten quite intensive, requiring the use of specialized equipment. These are ASIC miners. They can make billions of guesses per second.
How Long Does It Take to Get the Nonce?
How long it takes to get the right nonce depends on the protocol’s desired block time. This is the time it takes to mine a new block.
For example, Bitcoin’s block time is 10 minutes. So, every 10 minutes the target nonce is discovered, and a new block is mined and added to the blockchain.