So far, so good. Now, how do you go about finding the inverse of a modulo n? There are a couple of ways. Euclids algorithm can also compute the inverse of a number modulo n. Sometimes this is called the extended Euclidean algorithm.

Im not going to prove that it works or give the theory behind it. Details can be found in [863], or in any of the number theory texts previously listed.

The algorithm is iterative and can be slow for large numbers. Knuth showed that the average number of divisions performed by the algorithm is:

.843*log2(n) + 1.47

Solving for Coefficients

Euclids algorithm can be used to solve this class of problems: Given an array of m variables x1, x2,...xm, find an array of m coefficients, u1, u2...um, such that

u1*x1 + ...+ um*xm = 1

Fermats Little Theorem

If m is a prime, and a is not a multiple of m, then Fermats little theorem says

am-1 ≡ 1 (mod m)

(Pierre de Fermat, pronounced Fair-ma,  was a French mathematician who lived from 1601 to 1665. This theorem has nothing to do with his last theorem.)

The Euler Totient Function

There is another method for calculating the inverse modulo n, but its not always possible to use it. The reduced set of residues mod n is the subset of the complete set of residues that is relatively prime to n. For example, the reduced set of residues mod 12 is {1, 5, 7, 11}. If n is prime, then the reduced set of residues mod n is the set of all numbers from 1 to n- 1. The number 0 is never part of the reduced set of residues for any n not equal to 1.

The Euler totient function, also called the Euler phi function and written as φ(n), is the number of elements in the reduced set of residues modulo n. In other words, φ(n) is the number of positive integers less than n that are relatively prime to n (for any n greater than 1). (Leonhard Euler, pronounced Oiler,  was a Swiss mathematician who lived from 1707 to 1783.)

If n is prime, then φ(n) = n- 1. If n = pq, where p and q are prime, then φ(n) = (p- 1)(q- 1). These numbers appear in some public-key algorithms; this is why.

According to Eulers generalization of Fermats little theorem, if gcd(a,n) = 1, then

aφ(n) mod n = 1

Now it is easy to compute a-1 mod n:

x = aφ(n)-1 mod n

For example, what is the inverse of 5, modulo 7? Since 7 is prime, φ(7) = 7- 1 = 6. So, the inverse of 5, modulo 7, is

56-1 mod 7 = 55 mod 7 = 3

Both methods for calculating inverses can be extended to solve for x in the general problem (if gcd(a,n) = 1):

(a*x) mod n = b

Using Eulers generalization, solve

x = (b*aφ(n)-1) mod n

Using Euclids algorithm, solve

x = (b*(a-1 mod n)) mod n

In general, Euclids algorithm is faster than Eulers generalization for calculating inverses, especially for numbers in the 500-bit range. If gcd(a,n) ≠ 1, all is not lost. In this general case, (a*x) mod n = b, can have multiple solutions or no solution.

Chinese Remainder Theorem

If you know the prime factorization of n, then you can use something called the Chinese remainder theorem to solve a whole system of equations. The basic version of this theorem was discovered by the first-century Chinese mathematician, Sun Tse.