A nonce (short for “number used once”) is a unique, one-time-use, random, or pseudo-random number that ensures the integrity and uniqueness of operations within protocols and cryptographic processes. Nonces are vital to blockchain security and efficiency, supporting transaction validation, mining, and replay attack prevention. Originally borrowed from the cryptography world, as we’ll see, the term has taken a broader meaning.
What is the purpose of a nonce?
A nonce serves various purposes in cryptographic systems and blockchain networks. In Ethereum, it is a semi-sequential counter that ensures transactions are processed in order. In Bitcoin, the nonce refers to the random number miners adjust to try to find a hash that meets the difficulty target of the network’s consensus mechanism. Nonces also protect against replay attacks by guaranteeing transaction uniqueness, as any attempt to reuse a previous transaction will be rejected.
How nonces work across blockchain networks
Nonces are crucial to maintaining the security and functionality of blockchain ecosystems. Here's how they are used in different blockchain protocols:
Ethereum
Functions as a sequential counter that tracks the number of transactions sent from an externally owned account (EOA) or contracts created by a contract account.
This strict mechanism ensures that each account can only execute one transaction with a specific nonce, preventing transaction replay and maintaining a precise order of operations.
ZKsync
The NonceHolder system contract manages nonces for all accounts, simplifying transaction ordering for operators by tracking each account’s current nonce.
It ensures that the address-nonce combination is always unique, preventing duplicate transactions.
The bootloader (a special program that processes transactions in batches) ensures a nonce is unused before a transaction’s validation step.
Deployment nonces increment only after successful deployment, either sequentially (increasing by one from the previous one) or arbitrarily (assigning a random value).
Bitcoin
In Bitcoin’s Proof of Work (PoW) system, miners engage in a competitive computational process, repeatedly generating different nonces (random numbers) to calculate block hashes. They attempt to create a hash that meets specific cryptographic criteria, such as starting with a predetermined number of zero digits.
The mining competition is a race to find the first valid nonce that produces a block hash matching the network's requirements. The successful miner earns the privilege of adding the next block to the blockchain.
When a miner finds a hash that meets the network's difficulty standards, they are rewarded with newly minted bitcoins and transaction fees. This incentivizes miners to continue securing the network and validating transactions.
Solana
A recent block hash is retrieved from a validator cluster, limiting the validity of transactions to 150 blocks (approximately 90 seconds). This time-restricted approach mitigates replay attacks by ensuring that transaction signatures cannot be reused after their validity expires.
Solana introduces a mechanism where a specialized nonce account stores a nonce value. Then, exclusive control is granted to a nonce authority, which can either generate a new nonce or advance the stored value as needed. This allows for more sophisticated transaction management.
To extend transaction validity beyond the standard 150-block limit, Solana implements a durable nonce feature. The nonce account uses the advanceNonce instruction to create a durable transaction. The nonce authority must sign the durable transaction, creating a secure method for managing long-lived transaction signatures.
Related Terms
No items found.
Cyfrin Updraft
Learn smart contract development, how to write secure smart contracts, and scalable protocols from world's leading experts.