3.5 Modular Exponentiation

Throughout this book, we will be interested in numbers of the form

xa(modn).

In this and the next couple of sections, we discuss some properties of numbers raised to a power modulo an integer.

Suppose we want to compute 21234(mod789). If we first compute 21234,  then reduce mod 789, we’ll be working with very large numbers, even though the final answer has only 3 digits. We should therefore perform each multiplication and then calculate the remainder. Calculating the consecutive powers of 2 would require that we perform the modular multiplication 1233 times. This is method is too slow to be practical, especially when the exponent becomes very large. A more efficient way is the following (all congruences are mod 789).

We start with 224(mod789) and repeatedly square both sides to obtain the following congruences:

24421628162256216256249232342643672128559225637251258021024286.

Since 1234=1024+128+64+16+2 (this just means that 1234 equals 10011010010 in binary), we have

21234286559367494481(mod789).

Note that we never needed to work with a number larger than 7882.

The same method works in general. If we want to compute ab(modn),  we can do it with at most 2log2(b) multiplications mod n,  and we never have to work with numbers larger than n2. This means that exponentiation can be accomplished quickly, and not much memory is needed.

This method is very useful if a, b, n are 100-digit numbers. If we simply computed ab,  then reduced mod n,  the computer’s memory would overflow: The number ab has more than 10100 digits, which is more digits than there are particles in the universe. However, the computation of ab(modn) can be accomplished in fewer than 700 steps by the present method, never using a number of more than 200 digits.

Algorithmic versions of this procedure are given in Exercise 56. For more examples, see Examples 8 and 24–30 in the Computer Appendices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247