Chinese Remainder Theorem

lean4-proofnumber-theorymodular-arithmeticvisualization
Z/mnZZ/mZ×Z/nZ\mathbb{Z}/mn\mathbb{Z} \cong \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z}

Statement

Let mm and nn be coprime natural numbers (i.e. gcd(m,n)=1\gcd(m,n) = 1). Then for any integers aa and bb there exists a unique xx modulo mnmn such that:

xa(modm)andxb(modn)x \equiv a \pmod{m} \qquad \text{and} \qquad x \equiv b \pmod{n}

In algebraic terms this is a ring isomorphism:

Z/mnZ    Z/mZ  ×  Z/nZ\mathbb{Z}/mn\mathbb{Z} \;\cong\; \mathbb{Z}/m\mathbb{Z} \;\times\; \mathbb{Z}/n\mathbb{Z}

Visualization

Worked example: solve x2(mod3)x \equiv 2 \pmod{3} and x3(mod5)x \equiv 3 \pmod{5}.

mod 3:  ...,  2,  5,  8, 11, 14, 17, 20, 23, ...   (2 mod 3)
mod 5:  ...,  3,  8, 13, 18, 23, 28, 33, ...        (3 mod 5)
                   ↑                  ↑
                   8 ≡ 2 (mod 3) and 8 ≡ 3 (mod 5)   ✓

Unique solution: x8(mod15)x \equiv 8 \pmod{15}.

Construction via Bezout:

StepActionValue
1Find u,vu, v with 3u+5v=13u + 5v = 1u=2, v=1u=2,\ v=-1
2e1=5va=5(1)(2)=10e_1 = 5v \cdot a = 5(-1)(2) = -102(mod3)\equiv 2 \pmod 3
3e2=3ub=3(2)(3)=18e_2 = 3u \cdot b = 3(2)(3) = 183(mod5)\equiv 3 \pmod 5
4x=e1+e2=10+18=8x = e_1 + e_2 = -10 + 18 = 88mod15=88 \bmod 15 = 8

Bijection table for m=2,n=3m=2, n=3 (all residues mod 6):

xmod6x \bmod 6xmod2x \bmod 2xmod3x \bmod 3
000
111
202
310
401
512

Every pair (mod 2,mod 3)(\text{mod }2, \text{mod }3) appears exactly once — the isomorphism is a bijection.

Proof Sketch

  1. Existence. By 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 → there exist u,vu, v with mu+nv=1mu + nv = 1. Set x=anv+bmux = a \cdot nv + b \cdot mu. Then xmodm=anvmodm=a(1mu)vmodm=amodm=ax \bmod m = a \cdot nv \bmod m = a(1 - mu)v \bmod m = a \bmod m = a (and similarly for bb).

  2. Uniqueness. If xx and xx' both satisfy both congruences then m(xx)m \mid (x - x') and n(xx)n \mid (x - x'). Since gcd(m,n)=1\gcd(m,n)=1, we have mn(xx)mn \mid (x - x'), so xx(modmn)x \equiv x' \pmod{mn}.

  3. Ring isomorphism. The map ϕ:Z/mnZZ/mZ×Z/nZ\phi: \mathbb{Z}/mn\mathbb{Z} \to \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z} sending xx to (xmodm,xmodn)(x \bmod m, x \bmod n) is a ring homomorphism. Existence and uniqueness show it is bijective.

Connections

CRT is the modular-arithmetic analogue of product decomposition. It generalises to any finite collection of pairwise coprime moduli. It is the basis for fast arithmetic in cryptography (RSA uses it for efficient decryption) and connects to 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 → via the multiplicativity ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\phi(n) for coprime m,nm,n. The structure mirrors 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 → in action, and the coprimality assumption echoes the role of primes in the Fundamental Theorem of ArithmeticFundamental Theorem of Arithmeticn=p1a1p2a2pkakn = p_1^{a_1} \cdot p_2^{a_2} \cdots p_k^{a_k}Every integer greater than 1 has a unique prime factorizationRead more →. The ring-theoretic generalization — Chinese Remainder Theorem (Rings) — states that for a ring RR and pairwise coprime ideals I1,,IkI_1, \ldots, I_k, the natural map RjR/IjR \to \prod_j R/I_j is surjective with kernel jIj\bigcap_j I_j.

Lean4 Proof

/-- **Chinese Remainder Theorem**: for coprime moduli `m` and `n`, the ring
    `ZMod (m * n)` is isomorphic to `ZMod m × ZMod n`.
    Mathlib provides the isomorphism as `ZMod.chineseRemainder`. -/
theorem crt {m n : } (h : Nat.Coprime m n) :
    ZMod (m * n) ≃+* ZMod m × ZMod n :=
  ZMod.chineseRemainder h