Modular Arithmetic and the Chinese Remainder Theorem
viewsCongruence
Congruence modulo is a relation. When two integers and leave the same remainder after being divided by the same positive integer (the modulus), we say and are congruent modulo , written as
For example
It works for negative numbers too, for example
In math and cryptography, we often have to deal with some "huge numbers"
Facing a huge number you've never seen before can be intimidating — you don't know where to start. Congruence lets us sort big numbers into groups. For example, with , all natural numbers fall into groups
| Remainder | Numbers |
|---|---|
| 1 | 1, 8, 15, ... |
| 2 | 2, 9, 16, ... |
| 3 | 3, 10, 17, ... |
| 4 | 4, 11, 18, ... |
| 5 | 5, 12, 19, ... |
| 6 | 6, 13, 20, ... |
| 0 | 7, 14, 21, ... |
When we see a number like , it feels unfamiliar. But if we know it leaves a remainder of when divided by , that is, , then we know it's "in the same group as , , , and so on", which at least gives us a basic feel for it.
Addition Property
If
then
Multiplication Property
If
then
From this we can further derive that if , then
Examples
Ans.
1.
2.
Modular Inverse
If
then is called the modular multiplicative inverse of , which can also be written as
For example, under , the modular inverses of are , that is, , because .
has a modular inverse modulo and are coprime. The modular inverse can be found with the Extended Euclidean algorithm, or with Fermat's little theorem, covered below:
Fermat's Little Theorem
Fermat has two famous theorems. This is the one about mod; the other is about an equation having no integer solutions (the one where he said the margin was too small to contain the proof).
If is a prime and is not a multiple of , then
or, in another form
The in the example above is exactly the case , .
The following phenomenon gives a feel for what Fermat's little theorem means. Divide the powers of by one by one
After returning to at the th power, multiplying by once more brings us back to , and the whole sequence of remainders starts over. What Fermat's little theorem really says is that the remainders of powers divided by a prime cycle like a clock, completing one lap every steps.
So when computing , we only need to look at the remainder of divided by , just like a clock only cares about the hour divided by . For example, , so .
Chinese Remainder Theorem
Legend has it that General Han Xin wanted to know how many soldiers he had, so he had them line up in rows of three, five, and seven, and found 2, 3, and 2 soldiers left over respectively.
We can list all the numbers that leave remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7:
- Remainder 2 mod 3: 2, 5, 8, 11, 14, 17, 20, 23...
- Remainder 3 mod 5: 3, 8, 13, 18, 23, 28...
- Remainder 2 mod 7: 2, 9, 16, 23...
The common number is 23, but if we keep going, we may run into more numbers that match. To find them, add the least common multiple of 3, 5, and 7, which is 105, to 23. So all possible numbers are .
Listing is one approach, but Suanfa Tongzong gives another solution:
Three people walking together, seventy is rare; five plum trees, twenty-one branches; seven children reunite at the half-month; subtract one hundred and five and you'll know
It means: multiply the remainder mod 3 by 70, the remainder mod 5 by 21, and the remainder mod 7 by 15, add them up, then take the remainder mod 105 to get the smallest value, like 23.
We know 105 is the least common multiple of 3, 5, and 7. 70 leaves remainder 1 when divided by 3, and remainder 0 when divided by 5 and 7. Likewise, 21 leaves remainder 1 mod 5, and 15 leaves remainder 1 mod 7.
Let's plug in Han Xin's example (remainders 2, 3, 2). By the multiplication and addition properties, let
then
Each term only leaves a remainder under its own modulus and is under the others, so the three conditions can be handled separately and then added together.
Finally, since leaves remainder when divided by , , or , subtracting multiples of doesn't change any of the remainders, so .
