Short answer: Binary exponentiation computes a^e with O(log e) multiplications by scanning the bits of the exponent. The square-and-multiply loop squares a running power for every bit and multiplies by the base when the bit is 1. The same loop with every multiplication reduced modulo m computes a^e mod m. It is the multiplication engine inside RSA, Diffie-Hellman, Miller-Rabin, and any other algorithm that needs a large power.
What the problem actually asks
Multiplying by a, e times, performs e−1 multiplications. The binary expansion of e writes the power as a product of terms a^{2^k} for the bits that are set. Those terms are successive squares. The algorithm never builds a multiplication chain fancier than this one. Addition chains can be shorter for a fixed exponent and are not what this loop does.
Modular reduction after every multiply keeps the operands the size of m. Reducing only at the end creates an enormous integer and is still mathematically right and practically wrong.
What the algorithm guarantees
The loop returns a^e, or a^e mod m if every product is reduced. The number of squarings is the bit length of e, and the number of extra multiplies is the number of 1 bits. Time is O(log e) multiplications. Extra memory is a few integers. A negative exponent is not handled unless a is inverted modulo m first, which needs the extended Euclidean algorithm and a coprime a.
Left-to-right and right-to-left scans both work and associate the multiplications differently. They return the same value. An off-by-one in the initial power, starting from 0 instead of 1, returns 0 for every positive exponent.
The working steps
- Set the result to 1 and the base to a modulo m if a modulus is in use. If e is 0, return 1, including the convention 0^0 if the caller has one.
- While e is positive, if the low bit of e is 1, multiply the result by the current base and reduce.
- Square the base and reduce. Shift e right by one bit.
- The right-to-left loop just described reads bits from low to high. The left-to-right loop scans from the high bit, squaring the result each time and multiplying by a when the bit is 1.
- For a negative exponent modulo m, replace a by its inverse and negate the exponent before entering the loop. If the inverse does not exist, the power is undefined in the residues.
The logarithmic bound counts multiplications, not bit operations. Fast multiplication changes the cost of each multiply and does not change the number of them. The loop is not a general addition-chain optimizer. Modular exponentiation with a secret exponent should use a constant-time ladder in cryptographic code. This description is the arithmetic algorithm, not a side-channel-resistant implementation.
Approaches that look simpler and fail
| Approach | Why it fails when the result starts at zero |
|---|---|
| Initialize the result to 0 | Every multiply stays 0. The empty product, a^0 and the starting value of a positive power, is 1. |
| Reduce modulo m only after the full power is known | The integer a^e has e·log a bits. The modular loop keeps every intermediate below m. |
| Shift the exponent and forget to square the base | The bit positions then all multiply the original base. The result is a multiplied by itself once per set bit, not a^e. |
| Use the fast loop for a secret exponent with a data-dependent branch on the bit | The arithmetic result is right and the timing leaks the bits. Cryptographic code needs a fixed sequence of multiplies. |
The modular inverse that a negative exponent needs is the extended Euclidean algorithm. Miller-Rabin, RSA, and Diffie-Hellman all call this loop. Binary exponentiation is the square-and-multiply power.
A working example
The Bash script and the Python script square the base for each bit of the exponent and reduce modulo 1000 at every multiplication. Both print 24, which is 2^10 mod 1000.
base=2
exp=10
mod=1000
result=1
base=$((base % mod))
while (( exp > 0 )); do
if (( exp & 1 )); then result=$(( (result * base) % mod )); fi
base=$(( (base * base) % mod ))
exp=$((exp >> 1))
done
echo "$result"
def modpow(base, exp, mod):
result = 1
base %= mod
while exp:
if exp & 1:
result = result * base % mod
base = base * base % mod
exp >>= 1
return result
assert modpow(2, 10, 1000) == 24
print(modpow(2, 10, 1000))
Where it is used
Binary exponentiation is the modular power inside RSA**, Diffie-Hellman**, and every pow(base, exp, mod) in a crypto library. OpenSSL and the Python built-in both square-and-multiply. Multiplying in a loop is the same function and far too slow for a 2048-bit exponent.
When you can drop this algorithm
Drop the O(e) multiply loop for any exponent longer than a few bits. Use square-and-multiply. Keep a constant-time variant when the exponent is a secret. The mathematical loop is the one described here.
Why this stays published
We publish this so a search for binary exponentiation returns the square-and-multiply bit scan and the initial value 1. Binary exponentiation computes a to the e with O(log e) multiplications by squaring for every bit and multiplying when the bit is set.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
