LECTURE NOTES in CRYPTOGRAPHY Thomas Johansson 2005/2006
Total Page:16
File Type:pdf, Size:1020Kb
1 LECTURE NOTES IN CRYPTOGRAPHY Thomas Johansson 2005/2006 c Thomas Johansson 2006 2 Chapter 1 Abstract algebra and Number theory Before we start the treatment of cryptography we need to review some basic facts from number theory and abstract algebra. 1.1 Number theory The set of integers {...,−2, −1, 0, 1, 2,...} is denoted by Z. Definition 1.1. Let a, b ∈ Z. We say that a divides b (written a|b) if there exists an integer c such that b = ac. The following facts are easily checked. (i) a|a. (ii) If a|b and b|c then a|c. (iii) If a|b and a|c then a|(bx + cy) for any x, y ∈ Z. (iv) If a|b and b|a then a = ±b. Let a and b be integers with b ≥ 1. Then an ordinary long division of a by b yields two integers q and r such that a = qb + r, where 0 ≤ r<b. (1.1) The integers q and r are called the quotient and the remainder, respectively, and they are unique. Definition 1.2. The remainder ( r )ofa divided by b is denoted a mod b. The quotient ( q )ofa divided by b is denoted a div b. Example 1.1. Let a =47and b =7.Thena mod b =5and a div b =6. We call an integer c a common divisor of integers a and b if c|a and c|b. 3 4 Chapter 1. Abstract algebra and Number theory Definition 1.3. A non-negative integer d is called the greatest common divisor of integers a and b if (i) d is a common divisor of a and b. (ii) for every other common divisor c it hold that c|d. The greatest common divisor of integers a and b is denoted gcd(a, b). Clearly, gcd(a, b) is the largest positive integer dividing both a and b (except gcd(0, 0) = 0). Example 1.2. The common divisors of 28 and 42 are {±2, ±7, ±14} and gcd(28, 42) = 14. In a similar fashion as above we define the least common multiple. Definition 1.4. A non-negative integer d is called the least common multiple of integers a and b if (i) a|d and b|d. (ii) for every integer c such that a|c and b|c we have d|c. The least common multiple of integers a and b is denoted lcm(a, b). Clearly, lcm(a, b) is the smallest positive integer divisible by both a and b. The following relationship is often useful. If a and b are positive integers, then lcm(a, b) · gcd(a, b)=a · b. (1.2) Two integers a, b are called relatively prime if gcd(a, b)=1. Also, an integer p ≥ 2 is called prime if its only positive divisors are 1 and p.Otherwisep is called a composite. Prime numbers play a fundamental role in cryptography. Let us review some known facts about them. Let π(x) denote the number of primes ≤ x. Some known facts about primes. (i) There are infinitely many primes. π(x) (ii) limx→∞ x/ ln x =1. (iii) For x ≥ 17, x/ ln x<π(x) < 1.25506x/ ln x. The following theorem is a well known result. Theorem 1.1 (Unique factorization theorem). Every integer n ≥ 2 can be written as a product of prime powers, e1 e2 ek n = p1 p2 ···pk , where p1,p2,...,pk are distinct primes and e1,e2,...ek are positive integers. Furthermore, the factorization is unique up to rearrangement of factors. 5 The proof of this theorem is not as easy as it may seem. We refer to any textbook in number theory. The unique factorization theorem can be used to express the greatest common divisor and the least common multiple in a simple way. e1 e2 ek f1 f2 fk If a = p1 p2 ···pk and b = p1 p2 ···pk ,whereei,fi, i =1, 2,...k are now nonnegative integers, then min(e1,f1) min(e2,f2) min(ek,fk) gcd(a, b)=p1 p2 ···pk (1.3) and max(e1,f1) max(e2,f2) max(ek,fk) lcm(a, b)=p1 p2 ···pk . (1.4) Definition 1.5. For n ≥ 1,letφ(n) denote the number of integers in the interval [1,n] which are relatively prime to n. This function is called the Euler phi function. Let us prove the following facts concerning the Euler phi function. Theorem 1.2. (i) If p is a prime, then φ(p)=p − 1. (ii) If gcd(a, b)=1then φ(ab)=φ(a)φ(b). e1 e2 ek (iii) If n = p1 p2 ···pk then e1 e1−1 e2 e2−1 ek ek−1 φ(n)=(p1 − p1 )(p2 − p2 ) ···(pk − pk ). Proof. The proof is left as an exercise. Let us consider the problem of computing the greatest common divisor between two integers. We could use the expression derived in (1.3), but this requires us to first find the prime factors of the two integers. This is not always an easy task, as we will see later. Fortunately, there is an easier and better way of calculating the greatest common divisor. It relies on the following simple fact. Lemma 1.3. If a and b are positive integers with a>b,thengcd(a, b)=gcd(b, a mod b). Proof. The proof is left as an exercise. Of course, we can now use the above lemma repeatedly until the problem is small enough to be trivially solved. This leads to the famous Euclidean algorithm for computing gcd(a, b). Algorithm 1.1 (Euclidean algorithm). INPUT: Two non-negative integers a, b where a ≥ b. OUTPUT: gcd(a, b) FLOW: 1. Set r0 ← a, r1 ← b, i ← 1. 2. While ri =0 do: 1.1 Set ri+1 ← ri−1 mod ri, i ← i +1. 2. Return(ri). 6 Chapter 1. Abstract algebra and Number theory In essence, the algorithm sets r0 = a, r1 = b and then computes the following set of long divisions, r0 = q1r1 + r2 r1 = q2r2 + r3 . ri−2 = qi−1ri−1 + ri ri−1 = qiri. Since gcd(a, b)=gcd(r0,r1)=gcd(r1,r2)=... = gcd(ri−1,ri)=ri, the algorithm terminates with the correct answer. Let us illustrate the Euclidean algorithm with an example. Example 1.3. Compute gcd(1495, 1365). We have 1495 = 1 · 1365 + 130 1365 = 10 · 130 + 65 130 = 2 · 65 + 0, and gcd(1495, 1365) = 65. The following fact is important. Theorem 1.4. Let a and b be two non-negative integers. Then there exist integers x, y such that gcd(a, b) canbewrittenas gcd(a, b)=ax + by. Proof. F ollowingthesetoflongdivisionsabovewehave gcd(a, b)=ri = ri−2 − qi−1ri−1 = ri−2 − qi−1(ri−3 − qi−2ri−2) . = r0x + r1y = ax + by, for some integers x, y ∈ Z. In the proof of the theorem, we see how we can calculate the integers x and y.Thereis also an extended version of Euclidean algorithm which returns not only gcd(a, b) but also the integers x, y. So when we need to calculate x, y we can either use Eulidean algorithm and then go “backwards” again, as in the proof, or we can use an extended version of the Eulidean algorithm. 7 Algorithm 1.2 (Extended Euclidean algorithm). INPUT: Two non-negative integers a, b where a ≥ b. OUTPUT: d =gcd(a, b) and two integers x, y such that d = ax + by. FLOW: 1. If b =0then return(a,x ← 1,y ← 0). 2. Set x2 ← 1,x1 ← 0,y2 ← 0,y1 ← 1. 3. While b>0 do: 3.1 q ← adiv b, r ← a − qb, x ← x2 − qx1,y ← y2 − qy1, 3.2 a ← b, b ← r, x2 ← x1,x1 ← x, y2 ← y1,y1 ← y. 4. Set d ← a, x ← x2, y ← y2 and Return(d, x, y). Example 1.4. Compute gcd(1495, 1365) and find two integers x, y such that gcd(1495, 1365) = 1495x + 1365y. qr xy a b x2 x1 y2 y1 1495 1365 1 0 0 1 1 130 1 -1 1365 130 0 1 1 -1 10 65 -10 11 130 65 1 -10 -1 11 2 0 21 -23 65 0 -10 21 11 -23 So gcd(1495, 1365) = 65 = 1495 · (−10) + 1365 · 11. If we instead of using the extended version of the Euclidean algorithm would use the standard version and then go backwards, we would get 1495 = 1 · 1365 + 130 1365 = 10 · 130 + 65 130 = 2 · 65 + 0 and backwards 65 = 1365 − 10 · 130 = 1365 − 10 · (1495 − 1 · 1365) =11· 1365 − 10 · 1495. 1.2 The integers modulo n Let n be a positive integer. If a and b are integers, then a is said to be congruent to b modulo n, which is written a ≡ b (mod n),ifn divides (a − b).Wecalln the modulus of the congruence. We can check the following properties. Theorem 1.5. For a, a1,b,b1,c∈ Z we have 8 Chapter 1. Abstract algebra and Number theory (i) a ≡ b (mod n) if and only if a and b leaves the same remainder when divided by n. (ii) a ≡ a (mod n). (iii) If a ≡ b (mod n) then b ≡ a (mod n). iv) If a ≡ b (mod n) and b ≡ c (mod n),thena ≡ c (mod n). v) If a ≡ a1 (mod n) and b ≡ b1 (mod n),thena + b ≡ a1 + b1 (mod n) and ab ≡ a1b1 (mod n). The properties ii), iii), and iv) are called reflexivity, symmetry and transitivity, respec- tively.