University of Manchester School of Computer Science Project Report 2016
Total Page:16
File Type:pdf, Size:1020Kb
University of Manchester School of Computer Science Project Report 2016 Primality Testing Author: Tong Ding Supervisor: Ian Pratt-hartmann Abstract Primality Testing Author: Tong Ding A prime number is a natural number n > 1 with no positive divisors other than 1 and n. This project investigates primality testing – that is, the problem of determining whether a given number is a prime. In recent decades, a number of sophisticated primality testing algorithms have been proposed. These algorithms fall into two categories: non-deterministic algorithms and deterministic al- gorithms. This project focuses on several non-deterministic and deterministic algorithms: our aim is to understand their mathematical foundations, to implement them and to measure their performance. We then investigate the prime generation problem and integer factoring prob- lem, and relate them to the topics which have important applications in cryptography. This report not only explains the algorithms in a comprehensive way, but also analyzes their performance in respect of running time and accuracy, then uses them to gather data on the density of primes. Supervisor: Ian Pratt-hartmann Acknowledgements I owe a debt of gratitude to my supervisor supervisor Dr. Ian Pratt-Hartmann for being a constant assistance and guidance throughout this year. I would also like to thank my family and friends for their support and faith during my study. 1 Contents 1 Introduction 3 1.1 Motivation . .3 1.2 Aims and Objectives . .4 1.3 Report Structure . .4 2 Background 5 2.1 Introduction to the Primality Problem . .5 2.2 Non-deterministic Algorithms for the Primality Problem . .6 2.3 Deterministic Algorithms for the Primality Problem . .7 2.4 Prime Generation Problem . .7 2.5 Integer Factorization Problem . .7 3 Non-deterministic Algorithms in Polynomial Time 8 3.1 Lehmann’s Test . .8 3.2 The Solovay-Strassen Test . 10 3.3 The Miller-Rabin Test . 14 4 Deterministic Algorithms in Polynomial Time 17 4.1 The AKS Test . 17 4.2 The Improved AKS Test . 21 5 Testing and Evaluation 22 5.1 Run Time . 22 5.2 Accuracy . 25 5.3 Density of Prime . 28 6 Prime Generation 30 6.1 The Sieve of Eratosthenes . 30 6.2 The Use of Primality Testing Algorithms . 31 7 Integer Factorization 32 8 Conclusion 33 8.1 Achievements . 33 8.2 Future Work . 33 Bibliography 34 2 Chapter 1 Introduction 1.1 Motivation The primality problem was first defined in classical antiquity. It can be stated as follows: Given a number n, decide whether n is prime. There is a naive procedure which works well for small inputs. Algorithm 1: Simple Method Input : Integer n > 2 Output: prime:0, composite:1 1 i 2; 2 while i × i < n do 3 if i divides n then 4 return 1; 5 else 6 i i + 1; 7 end 8 end 9 return 0; p This algorithm tests all the integers from 2 to n, to see whether they are divisors of n. As soon as a divisor is found, the calculation stops and returns n as a composite number. If no divisor is found, n will be returned as a prime. The method is guaranteed to return a correct answer, and is the best method for small num- bers. But we cannot use this method for large numbers, as it is too time consuming. For ex- ample, if an input number with 60 decimal digits happens to be prime, then the loop will run for more than 1030 rounds. Even if we assume that a very fast computer is used that can carry out one loop in 1 nanosecond, the computing will still take more than 1013 years. The question arises as to whether we can improve the performance of the naive procedure, increasing its efficiency while keeping its accuracy. A number of improved algorithms have been proposed to date. Based on these previous attempts, we decided to implement the im- proved methods and compare their performance for further study. This forms the main idea of the project. 3 1.2 Aims and Objectives The purpose of this project is to implement various primality testing algorithms, explain how these algorithm work, and present their performance. Nearly all the work is based on the book Primality Testing in Polynomial Time: From Randomized Algorithms to ”PRIMES is in P”[Die04]. The aims are: • Understand the mathematical basics in detail. • Fit the relevant knowledge into the algorithms. • Produce functions to test input numbers, based on the pseudo-codes. • Evaluate the performance of different methods by various approaches. • Improve the code according to the testing results and the most recent developments in the area. 1.3 Report Structure Chapter 2 talks about some basic notions and their background. Chapter 3 and Chapter 4 in- vestigate three typical randomized algorithms and one representative deterministic algorithm, respectively, and explaining the ways they work in detail. Chapter 5 considers statistics gath- ered from the running of previous algorithms. In Chapter 6, we focus on prime generation and discuss several methods to achieve that. In Chapter 7, the research goes further into the application in cryptography, which leads to a simple implementation of integer factorization. 4 Chapter 2 Background 2.1 Introduction to the Primality Problem Based on the introduction, we know that this project focuses on various efficiency approaches to solve the primality problem. Before we start talking about the algorithms, as a general background and a basis for analyzing primality test,there are some notions from number the- ory that are essential for the primality problem. Definition 2.1.1 (Prime and Composite). A positive integer n is a prime number if n > 1 and there is no number that divides n except 1 and n. If n is divisible by some a, 1 < a < n, then n is a composite number. A representation n = p · p1··· pr (r > 0; p1;:::; pr stands for prime number which is not necessarily distinct) is called a prime decomposition of n. By using such a representation, we have the following theorem: Theorem 2.1.2 (The Fundamental Theorem of Arithmetic). Every integer n > 1 can be written as a product of prime numbers in exactly one way (The order of the factors is disregarded). The theorem states two terms. For example, 600 = 2 × 2 × 2 × 3 × 5 × 5, 600 can be repre- sented as a product of primes, and 600 can just be written as a product of three 2s, one 3, two 5s, without no other primes in the product. Definition 2.1.3 (Greatest Common Divisor). If n and m are integers, then the largest integer that divides n and divides m is called the great- est common divisor of n and m and denoted by gcd(n;m). If gcd(n;m) = 1, then we say that n and m are relatively prime. There are quite efficient algorithms for finding the greatest common divisor. For example the Extended Euclidean Algorithm (see Algorithm 3.2.4 in [Die04]), which has time complexity O(log(n)log(m)). Theorem 2.1.4 (Integer Division with Remainder). If n is an integer and d is a positive integer, then there are unique integers q and r such that n = d · q + r and 0 6 r < d. d is the divisor, q is the quotient, r is the remainder. 5 The theorem of division with remainder is so important that we introduce a special notation for it. Definition 2.1.5 (Modulo and Congruent). • For an integer n and a positive integer d we let n mod d = r and ndivd = q, where r and q are the uniquely determined numbers that satisfy n = d · q + r and 0 6 r < d. • Let d > 2 be given. For arbitrary integers a and b, if we have a mod b = r and b mod d = r. We say that a is congruent to b modulo d and write a ≡ b (mod d). 2.2 Non-deterministic Algorithms for the Primality Prob- lem A non-deterministic algorithm is an algorithm that, even for the same input, can exhibit dif- ferent behaviors on different runs. Here we give the Fermat Test as a simple example of non- deterministic algorithm for the primality problem. As the basis for the Fermat primality test, we introduce Fermat’s little theorem first: Theorem 2.2.1 (Fermats Little Theorem). If p is a prime number, then for any integer a not divisible by p, ap−1 ≡ 1 (mod p) Algorithm 2: Fermat Test Input : Odd Integer n > 3 Output: prime:0, composite:1 1 Let a be randomly chosen from 2;:::;n − 2; n−1 2 if a 6= 1 (mod n) then 3 return 1; 4 else 5 r 6 end 7 eturn 0; By Fermats Little theorem, if an−1 6= 1 (mod n), then n is not a prime number, in other words, n must be a composite. Otherwise, if ap−1 ≡ 1 (mod b), n might be a prime. In conclusion, if this algorithm returns the answer as prime, there might be a probability that n is actually com- posite. So Fermat Test is a non-deterministic algorithm, because it will not always return the same result giving a particular input. The probability that Fermat Test gives a wrong answer 1 can be proved as up to 2 [Die04], of course this is too high in practical application. More con- vincing algorithms will be described in next chapter. In Algorithm 2 line 2, a method randomization is invoked, which is common in the imple- mentation of non-deterministic algorithms. The purpose of randomization is to generate ran- dom values in hope to verify the algorithm in the average case.