Number Theory
Modular Multiplicative Inverse

Modular Multiplicative Inverse

Suppose for an integer aa there exists integer bb such that ab=1a * b = 1, then bb is called multiplicative inverse of aa. Same idea can be extended to modular arithmetic as well.

Definition

For an integer aa, under modulo mm, multiplicative inverse of aa is defined as :-
(ab)%m=1(a * b) \,\%\, m = 1

Formally, if you have two integers aa and mm, bb is said to be modular multiplicative inverse of aa under modulo mm if it satisfies the following equation: ab1modma * b \equiv 1 \,mod\, m

Modular Division

To perform modular division, we usually start by finding modular multiplicative inverse of divisor as explained below in equation

  • (a/b)%m(a / b) \,\%\, m = (ab1)%m(a * b ^ {-1}) \,\%\, m

Modular multiplicative inverse lies in range [1,m1][1, m - 1] if it exists

Existence of Modular Multiplicative Inverse

Modular multiplicative inverse of aa under modulo mm exists, iff
gcd(a,m)=1gcd(a, m) = 1 i.e aa and mm are co-prime

Proof using Bézout's Identity:

If aa and mm are coprime, by Bézout's identity: There exist integers xx and yy such that: ax+my=1ax + my = 1

Rearranging the equation:
ax=1mya * x = 1 - m * y
ax=1+(y)ma * x = 1 + (-y) * m
ax1(modm)a * x \equiv 1 \pmod{m}

\therefore xx is the modular multiplicative inverse of aa modulo mm Because when we divide both sides by mm, we get a remainder of 1

The converse:
If a modular multiplicative inverse xx exists: ax1(modm)a * x \equiv 1 \pmod{m} This means ax=1+kma * x = 1 + k * m for some integer kk. Upon rearranging equation we get
axkm=1a * x - k * m = 1
gcd(a,m)\therefore gcd(a,m) must divide 11
gcd(a,m)=1\therefore gcd(a,m) = 1

This proves both necessity and sufficiency - MMI exists iff aa and mm are coprime.
Example:
For a=3a = 3 and m=7m = 7 Using Extended Euclidean Algorithm: 35+7(2)=13 \cdot 5 + 7 \cdot (-2) = 1 Therefore, 55 is the modular multiplicative inverse of 33 modulo 77
We can verify: 35=151(mod7)3 \cdot 5 = 15 \equiv 1 \pmod{7}