We discuss the Euclidean algorithm and use it to compute the multiplicative inverse.

Multiplicative Inverse. Given x,y∈N+x, y \in \mathbb{N}^+, zz is called the multiplicative inverse of xx (module yy) if

xz≡1(mody). xz \equiv 1 \pmod y.

Theorem 1

xx has a multiplicative inverse module y  ⟺  gcd⁡(x,y)=1y \iff \gcd(x, y) = 1.

Proof.
Define the set S={ax+by∣a,b∈Z}S = \{ ax + by \mid a, b \in Z\}. Let d=a∗x+b∗yd = a^* x + b^* y be the smallest positive integer in SS. Our proof relies on the following lemma.

Lemma: gcd⁡(x,y)=d\gcd(x,y) = d.

First, as dd is a linear combination of xx and yy, gcd⁡(x,y)∣d\gcd(x, y) | d and gcd⁡(x,y)≤d\gcd(x, y) \le d.

Second, we claim that d∣xd | x and d∣yd | y. By symmetry, we prove only the former. If not, suppose that d∤xd \nmid x. Then ∃k∈Z,ℓ∈[1,d−1]\exists k \in \mathbb{Z}, \ell \in [1, d - 1], s.t.,

x=kd+ℓ. x = k d + \ell.

Since ℓ=x−kd\ell = x - k d is a linear combination of xx and yy, it holds that ℓ∈S\ell \in S, contradicting dd being the smallest positive element in SS.

It concludes that dd equals to gcd⁡(x,y)\gcd(x, y).

□\square

The proof of the above theorem immediately follows from the lemma.

  1. gcd⁡(x,y)=1→x\gcd(x, y) = 1 \rightarrow x has a multiplicative inverse.
    In this case, d=a∗x+b∗y=1d = a^* x + b^* y = 1. a∗a^* is th multiplicative inverse of xx module yy.

  2. xx has a multiplicative inverse →gcd⁡(x,y)=1\rightarrow \gcd(x, y) = 1.
    If xx has a multiplicative inverse, there exists some zz and ll, such that zx=ly+1zx = ly + 1.

■\blacksquare

Theorem 2

If xx has a multiplicative inverse module yy, it is unique.

Proof.
Let aa and bb both be the multiplicative inverse of xx module yy, then

a≡a(xb)≡(ax)b≡b(mody) a \equiv a (xb) \equiv (ax) b \equiv b \pmod y

■\blacksquare

Basic Euclidean Algorithm

Euclidean algorithm computes gcd⁡(x,y)\gcd(x, y) as follows:

 unsigned gcd(unsigned x, unsigned y) {
     if(y == 0) return x;
     return gcd(y, x % y);
 }

Correctness

If y=0y =0, whether x=0x = 0 or not, we have gcd⁡(x,y)=x\gcd(x, y) = x.

If x<yx < y, xmod  y=xx \mod y = x. The algorithm exchanges xx and yy at the first recursion. It suffices to consider only x≥y>0x \ge y > 0.

If x≥y>0x \ge y > 0, then gcd⁡(x,y)=gcd⁡(y,xmod  y)\gcd(x, y) = \gcd(y, x \mod y). Let d=gcd⁡(x,y)d = \gcd(x, y). Clearly d∣xd | x and d∣yd | y. We claim that

d∣(xmod  y) d | (x \mod y)

Suppose x=kdx = k d and y=ℓdy = \ell d, then xmod  y=d⋅(kmod  ℓ)x \mod y = d \cdot (k \mod \ell), which finishes proof.

Time

Let nn be the the total number of bits to represent xx and yy. Then the algorithm stops after O(n)O(n) recursions.

Proof.

We can assume x≥yx \ge y. Otherwise, they are swapped by a recursive call.

The key observation is that, after every two recursive calls, the first argument xx shrinks at least by a factor of two.

  1. If y≤x/2y \le x / 2, the observation holds after the first call.
  2. If y>x/2y > x / 2, then after two recursive call, the first argument becomes xmod  yx \mod y, which is smaller than x/2x / 2.

□\square

Extended Euclidean Algorithm

We extend the basic version to return a pair a∗,b∗∈Na^*, b^* \in \mathbb{N} s.t. gcd⁡(x,y)=d=a∗x+b∗y\gcd(x, y) = d = a^* x + b^* y.

 std::pair<unsigned, unsigned> gcd(unsigned x, unsigned y) {
     if(y == 0) return {1, 0};
     auto (a', b') = gcd(y, x mod y);
     return (b', a' - b' * (x / y));
 }

Correctness

Suppose s=a′y+b′⋅(xmod  y)s = a' y + b' \cdot (x \mod y). Then

s=a′y+b′(x−⌊x/y⌋⋅y)=b′x+(a′−b′⋅⌊x/y⌋)⋅y s = a' y + b'(x - \lfloor x / y \rfloor \cdot y) =b' x + (a' - b' \cdot \lfloor x / y \rfloor) \cdot y

This is why the algorithm work.

□\square

Example

gcd⁡(9,6)=(3,1,−1)[−1=0−1∗9/6]→gcd⁡(6,3)=(3,0,1)[1=1−0∗6/3]→gcd⁡(3,0)=(3,1,0) \begin{aligned} & \gcd(9, 6) = (3, 1, -1) \quad [-1 = 0 - 1 * 9 / 6] \\ & \rightarrow \gcd(6, 3) = (3, 0, 1) \quad [1 = 1 - 0 * 6 / 3]\\ & \quad \rightarrow \gcd(3, 0) = (3, 1, 0) \\ \end{aligned}