Free tool

Modular exponentiation calculator

Compute ab mod n for numbers of any size — and see the square-and-multiply steps that keep it fast.

Raising a number to a huge power and reducing it modulo another number is the single most common operation in public-key cryptography. Done naively it is impossible — 6517 already has 31 digits, and real exponents have hundreds. Done by square and multiply, it takes a few dozen steps.

Enter values and press Compute.

Arbitrary size — the arithmetic uses BigInt, so 600-digit inputs are fine. Everything runs in your browser.

Why square and multiply works

Instead of multiplying a by itself b times, you repeatedly square the base and multiply the running result only where the exponent has a 1 bit. An exponent with 2048 bits needs about 2048 squarings rather than 22048 multiplications.

Reducing modulo n at every step keeps every intermediate number below , so nothing ever grows out of control. That is why your phone can do RSA with 600-digit numbers in milliseconds.

6517 mod 3233
17 in binary = 10001
→ square five times, multiply at bits 0 and 4
→ 2790

This is the operation underneath RSA encryption and Diffie–Hellman key exchange. Reversing it — recovering the exponent — is the discrete logarithm problem, and nobody knows how to do that efficiently.

Keep going

Discrete log calculator

Go the other way: recover the exponent.

Open →

RSA calculator

Generate keys and encrypt with them.

Open →

Modular arithmetic

The clock math this all runs on.

Open →