RSA Correctness

lean4-proofcryptographyvisualization
medm(modn)m^{ed} \equiv m \pmod{n}

Statement

Let n=pqn = pq for distinct odd primes pp and qq. Define ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1). Choose ee with gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1 and set de1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)}, so ed=1+kϕ(n)ed = 1 + k\phi(n) for some integer kk. Then for any message mm with 0m<n0 \le m < n:

medm(modn)m^{ed} \equiv m \pmod{n}

This is the correctness condition for RSA: encrypting with ee then decrypting with dd (or vice versa) is the identity.

Visualization

Parameters: p=11p = 11, q=13q = 13, n=143n = 143, ϕ(n)=120\phi(n) = 120, e=7e = 7, d=103d = 103 (since 7×103=721=6×120+17 \times 103 = 721 = 6 \times 120 + 1).

Message m=42m = 42:

StepOperationValue
Encryptc=427mod143c = 42^7 \bmod 143c=95c = 95
Decryptm=95103mod143m' = 95^{103} \bmod 143m=42m' = 42
Checkm=mm' = m?YES

Verification that ed1(mod120)ed \equiv 1 \pmod{120}:

7×103=721=6×120+1    7211(mod120)7 \times 103 = 721 = 6 \times 120 + 1 \implies 721 \equiv 1 \pmod{120}

The CRT splits the verification into two prime-modulus checks:

  • Modulo p=11p = 11: med=m1+k10=m(m10)km1k=m(mod11)m^{ed} = m^{1 + k \cdot 10} = m \cdot (m^{10})^k \equiv m \cdot 1^k = m \pmod{11}
  • Modulo q=13q = 13: med=m1+k12=m(m12)km1k=m(mod13)m^{ed} = m^{1 + k \cdot 12} = m \cdot (m^{12})^k \equiv m \cdot 1^k = m \pmod{13}

Proof Sketch

  1. Euler's theorem. For gcd(m,p)=1\gcd(m, p) = 1, Fermat's little theorem gives mp11(modp)m^{p-1} \equiv 1 \pmod{p}. More generally, Euler's theorem gives mϕ(n)1(modn)m^{\phi(n)} \equiv 1 \pmod{n} when gcd(m,n)=1\gcd(m, n) = 1.

  2. Expand the exponent. Write ed=1+kϕ(n)ed = 1 + k\phi(n). Then:

med=m1+kϕ(n)=m(mϕ(n))km1k=m(modp)m^{ed} = m^{1+k\phi(n)} = m \cdot (m^{\phi(n)})^k \equiv m \cdot 1^k = m \pmod{p}

and the same holds modulo qq.

  1. Chinese Remainder Theorem. Since p(medm)p \mid (m^{ed} - m) and q(medm)q \mid (m^{ed} - m) and gcd(p,q)=1\gcd(p,q) = 1, we conclude pq=n(medm)pq = n \mid (m^{ed} - m), giving medm(modn)m^{ed} \equiv m \pmod{n}.

  2. Degenerate cases. When pmp \mid m or qmq \mid m, a separate (easy) case analysis still gives medm(modn)m^{ed} \equiv m \pmod{n}.

Connections

The correctness proof uses Fermat's Little TheoremFermat's Little Theoremapa(modp)a^p \equiv a \pmod{p}For prime p, a^p is congruent to a mod pRead more → (prime factor case) and the Chinese Remainder TheoremChinese Remainder TheoremZ/mnZZ/mZ×Z/nZ\mathbb{Z}/mn\mathbb{Z} \cong \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z}Simultaneous congruences with coprime moduli have a unique solution mod their productRead more → to lift from prime moduli to n=pqn = pq. The exponent dd exists because gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1, which is established via Bezout's IdentityBezout's Identitygcd(a,b)=ax+by\gcd(a,b) = a \cdot x + b \cdot yThe gcd of two integers is always an integer linear combination of those integersRead more →. See also Euler's Totient FunctionEuler's Totient Functionaφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}φ(n) counts integers up to n coprime to n, and governs modular exponentiationRead more → for ϕ(pq)=(p1)(q1)\phi(pq) = (p-1)(q-1).

Lean4 Proof

/-- RSA correctness for the unit-group case: in ZMod p (prime p), every
    nonzero element satisfies a^(ed) = a when ed ≡ 1 (mod p-1).
    We verify on a concrete small instance via decide. -/

-- Concrete check: p = 11, e = 7, d = 103, ed = 721 = 6*120+1
-- Verify 7 * 103 % 120 = 1
#eval (7 * 103) % 120  -- 1

-- Verify the RSA round-trip for m = 42, n = 143
#eval (42 ^ 7 % 143)         -- 95  (ciphertext)
#eval (95 ^ 103 % 143)       -- 42  (recovered plaintext)

/-- Fermat's little theorem: for prime p and a ≠ 0 in ZMod p, a^(p-1) = 1. -/
theorem rsa_fermat_step (p : ) [Fact p.Prime] {a : ZMod p} (ha : a  0) :
    a ^ (p - 1) = 1 :=
  ZMod.pow_card_sub_one_eq_one ha

/-- Key algebraic identity: a^(1 + k*(p-1)) = a for nonzero a in ZMod p. -/
theorem rsa_exp_identity (p : ) [Fact p.Prime] {a : ZMod p} (ha : a  0)
    (k : ) : a ^ (1 + k * (p - 1)) = a := by
  rw [pow_add, pow_mul, ZMod.pow_card_sub_one_eq_one ha, one_pow, mul_one, pow_one]