Modular Arithmetic and Primality
Modular Arithmetic and Primality Modular Arithmetic and Primality
Convenient in many cases when we want to restrict potentially large numbers to a specific range: time of day Can work with numbers in the computer word range (e.g. 64 bits) while still working with numbers which could initially be much larger 8 (mod 3) = 2 = 11 (mod 3) = 14 (mod 3): Congruent or in the same equivalence class. Three equivalence classes for mod 3 (those with remainder 0, 1, or 2). All congruent numbers can be substituted for each other in modular arithmetic. – 8 + 4 = 14 + 7 = 8 + 1 (mod 3) = 0 – 8 · 4 = 14 · 7 = 8 · 1 (mod 3) = 2 Simplified form is a number between 0 and mod-1, (not negative, etc.) Thus during arithmetic we can reduce intermediate results to their remainder Modulo N at any stage, which leads to significant efficiencies – 2 600 (mod 31) = (2 5 ) 120 = 32 120 = 1 120 = 1 (mod 31) CS 312 - Complexity Examples - Arithmetic and RSA 18
Assume we start with numbers Mod N - Thus they are numbers between 0 and N-1 which means length of numbers is n = log(N) – If not already in Modulus range then Modular reduction requires an initial standard division which is O(n 2 ) Modular Addition is O(n) since it requires one addition and possibly one subtraction if sum is greater than N. Modular Multiplication is O(n 2 ) – Just standard multiplication followed by a division if product exceeds N Modular division is O(n 3 ) – more later CS 312 - Complexity Examples - Arithmetic and RSA 19
- 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 and 12: ⎧⎧ 2(x⋅ ⎣⎣y/2⎦⎦) if y
- Page 13 and 14: x y y binary # of x’s 15 8 0 1 30
- Page 15 and 16: function multiply(x,y) Input: Two n
- Page 17: Is multiplication O(n 2 ) - Could w
- 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
Convenient in many cases when we want to restrict potentially large<br />
numbers to a specific range: time of day<br />
Can work with numbers in the computer word range (e.g. 64 bits)<br />
while still working with numbers which could initially be much larger<br />
8 (mod 3) = 2 = 11 (mod 3) = 14 (mod 3): Congruent or in the same<br />
equivalence class. Three equivalence classes for mod 3 (those with<br />
remainder 0, 1, or 2). All congruent numbers can be substituted for<br />
each other in modular arithmetic.<br />
– 8 + 4 = 14 + 7 = 8 + 1 (mod 3) = 0<br />
– 8 · 4 = 14 · 7 = 8 · 1 (mod 3) = 2<br />
Simplified form is a number between 0 <strong>and</strong> mod-1, (not negative, etc.)<br />
Thus during arithmetic we can reduce intermediate results to their<br />
remainder Modulo N at any stage, which leads to significant<br />
efficiencies<br />
– 2 600 (mod 31) = (2 5 ) 120 = 32 120 = 1 120 = 1 (mod 31)<br />
CS 312 - Complexity Examples - <strong>Arithmetic</strong> <strong>and</strong> RSA 18