Arithmetic modulo p
A clock with p hours
Arithmetic modulo p is counting on a clock with p hours: after p − 1 comes 0, and every result keeps only its remainder after division by p. Writing a ≡ b (mod p) means that a and b leave the same remainder. Addition and multiplication work as usual; they just wrap around. When p is prime, every non-zero number has an inverse, so you can divide too — the extended Euclidean algorithm finds it in a number of steps that grows with the number of digits, not with the number itself. The powers of any non-zero number eventually come back to 1 and circle in a cycle whose length divides p − 1. Diffie–Hellman, RSA, elliptic curves and ML-KEM all stand on these facts.
Clock modulo p
Step 2 of 2 · Move it 7 clockwise: 9 + 7 = 16 = 1 · 13 + 3, so 9 + 7 ≡ 3 (mod 13). Only the remainder after dividing by 13 counts.
Undo the power
Computing y = gˣ mod p takes a few thousand multiplications even for a 2048-bit p, because fast exponentiation (square-and-multiply) reads the exponent bit by bit. Nobody knows a comparably simple way back: recovering x from y is the discrete logarithm problem. A search needs up to p tries, baby-step giant-step about √p multiplications, and the best known algorithm modulo p, the number field sieve, fewer — but still far too many for 2048 bits. Diffie–Hellman key exchange from the ECDH station rests on this one-wayness. In the “Break it” mode of the toy below you break small groups on your own device; the times for a real size are an extrapolation from your measurement, not a computation. Records are far from 2048 bits: a discrete logarithm modulo a 795-bit prime was computed in 2019 at a cost of about 3,100 years of a single core. The subgroup size matters too: the discrete logarithm is only as hard as in the largest subgroup of prime order (the Pohlig–Hellman algorithm), so standard groups use safe primes p = 2q + 1 with a prime q — like the groups in this toy.
Toy implementation — do not use in production12–36-bit groups fall in seconds, and BigInt exponentiation does not run in constant time. Real key exchange: ECDH (X25519, P-256) through WebCrypto.
Discrete logarithm
Drawing a secret exponent…
Real size: the ffdhe2048 group from RFC 7919 (TLS), g = 2 and a random exponent x < q = (p − 1) / 2, so up to 2047 bits.
The second lesson waits in the “Break it” mode of the clock above: the modulus must be prime. Modulo a composite n, the product of two non-zero numbers can be 0, some numbers have no inverse, and each such number a gives away a divisor of n, because gcd(a, n) > 1. In RSA the modulus is composite on purpose and its factors are the private key — so a divisor found is a broken key.
Where modular arithmetic runs
- TLS — ffdhe groupsRFC 7919 defines the groups for Diffie–Hellman in TLS: safe primes p from 2048 to 8192 bits with g = 2. It estimates the strength of the smallest one, ffdhe2048, at 103 bits.Source (TLS — ffdhe groups)
- RSA — PKCS #1RSA encryption is the exponentiation c = mᵉ mod n, and the private exponent d satisfies e · d ≡ 1 (mod λ(n)) — it is the modular inverse of e.Source (RSA — PKCS #1)
- X25519Curve25519 computes modulo the prime p = 2²⁵⁵ − 19, and at the end X25519 divides by z by multiplying with z^(p − 2) — the inverse from Fermat’s little theorem.Source (X25519)
- ML-KEMPost-quantum ML-KEM (FIPS 203) works with polynomials whose coefficients are taken modulo the prime q = 3329, and its fast multiplication uses ζ = 17 — a number of order 256 modulo q.Source (ML-KEM)
- Logjam (2015)Researchers precomputed data for the most common 512-bit DH group in TLS and then solved discrete logarithms in that group quickly; the attack downgraded connections to export-grade DH, and 8.4% of the top million domains were initially vulnerable.Source (Logjam (2015))
Leave modular arithmetic to libraries
For cryptography, use ready-made operations: key exchange and signatures from WebCrypto or from an audited
library that computes in constant time. When you compute modulo yourself — in tests, tools, exercises — use
BigInt, bring the remainder into 0 … m − 1, and do not assume that every number has an inverse until you
know the modulus is prime.
// remainder always in 0 … m − 1
const mod = (a, m) => ((a % m) + m) % m;
mod(-3n, 7n); // 4n
mod(2n ** 53n + 1n, 7n); // 5n — BigInt keeps every digitDo not exponentiate secrets with your own code: the time of a naive square-and-multiply on BigInt depends on
the bits of the exponent, and timing measurements can reveal the key (Kocher, 1996). Do not compute modulo with
number above 2⁵³ — the result silently stops being exact — and remember that % in JS keeps the sign of the
dividend. Do not invent your own groups: a p that is too small, or a p − 1 without a large prime factor, gives
away the discrete logarithm.
// ✕ % keeps the sign of the dividend
-3 % 7; // -3, not 4
// ✕ number loses digits above 2^53
(2 ** 53 + 1) % 7; // 4, but should be 5