Skip to content
Trapdoor·Lab

Zero-knowledge proof: Ali Baba's cave

1Toy

A password you never reveal

A zero-knowledge proof convinces you that a statement is true — here: “I know the password to the door” — and reveals nothing beyond that. The best-known picture comes from the paper by Quisquater and co-authors, “How to Explain Zero-Knowledge Protocols to Your Children” (CRYPTO ’89): a ring-shaped cave whose two tunnels meet deep inside at a door that opens with a password. The prover enters tunnel A or B, the verifier waits at the entrance and only then calls the side the prover must come out of. The order matters: first the commitment (entering a tunnel), then the challenge, last the response (coming out). Whoever knows the password always comes out on the right side; whoever does not gets it right with probability 1/2.

A recording of such a session convinces nobody else. Someone without the password can shoot an identical film: they enter at random and cut every take in which the verifier called the other side. Since the same thing can be produced without the secret, the conversation carries no knowledge of it — that is the definition of zero knowledge: everything the verifier sees can be simulated without the secret.

Ali Baba’s cave

A thought experiment, not a protocolReal zero-knowledge proofs replace the door with a hard mathematical problem — e.g. the discrete logarithm in Schnorr’s protocol.

You are the verifier. The prover goes in first while you wait at the entrance, unable to see which tunnel they took. Then you call the side they must come out of.

The prover is already in the cave. Call side A or B — or press play, and crypto.getRandomValues picks the challenges.

door

P — prover, V — verifier. Dashed line: the way in, solid: the way out; ✕ — the door did not give. Dashed door: opened with the password.

Rounds passed: 0 of 20
0510152010.501/2ⁿ

Each round halves the odds of someone without the password: after 20 rounds they are 1 in 1,048,576.

2Break it

Guess or predict the challenge

A cheater without the password can only guess which side the verifier will call: one round is a 1/2 chance, n rounds in a row 1/2ⁿ, and after 20 rounds less than one in a million. As long as the challenges are truly random and come only after the prover is inside, guessing does not help: just add rounds. A predictable challenge is a different story. If the verifier alternates, draws from a generator seeded with the clock (like MT19937 in the Randomness station) or reveals the challenge before the prover commits, the cheater enters the right tunnel every time and passes all rounds — 1/2²⁰ turns into certainty. In non-interactive proofs (the Fiat–Shamir transform) a hash function takes the verifier’s place: the challenge is a hash of the commitment and all public data. When the hash leaves out the commitment or the statement itself, the prover can pick them to fit a challenge already known — the same mistake. Try both attacks in the toy’s “Break it” mode above.

3In the real world

Where proofs reveal nothing

  • Schnorr signatures in Bitcoin (BIP-340)A Schnorr signature is the non-interactive version of a proof of knowing the private key: the challenge e does not come from a verifier but is the “BIP0340/challenge” hash of the point R, the public key P and the message.Source (Schnorr signatures in Bitcoin (BIP-340))
  • ZcashShielded transactions are encrypted on the blockchain, yet the network still checks that they follow the rules — thanks to zk-SNARK proofs. The Orchard protocol (Network Upgrade 5, May 2022) uses the Halo 2 proving system, which needs no trusted setup.Source (Zcash)
  • Google longfellow-zkIn July 2025 Google open-sourced its ZKP libraries for age assurance: a website can check that you are over 18 without getting any other data. EU Member States can build them into the European Digital Identity Wallet (EUDI).Source (Google longfellow-zk)
  • Frozen Heart (2022)Trail of Bits found weak Fiat–Shamir in implementations of Girault, Bulletproofs and PlonK proofs (among them SnarkJS, gnark, Dusk Network): the hash did not cover all public data, so a prover could predict the challenge and forge a proof.Source (Frozen Heart (2022))
  • BitwardenAdvertises “zero-knowledge encryption”: no one — not even Bitwarden — can decrypt the stored data, because the key stays with the user. That is a zero-knowledge architecture, not a zero-knowledge proof: the server proves nothing to anyone, it only stores ciphertext.Source (Bitwarden)

Do not mix the two up: “zero-knowledge” in ads for password managers and clouds usually means the server knows only ciphertext — the account on this site works that way too. A zero-knowledge proof is a protocol in which one party convinces another. The cave is a picture of it; real protocols are Schnorr (a proof of knowing a discrete logarithm) and graph 3-colouring, on which Goldreich, Micali and Wigderson showed that under standard cryptographic assumptions every statement in NP has a zero-knowledge proof. Both have their own stations: The Schnorr protocol and Graph coloring.

4Take it to your project

An unpredictable challenge, the whole transcript

Good practice

Do not design your own protocol — use a documented standard and a proven library (e.g. Schnorr signatures from BIP-340). If you implement the verifier of an interactive protocol, draw the challenge from a cryptographic generator only after the commitment has arrived. In the non-interactive version compute the challenge as a hash of the whole transcript: the context (domain separation), the statement (e.g. the public key), the commitment and the message.

JavaScript
import { sha256 } from '@noble/hashes/sha2.js';
import { concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';

// Fiat–Shamir: challenge = hash of the WHOLE transcript,
// computed only after the commitment
const e = sha256(concatBytes(
  utf8ToBytes('myapp/schnorr-proof/v1'), // context
  publicKey,  // statement: whose key it is
  commitment, // commitment R
  message,
));
Anti-pattern

Do not take challenges from a counter, the clock or Math.random() — whoever predicts them enters the right tunnel every time. Do not compute the Fiat–Shamir hash over the commitment alone: without the statement and the context, the prover picks them to fit the challenge (Frozen Heart). And do not call client-side encryption a zero-knowledge proof — that is a zero-knowledge architecture.

JavaScript
// ✕ predictable challenges
const challenge = round % 2;      // alternating
const coin = Math.random() < 0.5; // non-cryptographic generator
// ✕ weak Fiat–Shamir: no key and no context in the hash
const e = sha256(commitment);