Chinese Remainder Theorem
- •
What it is
- •
Given a system of congruences with pairwise coprime moduli, there's a unique solution modulo the product of those moduli.
- •
- •
When to apply
- •
You're given for coprime and need to recombine them into — also a building block for attacks like Håstad's broadcast attack.
- •
- •
Symbols and assumptions
- •
means divides . Here the positive moduli are pairwise coprime; .
- •
- •
Math — step by step
- •
- For each modulus, set . This is divisible by every other modulus, so its contribution will be zero in all the other congruences.
- •
- Compute . Now is 1 modulo its own modulus and 0 modulo all the others. It acts like a switch selecting one remainder.
- •
- Add the desired remainders times those switches: . Reducing modulo any leaves only .
- •
- Any two solutions differ by a multiple of every , hence by a multiple of . For non-coprime moduli, first require compatible remainders modulo each gcd; uniqueness is modulo the lcm instead.
- •
- •
- •
Python
- •
from sympy.ntheory.modular import crt x, mod = crt(moduli, residues) # sagemath also has crt(residues, moduli)
- •
- •
SageMath
- •
crt([2, 3, 5], [5, 11, 17]) # crt(residues, moduli). result is 872
- •
- •
- •
- •
Archived notes
Linked references 8
- Cayley-Hamilton Reduction of Matrix Exponentiation to Polynomial Coefficients
- Composite-Modulus Key Exchange Break (CRT Projection Attack)
- cryptohack
- Extended Euclidean Algorithm
- Hastad's Broadcast Attack
- Matrix Discrete Log via Characteristic Polynomial Factorization (Extension Fields + CRT)
- Pohlig-Hellman Algorithm
- RSA (Textbook Construction)