Nonce

The mining field

Diagram showing the location of the nonce field inside the block header.

The nonce is a spare field at the end of the block header used for mining.

Nonce is an abbreviation of number used once.
cryptography.fandom.com/wiki/Cryptographic_nonce

I like to call it the "mining field".

Anyway, the easiest way to understand its purpose is to adjust the nonce in a block header to see how it affects the block hash:

tool-6623fb197f3fe
Tool Icon

Block Header

Decode and encode a block header.

0 bytes
Block Header (Fields)
Version
0d
0d

This is the HASH256 of the hex block header. It's also in reverse byte order, because that's how block hashes are displayed in block explorers.

0 secs

Usage

What is the nonce field used for?

The nonce is a 4-byte field that can hold numbers between 0 and 4294967295 (0x0 to 0xffffffff in hex).

Miners increment the nonce value when mining so that they can get completely different hash results for the block header of their candidate block. They hope to stumble upon a "magic" nonce value that will produce a block hash that is below the current target.

So there's no skill in trying to find a nonce that works. It's just a spare field that allows miners to quickly re-hash their block header without having to reconstruct the entire block.

Mining simulator showing multiple hash attempts by incrementing the nonce.
This is a slowed-down simulation of what the mining process looks like under the hood.
This SHA-256 video shows the mining process in action.

Notes

Some extra details about how the nonce works:

Limitations

Does every block have a "magic" nonce value?

Miners will usually exhaust the 4-byte nonce field in the block header without finding a "magic" nonce value that produces a block hash below the current target.

So no, most blocks will not have a "magic" nonce value.

When a miner exhausts the nonce field the obvious next step is to adjust the time field. This produces a slightly different block header, which allows the miner to increment through the nonce field again in an attempt to mine the same block of transactions.

However, miners are so fast that they'll exhaust the nonce field in less than 1 second, so the time field isn't much help in this situation. Therefore, miners look for other ways to modify the block header without having to reconstruct the entire block of transactions (which would take more time)…

Having such a small 4-byte nonce field in the block header was possibly a design mistake by Satoshi.

ExtraNonce

The ExtraNonce is located inside the scriptSig of a coinbase transaction.

If a miner exhausts the nonce field, they will move on to adjusting what's referred to as an "ExtraNonce".

The ExtraNonce is located inside scriptSig of the coinbase transaction. Miners are free to put any data they like in the scriptSig, which means they can modify it to serve as an unofficial field for an additional nonce value.

This works because:

  1. Changing the contents of the scriptSig changes the transaction data.
  2. Changing the transaction data for the coinbase transaction changes its TXID.
  3. Changing the TXID for the coinbase transaction changes the merkle root.
  4. Changing the merkle root changes the block header.

So a change to the scriptSig in the coinbase transaction is a quick and easy way to modify the block header via the merkle root.

There is no "official" position for an ExtraNonce inside the scriptsig, so it's not always easy to identify which part the miner used for the ExtraNonce. However, miners typically increment some data toward the start of the scriptsig field.

Terminology

Why is it called a nonce?

Good question.

It's a term used in cryptography for "number used once", so it basically refers to any time you need to use a one-off random number for some cryptographic purposes.

Unless you're British of course, in which case it doesn't mean that at all.

Gifs

Here's a terrible gif I made in 2016 that shows how the nonce is used to change the block hash:

Animated gif of the nonce being used to change the block hash.

And here's another one showing a rough visualization of the mining process:

Animated gif of the nonce being used during the mining process.

If those gifs don't explain everything, I don't know what will.

Resources