MathLabs

Arithmetic and number theory

Chinese remainder theorem

A system of congruences with coprime moduli always has a unique solution modulo their product.

IntuitionCounting soldiers without counting past 77

A general lines up his soldiers in rows of 33 and sees 22 left over; in rows of 55, 33 are left over; in rows of 77, 22 are left over. How many soldiers are there? Even though the general never counted past 77, those three tiny remainders (2,3,2)(2,3,2) pin down the exact count modulo 3×5×7=1053\times5\times7=105: the answer is 2323 (plus any multiple of 105105). This is the Chinese remainder theorem (CRT): as long as the moduli share no common factor in pairs, knowing the remainders modulo each mim_i carries the exact same information as knowing the remainder modulo their product m1m2⋯mkm_1 m_2\cdots m_k. It lets us split one hard computation modulo a huge number into several easy, independent computations modulo its prime-power factors, and then reassemble the result.

A network diagram illustrating how a problem modulo M splits into independent subproblems modulo its coprime factors and recombines.
Residues modulo m=15=3×5m = 15 = 3 \times 5: each residue x mod 15x \bmod 15 is uniquely determined by its pair of remainders (x mod 3, x mod 5)(x \bmod 3,\ x \bmod 5).

SchoolStatement and explicit formula

Definition: A system of simultaneous congruences

Given moduli m1,…,mkm_1,\dots,m_k that are pairwise coprime — gcd⁡(mi,mj)=1(i≠j)\gcd(m_i,m_j)=1\quad(i\neq j) — and any integers a1,…,aka_1,\dots,a_k, the system x≡ai(modmi)(1≤i≤k)x \equiv a_i \pmod{m_i}\quad(1\le i\le k) asks for an integer xx that simultaneously leaves remainder aia_i modulo mim_i for every ii. Setting M=m1m2⋯mk,Mi=M/miM = m_1 m_2 \cdots m_k,\quad M_i = M/m_i and letting yiy_i be the inverse of MiM_i modulo mim_i (so Miyi≡1(modmi)M_i y_i \equiv 1 \pmod{m_i}, which exists since gcd⁡(Mi,mi)=1\gcd(M_i,m_i)=1), the solution is given explicitly by x≡∑i=1kaiMiyi(modM)x \equiv \sum_{i=1}^{k} a_i M_i y_i \pmod{M}.

x≡ai(modmi)(1≤i≤k),gcd⁡(mi,mj)=1 (i≠j)x \equiv a_i \pmod{m_i}\quad(1\le i\le k),\qquad \gcd(m_i,m_j)=1\ (i\neq j)

Each term aiMiyia_i M_i y_i in the sum is engineered to be ≡ai(modmi)\equiv a_i \pmod{m_i} (since Miyi≡1(modmi)M_i y_i\equiv1\pmod{m_i}) and ≡0(modmj)\equiv 0 \pmod{m_j} for every j≠ij\neq i (since mjm_j is a factor of Mi=M/miM_i = M/m_i). So when we reduce the whole sum modulo a fixed mim_i, all terms except the ii-th vanish, leaving aia_i — exactly as required.

x≡∑i=1kaiMiyi(modM),M=m1⋯mk, Mi=M/mi, Miyi≡1(modmi)x \equiv \sum_{i=1}^{k} a_i M_i y_i \pmod{M},\qquad M = m_1\cdots m_k,\ M_i = M/m_i,\ M_i y_i \equiv 1 \pmod{m_i}
Two moduli vs. kk moduli
CaseHypothesisUnique modulo
22 moduli m1,m2m_1,m_2gcd⁡(m1,m2)=1\gcd(m_1,m_2)=1m1m2m_1 m_2
kk moduli m1,…,mkm_1,\dots,m_kgcd⁡(mi,mj)=1\gcd(m_i,m_j)=1 for i≠ji\neq jM=m1⋯mkM=m_1\cdots m_k

UndergraduateTheorems

If m1,…,mkm_1,\dots,m_k are pairwise coprime (gcd⁡(mi,mj)=1(i≠j)\gcd(m_i,m_j)=1\quad(i\neq j)), then for any integers a1,…,aka_1,\dots,a_k the system x≡ai(modmi)(1≤i≤k)x \equiv a_i \pmod{m_i}\quad(1\le i\le k) has a solution xx, and any two solutions are congruent modulo M=m1⋯mkM=m_1\cdots m_k.

Why is it true?

Because the moduli share no prime factors, the condition of leaving remainder a1a_1 modulo m1m_1 and the condition of leaving remainder a2a_2 modulo m2m_2 are completely independent constraints — like specifying an object's xx-coordinate and yy-coordinate separately. Bézout's identity gives us "basis vectors" that are 11 modulo one modulus and 00 modulo all the others, letting us build a solution directly as a linear combination.

Proof

We first prove the two-modulus case (k=2k=2) and then extend by induction. Since gcd⁡(m1,m2)=1\gcd(m_1,m_2)=1, Bézout's identity gives integers u,vu,v with m1u+m2v=1m_1 u + m_2 v = 1. Set x0=a1m2v+a2m1ux_0 = a_1 m_2 v + a_2 m_1 u.

Check modulo m1m_1: from m1u+m2v=1m_1 u + m_2 v = 1 we have m2v=1−m1u≡1(modm1)m_2 v = 1 - m_1 u \equiv 1 \pmod{m_1}, while m1u≡0(modm1)m_1 u \equiv 0 \pmod{m_1}, so x0≡a1⋅1+a2⋅0=a1(modm1)x_0 \equiv a_1\cdot 1 + a_2\cdot 0 = a_1 \pmod{m_1}.

Symmetrically, modulo m2m_2: m1u=1−m2v≡1(modm2)m_1 u = 1-m_2 v \equiv 1 \pmod{m_2} and m2v≡0(modm2)m_2 v \equiv 0 \pmod{m_2}, so x0≡a1⋅0+a2⋅1=a2(modm2)x_0 \equiv a_1\cdot 0 + a_2\cdot 1 = a_2 \pmod{m_2}. This proves existence for k=2k=2.

For uniqueness when k=2k=2: if xx and x′x' both solve the system, then x−x′≡0(modm1)x-x'\equiv0\pmod{m_1} and x−x′≡0(modm2)x-x'\equiv0\pmod{m_2}, i.e. both m1m_1 and m2m_2 divide d=x−x′d=x-x'. Multiply the Bézout equation m1u+m2v=1m_1 u+m_2 v=1 by dd: d=dm1u+dm2vd = d m_1 u + d m_2 v. Since m2∣dm_2\mid d, the first term dm1ud m_1 u is a multiple of m1m2m_1 m_2; since m1∣dm_1\mid d, the second term dm2vd m_2 v is also a multiple of m1m2m_1 m_2. Hence m1m2∣dm_1 m_2 \mid d, i.e. x≡x′(modm1m2)x\equiv x'\pmod{m_1 m_2}.

For general k>2k>2, induction: the first two congruences are equivalent (by the k=2k=2 case) to a single congruence modulo m1m2m_1 m_2; since m3m_3 is coprime to both m1m_1 and m2m_2, it is coprime to their product m1m2m_1 m_2, so we can combine again, and so on up to kk. Notice also that the explicit sum x≡∑i=1kaiMiyi(modM)x \equiv \sum_{i=1}^{k} a_i M_i y_i \pmod{M} works directly for any kk by the exact same reasoning: Mi=M/miM_i = M/m_i is coprime to mim_i so its inverse yiy_i exists, and every term j≠ij\neq i contains mim_i as a factor of MjM_j. ■\blacksquare

When gcd⁡(m1,m2)=1\gcd(m_1,m_2)=1, the map ψ(x mod m1m2)=(x mod m1, x mod m2)\psi(x \bmod m_1 m_2) = (x\bmod m_1,\ x\bmod m_2) is a ring isomorphism Z/(m1m2)Z≅(Z/m1Z)×(Z/m2Z)\mathbb{Z}/(m_1 m_2)\mathbb{Z} \cong (\mathbb{Z}/m_1\mathbb{Z})\times(\mathbb{Z}/m_2\mathbb{Z}), and restricting ψ\psi to invertible elements proves φ(m1m2)=φ(m1) φ(m2)\varphi(m_1 m_2) = \varphi(m_1)\,\varphi(m_2).

Why is it true?

This is the structural meaning of CRT: arithmetic modulo m1m2m_1 m_2 literally is two independent copies of modular arithmetic (one mod m1m_1, one mod m2m_2) running side by side. That is both why Euler's totient φ\varphi factors cleanly across coprime numbers, and why computers can speed up big-integer and cryptographic calculations by doing them in each small component separately.

Proof

First, ψ\psi is well-defined: if x≡x′(modm1m2)x\equiv x'\pmod{m_1 m_2} then m1m2∣(x−x′)m_1 m_2\mid(x-x'), so both m1m_1 and m2m_2 divide x−x′x-x', meaning x≡x′(modm1)x\equiv x'\pmod{m_1} and x≡x′(modm2)x\equiv x'\pmod{m_2}.

Second, ψ\psi respects addition and multiplication in each coordinate because reduction modulo mim_i does (proved in the first topic on congruences), so ψ\psi is a ring homomorphism.

Third, Theorem 1 above says precisely that for every pair (a1,a2)(a_1,a_2) there is an xx mapped to it (so ψ\psi is surjective) and that xx is unique modulo m1m2m_1 m_2 (so ψ\psi is injective). Hence ψ\psi is a bijection and therefore a ring isomorphism.

Finally, in a product ring R1×R2R_1\times R_2, an element (u1,u2)(u_1,u_2) has a multiplicative inverse (v1,v2)(v_1,v_2) iff u1v1=1u_1 v_1=1 in R1R_1 and u2v2=1u_2 v_2=1 in R2R_2 — that is, iff both coordinates are invertible. Since a ring isomorphism preserves invertibility, the invertible elements of Z/(m1m2)Z\mathbb{Z}/(m_1 m_2)\mathbb{Z} (of which there are φ(m1m2)\varphi(m_1 m_2)) correspond bijectively to pairs of invertible elements in (Z/m1Z)×(Z/m2Z)(\mathbb{Z}/m_1\mathbb{Z})\times(\mathbb{Z}/m_2\mathbb{Z}) (of which there are φ(m1) φ(m2)\varphi(m_1)\,\varphi(m_2)). Counting both sides gives φ(m1m2)=φ(m1) φ(m2)\varphi(m_1 m_2)=\varphi(m_1)\,\varphi(m_2). Combined with φ(pr)=pr−pr−1\varphi(p^r)=p^r-p^{r-1} for a prime power (where the non-coprime numbers are just the pr−1p^{r-1} multiples of pp), this immediately yields the general product formula for φ(n)\varphi(n) used in the previous topic. ■\blacksquare

AdvancedReal-World Applications and Worked Examples

Beyond classical puzzles, the Chinese remainder theorem is a workhorse of modern computation. Every production RSA implementation (OpenSSL, BoringSSL, hardware security modules) uses CRT-RSA (Quisquater–Couvreur, 1982) to compute cd mod pqc^d \bmod pq by computing modulo pp and modulo qq separately and recombining via CRT — roughly a 44-times speedup because modular exponentiation costs O((log⁡m)3)O((\log m)^3) and halving the bit-length of the modulus cuts the work per exponentiation by 23=82^3=8 (done twice, so 8/2=48/2=4 times faster overall). The same idea underlies Residue Number Systems (RNS) in fast big-integer libraries and homomorphic encryption, as well as astronomical and calendar cycle calculations.

Example: Solving Sunzi's original soldier puzzle

Find all integers xx satisfying x≡2(mod3)x\equiv2\pmod3, x≡3(mod5)x\equiv3\pmod5, and x≡2(mod7)x\equiv2\pmod7.

Solution

The moduli m1=3,m2=5,m3=7m_1=3,m_2=5,m_3=7 are pairwise coprime with product M=3×5×7=105M = 3\times5\times7 = 105, so the explicit CRT formula applies with M1=105/3=35M_1 = 105/3 = 35, M2=105/5=21M_2 = 105/5 = 21, M3=105/7=15M_3 = 105/7 = 15.

Find each inverse yiy_i of MiM_i modulo mim_i: (1) 35≡2(mod3)35 \equiv 2 \pmod 3, and 2×2=4≡1(mod3)2\times2=4\equiv1\pmod3, so y1=2y_1=2; (2) 21≡1(mod5)21 \equiv 1 \pmod 5, so y2=1y_2=1; (3) 15≡1(mod7)15 \equiv 1 \pmod 7, so y3=1y_3=1.

Substitute into x≡∑i=1kaiMiyi(modM)x \equiv \sum_{i=1}^{k} a_i M_i y_i \pmod{M} with (a1,a2,a3)=(2,3,2)(a_1,a_2,a_3)=(2,3,2): x≡2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233(mod105)x \equiv 2\cdot35\cdot2 + 3\cdot21\cdot1 + 2\cdot15\cdot1 = 140 + 63 + 30 = 233 \pmod{105}.

Since 233=2×105+23233 = 2\times105 + 23, reducing modulo 105105 gives x≡23(mod105)x \equiv 23 \pmod{105}. Quick check: 23=3×7+2≡2(mod3)23 = 3\times7+2\equiv2\pmod3, 23=5×4+3≡3(mod5)23=5\times4+3\equiv3\pmod5, 23=7×3+2≡2(mod7)23=7\times3+2\equiv2\pmod7. All three hold, and the smallest positive solution is **2323**.

Example: Fast RSA decryption via CRT (CRT-RSA)

In the RSA example from the previous topic (p=5,q=11,n=55,d=27p=5,q=11,n=55,d=27, ciphertext c=8c=8), compute m=827 mod 55m = 8^{27} \bmod 55 by splitting it into two tiny calculations modulo 55 and modulo 1111 instead of working modulo 5555.

Solution

Modulo p=5p=5: reduce the base 8≡3(mod5)8\equiv3\pmod5, and reduce the exponent d=27d=27 modulo p−1=4p-1=4 using Fermat's little theorem (27=4×6+327 = 4\times6+3, so dp=3d_p = 3). Then mp=33=27≡2(mod5)m_p = 3^3 = 27 \equiv 2 \pmod 5 — a single tiny cube!

Modulo q=11q=11: the base is 88, and reduce the exponent 2727 modulo q−1=10q-1=10 using Fermat (27=10×2+727 = 10\times2+7, so dq=7d_q = 7). Compute 87 mod 118^7 \bmod 11: since 8≡−3(mod11)8\equiv-3\pmod{11}, we have 82≡9≡−28^2\equiv9\equiv-2, 84≡48^4\equiv4, 87=84⋅82⋅8≡4⋅(−2)⋅(−3)=24≡2(mod11)8^7 = 8^4\cdot8^2\cdot8\equiv 4\cdot(-2)\cdot(-3)=24\equiv2\pmod{11}.

Now recombine m≡2(mod5)m\equiv2\pmod5 and m≡2(mod11)m\equiv2\pmod{11} by CRT: since both remainders happen to be 22, the unique solution modulo 5555 is immediately m≡2(mod55)m\equiv2\pmod{55} (in general, with different mp,mqm_p,m_q, one applies the two-modulus CRT formula once). Notice we never squared a number larger than 1111 and never used an exponent larger than 77 — for 20482048-bit RSA primes this same split cuts decryption time to roughly a quarter.

ResearchCRT at the research frontier: fast arithmetic, fault attacks, and lattice cryptography

Find the smallest non-negative integer xx satisfying x≡1(mod3)x\equiv1\pmod3 and x≡2(mod5)x\equiv2\pmod5.

If m1=4,m2=9,m3=25m_1=4,m_2=9,m_3=25, the solution to a CRT system with these moduli is unique modulo:

What can you say about the system x≡1(mod4)x\equiv1\pmod4 and x≡0(mod6)x\equiv0\pmod6?

Why does CRT speed up RSA decryption cd mod pqc^d\bmod pq by roughly 44 times?

References

  1. Jean-Jacques Quisquater, Christophe Couvreur (1982). Fast decipherment algorithm for RSA public-key cryptosystem · DOI:10.1049/el:19820617
  2. David Harvey, Joris van der Hoeven (2021). Integer multiplication in time O(n log n) · DOI:10.4007/annals.2021.193.2.4