Public-Key Cryptography Theory and Practice
Total Page:16
File Type:pdf, Size:1020Kb
Integer Arithmetic Arithmetic in Finite Fields Arithmetic of Elliptic Curves Public-key Cryptography Theory and Practice Abhijit Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur Chapter 3: Algebraic and Number-theoretic Computations Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Integer Arithmetic In cryptography, we deal with very large integers with full precision. Standard data types in programming languages cannot handle big integers. Special data types (like arrays of integers) are needed. The arithmetic routines on these specific data types have to be implemented. One may use an available library (like GMP). Size of an integer n is O(log n ). | | Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Basic Integer Operations Let a, b be two integer operands. High-school algorithms Operation Running time a + b O(max(log a, log b)) a b O(max(log a, log b)) − ab O((log a)(log b)) a2 O(log2 a) (a quot b) and/or (a rem b) O((log a)(log b)) Fast multiplication: Assume a, b are of the same size s. Karatsuba multiplication: O(s1.585) FFT multiplication: O(s log s) [not frequently used in cryptography] Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Binary GCD To compute the GCD of two positive integers a and b. Write a = 2αa′ and b = 2βb′ with a′, b′ odd. gcd(a, b)= 2min(α,β) gcd(a′, b′). Assume that both a, b are odd and a > b. gcd(a, b)= gcd(a b, b). − Write a b = 2γc with γ > 1 and c odd. − Then, gcd(a, b)= gcd(c, b). Repeat until one operand reduces to 0. Running time of Euclidean gcd: O(max(log a, log b)3). Running time of binary gcd: O(max(log a, log b)2). Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Extended Euclidean GCD To compute the GCD of two positive integers a and b. Define three sequences ri , ui , vi . r = a, u = 1, v = 0, Initialize: 0 0 0 r = b, u = 0, v = 1. 1 1 1 Iteration: For i = 2, 3, 4,..., do the following: Compute the quotient qi = ri−2 quot ri−1. Compute r = r − q r − . i i 2 − i i 1 Compute u = u − q u − . i i 2 − i i 1 Compute v = v − q v − . i i 2 − i i 1 Break if ri = 0. Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Extended Euclidean GCD (contd.) We maintain the invariance ui a + vi b = ri for all i. Suppose the loop terminates for i = j (that is, rj = 0). gcd(a, b)= rj−1 = uj−1a + vj−1b. One needs to remember the r, u, v values only from the two previous iterations. One can compute only the r and u sequences in the loop. One gets v − = (r − u − a)/b. j 1 j 1 − j 1 The binary gcd algorithm can be similarly modified so as to compute the u and v sequences maintaining the invariant ui a + vi b = ri for all i. Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Extended Euclidean GCD (Example) To compute gcd(78, 21)= 78u + 21v. i qi ri ui vi ui a + vi b 0 78 1 0 78 1 − 21 0 1 21 − 2 3 15 1 3 15 3 1 6 1− 4 6 4 2 3− 3 11 3 5 2 0 7− 26 0 − Thus, gcd(78, 21)= 3 = 3 78 + ( 11) 21. × − × Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Modular Integer Arithmetic Let n N. Define Zn = 0, 1, 2,..., n 1 . ∈ { − } a + b if a + b < n Addition: a + b (mod n)= (a + b n if a + b > n − a b if a > b Subtraction: a b (mod n)= − − (a b + n if a < b − Multiplication: ab (mod n) = (ab) rem n. Inverse: a Z∗ is invertible if and only if gcd(a, n)= 1. ∈ n But then 1 = ua + vn for some integers u, v. Take a−1 u (mod n). ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Example of Modular Arithmetic Take n = 257, a = 127, b = 217. Addition: a + b = 344 > 257, so a + b 344 257 87 (mod n). ≡ − ≡ Subtraction: a b = 90 < 0, so − − a b 90 + 257 167 (mod n). − ≡− ≡ Multiplication: ab (127 217) rem 257 27559 rem 257 60 (mod n). ≡ × ≡ ≡ Inverse: gcd(b, n)= 1 = ( 45)b + 38n, so − − b 1 45 + 257 212 (mod n). ≡− ≡ Division: a/b ab−1 (127 212) rem 257 196 (mod n). ≡ ≡ × ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Modular Exponentiation: Slow Algorithm e Let n N, a Zn and e N . To compute a (mod n). ∈ ∈ ∈ 0 Compute a, a2, a3,..., ae successively by multiplying with a modulo n. Example: n = 257, a = 127, e = 217. a2 a a 195 (mod n), ≡ × ≡ a3 a2 a 195 127 93 (mod n), ≡ × ≡ × ≡ a4 a3 a 93 127 246 (mod n), ≡ × ≡ × ≡ · · · a216 a215 a 131 127 189 (mod n), ≡ × ≡ × ≡ a217 a216 a 189 127 102 (mod n). ≡ × ≡ × ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Right-to-left Modular Exponentiation To compute ae (mod n). Binary representation: e = (el−1el−2 . e1e0)2 = l−1 l−2 1 0 e − 2 + e − 2 + + e 2 + e 2 . l 1 l 2 · · · 1 0 l−1 el−1 l−2 el−2 1 e1 0 e0 ae a2 a2 a2 a2 (mod n). ≡ · · · 2 3 l−1 i Compute a, a2, a2 , a2 ,..., a2 and multiply those a2 modulo n for which ei = 1. Also for i > 1, we have i i−1 2 a2 a2 (mod n). ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Right-to-left Modular Exponentiation (Example) Take n = 257, a = 127, e = 217. 7 6 4 3 0 e = (11011001)2 = 2 + 2 + 2 + 2 + 2 . So 7 6 4 3 0 ae a2 a2 a2 a2 a2 (mod n). ≡ 2 a2 195 (mod n), a2 (195)2 246 (mod n), 3≡ ≡ 4≡ a2 (246)2 121 (mod n), a2 (121)2 249 (mod n), 5 ≡ ≡ 6 ≡ ≡ a2 (249)2 64 (mod n), a2 (64)2 241 (mod n) and 7 ≡ ≡ ≡ ≡ a2 (241)2 256 (mod n). ≡ ≡ ae 256 241 249 121 127 102 (mod n). ≡ × × × × ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Left-to-right Modular Exponentiation To compute ae (mod n). Binary representation: e = (el−1el−2 . e1e0)2 = l−1 l−2 1 0 e − 2 + e − 2 + + e12 + e02 . l 1 l 2 · · · Define ǫ = (e − e − . e )2 for i = l, l 1, l 2,..., 0. i l 1 l 2 i − − ǫl = 0, and ǫi = 2ǫi+1 + ei for i < l. aǫl 1 (mod n) and aǫi (aǫi+1 )2 aei (mod n). ≡ ≡ × ǫ Finally, ǫ0 = e, so output a 0 (mod n). Initialize product to 1 (corresponds to i = l). For i = l 1, l 2,..., 1, 0, square product. − − If ei = 1, then multiply product by a. Square-and-(conditionally)-multiply algorithm Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Left-to-right Modular Exponentiation (Example) Take n = 257, a = 127 and e = 217. We have the binary representation: e = (11011001)2. ǫ i ei ǫi a i (mod n) 8 0 1 − 2 7 1 (1)2 = 1 1 127 127 (mod n) ×2 ≡ 6 1 (11)2 = 3 127 127 93 (mod n) ×2 ≡ 5 0 (110)2 = 6 93 168 (mod n) 2 ≡ 4 1 (1101)2 = 13 168 127 69 (mod n) 2 × ≡ 3 1 (11011)2 = 27 69 127 183 (mod n) × 2 ≡ 2 0 (110110)2 = 54 183 79 (mod n) 2 ≡ 1 0 (1101100)2 = 108 79 73 (mod n) 2 ≡ 0 1 (11011001)2 = 217 73 127 102 (mod n) × ≡ Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Primality Testing A fundamental problem in computational number theory. Probabilistic (that is, randomized) algorithms solve the problem reasonably efficiently with arbitrarily small probability of error. Some of these probabilistic algorithms can be converted to deterministic polynomial-time algorithms under certain unproven assumptions (Extended Riemann Hypothesis). The first known deterministic polynomial-time algorithm with proofs not dependent on any conjectures is from Agarwal, Kayal and Saxena (2002). The AKS algorithm is not yet practical. Public-key Cryptography: Theory and Practice Abhijit Das Integer Arithmetic GCD Arithmetic in Finite Fields Modular Exponentiation Arithmetic of Elliptic Curves Primality Testing Fermat Test Fermat’s little theorem: If n is prime, then an−1 1 (mod n) for all a coprime to n.