Prove you know the password without saying it
A website wants to know you have the password. The usual way is to send it, and then the site has it too. A zero-knowledge proof convinces somebody that you know a secret without showing them the secret. This lab shows the ideas with tiny numbers, so it is a toy for learning and not safe to use.
By the end of this lesson
Note
I can explain how a prover convinces a verifier that they know a secret without revealing it.
Key words
- Prover: the one who claims to know a secret.
- Verifier: the one who checks the claim.
- Challenge: a random question the verifier picks.
- Commitment: a sealed value that can be opened later and cannot be changed.
- Zero knowledge: the verifier learns that the claim is true and nothing else.
- Merkle root: one short hash that stands for a whole list.
Starter: what you already know
Note
Answer in your head. 1. What does a hash do to a long message? 2. If you flip a fair coin twice, what is the chance of two heads? 3. Why should a website never store your password as plain text? Answers: turns it into a short fingerprint; one in four; anybody who reads the file would have it.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, jupyter, and the terminal reaches its Python with docker exec jupyter, which runs one command inside it. The JupyterLab link opens its web page, port 8888. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
Many rounds turn luck into proof
A cave has two paths joined by a locked door. The prover walks in by one path, and the verifier shouts which path to come out of. Knowing the password, the prover passes every time: 20 of 20 rounds. A cheater only passes when the guess was right. Of 1000 cheaters, 493 survive round one, 243 round two and none are left after round ten. The chance of passing 20 rounds by luck is 1 in 1048576.
docker exec jupyter python3 steps/cave.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A commitment is a sealed envelope
The script publishes a hash of the move rock and a random salt, and keeps both secret. Later it opens them: rock with the salt checks out as True, paper as False, so the move cannot be changed. Without a salt, a guesser hashes the three moves and finds the match, so the salt is what hides the move.
docker exec jupyter python3 steps/commit.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Schnorr proves you know a secret number
The secret is x = 65 and the public key is y = 227. The prover sends t, the verifier sends the challenge c = 77, and the prover answers s = 38. The verifier checks one equation and gets True, without ever seeing 65. Then the script answers a second challenge with the same random k, and x is worked out as 65. A prover must never reuse k.
docker exec jupyter python3 steps/schnorr.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Tip
Why does the verifier pick the challenge after seeing t? Answer: if the prover knew the challenge first, they could make a t that fits without knowing the secret.
A fake conversation looks just like a real one
This script does not know x. It picks the challenge and the answer first, then makes a t that fits. The check passes. So a record of the conversation proves nothing to a third person, which is exactly why it gives away nothing about the secret.
docker exec jupyter python3 steps/simulate.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A hash can play the verifier
Fiat-Shamir replaces the verifier's random challenge with a hash of the public key, t and a message. Now the prover can make the proof alone, and anybody can check it. The challenge came out as 14. The proof checks out for its own message and fails for a different one.
docker exec jupyter python3 steps/fiatshamir.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A Merkle proof shows one name is on the list
Eight names are hashed in pairs, then the pairs in pairs, up to one root. To show dev is on the list, you give 3 sibling hashes, not all 8 names. Hashing dev up the path gives the root, so the answer is True. The name dan with the same proof gives False.
docker exec jupyter python3 steps/membership.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think zero knowledge means the verifier learns nothing at all. The verifier does learn one thing: that the claim is true. What they do not learn is the secret, or anything that helps them find it. A proof that taught them nothing would convince nobody.
The same work in a notebook
Open JupyterLab from the bar and double-click start.ipynb. It runs a Schnorr check, a Fiat-Shamir proof and a Merkle proof. Click a cell and press Shift and Enter to run it. The last cell is where your own question goes, and zk.py is short enough to read in full.
Exit check
Tip
Answer without looking back. 1. Why does a cheater get caught after many rounds? 2. What stops a guesser opening a hash commitment? 3. What happens if a Schnorr prover uses the same k twice? 4. What replaces the verifier in Fiat-Shamir? Answers: each round halves their chance; the random salt; the secret can be worked out; a hash.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service, JupyterLab with zero-knowledge proofs written in the Python standard library, and nothing here marks itself: the student hands in a notebook of their own, downloaded from JupyterLab, and a sentence on what it showed. Key learning points: 1. Repeated random challenges make cheating unlikely. 2. A commitment hides a value and fixes it. 3. Schnorr proves knowledge of a secret, and Fiat-Shamir makes it non-interactive. Common mistake: thinking the verifier learns nothing at all. The numbers are tiny and insecure on purpose. Equipment: the lab and a browser. Tip: play the cave game in the room with two doors before the first Run. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.