This is the fifth post in a series of posts on the Chinese remainder theorem (CRT). When solving a congruence equation with a composite modulus, it is often easier to convert the problem to one of solving several congruence equations with smaller moduli that are primes or powers of primes. Then combine the individual solutions using the Chinese remainder theorem. In this post, we demonstrate this process for modular exponentiation where the exponent and the modulus are large. Given , the algorithm discussed here is to produce a system of equations with smaller moduli and exponents that give the same answer as for the original problem.
The previous posts in the series on CRT: first post; second post; third post; fourth post
____________________________________________________________________________
Preliminary discussion
The exponentiation is usually programmed using the fast powering algorithm. The CRT method will convert the exponentiation to several exponentiations that involve much smaller exponents and moduli, thus greatly reducing the calculation time, in particular speeding up the fast powering algorithm. One application of the CRT method is to improve the run time of the decryption process in the RSA algorithm (up to four times faster).
As already mentioned, the goal of the algorithm discussed here is to produce an equivalent system of linear congruence equations. Once this system is produced, the Chinese remainder theorem only guarantees a solution and does not actually produce a solution. So we need to know how to Chinese remainder, i.e. using an algorithm for solving a simultaneous linear congruences. We can use the one discussed here (the first method) or here (the second method). The following examples are worked using the second method.
The CRT algorithm discussed here makes use of Euler’s theorem, which states that whenever and the modulus are relatively prime where is the phi function evaluated at . For this reason, the algorithm requires the evaluation of the phi function.
When calculating , the use of the phi function is to reduce the exponent by the largest multiple of . For example, since and , the problem of is converted to finding . Here, the original exponent of 250 is reduced to 10 after taking out the largest multiple of 16. Note that . In general, we want to replace the original exponent by a smaller exponent where .
____________________________________________________________________________
Examples
In the following three examples, we use the algorithm discussed here to solve systems of linear congruence equations (this is the iterative approach). These examples are by no means realistic since the numbers used are small. So they are for demonstration of how CRT works.
Example 1
Calculate .
First, factor the modulus . Now the problem is converted to solving the following system of two equations:
By CRT, any solution to the two equations is also a solution to the original equation. However, the exponent of 3163 should first be reduced. To this end, calculate the phi function, where and . We should reduce from 3163 the largest multiple of 54 in the first equation and reduce the largest multiple of 42 in the second equation. In other words, reduce the exponent 3163 modulo the two phi function values:
As a result, we solve the following two equations:
Note that the original exponentiation is turned into the easier ones of and . The following gives the solution to the above two equations.
where 23 is obtained by solving for in
By CRT, the answer to the original problem is .
Example 2
Calculate .
In this example, the number 3 and the modulus 3969 are not relatively prime. The CRT method still applies. As in Example 1, the problem can be reduced in the following way:
The first equation is congruent to 0 since contains 81 as a factor. The following gives the solution to the above two equations:
By CRT, the answer to the original problem is .
Example 3
The above 2 examples use small numbers to illustrate the CRT technique. In this example, we use slightly larger numbers. Calculate where and .
As in the other examples, we break up the problem in three congruences. The three factors of the modulus are prime numbers. Thus we reduce the exponent by multiples of a prime factor less one.
Then the original problem is transformed to solving the following three equations.
Notice that the original exponentiation is transformed to the smaller ones of , and . The remaining task is to solve the system of three equations. One way to find to solution to the above three equations is to use the iterative approach, starting with the solution to the first equation. Then find the solution to the first two equations and then the solution to all three equations.
where 151 is obtained by solving for in
where 970 is obtained by solving for in
By CRT, the answer to the original problem is .
____________________________________________________________________________
The CRT algorithm to speed exponentiation
Suppose we wish to evaluate where the prime factorization of is . The numbers are distinct prime numbers and each exponent . To prepare for the calculation, do the following:

Let for each .
Calculate for each .
Case 1. The base and the modulus are relatively prime.
Then the answer to the original exponentiation problem is identical to the solution to the following system of congruence equations:
where for each . If possible, each should be reduced modulo .
Case 2. The base and the modulus are not relatively prime.
In this case, and have prime factors in common (at least one ). The idea here is that for any that is a prime factor of , the equation in Case 1 is replaced by . Then solve the resulting system of equations (see Example 2). Essentially, Case 2 can fall under Case 1 with being congruent to zero. We call out Case 2 for the sake of clarity.
The original exponentiation boils down to solving an appropriate system of CRT congruences as described above. Once the equivalent system of congruences is set up, use the algorithm discussed here or here to do Chinese remaindering.
Comment
Both Case 1 and Case 2 produce a system of linear congruence equations that have identical solution to the original equation. This is a result of using CRT (see Theorem D and Theorem G here). The savings in the calculation come in the form of the smaller exponentiations in the resulting congruence equations.
In Case 2, some of the congruence equations are . This is because the base and some moduli are not relatively prime. For these moduli, would contain as a factor (assuming that ). Hence, .
The twoequation case
When the modulus is the product of two factors that are relatively prime, the CRT algorithm involves only two equations. We write out the solution explicitly for this case. To evaluate where and and are relatively prime, solve the following system of two equations,
The solution is where is the multiplicative inverse of modulo . If possible, each should be reduced modulo .
____________________________________________________________________________
RSA application
The algorithm to speed the exponentiation is possible because the factorization of the modulus is known (as a result, the values of are known). Knowing the values of makes it possible to reduce the large exponent . For this reason, the decryption process in the RSA algorithm is a perfect place to apply the CRT technique described here.
With the RSA cryptosystem, a public key consists of and , where is a large modulus that is a product of two large primes and (the two primes are not published) and is the encryption exponent. Say Bob is the originator of the RSA public key. Bob also generates a private key, which is a number that is used for decrypting any messages that he receives. The number must be kept private. The prime factors and must also be kept secret since knowing and can derive .
Suppose that Alice has a message to send to Bob. She can do so using the published key of and through the exponentiation . Here, is the plaintext (the message to be sent) and is the ciphertext (the encrypted message). Upon receiving the ciphertext , Bob can then decrypt through the exponentiation where is the decryption exponent. In realistic RSA calculation, the public modulus and the private decryption exponent are large integers ( is at minimum a 2048bit number). With CRT, the decryption can be reduced to two much smaller exponentiations. The effect can be at least four times faster.
We illustrate this with an example. This is a toy example since the numbers used are small. It is only intended as an illustration.
Example 4
Suppose the public key consists of and . Bob has the additional information of where and , which are kept secret. Knowing and allows Bob to compute . Suppose that Bob receives a message 4831984 from Alice. Use the CRT approach to find the plaintext .
The exponentiation is , which is equivalent to the following two equations by CRT:
which is further simplified to:
where and . Then the following is the plaintext (the original message).
where 1319 is obtained by solving for in
The answer is .
____________________________________________________________________________
Closing comment
In conclusion, we state the explcit formula for providing the CRT answer to the RSA decryption.
where , and is the multiplicative inverse of modulo .
The decryption formula of (1) represends tremendous saving in calculation (up to four times faster). It is possible only for the holder of the RSA private key. It requires knowledge of the decryption exponent , which is calculated from the factors and of the modulus .
____________________________________________________________________________