Modulo Arithmetic GCD - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Modulo Arithmetic GCD

Description:

Finding GCD requires Euclid's algorithm: O(n3) ... Extended Euclid Example. gcd2(25,11) gcd(11, 25 = 3) gcd(3, 11%3 = 2) gcd(2, 3%2 = 1) ... – PowerPoint PPT presentation

Number of Views:423
Avg rating:3.0/5.0
Slides: 26
Provided by: rivit
Category:

less

Transcript and Presenter's Notes

Title: Modulo Arithmetic GCD


1
Modulo ArithmeticGCD CertificatesPrimality
2
Modulo
  • Division 23 / 5 4
  • Modulo 23 5 3
  • Invariant b(a/b) ab a
  • Thus, a-(a/b)b ab
  • What is -23 / 5?
  • Sometimes -4, sometimes -5
  • What is -23 5?
  • 5(-4) 3 or 5(-5) 2

3
Modular Arithmetic
  • Modulo has strict math
  • Expressions modified by (mod N)
  • -23 7 (mod 5)
  • Book also uses operator mod
  • Same as C/Java/Cs but always returns a
    positive number
  • stuff nonsense (mod N)
  • means
  • stuff mod N nonsense mod N

4
Properties of mod
  • Substitution
  • Given ab(mod N) and xy(mod N)
  • Then ax by (mod N)
  • And ax by (mod N)
  • It is well-behaved
  • x(yz) (xy)z (mod N)
  • xy yx (mod N)
  • x(yz) xy xz (mod N)

5
Properties of mod
  • Prove (if there is time)
  • (a b) N (a N b N) N

6
Modular Exponentiation
  • How can we compute xy mod N?

7
Modular Exponentiation
  • int expmod(int x, int y, int N)
  • if (y 0) return 1
  • int z expmod(x,y/2,N)
  • if (y 1) return (zz) N
  • else return x(zz) N
  • Correct?
  • Speed?
  • Can we do better?

8
Greatest Common Divisor
  • Given two positive integers, x and y, there is a
    unique largest integer z such that
  • xz 0 and yz 0
  • That z is called the Greatest Common Divisor
    (GCD) of x and y

9
Algorithm for the GCD
  • How can we find gcd(x,y)?
  • We know x and y have unique prime factors
  • We know the GCD is the product of their common
    prime factors
  • Prime factorization is slow

10
Algorithm for the GCD
  • Observation
  • (ax) mod (bx) (a mod b) x
  • That is, modulo does not remove common factors
  • Core of Eulcid's algorithm
  • gcd(a,b) return (b0) ? a gcd(b,ab)
  • Correct? Speed? Can we do better?
  • How fast is ?

11
GCD and certificates
  • Is 52 the GCD of 4524 and 30160?

12
GCD and certificates
  • Lemma
  • if
  • ad0 and
  • bd0 and
  • daxby for some x,y,
  • then dgcd(x,y)
  • Proof (let ggcd(a,b))
  • Since d is a common divisor, dg
  • Since g divides a and b, it must divide axby,
    which is d, too
  • Hence gd, which means g d

13
GCD and certificates
  • Check 1508 74524 -130160 is the GCD of
    4524 and 30160
  • Check requires
  • 2 multiplies O(n2), 1 add O(n), and 2 modulos
    O(n2)
  • Total O(n2)
  • Finding GCD requires Euclid's algorithm O(n3)
  • 7 and -1 form a certificate that 1508
    gcd(4524,30160)

14
Finding a certificate
  • Easy case
  • gcd(a,0) a 1a anything0
  • Inductive step
  • Assume gcd(b,ab) xb y(ab)
  • gcd(a,b) gcd(b,ab) (Euclid)
  • xb y(ab)
  • xb y(a-(a/b)b)
  • ya (x-(a/b)y)b

15
Extended Eulcid GCD
  • int gcd2(int a, int b)
  • int ans 1,anything,a
  • if (b ! 0)
  • int tmp gcd2(b,ab)
  • ans2 tmp2
  • ans0 tmp1
  • ans1 tmp0 (a/b)tmp1
  • return ans

16
Extended Euclid Example
  • gcd2(25,11)
  • gcd(11, 2511 3)
  • gcd(3, 113 2)
  • gcd(2, 32 1)
  • gcd(1, 21 0)
  • base case, use 0 return 1, 0, 1
  • return 0, 1-(2/1)0 1, 1
  • return 1, 0-(3/2)1 -1, 1
  • return -1, 1-(11/3)-1 4, 1
  • return 4, -1-(25/11)4 -9, 1

17
Why certificates?
  • Certificates (easier to check than to find) are
    neat, but...
  • Also basis of modular division
  • x inv(a) mod N iff ax 1 mod N
  • inv(a) exists modulo N if and only if gcd(a,N)
    1 then we have gcd2(a,N) inv(a),junk,1
  • a div b ainv(b) mod N
  • Hence, inv(11) -9 (mod 25)

18
Primality Testing
19
Fermat's Little Theorem
  • If p is prime, then
  • ap-1 1 (mod p) for all 0 lt a lt p
  • Example 5
  • 14 1 1 (mod 5)
  • 24 16 1 (mod 5)
  • 34 81 1 (mod 5)
  • 44 256 1 (mod 5)

20
Huh?
  • It's about relative primality
  • 12 8 2
  • 22 8 4
  • 32 8 6
  • 42 8 0
  • 52 8 2
  • 62 8 4
  • 72 8 6
  • 13 8 3
  • 23 8 6
  • 33 8 1
  • 43 8 4
  • 53 8 7
  • 63 8 2
  • 73 8 5

21
Product of set
  • Given S1, 2, ..., p-1
  • If a and p are relatively prime,
  • a, 2a, ... (p-1)a S (mod p)
  • Take the product of each side
  • ap-1 (p-1)! (p-1)! (mod p)
  • if p is prime, (p-1)! has an inverse mod p, so
  • ap-1 1 (mod p)
  • which proves Fermat's little theorem

22
How well does it work?
  • Lemma if ap-1 ! 1 (mod p), then either
  • p is a Carmichael Number, or
  • ap-1 ! 1 (mod p) for at least half of all
    possible a values
  • Extremely few Carmichael numbers, and book
    discusses a workaround for them
  • Hence, at worst 1/2 chance of error per a we test

23
One-sided error reduction
  • No false negatives (if Fermat says it is
    composite, it is composite)
  • At worst 1/2 chance of false positive
  • Pick k different random a then under 1/2k chance
    of false positive

24
Random Prime Number
  • Lagrange proved there are roughly x/ln(x) primes
    less than x
  • Meaning n-bit number has 1/(nln(2)) chance of
    being prime
  • Hence
  • Pick a random number
  • Run a primality test
  • Repeat if composite

25
Industrial-grade primes
  • For most work it's OK to just check a2 (maybe 3
    5 too)
  • To see why,
  • About 1,000,000,000 primes less than
    25,000,000,000
  • About 20,000 composites less than 25,000,000,000
    fool Fermat with a2
  • under 200 of them are Carmichael numbers
  • This difference increases as numbers get larger
Write a Comment
User Comments (0)
About PowerShow.com