Modular exponentiation — computing a^b mod m — looks like a simple arithmetic exercise, but computing it efficiently is what makes modern public-key cryptography practical. This calculator computes a^b mod m using the square-and-multiply algorithm and shows exactly how it works, bit by bit.
Why not just compute a^b and then take the remainder?
For small numbers, computing a^b directly and then reducing mod m works fine. But cryptographic operations use exponents and moduli hundreds of digits long — a^b itself would have far more digits than atoms in the observable universe, long before you ever get to the "mod m" step. Square-and-multiply avoids this entirely by reducing modulo m after every squaring and multiplication, so no intermediate value ever exceeds m. Combined with the fact that it needs only about log2(b) multiplications instead of b − 1, this is what makes operations like RSA encryption with a 2048-bit exponent complete in milliseconds rather than being computationally impossible.
Reading the Square-and-Multiply Steps and Binary Exponent tabs
The algorithm scans the exponent's binary digits from most significant to least significant. At every bit, it squares the running result and reduces mod m; if that bit is a 1, it also multiplies by the base and reduces again. The Binary Exponent tab shows which bits are 1 and which are 0 — each 1-bit is one extra multiplication beyond the squarings every bit requires. A 20-bit exponent, however large the decimal number it represents, still only costs 20 squarings and at most 20 multiplications.
Limits and edge cases
This calculator only supports non-negative exponents. A negative exponent — such as a^(-1) mod m — requires computing a's modular inverse, which only exists when a and m are coprime and needs a different algorithm (the extended Euclidean algorithm) entirely; it isn't supported here. A modulus of 1 always produces 0, since every integer is divisible by 1. And while the base can be entered as negative, it's normalized into the range [0, m) before the algorithm runs, matching the standard mathematical convention that a mod m is always non-negative.