BB84 Quantum Key Lab
Play Alice, Bob and Eve. Send photons in random bases, sift the matching ones into a key, switch on an eavesdropper or channel noise, and see how the error rate exposes her and how much secret key is left.
Reviewed December 2026. New to this? Start on the Protocol tab and press “Add Eve (100%)”.
| # | Alice bit | Alice basis | Eve basis | Bob basis | Bob bit | Kept? | Check |
|---|---|---|---|---|---|---|---|
| 1 | 1 | + | – | × | 0 | no | |
| 2 | 1 | × | – | + | 0 | no | |
| 3 | 0 | + | – | + | 0 | yes | compared: match |
| 4 | 0 | + | – | + | 0 | yes | kept |
| 5 | 0 | + | – | + | 0 | yes | kept |
| 6 | 0 | × | – | + | 1 | no | |
| 7 | 0 | × | – | × | 0 | yes | kept |
| 8 | 0 | + | – | + | 0 | yes | kept |
| 9 | 1 | × | – | + | 1 | no | |
| 10 | 1 | + | – | + | 1 | yes | kept |
| 11 | 0 | × | – | × | 0 | yes | kept |
| 12 | 1 | × | – | + | 1 | no | |
| 13 | 0 | × | – | × | 0 | yes | kept |
| 14 | 0 | × | – | × | 0 | yes | kept |
| 15 | 0 | + | – | + | 0 | yes | compared: match |
| 16 | 1 | × | – | × | 1 | yes | kept |
| 17 | 1 | + | – | × | 0 | no | |
| 18 | 0 | × | – | + | 0 | no | |
| 19 | 0 | + | – | + | 0 | yes | compared: match |
| 20 | 1 | × | – | + | 1 | no | |
| 21 | 1 | × | – | + | 1 | no | |
| 22 | 0 | × | – | + | 1 | no | |
| 23 | 1 | × | – | + | 0 | no | |
| 24 | 1 | + | – | × | 1 | no |
+ is the rectilinear basis, × the diagonal basis. Bob keeps a bit only when his basis equals Alice’s; the other half are discarded after the bases are announced. Rows marked “compared” are revealed in public and thrown away. “Kept, wrong” shows an error the comparison sample did not happen to catch.
Sifted key
12 bits (50%)
Compared in public
3 bits, 0 wrong
Estimated error rate
0.0% (expected 0.0%)
Verdict
keep: 9 secret bits
A five-minute path
- Protocol tab with a clean channel: count the rows where Alice’s and Bob’s bases match. Their bits always agree.
- Press “Add Eve (100%)”. Some kept rows now say “MISMATCH”. About a quarter of the compared bits are wrong.
- Lower Eve to 30% and raise the photon count. The error rate falls with her share.
- Eavesdropper tab: read how fast the chance of escaping shrinks as more bits are compared.
- Key rate tab: find where the curve hits zero.
- Big run tab: send 5000 photons with 2% noise and see how few secret bits remain.
What the lab does and does not do
- Ideal single photons, no loss, noise as random bit flips.
- Eve uses intercept-resend on a chosen share of photons.
- The final key length comes from the asymptotic formula 1 − 2h(Q), not from running an actual code.
- The public channel is assumed authenticated.
Reference tables
Computed by the same code as the lab.
| Alice’s basis | Bob’s basis | Bob’s bit | Kept? |
|---|---|---|---|
| + | + | Alice’s bit, for certain | yes |
| × | × | Alice’s bit, for certain | yes |
| + | × | 0 or 1, 50% each | no |
| × | + | 0 or 1, 50% each | no |
| Share intercepted | Error rate | Above 11%? |
|---|---|---|
| 0% | 0.0% | no |
| 10% | 2.5% | no |
| 25% | 6.3% | no |
| 44% | 11.0% | no |
| 50% | 12.5% | yes, abort |
| 75% | 18.8% | yes, abort |
| 100% | 25.0% | yes, abort |
| m | (3/4)^m |
|---|---|
| 5 | 2.37e-1 |
| 10 | 5.63e-2 |
| 20 | 3.17e-3 |
| 40 | 1.01e-5 |
| 80 | 1.01e-10 |
| 160 | 1.02e-20 |
Eight guides
What is BB84? Quantum key distribution in plain steps
8 min read
How two people can agree a secret key by sending single photons, and what makes it different from ordinary encryption.
Bases and sifting: why half the photons are thrown away
7 min read
What happens when Bob measures in the wrong basis, and why the sifted key is about half as long as the number of photons sent.
How an eavesdropper is caught: the intercept-resend attack
9 min read
Why measuring photons in transit introduces a 25 percent error rate, and how a short public comparison exposes it.
The error rate and the 11 percent line
8 min read
Noise and eavesdropping look the same to Alice and Bob. Why they must treat all errors as Eve’s, and where the threshold comes from.
Error correction and privacy amplification
8 min read
After sifting, Alice and Bob still have mismatches and Eve may know some bits. Two classical steps fix both.
BB84 worked by hand: a short run from start to key
9 min read
Follow a small run step by step, then see how the numbers scale up to thousands of photons.
BB84 in real systems: where theory and practice part
8 min read
Single photons, loss, detectors and the attacks that exploit imperfect equipment.
QKD versus post-quantum cryptography: two answers to Shor
8 min read
Two different defences against a future quantum computer, what each needs, and why they are not the same thing.
Predict, then check
Eve intercepts every photon and the channel is noiseless. What error rate do Alice and Bob see?
25.0%.
Eve intercepts half of the photons and the channel flips 3% of the bits. What is the expected error rate?
14.75%, from e + p − 2ep with e = 12.5% and p = 3%.
30 sifted bits are compared and Eve intercepts everything. How likely is she to get away?
(3/4)^30 = 1.79e-4.
At a 5% error rate, what fraction of the sifted key becomes secret?
0.427, so about 427 of every 1000 sifted bits.
About how many photons must Alice send to get 1000 sifted bits?
About 2000, since only the half with matching bases is kept.
Related on QuantumAtlas
Frequently asked questions
What is BB84?
The first quantum key distribution protocol, proposed by Charles Bennett and Gilles Brassard in 1984. Alice sends photons prepared in random bases, Bob measures in random bases, and they keep the positions where the bases matched.
How does BB84 detect an eavesdropper?
Measuring a photon in the wrong basis disturbs it. An eavesdropper who intercepts every photon causes a 25 percent error rate in the sifted key, which Alice and Bob see when they compare a sample.
Why is only half of the key kept?
Bob guesses the basis, so he is right half the time. The positions where the bases differ give random results and are discarded.
What is the 11 percent limit?
For long keys, the secret fraction 1 − 2h(Q) reaches zero at an error rate near 11 percent. Above it, Alice and Bob abort.
Is the simulation exact?
The quantum part is modelled exactly: the same basis gives the bit, a different basis gives a fair coin. The random choices use a seeded generator, and the simulated error rates were checked against the closed-form values.
Does the lab run real error correction?
No. It uses the asymptotic key-fraction formula to give the final key length. It does not implement an error-correcting code or a hash function.
Can the lab tell me if a real QKD system is secure?
No. It assumes ideal single photons and a simple attack. Real systems have extra weaknesses.
Is BB84 the same as post-quantum cryptography?
No. BB84 uses quantum physics to distribute keys. Post-quantum cryptography is ordinary software based on maths believed to resist quantum computers.
Does the protocol need a secret to start?
It needs an authenticated classical channel, which in practice means a small pre-shared secret or another authentication method.
Where do the photons go?
About half are lost to sifting, some are used for the error estimate, and the rest shrinks further by error correction and privacy amplification.
Sources and further reading
- C. H. Bennett and G. Brassard, “Quantum cryptography: public key distribution and coin tossing” (1984). The paper that introduced the protocol, presented at a conference in Bangalore.
- P. W. Shor and J. Preskill, “Simple proof of security of the BB84 quantum key distribution protocol” (2000). The security proof behind the 1 − 2h(Q) key fraction and the roughly 11 percent limit.
- A. K. Ekert, “Quantum cryptography based on Bell’s theorem” (1991). A different protocol that uses entangled pairs.
- Algorithm page: BB84 protocol. A shorter overview on this site.
- Lesson: quantum cryptography. A gentle introduction on this site.
References were written from memory of the original works. Check the originals for exact citations.