Modular Exponentiation

Learn how to efficiently compute the modular exponentiation with the help of some examples and a flowchart.

We promised you that we would go through some of the mathematical details of the RSA algorithm. If you are not interested in the details, feel free to skip to the next section.

Modular exponentiation means that we want to find cc, given general integers aa, bb, and NN, where

c=abmodN.c = a^b \bmod N.

In other words, cc is the remainder of dividing aba^b by NN. We could use a brute force method to find cc: calculate aba^b and then divide by NN to find the remainder cc. The difficulty is that aa, bb, and NN might be large numbers, which need to be stored as exact binary sequences, and aba^b is extremely large and must be stored exactly. Also, carrying out the division by NN will be time consuming. However, once we have chosen aa and NN, there is a simple algorithm to find cc that avoids the large number issues:

  1. Set c=1,b=0c=1,b^′ =0.
  2. Increase bb^′ by 11.
  3. Set c=(ac)bmodNc=(a·c)b^′ \bmod N.
  4. If b<bb^′ <b, go to step 2. Otherwise, cc is the solution to c=abmodNc = a^b \bmod N.

Note that bb^{\prime} serves as an index to keep track of the loops through the algorithm. Here is a flow chart for the algorithm:

Get hands-on with 1400+ tech skills courses.