Try   HackMD

Chinese Remainder Theorem (CRT)

The CRT states that if you have

M=∏i=0nβˆ’1mi and
mi
are all co-prime, then every number mod
M
is uniquely represented by its remainders mod
mi
.

Example

Suppose

M=420 and
{mi}={5,7,12}
.

37=2mod537=2mod737=1mod12

This is the only number (mod 420) for which the statements above are true.

Residue Number System

Extending the example above, we can represent

37 as
(2,2,1)
as a shorthand.

We can then do operations on numbers in this representation, e.g:

(2,2,1)2=(22,22,12)=(4,4,1)

After doing as much work as we want in this number system, we can convert back to a regular mod

420 number.

Consider that if we knew the values of these three special numbers:

b0=(1,0,0)b1=(0,1,0)b2=(0,0,1)

Then we could easily compute:

(4,4,1)=4βˆ—(1,0,0)+4βˆ—(0,1,0)+1βˆ—(0,0,1)

Taking

b0=(1,0,0) as an example, this number must be
0
under modulo
7
and
12
, and is therefore a multiple of
7βˆ—12=84
:

b0=(1,0,0)=84x

Considering the mod

5 component (the
1
in
(1,0,0)
), we have:

84x=14x=1x=4βˆ’1x=4mod5

(

4βˆ’1=4 can be found using the Extended Euclidean Algorithm.)

Therefore:

b0=84x=84βˆ—4=336=βˆ’84

Following the same method, we obtain:

b0=βˆ’84b1=120b2=βˆ’35

And:

(4,4,1)=4βˆ—(βˆ’84)+4βˆ—120+1βˆ—(βˆ’35)=4βˆ—(120βˆ’84)βˆ’35=109mod420

We can verify this matches the traditional calculation:

372=1369=109mod420

Further Reading