2WF15 - Discrete Mathematics 2 - Part 1
Total Page:16
File Type:pdf, Size:1020Kb
1 2WF15 - Discrete Mathematics 2 - Part 1 Algorithmic Number Theory Benne de Weger version 0.54, March 6, 2012 version 0.54, March 6, 2012 2WF15 - Discrete Mathematics 2 - Part 1 2 2WF15 - Discrete Mathematics 2 - Part 1 version 0.54, March 6, 2012 Contents i Contents 1 Multi-precision arithmetic 1 1.1 Notation . .1 1.2 Representation of integers . .2 1.3 Efficient arithmetic with integers . .3 1.4 Efficient polynomial arithmetic . .8 2 Euclidean and Modular Algorithms 11 2.1 The Euclidean Algorithm . 11 2.2 Efficient Modular Arithmetic . 16 2.3 The Chinese Remainder Theorem . 23 ∗ 3 Multiplicative structure of Zn 27 3.1 Euler (and Fermat) . 27 3.2 Order of an element . 29 3.3 Primitive roots . 30 3.4 Algorithms . 32 4 Quadratic Reciprocity 35 4.1 Quadratic residues and the Legendre symbol . 35 4.2 The Quadratic Reciprocity Law . 36 4.3 Another proof . 39 4.4 The Jacobi symbol . 41 4.5 Modular Square Roots . 44 5 Prime Numbers 47 5.1 Prime Number Distribution . 47 5.2 Probabilistic Primality Testing . 51 5.3 Deterministic Primality Testing . 54 5.4 Prime Number Generation . 55 version 0.54, March 6, 2012 2WF15 - Discrete Mathematics 2 - Part 1 ii Contents 6 Multiplicative functions 59 6.1 Multiplicative functions . 59 6.2 The M¨obius function . 61 6.3 M¨obiusinversion . 62 6.4 The Principle of Inclusion and Exclusion . 63 6.5 Fermat and Euler revisited . 64 7 Continued Fractions 67 7.1 The Euclidean Algorithm revisited . 67 7.2 Continued Fractions . 68 7.3 Diophantine approximation . 70 Bibliography 75 2WF15 - Discrete Mathematics 2 - Part 1 version 0.54, March 6, 2012 1.1 Notation 1 Chapter 1 Multi-precision arithmetic Introduction General references for this chapter: [CP, Chapter 9], [GG, Chapters 2, 8], [Kn, Chapter 4], [Sh, Chapter 3]. In this chapter the following topics will be treated: • representation of integers and polynomials in a computer, • efficient algorithms for elementary arithmetic operations for the following objects: { integers, { polynomials. 1.1 Notation First the necessary notation. We use the following notation for sets of numbers. • N = f1; 2; 3;:::g is the set of natural numbers (excluding 0), • Z = f:::; −3; −2; −1; 0; 1; 2; 3;:::g is the set of integral numbers or integers, • Q is the set of rational numbers, • R is the set of real numbers. If a set S contains 0, we sometimes write S∗ = S n f0g. The cardinality of a set S is denoted by jSj, or by #S. We will use the floor function bxc, which is the largest integer that is at most x, and the ceiling function dxe, which is the smallest integer that is at least x. The symbol log will always stand for the natural logarithm. The logarithm to the base b will be denoted by logb. To get an idea of the growth of f(x) for x ! 1, we say that a function f(x) is of the order of g(x) for some useful function g, if there exists a constant c such that f(x) < cg(x) for all large enough x. The notation for this is the big O-notation: f(x) = O(g(x)): version 0.54, March 6, 2012 2WF15 - Discrete Mathematics 2 - Part 1 2 1.2 Representation of integers For g(x) we usually take simple functions such as xα, eαx, log x, etc. Of course there is a similar big O concept for e.g. x # 0. 2 n + 3 3 Example: p = O(n 2 ). Or, if we want to be a bit more accurate: 2 n − 1000000 log n 2 n + 3 1 3 p = n 2 + O(n log n). 2 n − 1000000 log n 2 1.2 Representation of integers A problem with doing arithmetic with computers is that numbers can only be stored in rather small memory words, and that computer arithmetic therefore can, at first sight, only work with small sized numbers. Most computers nowadays have a 32 bit architecture, some have 64 bit capabilities, but there are also many processors that still have 16 or 8 bit registers only, e.g. smartcard processors. In an n bit word we can represent 2n different numbers, e.g. f0; 1; 2;:::; 2n − 1g. In many applications, notably cryptography, we often need to do exact arithmetic with numbers that require hundreds or thousands of bits. In other words, overflow or rounding errors are not tolerated at all. This means that the standard arithmetic functions provided by the processor or the (operating system) software, operating on single (or at best double) words only, are usually not sufficient, and we must find efficient ways to do arithmetic with large integers. When we want to store large integers that do not fit in the word size of some computer, we have to use an array of words to represent one integer. There are many different ways to represent integers. We don't want to get into too much implementation detail issues such as big / little endian or 2-complement representations. Instead we'll keep it at a fairly mathematical level. We also do not treat how to represent (approximations of) non-integral rational or real numbers. Basically what we do is to use a so-called radix b representation, which is a generalization of the familiar decimal notation. Let b ≥ 2 be an integer, the so called radix. The elements of the set f0; 1; : : : ; b − 1g are called radix b digits, and we assume that they all fit in one memory word, and that operations on them can be handled by the available processor. Note that this means that the bit length of a word is ≥ dlog2 be. Which value we take as b will depend on the architecture of our computer. Popular choices are b = 28; 216; 232, reflecting the word size (2 to the power the bit size of a word), as this optimizes the storage. However, as we'll see soon, it may also be wise to take e.g. b = 231 or even b = 216 on a 32-bit computer. Any integer n 2 Z admits a radix b representation [n]b, as follows. Let m 2 N be such that m jnj < b , so that m ≥ blogb jnjc + 1. Then, repeatedly using division with remainder by b, it follows that there exist n0; n1; : : : ; nm−1 2 f0; 1; : : : ; b − 1g such that m−1 2 m−1 X i jnj = n0 + n1b + n2b + ::: + nm−1b = nib : i=0 Then as the radix b representation of n we write [n]b = ±[nm−1; nm−2; : : : ; n0]b; with ± = + or left out if n ≥ 0, and ± = − if n < 0. Leading zeroes are allowed, but usually are omitted, e.g. [43]16 = [2; 11]16 = [0; 0; 2; 11]16: 2WF15 - Discrete Mathematics 2 - Part 1 version 0.54, March 6, 2012 1.3 Efficient arithmetic with integers 3 The parameter m is called the word size of [n]b. When it is clear from the context which b is meant, we often write [n]b = ±nm−1nm−2 : : : n0. Omitting commas should only be done when confusion is not possible. When b ≤ 10 we use the well known arabic symbols 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 for the digits. When b > 10 (but not too large) we run out of symbols, so after 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 we sometimes continue with A, B, C, :::. When b = 10 we get essentially our every day decimal representation. When b = 2 we get the well known binary representation; in that case the word size is equal to the bitsize. When b = 16 the system is called hexadecimal representation, and in that case often the digits 10; 11; 12; 13; 14; 15 are written as A, B, C, D, E, F. Example: [43]16 = 2B. This hexadecimal system is well known in the computing world. 1.3 Efficient arithmetic with integers 1.3.1 Addition and subtraction Addition and subtraction is essentially easy: we use the primary school method, which is the most efficient way of doing it. This works by adding the digits backwards, and keeping track of "carries". We start with a formal description of addition of positive numbers. Addition of a positive and a negative number is equivalent to subtraction of two positive numbers, to be treated below. Addition of two negative numbers is equivalent to addition of two positive numbers, and adjusting the sign of the output. Addition of 0 is trivial. Algorithm 1.1 (Addition Algorithm ) Input: an integer b ≥ 2, and the radix b representations [x]b = [xm−1; : : : ; x0]b, [y]b = [yn−1; : : : ; y0]b (without leading zeroes) of numbers x; y 2 N Output: the radix b representation [z]b = [zk−1; : : : ; z0]b (without leading zeroes) of z = x + y Step 1: c 0, xi 0 for m ≤ i < maxfm; ng yi 0 for n ≤ i < maxfm; ng Step 2: for i = 0; 1;:::; maxfm; ng − 1 do zi xi + yi + c if zi ≥ b then zi zi − b, c 1 else c 0 Step 3: if c = 1 then k maxfm; ng + 1, zk−1 = 1 else k maxfm; ng output [zk−1; : : : ; z0]b Note that indeed zi 2 f0; 1; : : : ; b − 1g for all i. Next we give a formal description of subtraction of a positive number from a larger positive number. Subtraction of a positive number from a smaller positive number is essentially the same, by swapping the two numbers and adjusting the sign of the output.