BB84 worked by hand: a short run from start to key
Follow a small run step by step, then see how the numbers scale up to thousands of photons.
9 min read · reviewed December 2026
Photons and measurements
Alice sends twelve photons. For each she has a bit and a basis, and Bob has a basis. The table is generated by the lab’s simulator, so you can reproduce it by setting 12 photons and no Eve.
| # | Alice bit | Alice basis | Bob basis | Bob bit | Kept |
|---|---|---|---|---|---|
| 1 | 0 | + | × | 0 | no |
| 2 | 0 | + | + | 0 | yes |
| 3 | 0 | × | + | 0 | no |
| 4 | 1 | + | × | 0 | no |
| 5 | 0 | + | × | 0 | no |
| 6 | 1 | × | × | 1 | yes |
| 7 | 1 | × | + | 0 | no |
| 8 | 0 | + | + | 0 | yes |
| 9 | 0 | + | × | 0 | no |
| 10 | 0 | + | + | 0 | yes |
| 11 | 0 | × | × | 0 | yes |
| 12 | 1 | × | × | 1 | yes |
Sifting
Keep only the rows where the two bases agree. About half the rows survive. Where the bases differ, Bob’s bit is a coin flip, and both sides throw it away.
Checking
Alice and Bob pick a few of the surviving positions at random and read their bits out loud. With no Eve and no noise, all of them match. With Eve intercepting every photon, about a quarter will not.
Scaling up
With 12 photons the sample is far too small to say anything. Real runs use thousands or millions of photons. The table shows two seeded runs at 2 percent noise.
| Photons sent | Sifted | Compared | Measured error | Secret bits (asymptotic) |
|---|---|---|---|---|
| 10000 | 4924 | 1250 | 2.0% | 2634 |
| 100000 | 50110 | 12500 | 2.1% | 26601 |
The remaining steps
The leftover bits go through error correction and privacy amplification, as in the previous guide. What comes out is a key both sides share and Eve, within the model, knows almost nothing about.
Try it
Questions
Why does the estimate fluctuate so much for small runs?
Because each compared bit is a random sample. With 20 bits, one error moves the estimate by 5 percentage points.
Does the seed button repeat my run?
Each press uses a new seed, so you see a different random run. The same seed always gives the same run.
Can I set up a run that aborts?
Yes. Add Eve at 100 percent and use many photons. With only a few photons the comparison sample is too small to be reliable, so the result varies from run to run.
Read next
Sources and further reading
- C. H. Bennett and G. Brassard, “Quantum cryptography: public key distribution and coin tossing” (1984)
- P. W. Shor and J. Preskill, “Simple proof of security of the BB84 quantum key distribution protocol” (2000)
- A. K. Ekert, “Quantum cryptography based on Bell’s theorem” (1991)
- Algorithm page: BB84 protocol
- Lesson: quantum cryptography
Check the original sources for exact citations.