Miller-Rabin Primality Test
Total Page:16
File Type:pdf, Size:1020Kb
Miller-Rabin Primality Test Gary Fredericks September 22, 2014 About Me I Software engineer at Groupon I Fan of pure math, theoretical CS I @gfredericks_ Why Number Theory? About This Paper I "Probabilistic Algorithm for Testing Primality" I Michael O. Rabin, 1977 I Modies a deterministic but presumptive algorithm published by Gary L. Miller in 1976 I The algorithm has become known as the "Miller-Rabin Primality Test" I The paper itself is not very accessible (whoopsie-doodle) About This Talk I Motivate the problem I I.e., why I like numbers I Describe the algorithm I Try to give an intuition for why it works Numbers Numbers 0; 1; 2; 3; 4; 5; 6;::: 3 + 8 = 11 6 · 7 = 42 Addition Addition shifts the number line. 1 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,... 2 + 7: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,... ? + ? = 53671 I 0 + 53671 I 1 + 53670 I 2 + 53669 I 3 + 53668 I ... I 53671 + 0 Multiplication Multiplication scales the number line. 1 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, 2 * 3: 0, 1, 2, 3, 4, 5, ?·? = 53671 I 1 · 53671 I 53671 · 1 I Um. Divisibility Graph 5 21 15 9 10 7 18 3 20 14 6 2 12 4 22 11 13 17 8 19 23 16 Factoring 24 24 6⋅4 2⋅3⋅4 2⋅3⋅2⋅2 Unique Factorizations 24 8⋅3 6⋅4 12⋅2 4⋅3⋅2 6⋅2⋅2 3⋅2⋅2⋅2 Unique Factorizations A positive integer can be viewed as a multiset of primes. 24 2 3 2 2 Unique Factorizations I 1 = I 13 = 13 I 25 = 5 · 5 I 2 = 2 I 14 = 2 · 7 I 26 = 2 · 13 I 3 = 3 I 15 = 3 · 5 I 27 = 3 · 3 · 3 I 4 = 2 · 2 I 16 = 2 · 2 · 2 · 2 I 28 = 2 · 2 · 7 I 5 = 5 I 17 = 17 I 29 = 29 I 6 = 2 · 3 I 18 = 2 · 3 · 3 I 30 = 2 · 3 · 5 I 7 = 7 I 19 = 19 I 31 = 31 I 8 = 2 · 2 · 2 I 20 = 2 · 2 · 5 I 32 = 2 · 2 · 2 · 2 · 2 I 9 = 3 · 3 I 21 = 3 · 7 I 33 = 3 · 11 I 10 = 2 · 5 I 22 = 2 · 11 I 34 = 2 · 17 I 11 = 11 I 23 = 23 I 35 = 5 · 7 I 12 = 2 · 2 · 3 I 24 = 2 · 2 · 2 · 3 I 36 = 2 · 2 · 3 · 3 Concepts explained using unique factorization I Multiplication I Division I Divisibility I Prime / Composite I GCD I Coprimality I Reduc{ing,ed} fractions I Which fractions have nite decimal representations I Which numbers have "obvious" divisors? Multiplication Multiplication is just a multiset sum. 6 7 42 2 3 · 7 = 2 3 7 Division Division is just a multiset dierence. 6 7 42 2 3 ö7 2= 3 7 Divisibility a is divisible by b if b's factorization is a subset of a's. 20 100 2 5 2 5 2 2 5 Prime / Composite A prime number is a number whose prime factorization is itself. 41 42 41 2 3 7 GCD: 90 and 525 The greatest common divisor (GCD) of two numbers is the intersection of their factorizations. 90 525 15 2 3 5 3 5 7 gcd( 3 , 5 ) = 3 5 GCD: 90 and 527 90 527 1 gcd(2 3 5 , 17 31 ) = 3 Coprimality Two integers are coprime if they have no factors in common. 90 527 2 3 5 17 31 3 Reduc{ing,ed} fractions Reducing a fraction just means removing the common parts of the numerator and denominator's factorizations. 90 2 · 3 · 3¡ · 5¡ 2 · 3 6 = = = 525 3¡ · 5¡ · 5 · 7 5 · 7 35 A reduced fraction has a numerator and denominator that are coprime. Which fractions have nite decimal representations? 1 I 2 = 0:5 2 I 3 = 0:3 ::: 3 I 4 = 0:75 4 I 5 = 0:8 5 I 6 = 0:83 ::: 6 I 7 = 0:857142 ::: 7 I 8 = 0:875 8 I 9 = 0:8 ::: 9 I 10 = 0:9 10 I 11 = 0:90 ::: 11 I 12 = 0:916 ::: Which fractions have nite decimal representations? 1 1 I 2 = 2 = 0:5 2 2 I 3 = 3 = 0:3 ::: 3 3 0 75 I 4 = 2·2 = : 4 2·2 I 5 = 5 = 0:8 5 5 0 83 I 6 = 2·3 = : ::: 6 2·3 I 7 = 7 = 0:857142 ::: 7 7 0 875 I 8 = 2·2·2 = : 8 2·2·2 0 8 I 9 = 3·3·3 = : ::: 9 3·3·3 0 9 I 10 = 2·5 = : 10 2·5 I 11 = 11 = 0:90 ::: 11 11 0 916 I 12 = 2·2·3 = : ::: Which numbers have "obvious" divisors? I 1225285014 I 1222105395 I 6431163840 I 2202551775 I 7118220000 I 2729925887 Computational Problems Factorization Problem 3289540009901 ? Primality testing Factorization seems to be dicult. 1 Could it be an easier problem to simply identify whether a number is prime or composite without necessarily determining its factorization? 2 1Without a quantum computer 2Yes. Yes of course it could that's what this talk is about. The Miller-Rabin Primality Test The Miller-Rabin Primality Test 1 (defn probably-prime? 2 "Returns true if n is probably prime, 3 false if it is certainly not. A 4 higher test-count gives exponentially 5 higher certainty when it returns true, 6 but increases the runtime." 7 [n test-count] 8 (let[potential-witness#(+2(rand-int(-n3)))] 9 (->>(repeatedly test-count potential-witness) 10 (not-any?(fn[b](witness?nb)))))) Probably The central theorem in the paper is that for any composite number 3 n, at least 4 of the potential witnesses are actually witnesses. Reality Asserts prime Asserts composite prime 100% 0% composite 25% 75% Is it really a prime? 1 Probability of failure is: 4x 1 I 1 run: 4 1 I 2 runs: 16 1 I 10 runs: 1048576 I 100 runs: 1 1606938044258990275541962092341162602522202993782792835301376 What is a witness? 1 (defn witness? 2 "Returns true if b is a witness 3 to the compositeness of n." 4 [nb] 5 (or(witness-a?nb) 6 (witness-b?nb))) Where are we headed? For each of witness-a? and witness-b?: I What is the code/logic? I Why does this correctly detect compositeness? I How common are the witnesses? witness-a? witness-a? 1 (defn witness-a? 2 "Returns true if b^(n-1) is not 3 congruent to 1 mod n." 4 [nb] 5 (not=1(pow-modb(decn)n))) For primes 1 (defn witness-a-density 2 "Returns the fraction of candidate 3 witnesses that meet criteria (a)." 4 [n] 5 (let[c(->>(range2n) 6 (filter(fn[b](witness-a?nb))) 7 (count))] 8 (/c(-n2)))) 9 10 (->>(range3 10000) 11 (filter prime?) 12 (map witness-a-density) 13 (frequencies)) ;;=> {0 1228} For composites 1 (->>(range 10000) 2 (filter composite?) 3 (map witness-a-density) 4 (stats)) ;; Of 8769 numbers... ;; min: 0.20 ;; max: 1.00 ;; median: 1.00 ;; avg: 0.99 ;;=> nil witness-a? 1 (defn witness-a? 2 "Returns true if b^(n-1) is not 3 congruent to 1 mod n." 4 [nb] 5 (not=1(pow-modb(decn)n))) Fermat's Little Theorem For a prime p and 0 < b < p: bp−1 ≡ 1 mod p Modular Arithmetic Arithmetic using the integers 0 ::: n − 1, doing all addition and multiplication mod n. Multiplication mod 10 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 0 2 4 6 8 3 0 3 6 9 2 5 8 1 4 7 4 0 4 8 2 6 0 4 8 2 6 5 0 5 0 5 0 5 0 5 0 5 6 0 6 2 8 4 0 6 2 8 4 7 0 7 4 1 8 5 2 9 6 3 8 0 8 6 4 2 0 8 6 4 2 9 0 9 8 7 6 5 4 3 2 1 Multiplication, n = 28 Multiplication, n = 28,b = 2 5 13 10 26 19 27 20 24 12 15 6 1 2 3 17 11 4 16 22 8 25 21 18 14 9 7 23 0 Multiplication, n = 28,b = 3 23 17 13 15 24 11 16 5 8 20 0 12 25 4 27 19 26 9 18 22 1 3 14 6 10 21 7 2 Multiplication, n = 28,b = 4 6 19 13 14 5 24 21 27 0 20 26 12 7 17 10 22 15 3 9 1 4 2 8 16 23 18 11 25 Multiplication, n = 28,b = 5 13 9 25 17 24 5 1 16 8 0 20 27 12 11 4 21 23 6 19 18 3 2 14 15 26 7 10 22 Multiplication, n = 28,b = 6 11 25 13 10 27 22 4 20 24 8 18 6 17 15 3 1 23 26 16 9 21 5 12 2 14 7 19 0 Multiplication, n = 28,b = 7 20 8 16 0 4 17 24 25 5 12 13 9 7 1 22 18 3 6 21 14 15 2 19 26 27 10 11 23 Multiplication, n = 28,b = 8 26 19 12 23 9 5 16 17 10 24 2 22 15 3 8 14 21 0 1 18 25 7 4 27 13 11 20 6 Multiplication, n = 28,b = 9 13 21 16 14 5 4 17 8 22 20 23 2 12 18 25 24 11 15 19 1 9 10 3 27 6 0 26 7 Multiplication, n = 28,b = 10 17 3 9 2 21 27 23 6 14 4 7 13 18 20 0 12 16 15 8 10 24 26 25 1 11 22 5 19 Multiplication, n = 28,b = 11 17 8 27 19 14 4 16 5 13 3 0 15 25 24 9 12 20 23 18 11 26 1 2 22 6 10 21 7 Multiplication, n = 28,b = 12 14 21 0 10 15 22 3 7 1 17 12 8 26 2 4 5 24 23 19 9 20 16 25 27 18 11 13 6 Multiplication, n = 28,b = 13 23 14 19 25 17 21 9 5 24 4 22 6 26 2 27 0 15 13 1 11 3 20 8 18 10 16 7 12 Multiplication, n = 28,b = 14 15 3 11 17 19 21 23 27 7 14 25 22 1 4 9 6 13 5 8 0 12 2 18 16 10 24 26 20 Multiplication, n = 28,b = 15 26 24 18 16 12 0 25 11 21 7 27 13 17 3 6 10 15 1 19 20 5 23 9 2 4 8 14 22 Multiplication, n = 28,b = 16 6 19 13 14 5 12 21 27 0 24 26 20 7 17 10 22 15 3 9 1 16 2 4 8 23 18 11 25 Multiplication, n = 28,b = 17 25 5 13 1 9 24 16 17 0 8 20 7 19 12 11 4 15 26 27 18 22 3 21 23 6 10 14 2 Multiplication, n = 28,b = 18 21 11 14 7 25 2 0 15 8 16 5 18 4 1 19 6 13 22 9 24 10 12 23 20 27 26 17 3 Multiplication, n = 28,b = 19 24 16 8 20 12 6 18 4 0 2 26 27 25 10 22 9 17 19 23 3 15 1 13 21 7 5 14 11 Multiplication, n = 28,b = 20 19 26 5 16 13 6 12 9 8 27 23 2 15 20 11 1 25 22 24 17 18 14 4 21 0 10 3 7 Multiplication, n = 28,b = 21 22 18 6 14 2 17 13 26 10 5 21 1 25 20 9 8 16 0 23 19 4 24 11 7 12 3 27 15 Multiplication, n = 28,b = 22 13 27 6 17 9 24 23 20 10 2 15 8 3 16 22 25 1 21 18 11 14 7 4 0 19 12 26 5 Multiplication, n = 28,b = 23 27 16 5 17 4 8 3 19 15 13 0 9 25 11 22 23 10 1 2 18 6 26 20 21 7 14 12 24 Multiplication, n = 28,b = 24 14 21 0 15 22 10 3 7 1 17 24 16 26 2 5 8 20 23 19 9 12 4 25 27 18 11 13 6 Multiplication, n = 28,b = 25 17 21 8 14 5 4 13 16 18 24 15 2 12 22 9 20 11 23 27 1 25 26 3 19 6 0 10 7 Multiplication, n = 28,b = 26 21 14 7 23 0 9 3 10 17 13 22 8 2 12 27 24 4 1 16 26 20 6 15 25 18 11 5 19 Multiplication, n = 28,b = 27 15 16 12 13 17 11 23 5 24 4 22 6 26 2 18 0 10 27 1 25 3 21 7 20 8 19 14 9 Multiplication, n = 29 Multiplication, n = 29,b = 2 3 16 6 8 12 4 24 2 19 1 9 15 18 0 22 7 11 14 20 28 10 27 5 25 17 21 23 26 13 Multiplication, n = 29,b = 3 17 22 25 8 18 24 6 14 2 13 20 10 26 0 1 28 3 19 9 16 27 15 23 5 11 21 4 12 7 Multiplication, n = 29,b = 4 17 26 10 21 11 27 15 14 2 18 1 22 8 20 19 4 3 12 5 16 0 23 6 13 24 25 9 7 28 Multiplication, n = 29,b = 5 19 8 27 11 17 26 15 14 3 12 25 18 5 9 2 21 16 10 1 22 6 0 23 7 28 13 20 24 4 Multiplication, n = 29,b = 6 19 27 8 11 17 26 15 14 3 12 18 5 1 21 2 25 10 6 9 7 16 0 13 22 20 23 4 24 28 Multiplication, n = 29,b = 7 6 5