TExcelTAD
Home › Number Theory & Cryptography › The Chinese Remainder Theorem

The Chinese Remainder Theorem

A number is determined by its remainders modulo several coprime moduli. This 1700-year-old observation (from Sunzi's Mathematical Manual) lets RSA decrypt four times faster, lets computer algebra split big computations into small ones, and powers one of the classic attacks on textbook RSA.

Statement

Let n1,…,nkn_1, \ldots, n_k be pairwise coprime and N=n1n2⋯nkN = n_1 n_2 \cdots n_k. For any a1,…,aka_1, \ldots, a_k the system

x≡a1(modn1),…,x≡ak(modnk)x \equiv a_1 \pmod{n_1}, \quad \ldots, \quad x \equiv a_k \pmod{n_k}

has a solution, and it is unique modulo NN.

Construction. Let Ni=N/niN_i = N / n_i. Since gcd⁡(Ni,ni)=1\gcd(N_i, n_i) = 1, there is Mi=Ni−1 mod niM_i = N_i^{-1} \bmod n_i. Then

x=∑i=1kaiNiMi mod Nx = \sum_{i=1}^{k} a_i N_i M_i \bmod N

works, because NiMi≡1(modni)N_i M_i \equiv 1 \pmod{n_i} and NiMi≡0(modnj)N_i M_i \equiv 0 \pmod{n_j} for j≠ij \ne i.

Uniqueness. Two solutions differ by a number divisible by every nin_i, hence (pairwise coprime) by NN.

python · try it yourself
from math import prod

def crt(residues, moduli):
    N = prod(moduli)
    x = 0
    for a, n in zip(residues, moduli):
        Ni = N // n
        x += a * Ni * pow(Ni, -1, n)
    return x % N, N

# Sunzi's puzzle: remainder 2 mod 3, 3 mod 5, 2 mod 7
print(crt([2, 3, 2], [3, 5, 7]))
x, N = crt([1, 2, 3, 4], [5, 7, 9, 11])
print(x, N, [x % n for n in (5, 7, 9, 11)])
(23, 105)
1731 3465 [1, 2, 3, 4]

A ring isomorphism

The CRT says more: the map x↦(x mod n1,…,x mod nk)x \mapsto (x \bmod n_1, \ldots, x \bmod n_k) is a bijection Z/NZ→Z/n1×⋯×Z/nk\mathbb{Z}/N\mathbb{Z} \to \mathbb{Z}/n_1 \times \cdots \times \mathbb{Z}/n_k that respects addition and multiplication. So you can compute componentwise in small rings and convert back once at the end.

python · try it yourself
n1, n2 = 7, 11
x, y = 40, 59
to_pair = lambda v: (v % n1, v % n2)
px, py = to_pair(x), to_pair(y)
prod_pair = (px[0] * py[0] % n1, px[1] * py[1] % n2)
print(prod_pair, to_pair(x * y % 77))
(1, 6) (1, 6)

This "residue number system" view is how RSA-CRT decryption works: exponentiate modulo pp and modulo qq separately, each with half-size numbers, then recombine (RSA Encryption, Decryption and CRT Speed-Up).

Garner's algorithm

The sum formula needs the big products NiN_i. Garner's algorithm builds the answer digit by digit in a mixed-radix form, x=v1+v2n1+v3n1n2+⋯x = v_1 + v_2 n_1 + v_3 n_1 n_2 + \cdots, using only inverses modulo small numbers. RSA-CRT uses its two-modulus case: x=xq+q⋅((xp−xq) q−1 mod p)x = x_q + q \cdot \left( (x_p - x_q)\, q^{-1} \bmod p \right).

python · try it yourself
p, q = 1009, 2003
xp, xq = 123, 456
h = (xp - xq) * pow(q, -1, p) % p
x = xq + q * h
print(x, x % p, x % q)
449128 123 456

Non-coprime moduli

If the moduli share factors, a solution exists only if the congruences agree on the common part: x≡a1(modn1)x \equiv a_1 \pmod{n_1} and x≡a2(modn2)x \equiv a_2 \pmod{n_2} is solvable iff a1≡a2(modgcd⁡(n1,n2))a_1 \equiv a_2 \pmod{\gcd(n_1, n_2)}, and then it is unique modulo lcm⁡(n1,n2)\operatorname{lcm}(n_1, n_2). For example x≡3(mod6)x \equiv 3 \pmod 6 and x≡5(mod8)x \equiv 5 \pmod{8} agree modulo gcd⁡(6,8)=2\gcd(6, 8) = 2 (both residues are odd), so the system is solvable: x≡21(mod24)x \equiv 21 \pmod{24}.

In software

  • RSA private-key operations use CRT with pp and qq (about 3 to 4 times faster).
  • Håstad's broadcast attack: the same message sent with e=3e = 3 to three recipients gives m3m^3 modulo n1n2n3n_1 n_2 n_3 by CRT, and since m3<n1n2n3m^3 < n_1 n_2 n_3 an ordinary cube root recovers mm (Why Textbook RSA Is Insecure).
  • Number-theoretic transforms for big-number multiplication run modulo several word-sized primes and combine the results with CRT.

Exercises

Exercise 1: By hand

Solve x≡2(mod5)x \equiv 2 \pmod 5, x≡3(mod7)x \equiv 3 \pmod 7.

Answer

x≡17(mod35)x \equiv 17 \pmod{35}.

Solution

x=2+5tx = 2 + 5t and 2+5t≡3(mod7)2 + 5t \equiv 3 \pmod 7, so 5t≡15t \equiv 1 and t≡3t \equiv 3 (15=14+115 = 14 + 1). Then x=17x = 17. Check: 17=3⋅5+2=2⋅7+317 = 3 \cdot 5 + 2 = 2 \cdot 7 + 3.

Exercise 2: Eggs in a basket

A basket has fewer than 100 eggs. Counted by 3s, 1 is left; by 4s, 2 are left; by 5s, 3 are left. How many eggs?

Answer
Solution

All three say x≡−2x \equiv -2: modulo 3, 1≡−21 \equiv -2; modulo 4, 2≡−22 \equiv -2; modulo 5, 3≡−23 \equiv -2. So x≡−2(mod60)x \equiv -2 \pmod{60}, giving x=58x = 58 below 100.

Exercise 3: Compatibility (code)

Which of these systems are solvable? (a) x≡3(mod6)x \equiv 3 \pmod 6, x≡5(mod8)x \equiv 5 \pmod 8; (b) x≡1(mod6)x \equiv 1 \pmod 6, x≡2(mod4)x \equiv 2 \pmod 4. Solve the solvable one by search.

Answer

(a) x≡21(mod24)x \equiv 21 \pmod{24}; (b) none (1 is odd, 2 is even).

Solution
python · try it yourself
print([x for x in range(24) if x % 6 == 3 and x % 8 == 5])
print([x for x in range(12) if x % 6 == 1 and x % 4 == 2])
[21]
[]

Test yourself

If n1,n2,n3n_1, n_2, n_3 are pairwise coprime, a system of congruences modulo them has…

  1. no solution in general
  2. a unique solution modulo n1n2n3n_1 n_2 n_3
  3. exactly three solutions
  4. a solution only if all residues are equal

RSA uses the CRT to…

  1. choose ee
  2. speed up private-key operations by working modulo pp and qq
  3. generate primes
  4. pad messages

x≡1(mod4)x \equiv 1 \pmod 4 and x≡2(mod6)x \equiv 2 \pmod 6 is…

  1. solvable modulo 12
  2. solvable modulo 24
  3. unsolvable
  4. solvable with x=2x = 2