Path: blob/main/foundations/04-number-theory-rsa/sage/04d-chinese-remainder-theorem.ipynb
483 views
The Chinese Remainder Theorem
Module 04d | Number Theory and RSA
One equation is a constraint. Two simultaneous equations are a superpower.
Question: A number leaves remainder 2 when divided by 3 and remainder 3 when divided by 5. What is the number? Is there exactly one answer, or could there be many?
This puzzle is over 1,500 years old, it appears in the 3rd-century Chinese text Sunzi Suanjing. The answer unlocks one of the most useful theorems in all of cryptography: a way to split a hard problem into easier pieces, solve each piece separately, and then glue the solutions back together.
Objectives
By the end of this notebook you will be able to:
Solve systems of simultaneous congruences by hand and with SageMath's
CRT()andCRT_list()Construct the CRT solution using the extended GCD (connecting back to notebook 04b)
Explain the CRT isomorphism and verify it preserves both addition and multiplication
Apply CRT to RSA decryption (RSA-CRT speedup) and recognize its role in Pohlig-Hellman
Prerequisites
Completion of The Extended Euclidean Algorithm, you will need
xgcd()and Bezout's identityCompletion of Euler's Totient and Fermat's Theorem, you will need
euler_phi()andpower_mod()Comfort with modular arithmetic from Module 01
The Puzzle: Solving Two Congruences at Once
Let's start with the motivating question. We want to find such that:
The brute-force approach: just try numbers until both conditions are satisfied.
We found in the range , and in . Notice that . The solutions are , they form a single residue class modulo .
So the answer to our motivating question is: the number is 8 (mod 15), and the answer is unique modulo 15.
This is not a coincidence. It is a theorem.
The Chinese Remainder Theorem (Statement)
Theorem (CRT). Let and be positive integers with . For any integers and , the system
has a unique solution modulo .
The two key ingredients:
Existence, a solution always exists
Uniqueness, there is exactly one solution in
Misconception alert: "CRT works for any pair of moduli." No! The moduli must be coprime (). If , the system may have no solution (e.g., and ) or multiple solutions modulo . We will see a concrete failure case below.
Constructing the Solution with Extended GCD
Bridge from 04b: In the extended Euclidean algorithm notebook, we learned that when , Bezout's identity gives us integers such that
This is exactly what we need to construct the CRT solution. Here is the recipe:
Compute with
xgcd(m, n)so that .The solution is: .
Why does this work? Reduce modulo : since , we have , so . Similarly, .
Checkpoint: Before running the next cell, try the construction by hand for , , , . You need and such that . Can you find ? Then .
SageMath's Built-in CRT
SageMath provides CRT(a, b, m, n) for two congruences and CRT_list(remainders, moduli) for any number of simultaneous congruences. Let's verify our hand computation and then try a larger example.
Generalization to Multiple Moduli
The CRT generalizes naturally. Given pairwise coprime moduli (meaning for all ), the system
has a unique solution modulo .
Misconception alert: "Pairwise coprime" is stronger than just "all share no common factor." For example, have , but they are not pairwise coprime since . CRT would not apply to this triple.
CRT as an Isomorphism
The CRT is much more than a system-solver. It tells us that two apparently different algebraic structures are the same thing in disguise:
The isomorphism sends to the pair . This is a ring isomorphism, it preserves both addition and multiplication.
Let's build the complete mapping for .
Verifying: It Preserves Addition AND Multiplication
A ring isomorphism must satisfy and , where operations on the left happen in and operations on the right happen component-wise in .
Checkpoint: Pick any two elements, say and in . Predict: what is ? Does it equal component-wise?
Visualizing: Addition Tables Match
To make the isomorphism tangible, let's display the addition table of side-by-side with the component-wise addition in . We'll use a small subset to keep it readable.
Application: RSA-CRT (4x Speedup)
Crypto foreshadowing: In RSA, we compute where . Since , the CRT isomorphism tells us:
Instead of one expensive exponentiation mod (a large number), we can compute:
, exponentiation mod (half the bits!)
, exponentiation mod (half the bits!)
Combine:
Since modular exponentiation cost is roughly cubic in the number of bits, halving the modulus size gives roughly a cost, a 4x speedup.
Let's see it in action with a toy RSA example (and then measure a real speedup).
Connection: CRT in Pohlig-Hellman
Bridge to Module 06 (DLP/DH): The Pohlig-Hellman algorithm solves the discrete logarithm problem in a group of composite order by:
Solving the DLP in each prime-power subgroup of order (much easier!)
Combining the results with CRT to recover the full discrete log modulo
This is the same "split and recombine" strategy as RSA-CRT, but applied to a different problem. The CRT is the glue.
Here is a miniature example: solve where .
Exercises
Exercise 1 (Fully Worked)
Problem: Find such that and .
Solution:
Check: (both prime, so coprime). CRT applies.
Find Bezout coefficients: .
Construct: .
Exercise 2 (Guided with TODOs)
Problem: Solve the system , , .
Steps:
Verify the moduli are pairwise coprime.
Use
CRT_list()to find the solution.Verify by checking each congruence.
What is the solution modulo? (What is ?)
Exercise 3 (Independent)
Problem: Implement your own CRT solver from scratch (without using SageMath's CRT() or CRT_list()), using only xgcd().
Your function my_crt(a, b, m, n) should:
Check that (raise an error if not)
Use
xgcd()to find Bezout coefficientsReturn the unique solution modulo
Then extend it to my_crt_list(remainders, moduli) that handles arbitrarily many congruences by applying the two-modulus version iteratively.
Test both functions against SageMath's built-in CRT() and CRT_list().
Summary
| Concept | Key idea |
|---|---|
| CRT existence and uniqueness | When , the system , has exactly one solution modulo |
| Construction via extended GCD | Bezout coefficients from xgcd() (notebook 04b) directly give the CRT solution formula |
| Ring isomorphism | preserves both addition and multiplication, which is why RSA can "split" computations |
| RSA-CRT speedup | Decrypt mod and mod separately, then combine. Each half-size exponentiation is roughly 8x cheaper, giving about a 4x speedup overall |
| Pohlig-Hellman connection | Solve the DLP in small prime-power subgroups, then recombine with CRT. The same "split and recombine" pattern |
Crypto foreshadowing: In the next notebook, we'll generate RSA keys. The CRT isomorphism is why RSA works: the structure of for is secretly controlled by , and only someone who knows and can exploit this.
Next: RSA Key Generation