Modular Exponentiation Calculator

Compute (a^b) mod m instantly — even for enormous cryptographic-size exponents.

Ad placeholder (leaderboard)
Enjoying the tools? Go Pro for £4.99 (one-time) and remove all ads — forever, on this device. Remove ads — £4.99

A modular exponentiation calculator that computes (a ^ b) mod m correctly and instantly for arbitrarily large inputs, then shows the complete square-and-multiply trace so you can follow every step. It is the right tool for cryptography students, competitive programmers, and anyone checking RSA or Diffie-Hellman arithmetic by hand.

Why ordinary calculators fail here

Suppose you want 2^(2^32) mod 10^9+7. The exponent alone is over four billion. Raising 2 to that power directly produces a number with more than a billion digits — impossible to store in any normal floating-point type. Modular exponentiation solves this by never forming the full power: it keeps every intermediate value below m² throughout the computation.

The square-and-multiply algorithm

The algorithm exploits the binary representation of the exponent:

Step 1. Write b in binary: e.g. 13 = 1101₂.

Step 2. Scan the bits from the least significant to the most significant.

Step 3. At each bit position:

  • Square the running base: base = base² mod m
  • If the current bit is 1, multiply the running result: result = result × base mod m
  • If the bit is 0, skip the multiplication.

Because each multiplication is followed immediately by reduction mod m, the numbers involved never exceed m². For a 1024-bit RSA modulus, that is about 2048 bits — vastly smaller than the raw a^b.

Worked example: 4^13 mod 497

Exponent 13 in binary is 1101.

StepBitBase after squaringResult after multiply
1144
20164 (no multiply)
312561024 mod 497 = 30
41256² mod 497 = 42930 × 429 mod 497 = 445

Result: 4¹³ mod 497 = 445. The algorithm did 4 multiplications instead of 12 repeated multiplications, and no intermediate value exceeded 497² = 247,009.

RSA connection

In RSA, encryption is c = m^e mod n and decryption is m = c^d mod n. Both e and d are typically hundreds of bits long, making square-and-multiply the only practical approach. Enter any toy RSA triple into this calculator (try the preset: base = 65, exponent = 17, modulus = 3233) to verify that the result matches the textbook answer.

Solve for the exponent

The amber “Solve for exponent” panel inverts the problem: given base, target, and a small modulus, find the smallest e such that base^e ≡ target (mod m). This is the discrete logarithm — easy to verify, believed to be hard to compute for large m, and the security foundation of Diffie-Hellman and elliptic-curve cryptography.

Everything runs locally in your browser using JavaScript BigInt. No numbers are sent to any server.

Ad placeholder (rectangle)