26.12.2013 Views

Extended Euclidean Algorithm and modular inverses

Extended Euclidean Algorithm and modular inverses

Extended Euclidean Algorithm and modular inverses

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Linear Diophantine Equations <strong>and</strong><br />

Modular Inverses<br />

Solve the linear Diophantine equation<br />

ax + by = 1 (1)<br />

given a, b ∈ Z, b > 0, <strong>and</strong> gcd(a, b) = 1. If gcd(a, b) ≠ 1,<br />

(1) is insoluble. If b < 0, use −b <strong>and</strong> solve for (x, −y).<br />

Diophantine equations are named after Diophantus, a<br />

Greek mathematician who lived around 300-200 BCE.<br />

<strong>Euclidean</strong> <strong>Algorithm</strong><br />

a = bq 0 + r 0<br />

q 0 = ⌊a/b⌋, 0 < r 0 < b<br />

b = r 0 q 1 + r 1 q 1 = ⌊b/r 0 ⌋, 0 < r 1 < r 0<br />

r 0 = r 1 q 2 + r 2<br />

.<br />

q 2 = ⌊r 0 /r 1 ⌋, 0 < r 2 < r 1<br />

r n−3 = r n−2 q n−1 + r n−1 r n−1 = gcd(a, b)<br />

r n−2 = r n−1 q n + r n r n = 0<br />

Repeated division with remainder. Notice that the sequence<br />

of remainders (the r i ) is strictly decreasing, <strong>and</strong><br />

thus the sequence is finite (algorithm terminates).<br />

Theorem 1 (Lamé, 1844). n < 5 log 10 min(a, b).<br />

More exactly, Lamé’s Theorem states n ≤ log τ (min(a, b)+<br />

1) where τ = (1 + √ 5)/2 is the golden ratio.


<strong>Extended</strong> <strong>Euclidean</strong> <strong>Algorithm</strong><br />

Let A −2 = 0, A −1 = 1, B −2 = 1, B −1 = 0 <strong>and</strong><br />

A k = q k A k−1 + A k−2<br />

B k = q k B k−1 + B k−2<br />

for k = 0, 1, . . . . We have A n = a <strong>and</strong> B n = b (n from<br />

above), <strong>and</strong><br />

Putting k = n yields<br />

A k B k−1 − B k A k−1 = (−1) k−1 .<br />

A n B n−1 − B n A n−1 = (−1) n−1<br />

a(−1) n−1 B n−1 + b(−1) n A n−1 = 1 .<br />

Thus, a solution of (1) is given by<br />

x = (−1) n−1 B n−1 , y = (−1) n A n−1 .<br />

Computing x, y in this manner is known as the <strong>Extended</strong><br />

<strong>Euclidean</strong> <strong>Algorithm</strong>.<br />

Modular Inverses<br />

Consider the linear congruence ax ≡ 1 (mod m) given<br />

a, m ∈ Z, gcd(a, m) = 1 (i.e., finding a −1 (mod m)).<br />

We want x such that<br />

m | ax − 1 =⇒ ax − 1 = ym =⇒ ax − my = 1<br />

(can be solved using <strong>Euclidean</strong> <strong>Algorithm</strong>).


Example 1. For 95x ≡ 1 (mod 317), we obtain x =<br />

−10, so x ≡ 307 (mod 317) is a solution.<br />

Linear Congruences<br />

Consider the linear congruence ax ≡ b (mod m) given<br />

a, b, m ∈ Z,. This has a solution if <strong>and</strong> only if gcd(a, m) |<br />

b. We want x such that<br />

m | ax − d =⇒ ax − d = ym =⇒ ax − my = d<br />

Example 2. For 95x ≡ 21 (mod 317), we obtain x =<br />

−10 · 21 ≡ −210 ≡ 107 (mod 317) as a solution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!