Modular Inverse Calculator
- The extended Euclidean algorithm runs the gcd computation on (17, 3,120) and then back-substitutes, finding integers with 17 x (-367) + 3,120 x (2) = 1 - the Bezout identity.
- Reducing the first coefficient modulo 3,120 gives the inverse: 2,753.
- Check: 17 x 2,753 = 46,801, and 46,801 mod 3,120 = 1 - exactly 1, as an inverse must give.
- Modular inverses power RSA key generation, elliptic-curve point arithmetic and every affine cipher - anywhere division has to be replaced by multiplication inside modular arithmetic.
A modular inverse calculator finds the number x that solves a·x ≡ 1 (mod m) for a given integer a and modulus m. In other words, it finds the value that, when multiplied by a and divided by m, leaves a remainder of 1. For example, the modular inverse of 17 modulo 3,120 is 2,753: multiply 17 by 2,753 and you get 46,801, which is 1 modulo 3,120, as the check confirms. This is the exact pair from the classic RSA textbook example. Another familiar case is the inverse of 7 modulo 26, which is 15 — the arithmetic behind affine ciphers. Not every pair has an answer: when the numbers share a factor, no inverse exists. Since 12 and 30 share a gcd of 6, the equation 12x ≡ 1 (mod 30) has no solution.
The inverse is found with the extended Euclidean algorithm, the accepted standard for this kind of calculation because it works directly from the greatest common divisor and produces the Bezout identity as a by-product — so it both finds the inverse and reveals when none exists. This page shows the formula, the step-by-step workings, worked examples and sources, so you can follow each stage of the calculation rather than just read off a result.
Worked examples
17 modulo 3120 - the RSA textbook pair
Modular inverse = 2,753. Check a x inv mod m: 1; Bezout coefficient: -367. (17 · 3120)
| Modular inverse | 2,753 |
| Check a x inv mod m | 1 |
| Bezout coefficient | -367 |
7 modulo 26 - affine cipher arithmetic
Modular inverse = 15. Check a x inv mod m: 1; Bezout coefficient: -11. (7 · 26)
| Modular inverse | 15 |
| Check a x inv mod m | 1 |
| Bezout coefficient | -11 |
12 modulo 30 - no inverse exists
Modular inverse = No inverse exists. gcd(a, m): 6. (12 · 30)
| Modular inverse | No inverse exists |
| gcd(a, m) | 6 |
How the modular inverse is calculated
The extended Euclidean algorithm is the standard way to find a modular inverse, and it does two jobs at once: it confirms that the two numbers are coprime, and it produces the Bezout identity as a by-product. The Bezout identity expresses the greatest common divisor of two numbers as a whole-number combination of them. When the gcd is 1, the coefficients in that identity hand you the inverse directly. A modular inverse calculator simply runs this process and then reduces the coefficient into the correct range.
Take the pair 17 and 3,120, the exact numbers from the classic RSA textbook example. The Euclidean steps are: 3,120 = 183 × 17 + 9; 17 = 1 × 9 + 8; 9 = 1 × 8 + 1; 8 = 8 × 1 + 0. The last nonzero remainder is 1, so 17 and 3,120 are coprime and an inverse exists. Now back-substitute. From 9 = 1 × 8 + 1 we get 1 = 9 − 8. From 17 = 1 × 9 + 8 we get 8 = 17 − 9, so 1 = 9 − (17 − 9) = 2 × 9 − 17. From 3,120 = 183 × 17 + 9 we get 9 = 3,120 − 183 × 17, so 1 = 2 × (3,120 − 183 × 17) − 17 = 2 × 3,120 − 367 × 17.
That last line is the Bezout identity: 1 = 2 × 3,120 − 367 × 17. Read it modulo 3,120 and the 2 × 3,120 term vanishes, leaving −367 × 17 ≡ 1 (mod 3,120). So −367 is a valid coefficient, but it is negative. Reduce it by adding 3,120 once: −367 + 3,120 = 2,753. The modular inverse of 17 modulo 3,120 is therefore 2,753, and the check confirms it: 17 × 2,753 = 46,801, which is 1 modulo 3,120. The same routine gives the inverse of 7 modulo 26 as 15, the arithmetic behind affine ciphers.
The algorithm also tells you when to stop. If the gcd turns out to be greater than 1, no inverse exists, because the numbers share a factor. For example, 12 and 30 share a gcd of 6, so 12x = 1 (mod 30) has no solution — no multiple of 12 can land one step past a multiple of 30. Running the Euclidean steps first, before any back-substitution, is what lets a modular inverse calculator report this cleanly instead of returning a meaningless coefficient.
When no inverse exists
A modular inverse calculator can only return an answer when the inverse actually exists, and that is not guaranteed. The condition is coprimality: a number a has an inverse modulo m only when the greatest common divisor of a and m is 1. When a and m share a factor larger than 1, no integer x can satisfy ax ≡ 1 (mod m), and the calculator should tell you so rather than produce a meaningless result.
The clearest example is the pair 12 and 30. They share a gcd of 6, so the equation 12x = 1 (mod 30) has no solution. You can see why without any machinery: every multiple of 12 is even, and every number congruent to 1 modulo 30 is also a multiple of 6 plus 1, which is never divisible by 6. Dividing through by the shared factor 6 turns the equation into 2x ≡ 1 (mod 5), and 2 has no inverse modulo 5 because 2 and 5 are coprime — wait, that last step is wrong; the honest version is simpler. Since 6 divides both 12 and 30, any value of 12x is a multiple of 6, while 1 is not, so the two sides can never meet. The shared factor blocks the inverse, and the calculator detects it by computing the gcd before doing anything else.
When the gcd is not 1, the fix is to change the modulus, not the number. Pick a modulus that is coprime to your a. If a is 12, a modulus of 35 works because gcd(12, 35) = 1, and an inverse exists. If a is 17, the classic RSA textbook example uses modulus 3,120, and the inverse is 2,753: the check confirms that 17 × 2,753 = 46,801, which is 1 modulo 3,120. The affine cipher case is the same idea in miniature — the inverse of 7 modulo 26 is 15, and 7 and 26 are coprime. In each case, the modulus was chosen so that it shares no factor with the number being inverted.
The tool that finds the inverse is the extended Euclidean algorithm, which produces the Bezout identity as a by-product. That identity expresses the gcd of the two numbers as a combination of them, and when the gcd is 1 the coefficients in that combination give you the inverse directly. Running the same algorithm is also how a modular inverse calculator checks the coprimality condition in the first place: if the algorithm ends with a gcd other than 1, there is no inverse to report.
- 12 and 30 share a gcd of 6, so 12x = 1 (mod 30) has no solution.
- A modulus coprime to your number restores the inverse — for example, 12 modulo 35.
- The extended Euclidean algorithm finds the inverse and yields the Bezout identity as a by-product.
Modular inverses in RSA
RSA is the best-known place where a modular inverse calculator does real cryptographic work. In RSA, two exponents are chosen so that multiplying them gives 1 modulo the totient of the modulus. One exponent is public and is used to encrypt; the other is private and is used to decrypt. The private exponent is exactly the modular inverse of the public exponent modulo the totient, and finding it is a single modular inverse calculation.
The classic textbook example makes this concrete. Take a public exponent of 17 and a totient of 3,120. The modular inverse of 17 modulo 3,120 is 2,753. That is the exact pair used in the standard RSA illustration: 17 x 2,753 = 46,801, and 46,801 is 1 modulo 3,120, as the check confirms. So 2,753 is the private exponent paired with the public exponent 17. Anyone who knows the public exponent and the totient can compute the private exponent; anyone who knows only the public exponent and the modulus is not supposed to be able to.
The same operation appears in older, simpler ciphers. The inverse of 7 modulo 26 is 15, and that is the arithmetic behind affine ciphers, where a letter is multiplied by 7 and shifted, and decryption multiplies by 15 to undo it. The RSA case and the affine case are the same problem with different numbers: find x such that a times x is 1 modulo m.
Not every pair has an inverse. No inverse exists when the numbers share a factor: 12 and 30 share a gcd of 6, so 12x = 1 (mod 30) has no solution. This is why RSA requires the public exponent to be coprime to the totient — if it were not, the private exponent would not exist at all. Checking the gcd before searching for an inverse saves wasted work and explains a failed calculation immediately.
The inverse is found with the extended Euclidean algorithm, which produces the Bezout identity as a by-product. Run it on 17 and 3,120 and the coefficients it returns include 2,753, the inverse. Run it on 12 and 30 and it returns a gcd of 6 rather than 1, which is the signal that no inverse exists. A modular inverse calculator simply automates that procedure, so you can check the 17 and 2,753 pair, test the affine-cipher pair 7 and 26, or confirm that 12 has no inverse modulo 30.
Checking an inverse by hand
You do not have to trust a modular inverse calculator's answer. Every result can be verified with one multiplication, and that same check is the one the calculator prints for every result. The rule is simple: if inv is the inverse of a modulo m, then a × inv mod m must equal 1. Multiply the two numbers, divide by the modulus, and confirm that the remainder is exactly 1. If it is, the pair is correct. If it is not, the inverse is wrong, no matter how the number was produced.
The classic RSA textbook example shows how this works with larger numbers. The modular inverse of 17 modulo 3,120 is 2,753. To check it, multiply 17 by 2,753: the product is 46,801. Now reduce 46,801 modulo 3,120. Since 3,120 × 15 = 46,800, the remainder is exactly 1. That confirms the pair. The check is not a separate or approximate test; it is the definition of the inverse restated as arithmetic you can do yourself, and it is the same line the calculator prints each time.
Smaller cases behave the same way. The inverse of 7 modulo 26 is 15, the arithmetic behind affine ciphers. Multiply 7 by 15 to get 105. Since 26 × 4 = 104, the remainder is 1, so 15 is confirmed as the inverse of 7 modulo 26. An affine cipher uses exactly this relationship to shift and unscramble letters, so a single multiplication catches an error before it reaches the encryption step.
The check also explains when no inverse can exist. If a and m share a factor greater than 1, no number can multiply a to leave a remainder of 1. Take 12 and 30: they share a gcd of 6, so 12x = 1 (mod 30) has no solution. Any product of 12 and a whole number is a multiple of 6, and a multiple of 6 can never be 1 more than a multiple of 30. The back-multiplication check fails for every candidate, which is why the calculator reports that no inverse exists rather than returning a number.
Behind the scenes, the inverse is found with the extended Euclidean algorithm, which produces the Bezout identity as a by-product. That identity expresses the gcd of a and m as a combination of the two numbers, and when the gcd is 1, the coefficients in that combination give the inverse directly. The printed check is what ties the method to the result: whatever route the algorithm takes, the final answer must survive the multiplication test. When you use a modular inverse calculator, read the check line first, then reproduce it by hand if you want full confidence in the pair.
FAQs
What is the modular inverse of 17 mod 3120?
The modular inverse of 17 modulo 3,120 is 2,753. You can check it by multiplying: 17 x 2,753 = 46,801, which is congruent to 1 modulo 3,120, exactly as an inverse must be. That check is the definition at work: the product of a number and its inverse leaves a remainder of 1.
How do I find a modular inverse by hand?
Use the extended Euclidean algorithm to write the Bezout identity, then reduce the first coefficient modulo m. The algorithm tracks the combinations of a and m as it works down to their greatest common divisor. Once you have the identity, reducing that first coefficient modulo m gives you the inverse, and you can confirm it by multiplying.
Why does 12 have no inverse mod 30?
Because gcd(12, 30) = 6, not 1. Every multiple of 12 modulo 30 lands on a multiple of 6, so no multiple of 12 can ever land on 1 itself. Since an inverse would need to produce a remainder of 1, 12 simply has no inverse modulo 30. The shared factor of 6 blocks it.
When does a modular inverse exist?
A modular inverse exists exactly when a and m are coprime, meaning their greatest common divisor is 1. If they share any factor larger than 1, no inverse can exist. A prime modulus works for every a it does not divide, since a prime shares no factor with any number other than its own multiples.
Where are modular inverses used?
Modular inverses appear in RSA key generation, elliptic-curve arithmetic and affine ciphers. In the cipher case, the inverse of 7 modulo 26 is 15, which is what decryption uses. These applications all rely on division modulo m, and division modulo m means multiplying by the modular inverse instead.
Is the modular inverse unique?
Yes. Modulo m there is exactly one such value when it exists. For example, in the cipher example the inverse of 7 modulo 26 is 15, and no other value modulo 26 plays that role. Uniqueness follows from coprimality: once a and m are coprime, only one residue satisfies the inverse condition.