If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

# Fast modular exponentiation

## How can we calculate A^B mod C quickly if B is a power of 2 ?

Using modular multiplication rules:
i.e. A^2 mod C = (A * A) mod C = ((A mod C) * (A mod C)) mod C
We can use this to calculate 7^256 mod 13 quickly
7^1 mod 13 = 7
7^2 mod 13 = (7^1 *7^1) mod 13 = (7^1 mod 13 * 7^1 mod 13) mod 13
We can substitute our previous result for 7^1 mod 13 into this equation.
7^2 mod 13 = (7 *7) mod 13 = 49 mod 13 = 10
7^2 mod 13 = 10
7^4 mod 13 = (7^2 *7^2) mod 13 = (7^2 mod 13 * 7^2 mod 13) mod 13
We can substitute our previous result for 7^2 mod 13 into this equation.
7^4 mod 13 = (10 * 10) mod 13 = 100 mod 13 = 9
7^4 mod 13 = 9
7^8 mod 13 = (7^4 * 7^4) mod 13 = (7^4 mod 13 * 7^4 mod 13) mod 13
We can substitute our previous result for 7^4 mod 13 into this equation.
7^8 mod 13 = (9 * 9) mod 13 = 81 mod 13 = 3
7^8 mod 13 = 3
We continue in this manner, substituting previous results into our equations.
...after 5 iterations we hit:
7^256 mod 13 = (7^128 * 7^128) mod 13 = (7^128 mod 13 * 7^128 mod 13) mod 13
7^256 mod 13 = (3 * 3) mod 13 = 9 mod 13 = 9
7^256 mod 13 = 9
This has given us a method to calculate A^B mod C quickly provided that B is a power of 2.
However, we also need a method for fast modular exponentiation when B is not a power of 2.

## How can we calculate A^B mod C quickly for any B ?

### Step 1: Divide B into powers of 2 by writing it in binary

Start at the rightmost digit, let k=0 and for each digit:
• If the digit is 1, we need a part for 2^k, otherwise we do not
• Add 1 to k, and move left to the next digit

### Step 2: Calculate mod C of the powers of two ≤ B

5^1 mod 19 = 5
5^2 mod 19 = (5^1 * 5^1) mod 19 = (5^1 mod 19 * 5^1 mod 19) mod 19
5^2 mod 19 = (5 * 5) mod 19 = 25 mod 19
5^2 mod 19 = 6
5^4 mod 19 = (5^2 * 5^2) mod 19 = (5^2 mod 19 * 5^2 mod 19) mod 19
5^4 mod 19 = (6 * 6) mod 19 = 36 mod 19
5^4 mod 19 = 17
5^8 mod 19 = (5^4 * 5^4) mod 19 = (5^4 mod 19 * 5^4 mod 19) mod 19
5^8 mod 19 = (17 * 17) mod 19 = 289 mod 19
5^8 mod 19 = 4
5^16 mod 19 = (5^8 * 5^8) mod 19 = (5^8 mod 19 * 5^8 mod 19) mod 19
5^16 mod 19 = (4 * 4) mod 19 = 16 mod 19
5^16 mod 19 = 16
5^32 mod 19 = (5^16 * 5^16) mod 19 = (5^16 mod 19 * 5^16 mod 19) mod 19
5^32 mod 19 = (16 * 16) mod 19 = 256 mod 19
5^32 mod 19 = 9
5^64 mod 19 = (5^32 * 5^32) mod 19 = (5^32 mod 19 * 5^32 mod 19) mod 19
5^64 mod 19 = (9 * 9) mod 19 = 81 mod 19
5^64 mod 19 = 5

### Step 3: Use modular multiplication properties to combine the calculated mod C values

5^117 mod 19 = ( 5^1 * 5^4 * 5^16 * 5^32 * 5^64) mod 19
5^117 mod 19 = ( 5^1 mod 19 * 5^4 mod 19 * 5^16 mod 19 * 5^32 mod 19 * 5^64 mod 19) mod 19
5^117 mod 19 = ( 5 * 17 * 16 * 9 * 5 ) mod 19
5^117 mod 19 = 61200 mod 19 = 1
5^117 mod 19 = 1

## Notes:

More optimization techniques exist, but are outside the scope of this article. It should be noted that when we perform modular exponentiation in cryptography, it is not unusual to use exponents for B > 1000 bits.

## Want to join the conversation?

• Is there a tutorial/challenge for writing numbers in binary? If not that would be a great addition!
• This link no longer works. Is there another video perhaps?
• Why isn't this math on the learning dashboard, say in the world of math mission?
• what if the exponent has four digits
• In Notes They said that if B>1000 some good technique is used.
so what is the name of that techniques?
• Could one take the 5^117mod19=(5*17*16*9*5)mod 19 and make it equal to ((5*17)mod19)*(16*9)mod19*5mod19)mod19?
• What if the modulo operation has some exponent ?? For example-
1000 mod 17^2.
Is there any way to simplify it?
• Given: 'A mod B'
Typically, it isn't a big issue if B is large, because if A < B then 'A mod B = A'
On the other hand, if A is large then we do need techniques to simplify 'A mod B', because we don't have such a simple result.

That being said, in certain circumstances, it may be useful to break B into coprime factors and find A mod each coprime factor and reassemble them again using the Chinese Remainder Theorem. (This, however, would not allow you to split up B if it was a prime taken to an exponent, as each of the factors would not be coprime to eachother)
• So basically, 8 bit binary would represent if numbers like are in the whole number (going to use semicolons to space things):

128 : 64 : 32 : 16 : 8 : 4 : 2 : 1
0 0 0 0 0 0 0 0

So writing 112 would look like:
01110000

Since 64 + 32 + 16 = 112 since I included those numbers in the..... binary snippet?

Is my calculation correct?
• Yes,it is correct.
(1 vote)
• Can anyone tell how to convert digits into binary?
• I was doing some RSA exercises and had a problem when solving modular exponentiation. For example, 978^325 mod 1711. I tried the method above but it is still somehow hard to calculate. Is there any faster way to deal with it? Or did I miss some other important mathematical background of modular exponentiation so that it makes me feel hard to solve?
• It still requires some work, but it is a lot less working than trying to calculate 978^325 ( which would be a ridiculously big number) and then finding the value of the result mod 1711.

Writing a program to automate the process also helps.