Multiplicative Inverses via Euclid`s Algorithm Of course, the key was

advertisement
Lecture 9, 09-14-11
CS 2050, Intro Discrete Math for Computer Science
Multiplicative Inverses via Euclid’s Algorithm
Suppose that we want to find the multiplicative inverse of 79 in arithmetic modulo 1249 (where
1249 is a prime number, hence this multiplicative inverse exists and it is unique). So we are looking
for z such that
z × 79 ≡ 1 mod 1249 .
The approach is to use quantities involved in the computation of gcd(1249,79)=1 and write 1 as
a linear combination of 79 and 1249. The multiplicative inverse of 79 follows immediately, by
considering all quantities mod 1249. In particular, we will see below that we can write:
1 = − 332 × 79 + 21 × 1249 .
This immediately implies
1 ≡ − 332 × 79 mod 1249 .
Equivalently, since
−332 ≡ (1249−332) mod 1249 ,
or
−332 ≡ 917 mod 1249
we get
1 ≡ 917 × 79 mod 1249 .
Thus the multiplicative inverse of 79 in arithmetic modulo 1249 is 917.
Of course, the key was to write 1 = −332 × 79 + 21 × 1249.
How did we find the numbers -332 and 21?
1
We found the numbers -332 and 21 in the equation
1 = −332 × 79 + 21 × 1249
by working bottom-up with dividors, quotients and remainders
appearing in Euclid’s algorithm, while computing gcd(1249,79):
1249
79
64
15
4
3
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
−1 × 3
=
=
=
=
=
=
quotient
15
1
4
3
1
3
+
×
×
×
×
×
×
×
dividor
79
64
15
4
3
1
4
+
+
+
+
+
+
remainder
64
15
4
3
1
0
(Line
(Line
(Line
(Line
(Line
1)
2)
3)
4)
5)
from Line 5: 3 is the dividor and 4 is left-hand-side
in Line 4: 3 is the remainder and 4 is the dividor
−1 × (−3 × 4 + 15)
+
4
from Line 4: substitute the remainder 3
((−1) × (−3) + 1) × 4
+ (−1) × 15
rearrange so as to keep only 4 and 15 because:
4×4
+ (−1) × 15 in Line 4: 4 is the dividor and 15 is the left-hand-side
in Line 3: 4 is the remainder and 15 is the dividor
4 × (−4 × 15 + 64)
+ (−1) × 15
from Line 3: substitute the remainder 4
(4 × (−4) + (−1)) × 15
+
4 × 64
rearrange so as to keep only 15 and 64 because:
(−17) × 15
+
4 × 64
in Line 3: 15 is the dividor and 64 is the left-hand-side
in Line 2: 15 is the remainder and 64 is the dividor
−17 × (−1 × 64 + 79)
+
4 × 64
from Line 2: substitute the remainder 15
((−17) × (−1) + 4) × 64 + (−17) × 79
rearrange so as to keep only 64 and 79 because:
21 × 64
+ (−17) × 79 in Line 2: 64 is the dividor and 79 is the left-hand-side
in Line 1: 64 is the remainder and 79 is the dividor
21 × (−15 × 79 + 1249) + (−17) × 79
from Line 1: substitute 64
(21 × (−15) + (−17)) × 79 + 21 × 1249
rearrange so as to keep only 79 and 1249 because
−332 × 79
+ 21 × 1249
this was our original goal
2
Let us re-examine Euclid’s algorithm:
procedure gcd(a, b: positive integers);
x1 := a ;
y1 := b ;
q1 := x1 div y1 ;
r1 := x1 mod y1 ;
k := 1 ;
while rk 6= 0
begin
k := k + 1 ;
xk := yk−1 ;
Remark: Realize that, for all i, this assignment implies:
yk := rk−1 ;
yi = ri−1 and xi = ri−2 .
qk := xk div yk ;
rk := xk mod yk ;
end;
return(yk );
%comment: if k > 1 then yk = rk−1 ;
Therefore, in execution we will get:
x1
y1
r1
r2
r3
rk−4
rk−3
rk−2
q1 × y 1
q 2 × r1
q 3 × r2
q 4 × r3
q 5 × r4
...
= qk−2 × rk−3
= qk−1 × rk−2
= qk × rk−1
=
=
=
=
=
+
+
+
+
+
r1
r2
r3
r4
r5
Remark: Realize that, for all i, we have
ri = −qi × ri−1 + ri−2 .
+ rk−2
+ rk−1 Remark: rk−1 = gcd(x1 , y1 ) = gcd(a, b)
+ 0
L
Thus, at the (k−1)st line, for λQ
k−1 = −qk−1 and λk−1 = 1 we have
rk−1 = gcd(a, b) = −qk−1 × rk−2 +
rk−3
Q
L
= λk−1 × rk−2 + λk−1 × rk−3
L
Now supposing that, at the i-th line, for some λQ
i and λi we could write
L
gcd(a, b) = λQ
i × ri−1 + λi × ri−2
and recalling that, for all i, ri = −qi × ri−1 + ri−2 , thus also ri−1 = −qi−1 × ri−2 + ri−3 we get
L
gcd(a, b) = λQ
i × ri−1 + λi × ri−2
= λQ
× (−qi−1 × ri−2 + ri−3 ) + λLi × ri−2
i
L
= −λQ
×
q
+
λ
× ri−2 + λQ
i−1
i
i
i × ri−3
Q
Q
L
L
which implies that, at the (i−1)-st line, for λQ
i−1 = (−λi × qi−1 + λi ) and λi−1 = λi we can write
L
gcd(a, b) = λQ
i−1 × ri−2 + λi−1 × ri−3 .
3
procedure mult-inverse(a, b : positive integers);
q1 := a div b ;
r1 := a mod b ;
k := 1 ; q0 := a ; r0 := b ;
while rk 6= 0
begin
k := k + 1 ;
qk := rk−2 div rk−1 ;
rk := rk−2 mod rk−1 ;
end; %comment: gcd(a, b) = rk−1 ;
L
while i > 1
λQ
k−1 := −qk−1 ; λk−1 := 1 ; i := k−1 ;
begin
Q
L
λQ
i−1 := −λi × qi−1 + λi ;
λLi−1 := λQ
i ;
i := i−1 ;
end;
if gcd(a, b) = 1 then return(λQ
1 mod a) else return(error: gcd(a, b) 6= 1);
On input 1249, 79, the above algorithm computes:
k = 1, q0 = 1249, r0 = 79, q1 = 15, r1 = 64,
k = 2, q2 = 1 r2 = 15,
k = 3, q3 = 4 r3 = 4,
k = 4, q4 = 3 r4 = 3,
k = 5, q5 = 1 r5 = 1,
k = 6, q6 = 3 r6 = 0, hence gcd(1249, 79) = 1,
L
i = 5, λQ
5 = (−1), λ5 = 1,
Q
i = 4, λ4 = 4, λL4 = (−1),
L
i = 3, λQ
3 = (−17), λ3 = 4,
L
i = 2, λQ
2 = 21, λ5 = (−17),
Q
i = 1, λ1 = (−332), λL1 = 21,
return((-332) mod 1429)
return(917)
4
Download