Modular Arithmetic and Primality
Modular Arithmetic and Primality Modular Arithmetic and Primality
⎧⎧ 2(x⋅ ⎣⎣y/2⎦⎦) if y is even x⋅ y = ⎨⎨ ⎩⎩ x + 2(x⋅ ⎣⎣y/2⎦⎦) if y is odd function multiply(x,y) Input: Two n-bit integers x and y, where y ≥ 0 Output: € Their product if y = 0: return 0 z = multiply(x, floor(y/2)) if y is even: return 2z else: return x+2z CS 312 - Complexity Examples - Arithmetic and RSA 12
x y y binary # of x’s 15 8 0 1 30 4 0 2 60 2 0 4 120 1 1 8 120 Any x value added (when y is odd) in keeps getting doubled to its appropriate contribution as we unfold the recursion CS 312 - Complexity Examples - Arithmetic and RSA 13
- Page 1 and 2: Addition Multiplication Bigger Ex
- Page 3 and 4: Addition of two numbers of length n
- Page 5 and 6: Addition of two numbers of length n
- Page 7 and 8: Isn’t addition in a computer just
- Page 9 and 10: x y 15 11 At each step double x and
- Page 11: ⎧⎧ 2(x⋅ ⎣⎣y/2⎦⎦) if y
- Page 15 and 16: function multiply(x,y) Input: Two n
- Page 17 and 18: Is multiplication O(n 2 ) - Could w
- Page 19 and 20: Assume we start with numbers Mod N
- Page 21 and 22: There is a faster algorithm - Rathe
- Page 23 and 24: ⎧⎧ (x ⎣⎣y/2⎦⎦ ) 2 if y
- Page 25 and 26: x y y binary power of x z return va
- Page 27 and 28: function modexp (x, y, N) //Iterati
- Page 29 and 30: function primality(N) Input: Positi
- Page 31 and 32: How often does a composite number c
- Page 33 and 34: Primality testing is efficient! - O
⎧⎧ 2(x⋅ ⎣⎣y/2⎦⎦) if y is even<br />
x⋅ y = ⎨⎨<br />
⎩⎩ x + 2(x⋅ ⎣⎣y/2⎦⎦) if y is odd<br />
function multiply(x,y)<br />
Input: Two n-bit integers x <strong>and</strong> y, where y ≥ 0<br />
Output: € Their product<br />
if y = 0: return 0<br />
z = multiply(x, floor(y/2))<br />
if y is even: return 2z<br />
else: return x+2z<br />
CS 312 - Complexity Examples - <strong>Arithmetic</strong> <strong>and</strong> RSA 12