Chinese Remainder Theorem

Definition: A residue is the remainder of a division by a number called a modulus (i.e residue of 5/7 is 2).

Definition: A residue representation of a number x is the set of residues {r1, r2, …, rk} with respect to moduli {m1, m2, …, mk}.

If we use the residue representation {r1,r2,…, rk} of x, the Chinese remainder theorem makes it possible to determine |x| provided the greatest common factor of any pair of moduli is 1 (i.e (ri, rj) = 1 , i j). Such moduli are known as pairwise relatively prime(M. Shahkarami and G.A. Jullien, 2002, University of Calgary).

For the case of RSA cryptography, we are only interested in having 2 factors, commonly called p and q. Their product is commonly called n = p × q.

In this case the Chinese Remainder Theorem says that if we know the residues:
            Mp = M mod p
            Mq = M mod q
       we can calculate:
            M mod n = Mp × yq × q   +   Mq × yp × p
       where n is the modulus p×q, and
            (yq × q) mod p = 1;
yq is often called the inverse of q modulo p, and is thus often written (q-1 mod p)

We can prove that this value of M has the correct residues.
            M mod p = (Mp × yq × q) mod p        because the second term is a multiple of p.
                           = (Mp mod p) × ( (yq × q) mod p)
                           = Mp
       and similarly for Mq.

It only remains to prove that there is only one value of M mod n.

Imagine that there are two values of M, M0 and M1, each less than n.
            M0 mod p = Mp
       and
            M1 mod p = Mp
       Thus:
            ( M1M0 ) mod p = 0

In other words M1M0 is divisible by p, and by a similar argument it is also divisible by q. Because p and q are prime, this means that M1M0 is divisible by n. The only number less than n that is divisible by n is 0. therefore:
            M1 = M0
       i.e. there is only one solution.

In General

The Chinese Remainder Theorem is actually valid for many values, as hinted in the very first few paragraphs. We can express this as:
            x mod p1 = a1
            x mod p2 = a2
            x mod p3 = a3
            . . . . . . . . . . . . . .
            x mod pk = ak
The solution is:
            x  =  a1 P1 y1 + a2 P2 y2 + . . . . . . . . . .
                =  S   ak Pk yk

Pk is the product of all the pi values, except for pk, and yk is the inverse of Pk modulo pk, i.e.
            ( Pk yk ) mod pk = 1

We can see that x is a solution by calculating   x mod pk   using the formula above. Only the single term
            x mod pk  =  ( ak Pk yk ) mod pk
                   survives, because all the other values of Pi contain the factor pk.

The definition of Pk and yk above, lead to the desired result. We do need to be confident that we can always find a value yk. This is the case for prime number values, and in fact for any set of values for which no two nmbers have a common factor.

A Party Trick

Ask someone to take their age calculate the remainder on division by 3. Then calculate the remainder on division by 5, and also by 7, to give three numbers, a, b and c.

You can then calculate the person's age as:
            ( a × 70  +  b × 21  +  c × 15 ) mod 105

For a 35-year-old we have a = 2, b = 0, c = 0. 70 × 2 = 140. Subtract 105 and the result is 35.

One year later we have a = 0, b = 1, c = 1. 21 + 15 = 36.

People older than 104 can usually be distinguished from young children without difficulty.