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 be pairwise coprime and . For any the system
has a solution, and it is unique modulo .
Construction. Let . Since , there is . Then
works, because and for .
Uniqueness. Two solutions differ by a number divisible by every , hence (pairwise coprime) by .
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 is a bijection that respects addition and multiplication. So you can compute componentwise in small rings and convert back once at the end.
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 and modulo 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 . Garner's algorithm builds the answer digit by digit in a mixed-radix form, , using only inverses modulo small numbers. RSA-CRT uses its two-modulus case: .
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: and is solvable iff , and then it is unique modulo . For example and agree modulo (both residues are odd), so the system is solvable: .
In software
- RSA private-key operations use CRT with and (about 3 to 4 times faster).
- Håstad's broadcast attack: the same message sent with to three recipients gives modulo by CRT, and since an ordinary cube root recovers (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 , .
Answer
.
Solution
and , so and (). Then . Check: .
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 : modulo 3, ; modulo 4, ; modulo 5, . So , giving below 100.
Exercise 3: Compatibility (code)
Which of these systems are solvable? (a) , ; (b) , . Solve the solvable one by search.
Answer
(a) ; (b) none (1 is odd, 2 is even).
Solution
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 are pairwise coprime, a system of congruences modulo them has…
- no solution in general
- a unique solution modulo
- exactly three solutions
- a solution only if all residues are equal
RSA uses the CRT to…
- choose
- speed up private-key operations by working modulo and
- generate primes
- pad messages
and is…
- solvable modulo 12
- solvable modulo 24
- unsolvable
- solvable with