Search

Search papers and lessons

Proof of Work

Level 3 · 10 min read

Proof of work accepts a message only with a number whose hash falls below a target, so producing a valid answer costs real computing effort while checking it costs one hash.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Security & cryptography
  • Algorithms
  • ReadReady to readNot yet

TL;DR

Proof of work is a rule that a message counts only if it carries a number, called a nonce, that makes the hash of the message fall below a target value. Finding such a number takes many guesses, so it costs real computing time. Anyone can check the answer with a single hash. The idea makes spam and fake identities expensive, and Bitcoin uses it to decide who may add the next block to its ledger.

Builds on: Cryptographic Hash Function, Geometric Distribution, Expected Value · Leads to: Blockchain, Distributed Consensus

Where it's used

Bitcoin block production. Each new block of the Bitcoin ledger must come with a nonce that makes the block's hash fall below a target. The network adjusts that target over time so that a new block appears, on average, about every ten minutes, however much computing power joins or leaves. The effort, not a vote or a stake, is what gives a miner the right to propose the next block.

Stamping email. Hashcash, a scheme built for email, attaches a small proof-of-work stamp to each message. One stamp costs a sender almost nothing. Sending a million messages, each with its own stamp, costs a spammer a great deal of computing time, which is the point.

Slowing bots. Some websites ask a visitor's browser to solve a small puzzle before accepting a form. A person notices a fraction of a second. A bot that submits thousands of forms per minute notices the cost in full.

How it works

Use a toy hash whose outputs are the 8 values 0 to 7, which is a 3-bit hash. The puzzle is: find a nonce whose digest is 0. The block data is the string "alice pays bob 5".

  1. Try nonce 0. Compute the digest of the data together with nonce 0. Suppose it is 5. The target is 0, so 5 is too large and the block is rejected. Check: 5 is not 0.

  2. Try nonce 1. Suppose the digest is 3. Still too large, so reject. Check: the miner changed only the nonce, so the data is the same and the digest changed.

  3. Try nonce 2. Suppose the digest is 6. Reject again. Check: three tries so far, none valid.

  4. Try nonce 3. Suppose the digest is 0. It is below the target, so the block is accepted. Check: the miner needed four hashes to get here.

  5. Verify. Anyone who receives the block hashes the data with nonce 3, gets 0, and accepts. Check: the verifier spent one hash; the miner spent four.

  6. Why guessing is random. Each digest looks unrelated to the others, so each try has the same chance, 1 in 8, of being 0. The number of tries until the first success is therefore a geometric count, with an average of 8.

flowchart TD
  A[Pick data and nonce] --> B[Hash data with nonce]
  B --> C{Digest below target?}
  C -- no --> D[Increase nonce]
  D --> B
  C -- yes --> E[Valid block: broadcast it]
  E --> F[Anyone checks with one hash]

Making the target smaller makes each try less likely to succeed, so the average number of tries goes up. That single knob is how the network controls the rate at which blocks appear.

The math

Definition: Proof of work

Let HH be a cryptographic hash function whose digests have nn bits, read as integers in the range 00 to N−1N - 1, where N=2nN = 2^{n}. Given block data BB and a target TT with 1≤T≤N1 \le T \le N, a nonce xx is valid when H(B,x)<TH(B, x) \lt T. Finding a valid xx is the proof of work. Checking xx takes one evaluation of HH.

Step 2 in symbols: the chance one try succeeds

If digests behave like independent, independent uniform values over the NN possible outputs, then each try succeeds with probability

p=TN.p = \frac{T}{N}.

In the toy, N=8N = 8 and the target is T=1T = 1, which accepts only the digest 0. So p=1/8p = 1/8, matching step 4.

Step 5 in symbols: how many tries

Let KK be the random number of tries up to and including the first valid nonce. The tries are independent with success probability pp, so KK is geometric:

P(K=k)=(1−p)k−1p,k=1,2,3,…P(K = k) = (1 - p)^{k-1} p, \qquad k = 1, 2, 3, \ldots

Its mean is E[K]=1/p\mathbb{E}[K] = 1/p. In the toy, E[K]=8\mathbb{E}[K] = 8. The miner's expected work is 1/p1/p hashes, and the verifier's work is one hash.

Step 6 in symbols: time

Suppose the whole network computes rr hashes per second, and rr is the same for every try. The expected time to the next valid block is

E[time]=1p r.\mathbb{E}[\text{time}] = \frac{1}{p\,r}.

When pp is tiny, (1−p)k≈e−pk(1-p)^k \approx e^{-pk}, so the chance that no valid nonce appears in tt seconds is approximately e−rpte^{-rpt}. This is the exponential approximation: the time to the next block behaves roughly like an exponential waiting time.

Worked examples

Example
1. The 3-bit toy, by hand

Take N=8N = 8 and T=1T = 1, so p=1/8p = 1/8.

  1. The chance that the first try succeeds: 1/81/8.
  2. The chance that the first success is on try 3: (7/8)2×1/8=(49/64)×(1/8)=49/512(7/8)^2 \times 1/8 = (49/64) \times (1/8) = 49/512.
  3. Numerically, 49/512≈0.095749 / 512 \approx 0.0957.
  4. The chance that none of the first 8 tries succeeds: (7/8)8(7/8)^8. Compute (7/8)2=0.765625(7/8)^2 = 0.765625, then (7/8)4=0.586182(7/8)^4 = 0.586182, then (7/8)8=0.343609(7/8)^8 = 0.343609.
  5. Check: the average is 8 tries, and about a third of the time the first 8 tries all fail. That is plausible for a 1-in-8 event.

Answer: the first valid nonce appears on try 3 with probability about 0.096, and about 34 percent of the time, 8 tries find nothing.

Example
2. Twenty leading zero bits, the standard case

Use a 256-bit hash, so N=2256N = 2^{256}. Require the digest to start with 20 zero bits. That means H(B,x)<2236H(B, x) \lt 2^{236}, so T=2236T = 2^{236} and

  1. p=T/N=2236/2256=2−20=1/1,048,576p = T / N = 2^{236} / 2^{256} = 2^{-20} = 1 / 1{,}048{,}576.
  2. Expected tries: 1/p=220=1,048,5761/p = 2^{20} = 1{,}048{,}576.
  3. Suppose the network computes r=106r = 10^{6} hashes per second. Expected time: 1,048,576/106≈1.051{,}048{,}576 / 10^{6} \approx 1.05 seconds.
  4. The chance of no valid nonce in 2212^{21} tries, which is twice the average, is (1−2−20)221(1 - 2^{-20})^{2^{21}}. Since pp is tiny, this is about e−2≈0.135e^{-2} \approx 0.135.
  5. Check: e−2=1/7.389≈0.135e^{-2} = 1/7.389 \approx 0.135.

Answer: each block takes about a second at this rate, but about 13.5 percent of the time a single run of 2212^{21} tries still finds nothing. The rate 10610^6 per second is an assumption chosen to make the arithmetic concrete.

Example
3. A block time near ten minutes

This example is illustrative. Suppose the network computes r=1018r = 10^{18} hashes per second in total, and the target is set so that the expected number of tries per block is 6×10206 \times 10^{20}. Then p=1/(6×1020)p = 1/(6 \times 10^{20}).

  1. Expected time per block: 6×1020/1018=6006 \times 10^{20} / 10^{18} = 600 seconds.
  2. 600600 seconds is 10 minutes, the target spacing in this example.
  3. A block that takes more than 20 minutes is twice the expected time. The chance of that is about e−2≈0.135e^{-2} \approx 0.135, from the same exponential approximation as before.
  4. Try it: what is the chance that a block takes more than 30 minutes? Use e−3e^{-3}.

Answer: e−3≈0.0498e^{-3} \approx 0.0498, about 5 percent. Long gaps happen, but they are rare, and the average gap is still set by the target.

Common mistakes and tradeoffs

It is tempting to think that proof of work proves the computation was useful. In fact the puzzle is arbitrary. It proves only that a search was done, and the search has no value outside the puzzle.

It is tempting to think that the fastest machine always wins. In fact the search is a lottery. A machine with twice the hash rate has twice the chance per second, so over many blocks its share of wins matches its share of total power, but any single block can go to anyone.

It is tempting to think that checking a proof is as costly as producing one. In fact the verifier does one hash, while the prover does about 1/p1/p hashes. That gap is what makes the scheme useful, and it is also what the target controls.

The tradeoffs are real. Proof of work spends electricity and hardware on a puzzle with no other output. It works best when honest participants cannot cheaply fake effort. Its alternatives change what the scarce resource is: Byzantine fault tolerant consensus protocols that rely on votes among known members need no puzzle at all, and proof-of-stake systems choose proposers by a stake instead of by computing.

SituationProof of work fits?Why
Open network with no identitiesOften yesEffort gives a cheap, anonymous way to decide who speaks
Permissioned network of known membersUsually noVoting or a known leader is cheaper and faster
Latency-sensitive serviceUsually noRandom waits add delay that a deterministic rule avoids

When to use it. Use proof of work when you need a rate limit or a fair lottery among anonymous participants and can accept the energy cost.

When not to. Do not use it when a simple registry, an account, or a known set of voters already solves the problem.

History

In 1992, Cynthia Dwork and Moni Naor proposed making junk email cost the sender a modest amount of computation, so that honest users barely notice and bulk senders pay.

Adam Back's Hashcash, proposed in 1997, attached hash-based stamps to email and gave the approach its popular name.

In 2008, the Bitcoin white paper used proof of work to order transactions in a ledger that no one controlled. The network started in 2009, and proof of work became the best-known use of the idea.

Recap

  • A nonce is valid when hashing the data with it gives a digest below a target.
  • Each try succeeds with probability p=T/Np = T/N, so the number of tries is geometric with mean 1/p1/p.
  • Finding a valid nonce costs about 1/p1/p hashes, and checking one costs a single hash.
  • The expected time to a valid block is 1/(pr)1/(pr), so the target sets the pace.
  • Read Distributed Consensus to see why a network needs a rule like this, and Blockchain to see how valid blocks are chained together.

Check yourself

QuestionRecall: what makes a nonce valid under proof of work?Show answer

The nonce is valid when hashing the block data together with the nonce gives a digest that, read as an integer, is below the target TT.

QuestionComputation: a hash has 256 possible digests and the target accepts only the digest 0, so p=1/256p = 1/256. What is the probability that the first valid nonce is on try 2?Show answer

The chance is (1−1/256)×1/256=(255/256)×(1/256)=255/65536≈0.00389(1 - 1/256) \times 1/256 = (255/256) \times (1/256) = 255/65536 \approx 0.00389. The expected number of tries is 256, so a success on try 2 is unlikely, at under 0.4 percent.

QuestionWhy: why does doubling the network's total hash rate halve the expected time to a block, but not guarantee that the next block comes sooner?Show answer

The expected time is 1/(pr)1/(pr), so doubling rr halves it. But each try is random, so the time to the next block is still a random variable, and a long wait can still happen. Only the average moves.

QuestionApplied: a network sets its target for an expected block time of 10 minutes. Overnight the total hash rate doubles and the target does not change. What is the new expected block time?Show answer

Expected time is 1/(pr)1/(pr). With rr doubled and pp unchanged, the new time is 1/(p⋅2r)=10/2=51/(p \cdot 2r) = 10 / 2 = 5 minutes. The network would then need to raise the target to restore the 10-minute pace, which is why real systems adjust it.

Sources