Modular Inverse Calculator

Find the modular inverse of a (mod m), perform modular arithmetic operations, and solve modular equations step by step using the Extended Euclidean Algorithm.

Find a⁻¹ (mod m) — the number x such that a × x ≡ 1 (mod m).

Find x such that a × x ≡ 1 (mod m)
Calculates (a OP b) mod m. Division uses modular inverse.
Calculate b^e mod m efficiently using fast exponentiation.
Find GCD(a, b) and integers x, y such that ax + by = GCD(a, b) (Bézout's identity).

How to use

  • Modular Inverse
    Find a⁻¹ (mod m) — the number x such that a × x ≡ 1 (mod m). The inverse exists only when GCD(a, m) = 1 (a and m are coprime).
  • Modular Arithmetic
    Perform addition, subtraction, multiplication, or division modulo m. Division is computed using the modular inverse of the divisor.
  • Modular Exponentiation
    Calculate b^e mod m efficiently using the square-and-multiply algorithm. Essential for large exponents used in RSA cryptography.
  • GCD & Bézout's Identity
    Find the Greatest Common Divisor of two numbers and the Bézout coefficients x, y such that ax + by = GCD(a, b).

Key Formulas

a × x ≡ 1 (mod m) [inverse]
GCD(a, m) = 1 [inverse exists iff coprime]
(a + b) mod m = ((a mod m) + (b mod m)) mod m
(a × b) mod m = ((a mod m) × (b mod m)) mod m
b^e mod m [fast exponentiation]
ax + by = GCD(a, b) [Bézout's identity]
💡 Applications
RSA public-key cryptography
Chinese Remainder Theorem
Cryptographic hash functions
Error-correcting codes
Competitive programming

What is a Modular Inverse?

The modular inverse of a modulo m is the number x such that a × x ≡ 1 (mod m). It's the modular arithmetic equivalent of the reciprocal — just as 3 × (1/3) = 1 in regular arithmetic, 3 × x ≡ 1 (mod 7) has a solution x = 5 because 3 × 5 = 15 ≡ 1 (mod 7). The inverse exists if and only if GCD(a, m) = 1 — that is, a and m are coprime.

The Extended Euclidean Algorithm

The most efficient way to find a modular inverse is the Extended Euclidean Algorithm. It extends the standard Euclidean algorithm to find integers x and y such that ax + my = GCD(a, m). When GCD(a, m) = 1, this gives ax ≡ 1 (mod m), so x is the modular inverse. The algorithm runs in O(log min(a, m)) time.

Frequently Asked Questions

When does a modular inverse not exist?

The modular inverse of a (mod m) does not exist when GCD(a, m) > 1 — that is, when a and m share a common factor greater than 1. For example, 6⁻¹ (mod 9) does not exist because GCD(6, 9) = 3 ≠ 1. The inverse always exists when m is prime and a is not a multiple of m.

How is modular inverse used in RSA cryptography?

RSA encryption relies on modular inverse to generate the private key. Given public exponent e and totient φ(n), the private key d is the modular inverse of e (mod φ(n)) — that is, e × d ≡ 1 (mod φ(n)). Decrypting a message encrypted with e uses d to reverse the operation, exploiting the difficulty of factoring large numbers.

What is Fermat's Little Theorem method?

When m is prime, Fermat's Little Theorem provides a fast way to find the modular inverse: a⁻¹ ≡ a^(m−2) (mod m). This works because a^(m−1) ≡ 1 (mod m) for prime m when GCD(a,m)=1, so a × a^(m−2) ≡ 1. This can be computed efficiently using fast exponentiation.

What is Bézout's identity?

Bézout's identity states that for any integers a and b, there exist integers x and y such that ax + by = GCD(a, b). The Extended Euclidean Algorithm finds these coefficients. When GCD(a, m) = 1, this gives ax + my = 1, which means ax ≡ 1 (mod m) — so x is the modular inverse of a.

Related Calculators