Exploring two methods for finding modular inverses.
What is a modular inverse?
You are likely already very familiar with multiplicative inverses from the reals. Consider a∈R. The inverse of this value can be denoted by a−1=a1 such that a⋅a−1=1. The same concept applies to integers with respect to some modulus m. The inverse of an integer b∈Zm satisfies the the equation bb−1≡1(modm). This value b−1 is called the modular inverse of b(modm).
Unlike with R, not all elements of Zm will have an inverse.
Zm forms a ring, thus a multiplicative inverse is not guaranteed to exist for each element. By contrast, R is a field so by definition every non-zero element will have a multiplicative inverse. To quickly check if an inverse exists, you must confirm that gcd(b,m)=1. In other words, b and m must be relatively prime.
Via the Extended Euclidean Algorithm
The Extended Euclidean Algorithm has two parts. In the first part we apply the standard Euclidean algorithm that one would use to find gcd(a,b) for two integers a,b. Then we reverse the process and use substitution to find an equation of the form 1=ax+by for some integers x,y.
Example
Find 7−1(mod26): First we apply the standard Euclidean algorithm as if we were trying to find gcd(7,26). Note that while doing this, if gcd(n,m)=1 then no inverse exists for n(modm).
26=7(3)+57=5(1)+25=2(2)+12=2(1)+0
Notice that we can rewrite equation (3),
5=2(2)+1⇒1=5−2(2)
Let’s repeat this process of rewriting equations to represent their remainder and substitute them back into the equation:
26(3)+7(−11)=1 is the linear combination representing gcd(7,26)=1. From this we can derive the following two congruencies:
7(−11)≡1(mod26)26(3)≡0(mod26)
Therefore,
7−1(mod26)≡−11≡15
The Matrix Method
While the previous method works well, it can get a little complicated for large values and it becomes easy to make mistakes while performing the second portion of the operation. A simpler method is to use matrix interpretation of the algorithm. Essentially, we take the congruencies we found at above and put them in matrix form.
{axny≡1(modn)≡0(modn)⇒[an1001]
Solving this matrix using row addition will gives us the gcd(a,n) and the values of x,y. However, since the value of y is irrelevent to the inverse, we can safely ignore the last column and simplify further:
[an1001]∼[an10]
Let’s try to find 23−1(mod101) with the matrix method. All we must do is plug the values a=23,n=101 into the above matrix and solve for x: