Modular Arithmetic 🕰️
Zero knowledge is built on a clock. Seriously: the "wrap-around" arithmetic you use every time you read a clock face is the setting for secrets that are easy to use and (as far as we know) impossibly hard to undo.
Life on a clock
a mod n means: the remainder when a is divided by n. If it's 9 o'clock now, in 7 hours it's (9+7) mod 12 = 4 o'clock — we silently discard the 12s. The numbers form a closed little universe: add, subtract or multiply as much as you like, you never leave the set {0, 1, …, n−1}.
The rules of the universe
Modular arithmetic keeps all the algebra you know — with one twist, everything is "up to wrap-around":
- (a + b) mod n = ((a mod n) + (b mod n)) mod n — same for multiplication. Reduce early, reduce often.
- When n is prime, every non-zero element has a multiplicative inverse, and the exponents of any element cycle with a fixed period (its order, which divides n−1).
- Powers grow terrifyingly — yet remainders are cheap. Squaring repeatedly (a → a² → a⁴ → a⁸ …) computes aᵏ mod n in about log₂ k steps. Cryptography runs on this trick.
Powers that orbit
Fix a base g and a prime p, and list the powers g¹, g², g³, … mod p. They orbit through the residues and eventually return to 1, then repeat. If g is a generator, its powers visit every non-zero residue before repeating — a perfect shuffle with no pattern.
🧪 The one-way street: Discrete Log Hunt
Here's the asymmetry that makes modern cryptography possible. Computing y = gˣ mod p is instant — a few squarings. But given only g, p and y, recovering x (the discrete logarithm) appears to require grinding through candidates one by one. Multiply p by 10 and your grind grows by 10×; but add one measly bit to x and the honest direction barely notices.
Why this matters for ZK
- Secrets live in exponents. "I know x such that y = gˣ" is the canonical statement Schnorr proofs prove in zero knowledge (Chapter 6) — without ever revealing x.
- Keys from public noise. Diffie–Hellman lets two strangers mix exponents in public and emerge with a shared secret, because unwinding the mixing is a discrete log problem.
- Pedersen commitments (Chapter 4) are exponentials C = gᵐ·hʳ — hiding precisely because discrete logs are hard.
📌 Week 2 — key takeaways
- a mod n is remainder arithmetic on a clock — reduce early, all of algebra survives the wrap.
- Fast forward, slow backward: gˣ mod p costs ~log x steps; the reverse (discrete log) has no known fast algorithm — the workhorse asymmetry of cryptography.
- Prime moduli make it a field: every non-zero element has an inverse (a⁻¹ ≡ a^(p−2)); powers of a generator orbit all residues with period dividing p−1.
- Diffie–Hellman mixes exponents in public: (gᵇ)ᵃ = (gᵃ)ᵇ — shared secrets with no shared history, and security resting exactly on your Log Hunt experience.
- Best known attacks (baby-step giant-step √p, index calculus) only trim the exponent; elliptic curves keep even those at arm's length.
Checkpoint: Clock Universe
- 26
- 0
- 18
- 512
- Because mod makes the result small, so everything is small.
- Repeated squaring computes it in ~256 multiply-and-reduce steps.
- Because quantum computers help.
- Because we precompute all powers of g.
- No known efficient algorithm finds x from (g, p, gˣ mod p), even though the reverse direction is trivial.
- Division in modular arithmetic is impossible.
- Logarithms don't exist for primes.
- It has been mathematically proven that no fast algorithm exists.
- The modulus p.
- The base g.
- A hash of the protocol transcript.
- A hidden exponent, with only y = gˣ made public.