3.5Exponent Evaluation: Binary Exponentiation
Covers IGNOU MCS-211 §3.4.4
Computing a power by multiplying by itself takes multiplications. For the huge exponents used in cryptography (exponents with hundreds of digits) that is impossible. Binary exponentiation uses the binary digits of to need only about multiplications.
The idea
Write in binary. Each bit tells you whether a power is part of the product, and each such power is the square of the previous one.
A positive integer has bits, so .
Left-to-right binary exponentiation
Scan the bits from the most significant (always 1) to the least significant. Let b[i] be bit of ( is the least significant).
LeftToRightPower(a, b[0..s−1]) // b[s−1] = 1 is the leading bit
result ← a
for i ← s − 2 downto 0 do
result ← result * result // square
if b[i] = 1 then
result ← result * a // multiply by a
return resultEach step doubles the exponent and adds the next bit, just like reading a binary number from left to right.
Right-to-left binary exponentiation
Scan the bits from the least significant. Keep and multiply it into the result when bit is 1.
RightToLeftPower(a, b[0..s−1])
x ← a
if b[0] = 1 then result ← a else result ← 1
for i ← 1 to s − 1 do
x ← x * x // x = a^(2^i)
if b[i] = 1 then
result ← result * x
return result#include <stdio.h>
/* Left-to-right: scan the bits of n from the most significant one. */
unsigned long long power_lr(unsigned long long a, unsigned int n, int *mults)
{
int s = 0;
for (unsigned int t = n; t > 0; t >>= 1) /* s = number of bits of n */
s++;
unsigned long long result = a; /* the leading bit is 1 */
*mults = 0;
for (int i = s - 2; i >= 0; i--) {
result = result * result; (*mults)++;
if ((n >> i) & 1u) {
result = result * a; (*mults)++;
}
}
return result;
}
/* Right-to-left: scan the bits from the least significant one. */
unsigned long long power_rl(unsigned long long a, unsigned int n, int *mults)
{
unsigned long long x = a;
unsigned long long result = (n & 1u) ? a : 1;
*mults = 0;
for (n >>= 1; n > 0; n >>= 1) {
x = x * x; (*mults)++;
if (n & 1u) {
result = result * x; (*mults)++;
}
}
return result;
}
int main(void)
{
int m1, m2;
unsigned long long r1 = power_lr(4, 11, &m1), r2 = power_rl(4, 11, &m2);
printf("4^11: left-to-right = %llu (%d mults), right-to-left = %llu (%d mults)\n", r1, m1, r2, m2);
r1 = power_lr(3, 17, &m1);
r2 = power_rl(3, 17, &m2);
printf("3^17: left-to-right = %llu (%d mults), right-to-left = %llu (%d mults)\n", r1, m1, r2, m2);
return 0;
}4^11: left-to-right = 4194304 (5 mults), right-to-left = 4194304 (5 mults)
3^17: left-to-right = 129140163 (5 mults), right-to-left = 129140163 (5 mults)Analysis
In both methods, each of the loop iterations does one squaring and at most one extra multiplication. If is the number of multiplications, So : between and multiplications, compared with for brute force. More precisely, .
Tracing (Check Your Progress 3)
, so . The program tracks only the exponent of :
# Trace binary exponentiation by tracking only the exponent of a.
def left_to_right(n):
bits = bin(n)[2:] # most significant bit first
e, mults = 1, 0 # result = a^1
print(f"n = {n} = {bits} (binary), s = {len(bits)}")
for pos, bit in enumerate(bits[1:], start=1):
i = len(bits) - 1 - pos
e, mults = 2 * e, mults + 1
if bit == "1":
e, mults = e + 1, mults + 1
print(f" i = {i}, bit = {bit}: result = a^{e}")
print(f" left-to-right multiplications: {mults}")
def right_to_left(n):
bits = bin(n)[2:][::-1] # least significant bit first
x, mults = 1, 0 # x = a^1
r = 1 if bits[0] == "1" else 0 # result = a^r
for i, bit in enumerate(bits[1:], start=1):
x, mults = 2 * x, mults + 1
if bit == "1":
r, mults = r + x, mults + (1 if r > 0 else 0)
print(f" i = {i}, bit = {bit}: x = a^{x}, result = a^{r}")
print(f" right-to-left multiplications: {mults}")
left_to_right(283)
right_to_left(283)n = 283 = 100011011 (binary), s = 9
i = 7, bit = 0: result = a^2
i = 6, bit = 0: result = a^4
i = 5, bit = 0: result = a^8
i = 4, bit = 1: result = a^17
i = 3, bit = 1: result = a^35
i = 2, bit = 0: result = a^70
i = 1, bit = 1: result = a^141
i = 0, bit = 1: result = a^283
left-to-right multiplications: 12
i = 1, bit = 1: x = a^2, result = a^3
i = 2, bit = 0: x = a^4, result = a^3
i = 3, bit = 1: x = a^8, result = a^11
i = 4, bit = 1: x = a^16, result = a^27
i = 5, bit = 0: x = a^32, result = a^27
i = 6, bit = 0: x = a^64, result = a^27
i = 7, bit = 0: x = a^128, result = a^27
i = 8, bit = 1: x = a^256, result = a^283
right-to-left multiplications: 12Both methods use squarings plus extra multiplications ( has five 1-bits, and the first one costs nothing) = 12 multiplications, instead of 282. The study material’s two tables for agree with these traces.
Key takeaways
- has bits; is the product of the powers whose bit is 1.
- Left to right: square, then multiply by if the bit is 1.
- Right to left: square , then multiply the result by if the bit is 1.
- Multiplications: between and , i.e. ; needs 12.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Compute with left-to-right binary exponentiation, showing each step and the number of multiplications.
Show worked answer
22 = 10110₂, s = 5. Start result = a (leading bit). Bit 0: square → a²; no multiply. Bit 1: square → a⁴, multiply by a → a⁵. Bit 1: square → a¹⁰, multiply → a¹¹. Bit 0: square → a²². Multiplications: 4 squarings + 2 extra = 6 (instead of 21).