Modular Exponentiation Calculator

Square-and-Multiply · BigInt Support · RSA · Diffie-Hellman

Compute ab mod m for arbitrarily large integers using the fast binary (right-to-left) algorithm. Full step-by-step table, binary visualization, and cryptographic applications.

Quick Examples

What Is Modular Exponentiation?

Modular exponentiation is the calculation of ab mod m — raising a base a to an exponent b and keeping only the remainder when divided by modulus m. This operation is a cornerstone of modern number theory and public-key cryptography. No matter how large a and b are, the result is always an integer in the range [0, m−1].

For example, 3644 mod 645 = 36. While the intermediate value 3644 is an astronomically large number (over 300 decimal digits), the answer is simply 36.

Why Naive Computation Fails

The straightforward approach — compute ab first, then take the remainder — is completely impractical for large exponents. The number 21000 already has over 300 decimal digits. RSA uses exponents with thousands of bits, so the naive intermediate value would have more digits than atoms in the observable universe. No computer could store it, let alone compute it.

The key insight is that we can reduce modulo m after every single multiplication, keeping all intermediate values below m2. Combining this with the square-and-multiply trick reduces O(b) multiplications to O(log b).

The Square-and-Multiply (Binary Exponentiation) Algorithm

The algorithm processes the binary representation of b from LSB (least significant bit) to MSB. At step k (corresponding to bit k of b):

  • Always square: compute base = base × base mod m (this accounts for the positional value 2k)
  • If bit k = 1: also multiply result = result × base mod m (include this power in the product)
  • If bit k = 0: skip the multiplication (this power is not in the exponent)

The result accumulates only those powers of a whose corresponding bit in b is 1, exactly matching the binary decomposition b = ∑ bk · 2k.

O(log b) Complexity

The algorithm performs exactly ⌊log2 b⌋ squarings and (popcount(b) − 1) multiplications, where popcount counts the number of 1-bits in b. Since both quantities are O(log b), the total number of modular multiplications is O(log b). Each multiplication involves numbers at most m2 in size, requiring O((log m)2) bit operations. The overall bit complexity is O(log b × (log m)2).

For 2048-bit RSA parameters this means roughly 2000 multiplications of 2048-bit numbers — entirely feasible in milliseconds on any modern processor.

RSA Encryption and Decryption

RSA is the most widely deployed public-key cryptosystem. Key generation selects two large primes p and q, computes n = p × q, and chooses public exponent e coprime to φ(n) = (p−1)(q−1). The private exponent d = e−1 mod φ(n) is computed by the Extended Euclidean Algorithm.

  • Encryption: C = Me mod n
  • Decryption: M = Cd mod n

Both operations are pure modular exponentiation. The security of RSA depends on the fact that factoring n (and thus computing φ(n)) is computationally hard, even though encrypting and decrypting are fast.

Diffie-Hellman Key Exchange

Diffie-Hellman (1976) was the first practical public-key cryptographic protocol. Two parties agree publicly on a large prime p and a primitive root g. Each party selects a private random exponent and publishes their public value. The shared secret is gab mod p, computed by each party from the other's public value. An eavesdropper who sees both public values cannot compute the shared secret without solving the discrete logarithm problem — believed to be computationally hard for carefully chosen p and g.

Fermat's Little Theorem

Fermat's little theorem states: if p is prime and gcd(a, p) = 1, then ap−1 ≡ 1 (mod p). This is a necessary condition for primality (though not sufficient — Carmichael numbers satisfy it for all bases while being composite). The Fermat primality test and the more reliable Miller-Rabin test both use modular exponentiation as their core computation.

Reference Table: Example Computations

abmab mod mb in binarySquarings
36446453610100001009
21010002410103
72561391000000008
2103143211001116
562381102
1234567896991110010008

Frequently Asked Questions

What is modular exponentiation?
Modular exponentiation is the computation of ab mod m — raising a base a to an exponent b and taking the remainder when divided by m. The result is always in [0, m−1]. It is the central operation in RSA, Diffie-Hellman, ElGamal, and many primality tests.
Why not compute ab and then take mod?
For large exponents (RSA uses 65537 or larger) the value ab has millions of digits. No computer can store it. The square-and-multiply algorithm reduces mod m after every multiplication, keeping intermediate values below m2, so all arithmetic stays tractable even for 4096-bit RSA keys.
How does the square-and-multiply algorithm work?
The algorithm scans the binary digits of b from LSB to MSB. At each step k: (1) square the current base mod m to get a2k+1 mod m. (2) If bit k of b is 1, multiply the running result by the current base mod m. This exploits the binary decomposition b = ∑ bk × 2k, so ab = ∏ a2k over all k where bk = 1.
What is the time complexity of modular exponentiation?
The algorithm uses O(log b) modular multiplications — ⌊log2 b⌋ squarings and at most ⌊log2 b⌋ extra multiplications (one per 1-bit). Each multiplication on numbers < m costs O((log m)2) bit operations, giving total bit complexity O(log b × (log m)2). For 2048-bit b and m this is very fast, completing in milliseconds.
How is modular exponentiation used in RSA?
RSA encryption is C = Me mod n and decryption is M = Cd mod n. Both are modular exponentiation. The exponents e and d are large integers (often 17 bits and 2048 bits respectively in real usage). The square-and-multiply algorithm is what makes these operations fast enough to be practical. Without it, RSA would be too slow to use.
What is Fermat's little theorem?
Fermat's little theorem states that if p is prime and gcd(a, p) = 1, then ap−1 ≡ 1 (mod p). This means any base coprime to a prime p, raised to the (p−1)th power, gives 1 mod p. This is the foundation of the Fermat primality test and the RSA exponent choice: the public exponent e must be chosen so that e × d ≡ 1 mod φ(n), relying on Fermat's theorem for correctness.
What is the Diffie-Hellman key exchange?
Diffie-Hellman (DH) allows two parties to establish a shared secret over a public channel. Both agree on prime p and generator g. Alice sends A = ga mod p, Bob sends B = gb mod p. Both compute the shared key K = gab mod p (Alice as Ba mod p, Bob as Ab mod p). An attacker seeing A and B must solve the discrete logarithm to find a or b — a problem for which no efficient classical algorithm is known for properly chosen p.