Understanding modular exponentiation
Modular exponentiation computes the remainder of a base raised to an exponent, divided by a modulus. It is written as ab mod n and is a core operation in cryptography — used in RSA, Diffie-Hellman key exchange, and digital signatures.
For example, 310 mod 7. Computing 310 = 59049, then 59049 mod 7 = 4. The binary method achieves this efficiently by processing the exponent's bits rather than performing all 10 multiplications directly.
The binary (square-and-multiply) method works by scanning the exponent's binary representation from least significant to most significant bit, squaring the base at each step, and accumulating the result only when the current bit is 1.