Skip to content
AZ Tools

Modular Exponentiation Calculator (aᵇ mod m)

Compute a^b mod m exactly for arbitrarily large integers using fast binary exponentiation, plus the modular multiplicative inverse of the base.

a^b mod m
9
Modular inverse of a (mod m)2

How to use

  1. Enter the base (a), the exponent (b) and the modulus (m).
  2. Read a^b mod m, computed exactly even for very large numbers.
  3. Check the modular inverse of the base — shown only when gcd(a, m) = 1.
  4. Try a = 7, b = 256, m = 13 to get 9.

About this tool

Show

Modular exponentiation computes a raised to the power b, reduced modulo m — the core operation behind RSA, Diffie-Hellman and many other cryptographic and number-theory tasks. Computing a^b first and then taking the remainder is impossible for large exponents, so this tool uses fast binary exponentiation (square-and-multiply), reducing modulo m at every step. All arithmetic uses arbitrary-precision big integers, so the result is exact even when the base, exponent or modulus has hundreds of digits. It also reports the modular multiplicative inverse of the base modulo m (the value x with a·x ≡ 1) whenever the base and modulus are coprime. Everything runs locally in your browser — nothing is uploaded.

Frequently asked questions

Why not just compute a^b and take the remainder?

For large exponents a^b has astronomically many digits and cannot be formed in practice. Square-and-multiply keeps every intermediate value below m², so it stays fast and exact regardless of the exponent's size.

What is the modular inverse line?

It is the number x such that a·x ≡ 1 (mod m). It exists only when the base and modulus share no common factor (gcd = 1); otherwise the tool shows that no inverse exists. This is the value used when 'dividing' in modular arithmetic.

What does an exponent of 0 give?

a^0 mod m is 1 for any base (and 0 when m = 1). Exponents must be non-negative whole numbers; the base may be negative and is normalised into the range 0…m−1 first.

Is this calculation safe to use with real key material?

No. The arithmetic is correct, but a real implementation also has to be constant-time: a straightforward square-and-multiply takes a different amount of time depending on the bits of the exponent, and that timing difference has been used to recover private keys from live systems. Use this to understand and to check results, and use a vetted library for anything that touches a secret.

What makes an exponent so large tractable at all?

Reducing modulo m at every step, so the numbers never grow past the size of the modulus, and squaring rather than multiplying repeatedly — which turns an exponent of a thousand bits into about a thousand squarings instead of an astronomically long chain. That is why the exponent's size affects the time roughly linearly while its value does not appear anywhere in the cost.

Related tools