Modular Exponentiation Calculator (a^b mod n)

Compute a^b mod n with square-and-multiply in O(log b). Fermat primality test, multiplicative order, and a complete RSA encryption/decryption demo with steps.

Calculation parameters
Scegli il tipo di calcolo.
Base dell'esponenziazione (intero).
Esponente intero non negativo. Può essere molto grande.
Modulo intero > 1. Nel test di Fermat è il candidato primo.
Primo p per demo RSA.
Primo q per demo RSA.
Esponente pubblico RSA coprimo con (p−1)(q−1).
Messaggio in chiaro (intero < pq).
Was this calculator useful?

Modular exponentiation

Modular exponentiation computes ab mod n — the remainder of ab divided by n — efficiently. It is the fundamental operation of essentially all modern public-key cryptosystems: RSA, Diffie-Hellman, ElGamal, elliptic curves over finite fields. Without a fast algorithm for it, asymmetric cryptography would be computationally impractical.

Naive a^b followed by "% n" is impossible for large exponents: 2^1024 has over 300 decimal digits. The solution is square-and-multiply (or binary exponentiation), reducing complexity from O(b) to O(log b) modular multiplications.

Square-and-multiply

Express exponent b in binary and read it left-to-right (or right-to-left), squaring the result at each bit and multiplying by a when the bit is 1. Formally, if b = ∑ bi·2i:

ab = ab₀ · (a²)b₁ · (a⁴)b₂ · …

After each squaring and multiplication the result is reduced mod n, keeping intermediates below n². Total operations: at most 2·⌊log₂ b⌋. For b = 2^1024, about 2048 modular multiplications — a fraction of a second.

The four modes

  1. Direct a^b mod n: full square-and-multiply trace.
  2. Fermat test: verifies a^(n−1) ≡ 1 (mod n). By Fermat's little theorem any prime p satisfies this for all a coprime to p. An n failing it is definitely composite. Beware of Carmichael numbers (561, 1105, 1729, 2465, ...) which are composite yet pass the test for every coprime a.
  3. Multiplicative order: smallest k > 0 with a^k ≡ 1 (mod n). By Lagrange's theorem in (ℤ/nℤ)* the order divides φ(n). If ord_n(a) = φ(n), a is a primitive root.
  4. RSA demo: full encryption/decryption simulation with primes p, q, public e and message m. Computes n = pq, φ(n), d = e⁻¹ mod φ(n), then c = m^e mod n and verifies m' = c^d mod n.

Fermat's little theorem

If p is prime and gcd(a, p) = 1, then ap−1 ≡ 1 (mod p)

Quick proof: in ℤ/pℤ the numbers 1, 2, …, p−1 form a multiplicative group of order p−1. By Lagrange, every element's order divides the group order, so a^(p−1) = 1. This gives a very fast probabilistic primality test — but not foolproof: Carmichael numbers (discovered 1910) are composite and pass Fermat for every coprime base. Alford-Granville-Pomerance (1994) proved there are infinitely many, but they are rare: the smallest is 561 and only 2163 exist below 10⁹.

The RSA algorithm

RSA (Rivest, Shamir, Adleman, 1977) is the most used public-key cryptosystem:

  1. Key generation: pick large primes p, q (typically 1024 bits each). Compute n = p·q and φ(n) = (p−1)(q−1). Pick e coprime with φ(n) (often e = 65537). Compute d = e⁻¹ mod φ(n) via extended Euclidean.
  2. Public key: (n, e). Private key: (n, d). Primes p, q destroyed after d.
  3. Encryption: c = m^e mod n.
  4. Decryption: m = c^d mod n.

Correctness uses Euler's theorem: m^(e·d) ≡ m (mod n) since e·d ≡ 1 (mod φ(n)). Security rests on the difficulty of factoring n: without p, q (hence φ(n)), deriving d from e needs sub-exponential work (current record is the general number-field sieve, GNFS). RSA-2048 is considered secure for decades.

Other systems using modpow

  • Diffie-Hellman (1976): key agreement via discrete log. Alice/Bob agree on prime p and generator g, exchange g^a mod p and g^b mod p, compute shared g^(ab) mod p.
  • ElGamal (1985): encryption using discrete log. Used in PGP and GPG signatures.
  • DSA: digital signature standard based on discrete log in ℤ/pℤ.
  • Elliptic curves (ECC): replaces a^b mod p with [k]P on elliptic curves, achieving equivalent security with 10× smaller keys. Used by Bitcoin (secp256k1), TLS 1.3, Signal.

Advanced optimisations

  • Montgomery reduction: removes costly divisions via domain transformation.
  • CRT (Chinese remainder theorem): RSA decryption works separately mod p and mod q, then combines — 4× faster than direct mod n.
  • Sliding window: precomputes odd-exponent powers of a (a³, a⁵, …), reducing multiplications by ~20%.
  • Barrett reduction: Montgomery alternative with different latency characteristics.

Updated July 2026

Frequently Asked Questions

Why can't we compute a^b directly then mod n?

For large b, a^b is astronomically large. 2^1024 has 309 decimal digits, not storable in any machine integer. Square-and-multiply keeps intermediates below n², manageable even for multi-thousand-bit n.

Is square-and-multiply optimal?

Asymptotically yes (O(log b)), but refinements like sliding window cut constants by 15-25%. For small fixed exponents (e = 65537 in RSA) optimal addition chains are precomputed.

What is a Carmichael number?

A composite n such that a^(n−1) ≡ 1 (mod n) for every a coprime to n. They fool Fermat's test under any base. Examples: 561 = 3·11·17, 1105, 1729 (Hardy-Ramanujan), 2465. Miller-Rabin beats them.

Why is Miller-Rabin more reliable than Fermat?

Miller-Rabin (1976/1980) adds a non-trivial square root check: if it finds b with b² ≡ 1 (mod n) but b ≠ ±1, n is composite. For every odd composite n, at least 3/4 of bases a detect it. k rounds with random bases give error probability at most (1/4)^k.

How is d = e⁻¹ mod φ(n) computed in RSA?

Extended Euclidean algorithm: find x, y with e·x + φ(n)·y = gcd(e, φ(n)) = 1, giving d = x mod φ(n). Complexity O(log φ(n)). The calculator does this automatically in the RSA demo.

How secure is RSA-2048?

The public factoring record is RSA-829 (829 bits, October 2019). A 2048-bit number would require about 2^112 operations with GNFS — beyond current supercomputers. NIST recommends 2048 bits until 2030, 3072 bits for long-term security.

Why does the direct mode show only log₂(b) steps?

At each step the remaining exponent halves (b → ⌊b/2⌋), reaching zero in log₂(b) iterations. For b = 1000, ≈ 10 steps. For b = 2^1024, 1024 steps — milliseconds on modest hardware.

Comments (0)

No comments yet. Be the first!