Modular Exponentiation Calculator - Fast a^b mod n Tool

4 bits, 3 of them set

Prime modulus — Fermat applies

7^13 mod 11the textbook trace — four bits, five operations

7^13 mod 11

2

Digits in ab if expanded

11

the number you never build

Squarings

3

one per bit after the first

Multiplications

2

one per 1 bit past the leading one

Versus naive

12 multiplications down to 5

Work required, on a log scale — each bar is the number of modular multiplications

Multiply by a, b − 1 times12
Square and multiply5

Bars are logarithmic because a linear one would make the green bar invisible.

The exponent 13 in binary — read left to right

S = square, M = multiply by the base. Every bit costs a squaring; only the 1 bits cost a multiplication.

1set
1S M
0S
1S M

Step by step — 5 operations, never exceeding 11² in size

#BitOperationResult
01result = 7 (leading 1 bit)7
117² mod 115
215 × 7 mod 112
302² mod 114
414² mod 115
515 × 7 mod 112

Powers of 7 modulo 11 repeat every 10 steps

Exponent 13 lands on position 3 of this loop — which is why the answer never needed the full power.

a^0

1

a^1

7

a^2

5

a^3

2

a^4

3

a^5

10

a^6

4

a^7

6

a^8

9

a^9

8

Shortcuts available for this modulus

Factorisation of n

11

φ(n)

10

gcd(a, n)

1 — coprime

Order of a mod n

10

Because gcd(a, n) = 1, Euler’s theorem lets you shrink the exponent first: 13 mod 10 = 3, and 73 mod 11 = 2 — the same answer, from a much smaller exponent.

How to Use This Calculator

  1. Type or paste the three values into Base (a), Exponent (b) and Modulus (n). All three accept up to 650 digits, which covers a 2048-bit RSA modulus at 617 digits.
  2. Read the answer in the green result bar. The four cards under it show how many digits ab would have if you expanded it, and how few multiplications the fast method needed instead.
  3. Switch Square-and-multiply direction between left-to-right and right-to-left to see both standard traces. Same answer, within one multiplication of the same cost, completely different intermediate values.
  4. Follow the binary strip and the step table together — every bit of the exponent is one row marked S, and every 1 bit adds a second row marked M.
  5. Check the shortcuts panel for gcd(a, n), φ(n) and the multiplicative order. When the base and modulus are coprime you can replace b with b mod φ(n) and get the same result from a far smaller exponent.

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Modular Exponentiation Calculator: How to Compute a^b mod n Fast

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:Updated:
Modular Exponentiation Calculator showing a^b mod n solved by square-and-multiply, with each squaring step and the binary exponent laid out

A modular exponentiation calculator earns its keep on a single fact: 71000 is an 846-digit number, and 71000 mod 13 is 9. One of those is a wall of digits your calculator refuses to display. The other is a single character, and getting to it takes ten squarings — not a thousand multiplications, and never a single digit of the giant number in between.

That gap is why every public-key system on the internet is built on this one operation. Below: what actually breaks when you compute ab the obvious way, how square-and-multiply sidesteps it, the two directions the algorithm can run, the shortcut that skips most of the work entirely, and the four ways people arrive at a confidently wrong answer.

The 846-Digit Number a Modular Exponentiation Calculator Never Builds

Ask a spreadsheet for 7^1000 and it returns an error or, worse, 1.02E+845 — a rounded float that has thrown away every digit that matters. Remainders depend on the last digits, and a float keeps only the first fifteen. So the answer you get back is not slightly off. It is meaningless.

The fix is to never let the number grow. Multiplication and remainders commute in the way you’d hope:

(x × y) mod n = ((x mod n) × (y mod n)) mod n

Reduce after every single multiplication and the running value never exceeds n². For n = 13 that means nothing bigger than 169 ever appears, no matter how large the exponent is. Try 71000 mod 13 in the tool above and watch the Result column — it stays in single and double digits from the first step to the last. If plain remainders are what you actually need, the modulo calculator handles those on their own, and the exponent calculator covers ordinary powers with no modulus in sight.

Square and Multiply, Traced One Bit at a Time

Keeping values small solves the size problem but not the count problem — ab the naive way still needs b − 1 multiplications, and b in cryptography is routinely a 2048-bit number. The trick is that squaring doubles the exponent for free. From a1 you reach a2, a4, a8, a16 in four moves, and any exponent at all is a sum of those powers of two. That sum is just the binary form of b.

Take 713 mod 11. Thirteen in binary is 1101. Start with the leading 1 bit already consumed, so the accumulator holds 7, then walk the remaining bits — square every time, multiply by 7 whenever the bit is 1:

BitSquareMultiply by 7?Accumulator
1 (leading)7
17² = 49 ≡ 55 × 7 = 35 ≡ 22
02² = 4no4
14² = 16 ≡ 55 × 7 = 35 ≡ 22

713 mod 11 = 2, from three squarings and two multiplications. The naive route needs twelve multiplications, so even on a four-bit exponent the fast method is less than half the work. The saving compounds brutally: the operation count grows with the number of bits in b, not with b itself.

Exponent bBitsNaive multipliesSquare and multiply
1,0001099914
1,000,00020999,99925
10930≈ 109≈ 44
RSA-2048 exponent2,048≈ 10616≈ 3,072

Read the bottom row twice. A 2048-bit RSA decryption finishes in about three thousand modular multiplications — a few milliseconds. The naive version would need more operations than there are atoms in the observable universe, several hundred times over.

Left to Right or Right to Left: Two Routes, One Answer

Both variants get called square-and-multiply, and textbooks switch between them without warning, which is a reliable source of confusion when your trace does not match the one in the book. Flip the direction selector above and the step table changes completely while the answer does not.

Left to rightRight to left
What gets squaredthe running resultthe base
Values trackedonetwo (result and base)
Needs bit length firstyes — must start at the top bitno — consumes bits as they come
Multiplicationsone per 1 bit, minus the leading oneone per 1 bit
Typical usetextbook proofs, hand calculationlibrary code, streaming exponents

Run 713 mod 11 the other way to see how different the middle looks. Reading 1101 from the right, the base gets squared instead of the result: result starts at 1 with base 7, the first 1 bit makes result 7 while the base becomes 5, the 0 bit skips the multiply and the base becomes 3, the next 1 bit takes result to 7 × 3 = 21 ≡ 10 with base 9, and the final 1 bit gives 10 × 9 = 90 ≡ 2. Same answer, 2 — but not one of the intermediate values 7, 10, 5, 3, 9 appeared in the left-to-right table above.

That run cost three squarings and three multiplications against left-to-right’s three and two. The difference is one operation, permanently: left-to-right gets the leading 1 bit for free by starting the accumulator at a, while right-to-left has to multiply it in like any other bit. Nobody chooses between them on that basis. Right-to-left is what almost every real implementation uses, because the loop condition is simply “while the exponent is greater than zero, shift it right” — no bit-length lookup required, and the exponent can arrive one word at a time. Left-to-right is easier on paper, since you only ever track one number.

One warning that matters outside a classroom: both traces branch on the bit value, so the multiplication only happens on 1 bits. An attacker measuring power draw or timing can read the exponent straight off that pattern. Production crypto libraries use constant-time ladders precisely to close that channel. This page is a teaching tool, not a hardening one.

Cutting the Exponent Down With Euler and Fermat

There is a way to skip most of the squarings altogether. Powers modulo n cycle, and the cycle length divides φ(n), Euler’s totient — the count of integers below n sharing no factor with it. When gcd(a, n) = 1, Euler’s theorem says aφ(n) ≡ 1, so the exponent only matters modulo φ(n):

ab ≡ ab mod φ(n) (mod n), whenever gcd(a, n) = 1

Compute 31000 mod 100 by hand with that. φ(100) = 40, and 1000 mod 40 = 0, so the answer is 30 = 1. No trace, no squaring, one division. The tool reports φ(n) and the reduced exponent in the shortcuts panel whenever it can factor the modulus, and you can look up totients directly with the Euler phi calculator. When n is prime this collapses further into Fermat’s little theorem, φ(p) = p − 1, which is the version most people meet first.

The real cycle length is often shorter than φ(n) — that is the multiplicative order, and the calculator prints it beside φ. For 3 modulo 100 the order is 20, not 40. Both work for reduction; the order just works harder.

Two things have to be true before you use this. Fermat’s theorem also runs in reverse as a primality test, and it is famously imperfect: 2340 mod 341 = 1, which makes 341 look prime, but 341 = 11 × 31. Numbers like that are called Fermat pseudoprimes, and the Carmichael numbers (561 is the smallest) fool the test for every coprime base. If you need to know whether a number is genuinely prime, factor it with the prime factorization calculator rather than trusting a power test. The same exponentiation drives the Lucas-Lehmer test behind the Mersenne prime calculator, which does not have this weakness.

How RSA Becomes Two Calls to the Same Operation

Encryption with RSA is one modular exponentiation. Decryption is one modular exponentiation. That is the entire runtime of the algorithm. Use the standard toy key: p = 61, q = 53, so n = 3233 and φ(n) = 60 × 52 = 3120. Pick e = 17, and d = 2753 because 17 × 2753 = 46,801 = 15 × 3120 + 1.

encrypt: 6517 mod 3233 = 2790
decrypt: 27902753 mod 3233 = 65

Both are presets in the calculator — run them back to back and watch 65 come home. The reason it works is exactly the theorem from the last section: e and d were chosen so that e × d ≡ 1 mod φ(n), which makes the round trip a1. Anyone who can factor 3233 recovers φ(n), computes d, and reads the message; nobody can factor a 2048-bit n, which is the whole security argument. Notice what the tool does with a large modulus — it reports that φ(n) is unavailable. That message is the security assumption, printed.

Real implementations do not decrypt the way the second line above suggests. Because the private key holder knows p and q, they compute the result modulo each prime separately with much shorter exponents — d mod (p − 1) and d mod (q − 1) — then glue the two halves back together with the Chinese remainder theorem. Each half runs on numbers of half the bit length, and modular multiplication costs roughly the square of the operand size, so the two half-size exponentiations together take about a quarter of the work. In practice that is a 3 to 4 times speedup on every signature and decryption your browser performs.

Diffie-Hellman key exchange runs on the same machinery in the opposite direction. Two parties each pick a secret exponent, publish gsecret mod p, and raise what they receive to their own secret. Both land on gab mod p. Recovering the secret from the public value means solving a discrete logarithm, and no efficient method is known — exponentiation is cheap, undoing it is not.

Four Ways to Get a Confidently Wrong Answer

Reducing the exponent mod n instead of mod φ(n). The base reduces mod n. The exponent does not — it reduces mod φ(n), which is a different number. Computing 5117 mod 19 as 5117 mod 19 = 53 = 125 ≡ 11 gives the wrong result; via φ(19) = 18 and 117 mod 18 = 9 you get 59 ≡ 1, which is correct. Mixing up which modulus applies to which position is the single most common error on this topic.

Applying Euler when the base and modulus share a factor. Try 2100 mod 12. φ(12) = 4 and 100 mod 4 = 0, so the shortcut claims 20 = 1. The true answer is 4. The theorem requires gcd(a, n) = 1, and gcd(2, 12) = 2. Check coprimality first — the calculator prints the gcd and refuses the shortcut when it fails, and the greatest common factor calculator answers the same question standalone.

Overflow in a fixed-width language. In C, Java or Go, multiplying two 64-bit values near 263 wraps silently before you get to apply the modulus, and the wrapped result is still a perfectly plausible-looking number. Keep the modulus under 231 if you are using 64-bit intermediates, or reach for a big-integer type. This page uses BigInt for exactly that reason.

Leaving the result negative. The % operator in most languages returns a negative remainder for a negative left operand, so (−7) % 11 gives −7, not 4. Mathematical convention puts the answer in the range 0 to n − 1. Normalise with ((x % n) + n) % n before you compare anything. The calculator above does this automatically, which is why a negative base still produces a positive result — and if you want to see the binary machinery underneath, the decimal to binary calculator converts any exponent into the bit string the algorithm actually walks.

For the formal statements and the constant-time variants, the Wikipedia article on modular exponentiation covers the algorithm family, and RFC 8017 specifies exactly how RSA uses it in production.

Frequently Asked Questions

Still Have Questions?

The detailed content on this page provides comprehensive explanations and examples to help you understand better.