Skip to content
All apps

The Nonce Lottery

About this app

Proof of work with real SHA-256 in the browser: starting at nonce 0, a block of transactions searches for the number that makes its hash start with 1 to 5 zeros. The hash field flickers; counters set the tries against the expected 16^k, and the browser time against a laptop doing one million hashes/s. A chart shows that one more zero costs sixteen times as many tries and that each single case is a lottery. Verifying costs one hash. In chain mode, tampering with block 1 breaks the arrow to block 2.

Finding is costly, checking is cheap

In proof of work you look for a number, the nonce, such that the SHA-256 hash of a block plus nonce meets a condition, here: it starts with k zeros in hexadecimal. Since the hash is unpredictable, only trial and error helps. Each hex digit is zero with probability 1/16, so k zeros take 16ᵏ attempts on average. One more zero costs sixteen times as much work. Checking, in contrast, takes a single hash. Bitcoin uses a similar principle but compares the double SHA-256 hash of the block header with a target number.

What the app shows

A block with transactions searches from nonce 0 for the number that makes its hash start with 1 to 5 zeros, computed with real SHA-256 in the browser. The hash field shows the latest attempts, and counters compare the number of attempts with the expected 16ᵏ, and the browser’s time with a laptop doing one million hashes per second. A chart shows attempts per number of zeros. In chain mode a second block depends on the hash of the first. A code panel shows the matching Python program.

Try it yourself

Search several times with the same number of zeros and compare the attempt counts: each case is a lottery, on average 16ᵏ holds. Raise k by one and see the jump. Then verify a found block with a single hash. In chain mode change a transaction in block 1 from 40 to 400: its hash becomes invalid, the predecessor in block 2 no longer matches, and both blocks must be searched again.

Frequently asked questions

What is proof of work?
Proof of work is a way to show that computational work has been done. By trial and error one finds a nonce that makes a block’s hash meet a difficulty condition. Finding it is expensive, checking costs just one hash.
What is a nonce in a blockchain?
The nonce is a number in the block that is changed during mining until the block’s hash meets the difficulty condition. It carries no meaning of its own. Its only purpose is to allow different hash values to be tried.
Why does proof of work make a blockchain tamper-resistant?
Each block contains its predecessor’s hash. Changing an old block invalidates its proof of work and breaks the link to the next block. All following blocks would have to be recomputed faster than the rest of the network adds new ones. That is practically infeasible.
Does the app use real SHA-256?
Yes, the nonce search uses real SHA-256 in the browser. Difficulty is simplified to a number of leading hex zeros; Bitcoin compares with a target number instead.

Subject: Cryptography

More from Cryptography

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps