MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.

Slides:



Advertisements
Similar presentations
1 Lect. 12: Number Theory. Contents Prime and Relative Prime Numbers Modular Arithmetic Fermat’s and Euler’s Theorem Extended Euclid’s Algorithm.
Advertisements

Chapter 8 – Introduction to Number Theory. Prime Numbers prime numbers only have divisors of 1 and self –they cannot be written as a product of other.
Computability and Complexity
Primality Testing Patrick Lee 12 July 2003 (updated on 13 July 2003)
22C:19 Discrete Structures Integers and Modular Arithmetic
Introduction to Modern Cryptography Lecture 6 1. Testing Primitive elements in Z p 2. Primality Testing. 3. Integer Multiplication & Factoring as a One.
22C:19 Discrete Math Integers and Modular Arithmetic Fall 2010 Sukumar Ghosh.
Number Theory and Cryptography
Great Theoretical Ideas in Computer Science.
1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 Room: EECS 6402, Tel: , Fax :
1 Fingerprint 2 Verifying set equality Verifying set equality v String Matching – Rabin-Karp Algorithm.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
Lecture 3.2: Public Key Cryptography II CS 436/636/736 Spring 2012 Nitesh Saxena.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring
The Integers and Division

9/2/2015Discrete Structures1 Let us get into… Number Theory.
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
Information Security and Management 4. Finite Fields 8
The Polynomial Time Algorithm for Testing Primality George T. Gilbert.
Modular Arithmetic.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
CPSC 490 Number Theory Primes, Factoring and Euler Phi-function Mar.31 st, 2006 Sam Chan.
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers Miller-Rabin test.
The Complexity of Primality Testing. What is Primality Testing? Testing whether an integer is prime or not. – An integer p is prime if the only integers.
MA/CSSE 473 Day 11 Primality testing summary Data Encryption RSA.
PRIMES is in P Manindra Agrawal NUS Singapore / IIT Kanpur.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 31.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
22C:19 Discrete Structures Integers and Modular Arithmetic Fall 2014 Sukumar Ghosh.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
CS Modular Division and RSA1 RSA Public Key Encryption To do RSA we need fast Modular Exponentiation and Primality generation which we have shown.
MA/CSSE 473 Day 08 Extended Euclid's Algorithm Modular Division Fermat's little theorem.
Tuesday’s lecture: Today’s lecture: One-way permutations (OWPs)
Ch1 - Algorithms with numbers Basic arithmetic Basic arithmetic Addition Addition Multiplication Multiplication Division Division Modular arithmetic Modular.
Lecture 3.1: Public Key Cryptography I CS 436/636/736 Spring 2015 Nitesh Saxena.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
Chapter 1 Algorithms with Numbers. Bases and Logs How many digits does it take to represent the number N >= 0 in base 2? With k digits the largest number.
MA/CSSE 473 Day 10 Primality Testing. MA/CSSE 473 Day 10 In-class exam: Friday, Sept 28 –You may bring a two-sided 8.5x11 inch piece of paper containing.
MA/CSSE 473 Day 06 DivisionPrimes Modular Arithmetic.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 07 Extended Euclid's Algorithm Modular Division Fermat's little theorem intro.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
MA/CSSE 473 Day 9 Primality Testing Encryption Intro.
A Prime Example CS Lecture 20 A positive integer p  2 is prime if the only positive integers that divide p are 1 and p itself. Positive integers.
MA/CSSE 473 Day 9 Primality Testing Encryption Intro.
MA/CSSE 473 Day 07 Extended Euclid's Algorithm Modular Division
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
B504/I538: Introduction to Cryptography
MA/CSSE 473 Day 06 Euclid's Algorithm.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 06 Euclid's Algorithm.
Handbook of Applied Cryptography - CH4, from 4.1~4.3
Lecture 3.2: Public Key Cryptography II
Numerical Algorithms x x-1 Numerical Algorithms
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Material in the textbook on pages
Number Theory (Chapter 7)
Lecture 3.1: Public Key Cryptography I
Copyright © Zeph Grunschlag,
Mathematical Background for Cryptography
Patrick Lee 12 July 2003 (updated on 13 July 2003)
Clements MAΘ October 30th, 2014
Cryptography Lecture 20.
From the last time: gcd(a, b) can be characterized in two different ways: It is the least positive value of ax + by where x and y range over integers.
Presentation transcript:

MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing

MA/CSSE 473 Day 09 HW 5 due Monday. HW 6 has been updated for Fall. Due Thursday; larger than HW4 and 5 (more like HW3). A "late day" ends 24 hours after the assignment is due. Submit whatever you have done before that time if you want credit. HW3 scores –I would say that 48 points is approximately the boundary between "nothing to worry about" and "starting to get concerned". Modular inverse, division revisited Fermat's Little Theorem and its proof Primality Testing Why Dave Rader does not like the Levitin textbook…

Recap: Extended Euclid Algorithm Given positive integers a and b Extended Euclid algorithm returns integers x, y, and d, such that –d = gcd(a, b) –d = ax + by We use this as the basis for calculating modular inverse Another place to read about modular arithmetic, including exponentiation and inverse: Weiss Sections

Revisit: Modular Inverse In the real or rational numbers, every non-zero number a has an inverse 1/a, also written a -1 –x is the inverse of a iff ax = 1 –Every non-zero real number has a unique inverse Definition x is the multiplicative inverse of a (modulo N) if ax  1 (mod N) We denote this inverse by a -1 (if it exists) –Note that 2 has no inverse modulo 6 –Does 5 have an inverse (modulo 6)? a has an inverse modulo N if and only if gcd(a, N) = 1 i.e. a and N are relatively prime If a -1 exists, it is unique (among 1..N-1)

Calculate Modular Inverse (if it exists) Assume that gcd(a, N) = 1. The extended Euclid's algorithm gives us integers x and y such that ax + Ny = 1 This implies ax  1 (mod N), so x is the inverse of a Example: Find mod 25 –We saw before that -34* *25 = 1 –-34  16 (mod 25) –So = 16 mod 25 Recall that Euclid's algorithm is Ѳ(n 3 ), where n is the number of bits of N. Q1

Revisit: Modular division We can only divide b by a (modulo N) if N and a are relatively prime In that case b/a is defined to be b∙a -1 What is the running time for modular division? Q2

Primality Testing The numbers 7, 17, 19, 71, and 79 are primes, but what about (a typical social security number)? There are some tricks that might help. For example: –If n is even and not equal to 2, it's not prime –n is divisible by 3 iff the sum of its decimal digits is divisible by 3, –n is divisible by 5 iff it ends in 5 or 0 –n is divisible by 7 iff  n/10  - 2*n%10 is divisible by 7 –n is divisible by 11 iff (sum of n's odd digits) – (sum of n's even digits) is divisible by 11. –when checking for factors, we only need to consider prime numbers as candidates –When checking for factors, we only need to look for numbers up to sqrt(n) Q3

Primality testing But this approach is not very fast. Factoring is harder than primality testing. Is there a way to tell whether a number is prime without actually factoring the number? Like a few other things that we have done so far ion this course, this discussion follows Dasgupta, et. al., Algortihms (McGraw-Hill 2008)

Fermat's Little Theorem (1640 AD) Formulation 1: If p is prime, then for every number a with 1 ≤ a <p, a p-1  1 (mod p) Formulation 2: If p is prime, then for every number a with 1 ≤ a <p, a p  a (mod p) These are clearly equivalent. –How do we get from each to the other? We will examine a combinatorial proof of the first formulation.

Fermat's Little Theorem: Proof (part 1) Formulation 1: If p is prime, then for every number a with 1 ≤ a < p, a p-1  1 (mod p) Let S = {1, 2, …, p-1} Lemma –Multiplying all of the numbers in S by a (mod p) permutes S –I.e. {a ∙ n (mod p) : n  S} = S Example: p=7, a=3. Proof of the lemma –Suppose that a∙i  a∙j (mod p). –Since p is prime and a  0, a has an inverse. –Multiplying both sides by a -1 yields i  j (mod p). –Thus, multiplying the elements of S by a (mod p) takes each element to a different element of S. –Thus (by the pigeonhole principle), every number 1..p-1 is a∙i (mod p) for some i in S. k k362514

Fermat's Little Theorem: Proof (part 2) Formulation 1: If p is prime, then for every number a with 1 ≤ a <p, a p-1  1 (mod p) Let S = {1, 2, …, p-1} Lemma Multiplying all of the numbers in S by a (mod p) permutes S Therefore: {1, 2, …, p-1} = {a∙1 (mod p), a∙2 (mod p), … a∙(p-1) (mod p)} Take the product of all of the elements on each side. (p-1)!  a p-1 (p-1)! (mod p) Since p is prime, (p-1)! is relatively prime to p, so we can divide both sides by it to get the desired result.

Recap: Fermat's Little Theorem Formulation 1: If p is prime, then for every number a with 1 ≤ a <p, a p-1  1 (mod p) Formulation 2: If p is prime, then for every number a with 1 ≤ a <p, a p  a (mod p) Q4-5

Easy Primality Test? Is N prime? Pick some a with 1 < a < N Is a N-1  1 (mod N)? If so, N is prime; if not, N is composite Nice try, but… –Fermat's Little Theorem is not an "if and only if" condition. –It doesn't say what happens when N is not prime. –N may not be prime, but we might just happen to pick an a for which a N-1  1 (mod N) –Example: 341 is not prime (it is 11∙31), but  1 (mod 341) Definition: We say that a number a passes the Fermat test if a N-1  1 (mod N) We can hope that if N is composite, then many values of a will fail the test It turns out that this hope is well-founded If any integer that is relatively prime to N fails the test, then at least half of the numbers a such that 1 ≤ a < N also fail it. "composite" means "not prime"

How many "false positives"? If N is composite, and we randomly pick an a such that 1 ≤ a < N, and gcd(a, N) = 1, how likely is it that a N-1 is  1 (mod n)? If a N-1  1 (mod n) for some a that is relatively prime to N, then this must also be true for at least half of the choices of a < N. –Let b be some number (if any exist) that passes the Fermat test, i.e. b N-1  1 (mod N). –Then the number a∙b fails the test: (ab) N-1  a N-1 b N-1  a N-1, which is not congruent to 1 mod N. –Diagram on whiteboard. –For a fixed a, f: b  ab is a one-to-one function on the set of b's that pass the Fermat test, –so there are at least as many numbers that fail the Fermat test as pass it. Q6

Carmichael Numbers A Carmichael N number is a composite number that passes the Fermat test for all a with 0< a<N and gcd(a, N)=1. –The smallest Carmichael number is 561 –We'll see later how to deal with those –How rare are they? Let C(X) = number of Carmichael numbers that are less than X. –For now, we pretend that we live in a Carmichael-free world Q6-8

Where are we now? We ignore Carmichael numbers for now. If N is prime, a N-1  1 (mod N) for all a with 0 < a < N If N is not prime, then a N-1  1 (mod N) for at most half of the values of a. Pr(algorithm returns true for a if N is prime) = 1 Pr(alg. returns true for a if N is composite) ≤ ½ How to reduce the probability of error?

The algorithm To test N for primality –Pick positive integers a 1, a 2, …,a k < N at random –if a i N-1  1 (mod N) for all i = 1, 2, …, k: return the result of Miller-Rabin test (We'll look at that test later) –else: return false

Carmichael Numbers A Carmichael N number is a composite number that passes the Fermat test for all a with 0< a<N A way around them (Rabin and Miller): –For some t and u, N-1 = 2 t u. –As before, compute a N-1 (mod N), but do it this way: –Calculate a u (mod N), then repeatedly square, to get the sequence a u (mod N), a 2u (mod N), …, a 2 t u (mod N)  a N-1 (mod N) How does this help? More next time!

Generating Random Primes

Amortized algorithms P49-50 Growable array exercise from 220/230