C

C

Return to Articles

Calculating Modular Inverses

Mar 24, 2023

A math notebook and pen

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 aRa \in \mathbb{R}. The inverse of this value can be denoted by a1=1aa^{-1} = \frac{1}{a} such that aa1=1a \cdot a^{-1} = 1. The same concept applies to integers with respect to some modulus mm. The inverse of an integer bZmb \in \mathbb{Z}_m satisfies the the equation bb11(modm)bb^{-1} \equiv 1 \pmod{m}. This value b1b^{-1} is called the modular inverse of b(modm)b\pmod{m}.

Unlike with R\mathbb{R}, not all elements of Zm\mathbb{Z}_m will have an inverse.

Zm\mathbb{Z}_m forms a ring, thus a multiplicative inverse is not guaranteed to exist for each element. By contrast, R\mathbb{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\gcd{(b,m)} = 1. In other words, bb and mm 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)\gcd{(a,b)} for two integers a,ba, b. Then we reverse the process and use substitution to find an equation of the form 1=ax+by1 = ax + by for some integers x,yx, y.

Example

Find 71(mod26) 7^{-1} \pmod{26}: First we apply the standard Euclidean algorithm as if we were trying to find gcd(7,26)\gcd{(7,26)}. Note that while doing this, if gcd(n,m)1\gcd(n,m) \not= 1 then no inverse exists for n(modm)n \pmod{m}.

26=7(3)+57=5(1)+25=2(2)+12=2(1)+0\begin{align} 26 = 7(3) + 5 \\ 7 = 5(1) + 2 \\ 5 = 2(2) + 1 \\ 2 = 2(1) + 0 \end{align}

Notice that we can rewrite equation (3)(3),

5=2(2)+11=52(2) 5 = 2(2) + 1 \Rightarrow 1 = 5 - 2(2)

Let’s repeat this process of rewriting equations to represent their remainder and substitute them back into the equation:

1=52(2)=52[75(1)]=267(3)2(7[267(3)])=267(3)2(7(4)26)=267(11)26(2)=26(3)+7(11)\begin{align*} 1 & = 5 - 2(2) \\ & = 5 - 2[7 - 5(1)] \\ & = 26 - 7(3) - 2(7 - [26 - 7(3)]) \\ & = 26 - 7(3) - 2(7(4) - 26) \\ & = 26 - 7(11) - 26(-2) \\ & = 26(3) + 7(-11) \\ \end{align*}

26(3)+7(11)=1 26(3) + 7(-11) = 1 is the linear combination representing gcd(7,26)=1\gcd{(7,26)} = 1 . From this we can derive the following two congruencies:

7(11)1(mod26)26(3)0(mod26)\begin{gather*} 7({-11}) \equiv 1 \pmod{26} \\ 26(3) \equiv 0 \pmod{26} \end{gather*}

Therefore,

71(mod26)1115\begin{align*} 7^{-1} \pmod{26} & \equiv {-11} \\ & \equiv 15 \end{align*}

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.

{ax1(modn)ny0(modn)[a10n01]\begin{align*} \begin{cases} ax & \equiv 1 \pmod{n} \\ ny & \equiv 0 \pmod{n} \end{cases} \Rightarrow \begin{bmatrix} a & 1 & 0 \\ n & 0 & 1 \end{bmatrix} \end{align*}

Solving this matrix using row addition will gives us the gcd(a,n)gcd{(a,n)} and the values of x,yx, y. However, since the value of yy is irrelevent to the inverse, we can safely ignore the last column and simplify further:

[a10n01][a1n0] \begin{bmatrix} a & 1 & 0 \\ n & 0 & 1 \end{bmatrix} \sim \begin{bmatrix} a & 1 \\ n & 0 \end{bmatrix}

Let’s try to find 231(mod101)23^{-1} \pmod{101} with the matrix method. All we must do is plug the values a=23,n=101a = 23, n = 101 into the above matrix and solve for xx:

[2311010][23194][5994][59413][122413]\begin{align*} \begin{bmatrix} 23 & 1 \\ 101 & 0 \end{bmatrix} &\sim \begin{bmatrix} 23 & 1 \\ 9 & -4 \end{bmatrix} \\ &\sim \begin{bmatrix} 5 & 9 \\ 9 & -4 \end{bmatrix} \\ &\sim \begin{bmatrix} 5 & 9 \\ 4 & -13 \end{bmatrix} \\ &\sim \begin{bmatrix} 1 & 22 \\ 4 & -13 \end{bmatrix} \end{align*}

From this we find that x=22x = 22 is a solution to the congruency 23x1(mod101)23x \equiv 1 \pmod{101}. We can quickly verify the result to confirm that 231(mod101)=2223^{-1} \pmod{101} = 22:

23(22)5061(mod101)\begin{equation*} 23(22) \equiv 506 \equiv 1 \pmod{101} \end{equation*}