← Back to Blog

Modular Arithmetic and the Chinese Remainder Theorem

Congruence

Congruence modulo is a relation. When two integers aa and bb leave the same remainder after being divided by the same positive integer nn (the modulus), we say aa and bb are congruent modulo nn, written as

a≡b (mod n)a \equiv b \ (\text{mod } n)

For example

11≡5 (mod 3)11 \equiv 5 \ (\text{mod } 3)

38≡14 (mod 12)38 \equiv 14 \ (\text{mod } 12)

It works for negative numbers too, for example

−8≡7 (mod 5)-8 \equiv 7 \ (\text{mod } 5)

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 mod 7\text{mod } 7, all natural numbers fall into 77 groups

RemainderNumbers
11, 8, 15, ...
22, 9, 16, ...
33, 10, 17, ...
44, 11, 18, ...
55, 12, 19, ...
66, 13, 20, ...
07, 14, 21, ...

When we see a number like 131231312313131131231312313131, it feels unfamiliar. But if we know it leaves a remainder of 22 when divided by 77, that is, 131231312313131≡2 (mod 7)131231312313131 \equiv 2 \ (\text{mod } 7), then we know it's "in the same group as 22, 99, 1616, and so on", which at least gives us a basic feel for it.

Addition Property

If

a≡b (mod n)a \equiv b \ (\text{mod } n)

c≡d (mod n)c \equiv d \ (\text{mod } n)

then

a+c≡b+d (mod n)a + c \equiv b + d \ (\text{mod } n)

Multiplication Property

If

a≡b (mod n)a \equiv b \ (\text{mod } n)

c≡d (mod n)c \equiv d \ (\text{mod } n)

then

a×c≡b×d (mod n)a \times c \equiv b \times d \ (\text{mod } n)

From this we can further derive that if a≡b (mod n)a \equiv b \ (\text{mod } n), then

ak≡bk (mod n)a^k \equiv b^k \ (\text{mod } n)

ma≡mb (mod n)ma \equiv mb \ (\text{mod } n)

Examples

  1. 4096≡ ? (mod 13)4096 \equiv \ ? \ (\text{mod } 13)
  2. 2100+250≡ ? (mod 13)2^{100} + 2^{50} \equiv \ ? \ (\text{mod } 13)

Ans.

1.

4096=13×315+1≡1 (mod 13)4096 = 13 \times 315 + 1 \equiv 1 \ (\text{mod } 13)

2.

212=4096≡1 (mod 13)2^{12} = 4096 \equiv 1 \ (\text{mod } 13)

2100=(212)8×24≡18×16=16≡3 (mod 13)2^{100} = \left(2^{12}\right)^8 \times 2^4 \equiv 1^8 \times 16 = 16 \equiv 3 \ (\text{mod } 13)

250=(212)4×22≡14×4=4 (mod 13)2^{50} = \left(2^{12}\right)^4 \times 2^2 \equiv 1^4 \times 4 = 4 \ (\text{mod } 13)

2100+250≡3+4=7 (mod 13)2^{100} + 2^{50} \equiv 3 + 4 = 7 \ (\text{mod } 13)

Modular Inverse

If

ab≡1 (mod n)ab \equiv 1 \ (\text{mod } n)

then bb is called the modular multiplicative inverse of aa, which can also be written as

a−1≡b (mod n)a^{-1} \equiv b \ (\text{mod } n)

For example, under mod 11\text{mod } 11, the modular inverses of 33 are { 4+11z∣z∈Z }\{\, 4 + 11z \mid z \in \mathbb{Z} \,\}, that is, {…,−18,−7,4,15,26,…}\{\ldots, -18, -7, 4, 15, 26, \ldots\}, because 3×4=12≡1 (mod 11)3 \times 4 = 12 \equiv 1 \ (\text{mod } 11).

aa has a modular inverse modulo nn   ⟺  \iff aa and nn are coprime. The modular inverse can be found with the Extended Euclidean algorithm, or with Fermat's little theorem, covered below:

ap−2≡a−1 (mod p)a^{p-2} \equiv a^{-1} \ (\text{mod } p)

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 pp is a prime and aa is not a multiple of pp, then

ap−1≡1 (mod p)a^{p-1} \equiv 1 \ (\text{mod } p)

or, in another form

ap≡a (mod p)a^p \equiv a \ (\text{mod } p)

The 212≡1 (mod 13)2^{12} \equiv 1 \ (\text{mod } 13) in the example above is exactly the case a=2a = 2, p=13p = 13.


The following phenomenon gives a feel for what Fermat's little theorem means. Divide the powers of 22 by 1313 one by one

21,22,…,212≡2,4,8,3,6,12,11,9,5,10,7,1 (mod 13)2^1, 2^2, \ldots, 2^{12} \equiv 2, 4, 8, 3, 6, 12, 11, 9, 5, 10, 7, \mathbf{1} \ (\text{mod } 13)

213,214,…,224≡2,4,8,3,6,12,11,9,5,10,7,1 (mod 13)2^{13}, 2^{14}, \ldots, 2^{24} \equiv \htmlClass{math-highlight}{2, 4, 8, 3, 6, 12, 11, 9, 5, 10, 7, \mathbf{1}} \ (\text{mod } 13)

225,226,…,236≡2,4,8,3,6,12,11,9,5,10,7,1 (mod 13)2^{25}, 2^{26}, \ldots, 2^{36} \equiv \htmlClass{math-highlight}{2, 4, 8, 3, 6, 12, 11, 9, 5, 10, 7, \mathbf{1}} \ (\text{mod } 13)

After returning to 11 at the 1212th power, multiplying by 22 once more brings us back to 22, 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 pp cycle like a clock, completing one lap every p−1p - 1 steps.

So when computing ak (mod p)a^k \ (\text{mod } p), we only need to look at the remainder of kk divided by p−1p - 1, just like a clock only cares about the hour divided by 1212. For example, 100=12×8+4100 = 12 \times 8 + 4, so 2100≡24 (mod 13)2^{100} \equiv 2^4 \ (\text{mod } 13).

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 23+105n23 + 105n.

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

N=2×70+3×21+2×15=233N = 2 \times 70 + 3 \times 21 + 2 \times 15 = 233

then

N≡2×1+3×0+2×0=2 (mod 3)N \equiv 2 \times 1 + 3 \times 0 + 2 \times 0 = 2 \ (\text{mod } 3)

N≡2×0+3×1+2×0=3 (mod 5)N \equiv 2 \times 0 + 3 \times 1 + 2 \times 0 = 3 \ (\text{mod } 5)

N≡2×0+3×0+2×1=2 (mod 7)N \equiv 2 \times 0 + 3 \times 0 + 2 \times 1 = 2 \ (\text{mod } 7)

Each term only leaves a remainder under its own modulus and is 00 under the others, so the three conditions can be handled separately and then added together.

Finally, since 105105 leaves remainder 00 when divided by 33, 55, or 77, subtracting multiples of 105105 doesn't change any of the remainders, so 233−2×105=23233 - 2 \times 105 = 23.

15, 21, and 70 as basis vectors (this one uses remainders 1, 2, 6) (YouTube: 漫士沉思录 Meditation Math)
15, 21, and 70 as basis vectors (this one uses remainders 1, 2, 6) (YouTube: 漫士沉思录 Meditation Math)

Reference