Skip to main content

Inverse

The next operation we want to discuss is the inverse operation. The inverse of addition is subtraction, which can be done easily. But the concept of multiplicative inverse in modular arithmetic is less clear (for real numbers, the inverse of xx is simply 1/x1/x). Being able to compute an inverse is important in many algorithmic applications, and we will cover the theories behind it.

We say that xx is a multiplicative inverse of aa modulo mm if ax1(modm)ax \equiv 1 \pmod{m}. Does an inverse always exist? Let us try some simple examples.

Example 18

When m=5m=5, every nonzero number has an inverse. Inverses of 1,2,3,41,2,3,4 are 1,3,2,41,3,2,4 respectively. When m=4m=4, notice that 22 and 44 do not have an inverse, while 11 and 33 do (inverses are 11 and 33 respectively).

Let us start with a simple observation, which is easy to prove.

Exercise 47

If aa has a multiplicative inverse modulo mm, then gcd(a,m)=1{\sf gcd}(a,m) = 1.

So we know that gcd(a,m)=1{\sf gcd}(a,m) =1 is necessary for the existence of an inverse. It turns out that this is also sufficient.

Lemma 10

If gcd(a,m)=1{\sf gcd}(a,m) =1, then aa has a unique multiplicative inverse modulo mm.

Proof:

Consider multiplying aa by every number in modulo mm (i.e., 0,,m10,\ldots, m-1). So, we get 0,a,2a,,(m1)a0, a, 2a, \ldots, (m-1)a. We claim that these numbers are all distinct under modulo mm. Assume for contradiction that axay(modm)a x \equiv ay \pmod{m} for m1x>y0m-1 \ge x > y \ge 0. This would imply that m(xy)am \mid (x-y)a or mq=(xy)am q = (x-y)a for some qq. Since mm and aa do not share any common divisor (greater than 11), we know that m(xy)m \mid (x-y), a contradiction since xyx-y ranges between 11 and m1m-1.

The uniqueness of inverses allows us to write things like a1(modm)a^{-1} \pmod{m}. If we did not know uniqueness, such notation would not make sense.

Now we present an algorithm that computes the multiplicative inverse of aa modulo mm. Before going into an abstract setting, let us look at a simple example on how to derive an inverse. Let us try to compute the inverse of 1212 modulo 3535. Let xx be an inverse (we know it exists). We set up two equations that we know:

35x0(mod35),12x1(mod35)35x \equiv 0 \pmod{35}, 12x \equiv 1 \pmod{35}

By subtracting twice the second equation from the first, we have:

11x2(mod35)11 x \equiv -2 \pmod{35}

By subtracting this one from the above,

x3(mod35)x \equiv 3 \pmod{35}

At this point, let us warn the students that such derivations (by manipulating the equations) in general may not necessarily find a solution. The derivation gives us a necessary condition that, if an inverse exists, it must be equal to 33 under modulo 3535. This fact, in combination with the existence of the inverse (guaranteed by Lemma 10), implies that 33 is an inverse and the only one (modulo 3535). The lesson here is that such derivations alone do not always find all solutions we are looking for: It is the combination between mathematical derivation and the existential theorem that allows us to make a conclusion.

Now, let us turn the above example into a more general algorithm, which finds a1(modm)a^{-1} \pmod{m}, provided this exists.

Inverse(a,m)\mathrm{Inverse}(a,m)

  1. Aa,Bm,C1,D0A\leftarrow a, B \leftarrow m, C \leftarrow 1, D \leftarrow 0.
  2. while B1B\geq 1 do
    1. RA(modB)R \leftarrow A \pmod{B}
    2. ABA \leftarrow B, BRB \leftarrow R, CDC \leftarrow D and DCDA/BD \leftarrow C - D \cdot \lfloor A/B\rfloor
  3. return CC

Notice the simplicity of the above algorithm and its similarity to the Euclid's algorithm for computing the greatest common divisor. Its correctness follows from a simple loop invariant:

Theorem 17

Let xx be the multiplicative inverse a1a^{-1}. The following is loop invariant:

AxC(modm) and BxD(modm)Ax \equiv C \pmod{m} \text{ and } B x \equiv D \pmod{m}

Notice that this loop invariant implies the correctness of our algorithm. First it holds before the algorithm enters the loop since we have ax1(modm)a x \equiv 1 \pmod{m} (here we use the existence of the inverse) and mx0(modm)mx \equiv 0 \pmod{m}. When the algorithm terminates, we have that A=1A= 1 and B=0B=0, so by the loop invariant, xC(modm)x \equiv C \pmod{m} as desired.

Proof:

Assume that the statement holds before entering the loop. Let A,B,C,DA',B',C',D' be the values of A,B,C,DA,B,C,D after the end of the loop. The loop made the update so that A=BA' = B and C=DC'=D, so we have that AxC(modm)A' x \equiv C'\pmod{m}. Notice that B=R=ABA/BB'=R = A - B \cdot \lfloor A/B\rfloor. By multiplying BxD(modm)B x \equiv D \pmod{m} by A/B\lfloor A/B\rfloor and subtracting it from AxC(modm)Ax \equiv C \pmod{m}, we have:

(ABA/B)x(CDA/B)(modm)(A-B\cdot \lfloor A/B\rfloor) x \equiv (C-D \cdot \lfloor A/B\rfloor) \pmod{m}

In other words, BxD(modm)B'x \equiv D' \pmod{m}, concluding the proof.

Exercise 48

Prove that INVERSE can be implemented in O(n3)O(n^3) time, when aa and mm are nn-digit numbers.