A nonce is a number that is only used once. Hence its name is a portmanteau of number-used-once. Why is this kind of concept useful in cryptocurrencies’ mining? Because nonces allow you to vary something in a predictable way and prove that you actually achieved a certain result in a proof of work scheme.
Bitcoin blocks are chunks of data that are built up in a specific way, according to the Bitcoin protocol, and they can never be changed. That is the whole concept of a blockchain, each block is immutable forever, it remains there frozen just as it was stored the first time.
So, if a Bitcoin block is immutable, then what is it that miners vary with each attempt at solving a block? You guessed it: the nonce.
Essentially, mining goes something like this:
- Receive a block to work on and an initial nonce either from the network in solo mining or from a mining pool.
- Take the nonce, concatenate it with the block, and encrypt it.
- If the encrypted value begins with a certain number of zeroes, you’ve solved the block!
- If the encrypted value does not begin with a certain number of zeroes then INCREASE the nonce and GOTO number 2.
The above gets repeated until the Bitcoin network sends you a message that the block has been solved by someone and then you get a new block to work on. There are some simplifications on the above routine, for example, the block is not sent whole, the mining pools only send you a short summary of a block and a nonce, so you don’t nave to encrypt the whole block all the time over and over. By proving that you hashed this summary and the nonce, you prove that you attempted to solve the block for the pool. More than one protocol exists for this and each one works in a different way, technically, but they all have the same goal and produce the same results in the end.
All these attempts run on your personal computer, network of graphic cards or, in general, in your mining rig. So how does the world get to know if you solved the block or not? You send the nonce back to the mining pool or to the network and everyone verifies it. If it does indeed solve the block, you earn the block reward.
So the nonce is actually what gets verified! Since everyone (except for stale nodes that fell behind) is working on the same chunk of data for a block, the only thing that varies is the nonce. Therefore the solution to a mined block is the nonce!
So, in summary, while mining, everyone is looking for a nonce. It’s an integer number that can be any size. In fact there are infinite solutions to a Bitcoin block! Any number that produces a hash that has a certain number of zeroes in front of it is a winner! The race is then to find the first nonce that solves the block, not every nonce that solves it. That’s why miners who can attempt more nonces per second usually win the mining contest.
We hope this brief intro to the concept of nonces has helped you understand proof of work based cryptocurrency mining a little better.
Number Theory Illustration: Wikipedia