Bitcoin Nonce Explained
A Bitcoin nonce is an arbitrary number that is used in Bitcoin’s proof of work consensus algorithm. The nonce can be found as a 4-byte field in a block header, and sees its value adjusted by miners so that the hash of the block will be less than or equal to the current target hash value set by the network.
In the Bitcoin protocol, block headers are serialized in an 80-byte format and then hashed as part of Bitcoin’s proof of work algorithm, which makes the format of these serialized headers part of Bitcoin’s consensus rules. An example format of a block header, with the Bitcoin nonce included, can be found below.
|4||Version||The block version number indicates which set of consensus rules to follow.|
|32||Previous block header hash||Including the previous block header hash ensures no previous block can be changed without also changing this block’s header.|
|32||Merkle root hash||The merkle root is derived from the hashes of all transactions included in this block, which ensures that none of those transactions can be modified without modifying the header.|
|4||Time||The block time is when the miner started hashing the header. This time must be greater than the median time of the previous 11 blocks. Full nodes on the network will not accept blocks with headers which are more than two hours in the future according to their clock.|
|4||nBits||An encoded version of the target threshold. This block’s header hash must be less than or equal to the target hash value set by the network.|
|4||Nonce||An arbitrary number that miners change in order to modify the header hash and produce a hash that is less than or equal to the target hash value set by the network.|
Bitcoin Nonce & Proof of Work
The Bitcoin blockchain is maintained by a distributed network of anonymous peers, and in order to add a block to the blockchain, an individual must undergo the proof of work mining process. This requires that a miner (the individual participating in the mining process) take data from the block header as an input, and then repeatedly run it through a cryptographic hashing algorithm, which for Bitcoin is Secure Hash Algorithm 256 (SHA-256). Miners will hash slight variations of the input data, which for the mining process will be the nonce, until the hash of the header block results in a hash value that is less than or equal to the target hash value set by the network. Finding such a hash value during the mining process is known as a golden nonce.
Due to the fact that adding a block to the blockchain requires a significant amount of work to be invested, it means that malicious actors who want to modify past blocks will have to work harder in order to modify any previous blocks. This is because, in order to modify transactions included in a previous block, a malicious actor would have to modify all subsequent blocks that came before it. Thus, the cost to modify a previous block increases with every new block that is added to the blockchain, increasing the effect of the proof of work process.
The target hash value also represents what is known as network difficulty. This feature can be thought of as being a measure of how difficult it is to find a hash below the target hash. The network difficulty is adjusted every 2,016 blocks, which is based on the length of time it took to find the previous 2,016 blocks. The network does this by using timestamps stored in each block header to calculate the number of seconds elapsed between generation of the first and last of those last 2,016 blocks. Mining 2,016 is estimated to take approximately two weeks, thus, if the previous 2,016 blocks are found in less than two weeks, then the network difficulty is increased. Conversely, if the previous 2,016 blocks are discovered in more than two weeks, then the network difficulty is reduced. New blocks will only be added to the blockchain if their hashed value is at least as challenging as a difficulty value expected by the consensus protocol.