Binary Exponentiation
Binary exponentiation is a fast way to compute powers such as or .
The main idea is to write the exponent in base :
where each . Then
So instead of multiplying by a total of times, we repeatedly square and only multiply the powers whose binary digits are .
Modular Version
To compute , reduce modulo after each squaring and multiplication. This keeps the numbers small:
This is the standard method used in RSA, Elgamal, Diffie-Hellman, and Miller-Rabin.
Example
Compute :
so
and the powers can be built by repeated squaring.