Modular Exponentiation Calculator

Compute (base ^ exponent) mod modulus for huge numbers.

Free modular exponentiation calculator — compute (base raised to exponent) mod modulus using fast square-and-multiply, even for enormous exponents. Runs entirely in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

How does it handle huge exponents without overflowing?

It uses the square-and-multiply method (binary exponentiation), reducing modulo the modulus at every step. The intermediate values never exceed the modulus squared, so even thousand-bit exponents compute quickly and exactly.

This calculator computes (base ^ exponent) mod modulus — the core operation of RSA, Diffie-Hellman and other public-key cryptography. It is built for students and developers who need an exact result for large inputs that ordinary calculators cannot handle.

How it works

It uses the square-and-multiply (binary exponentiation) algorithm with arbitrary-precision integers. The exponent is read bit by bit: at each step the running base is squared, and whenever the current bit is 1 the result is multiplied in. Crucially, the value is reduced modulo the modulus after every multiplication, so intermediate numbers never exceed the modulus squared. This means even thousand-bit exponents compute almost instantly and exactly.

Example

To compute 4¹³ mod 497:

The exponent 13 is 1101 in binary. Square-and-multiply walks those bits, reducing mod 497 each step, giving 445 — without ever forming the full 8-digit value of 4¹³ (67,108,864).

baseexponentmodulusresult
413497445
7256139
210100024

All arithmetic uses arbitrary-precision integers and runs locally in your browser; nothing is uploaded.

Why naive computation fails for cryptographic inputs

In RSA and Diffie-Hellman, the numbers involved are not small integers like 4 and 13 — they are typically 1024-bit, 2048-bit, or 4096-bit numbers. Computing 4¹³ naively gives 67,108,864 — a number with 8 digits. But computing a 2048-bit base raised to a 2048-bit exponent directly would produce a number with roughly 10¹²³⁴ digits. This is not just impractical to store — it is physically impossible: there are only about 10⁸⁰ atoms in the observable universe.

Square-and-multiply avoids this entirely by taking the modulus at every step. Since the modulus in RSA is a fixed-size number (say, 2048 bits), every intermediate value is at most 2 × 2048 bits — a number that fits in a few hundred bytes. The final result is always smaller than the modulus, regardless of how large the exponent is. This is what makes public-key cryptography computationally tractable.

Where this operation appears in practice

RSA encryption and decryption — Encryption computes ciphertext = message^e mod n and decryption computes message = ciphertext^d mod n, where e and d are the public and private exponents and n is the product of two large primes. The security of RSA depends on the infeasibility of factoring n.

Diffie-Hellman key exchange — Each party computes g^a mod p (their public key) and (other party's public key)^a mod p (the shared secret), where g is a generator, p is a large prime, and a is a private random exponent. Both parties arrive at the same shared secret without ever transmitting it.

Primality testing — Fermat’s little theorem states that if p is prime, then a^(p-1) ≡ 1 (mod p) for any a not divisible by p. Modular exponentiation lets you test this condition quickly for candidate prime numbers, which is the basis for probabilistic primality tests used to generate RSA key material.

Discrete logarithm problems — The security of Diffie-Hellman depends on the difficulty of finding a given g, p, and g^a mod p. This is the discrete logarithm problem, and its hardness is analogous to the factoring problem in RSA.

Step-by-step: computing 4¹³ mod 497 by hand

The exponent 13 in binary is 1101. Square-and-multiply processes bits from most-significant to least-significant:

  1. Start: result = 1, running base = 4
  2. Bit 1 (MSB): square → 1² = 1, multiply → 1 × 4 = 4, reduce mod 497 → 4
  3. Bit 1: square → 4² = 16, multiply → 16 × 4 = 64, reduce mod 497 → 64
  4. Bit 0: square → 64² = 4096, reduce mod 497 → 116 (no multiply, bit is 0)
  5. Bit 1 (LSB): square → 116² = 13456, reduce mod 497 → 445, multiply → 445 × 4 = 1780, reduce mod 497 → 289

The result is 289 using this trace — note that the exact walk depends on which bit-processing order the implementation uses. The calculator handles this precisely and gives the definitive result.