Modular Exponentiation Fast Power

Compute a^b mod m quickly using efficient binary exponentiation for applications. Supports huge numbers with automatic GMP, BCMath, or native algorithm seamlessly. Visualize intermediate squares and multiplies, then export tables and PDFs for sharing. Learn formulas, usage tips, and common pitfalls, fast today.

Input Enter integers for base, exponent, and modulus.
Auto selects GMP, then BCMath, else native integers.
Example data Click a row to load.
#Base (a)Exponent (b)Modulus (m)a^b mod m
15117191
2413497445
375605611
4210001009942
5123456789123459742
Result
Compute to see the result, steps, and chart.
Steps & Chart
#BitOperationBaseResult
Formula used

Goal: compute a^b mod m efficiently.

Binary (fast) exponentiation writes b in base two: b = \sum\_i 2^i. Repeatedly square and, when a bit is set, multiply the current result by the current base, always reducing modulo m. Complexity is O(log b) multiplications with modulus reductions.

// Pseudocode
res ← 1
x ← a mod m
while b > 0:
  if b is odd: res ← (res · x) mod m
  x ← (x · x) mod m
  b ← floor(b / 2)
How to use this calculator
  1. Enter integers for Base a, Exponent b, and Modulus m.
  2. Choose Method or leave Auto for best available precision.
  3. Press Compute. The result, runtime, and step count will appear.
  4. Review the table for each square and multiply operation.
  5. Use Download buttons to export results CSV, steps CSV, or a PDF snapshot.
Example: Using Modular Exponentiation Fast Power

This worked example shows binary exponentiation on a=5, b=117, m=19.

  1. Reduce base: x = a mod m = 5 mod 19 = 5.
  2. Write exponent in binary: 117 = 11101012.
  3. Scan bits from least significant to most significant, squaring each loop and multiplying when the bit is 1.
LoopBitOperationBase (x)Result
11multiply then square55
20square65
31multiply then square179
40square49
51multiply then square1611
61multiply then square94
71multiply then square51

Therefore, 5117 mod 19 = 1. You can load this example from the table and compare steps.

Applications and Domains
  • Public‑key cryptography (RSA, Diffie–Hellman, ElGamal).
  • Digital signatures and key exchange protocols.
  • Primality testing and residue classes.
  • Hashing schemes and commitment protocols.
DomainWhy it matters
RSAExponentiation under large moduli enables encryption and signatures.
DH/ECPowers modulo m underpin shared secret derivation.
BlockchainResidues and proofs rely on modular operations.
Complexity & Performance

Binary exponentiation uses O(log b) squarings and at most that many multiplies.

Exponent bBitsIterations ≈
1,000≈1010
1,000,000≈2020
2404040
2204820482048

Tip: Using GMP accelerates large integer arithmetic; BCMath provides precise string math.

Edge Cases & Validation
InputConditionBehavior
m = 0Invalid modulusError: modulus must be non‑zero.
b < 0Negative exponentError: negative exponent unsupported.
a < 0Negative baseHandled: reduced modulo m to [0, m).
a ≡ 0 (mod m)Base multiple of mResult is 0 unless b = 0.
b = 0Zero exponentResult is 1 for any m ≠ 0.

These cases are checked or handled in the calculator’s implementation.

Related Calculators

Reverse Euclidean algorithm calculatorfast modular exponentiation calculatorLarge number modulo calculatormodular congruence solverpisano period calculator

Important Note: All the Calculators listed in this site are for educational purpose only and we do not guarentee the accuracy of results. Please do consult with other sources as well.