6.006 Intro to Algorithms
Recitation 22
April 29, 2011
Modular Exponentiation
Modular exponentiation is the problem of ﬁnding an efﬁcient way of computing
a
b
mod
n
. Mod
ular exponentiation is applicable in many security measures, such as RSA. The naive approach
to computing
a
b
mod
n
would be to calculate
a
mod
n
and then multiplying the result by
a
an
additional
b

1
times. This method requires using
O
(
b
)
multiplications to get the intended result.
One key observation to make is that we can create shortcuts by squaring results instead of
multiplying by
a
one at a time. For example, if we want to calculate
a
1
0
mod
n
and we’ve already
calculated
a
5
mod
n
, instead of multiplying the result by
a
5 more times, we can square
a
5
to
get
a
1
0
, getting to our intended result in just one multiplication instead of ﬁve. Squaring doubles
the exponent while multiplying increases the exponent by 1. Using a combination of squaring and
multiplying will result in modular exponentiation using
O
(log
b
)
multiplications to get the intended
result.
To ﬁgure out what order of squaring/multiplying we want to execute, it helps to take a look at
the binary representation of
b
. Given a binary representation of
b
, (
b
k
,b
k

1
,...,b
1
,b
0
), we iterate
through the digits of
b
from most signiﬁcant to least signiﬁcant. Every time we see a 1 digit, that
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '11
 byrns
 Math, Addition, LG, nbit numbers

Click to edit the document details