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.

Slides:



Advertisements
Similar presentations
Number Theory Algorithms and Cryptography Algorithms Prepared by John Reif, Ph.D. Analysis of Algorithms.
Advertisements

Section 4.1: Primes, Factorization, and the Euclidean Algorithm Practice HW (not to hand in) From Barr Text p. 160 # 6, 7, 8, 11, 12, 13.
Primality Testing Patrick Lee 12 July 2003 (updated on 13 July 2003)
CSE 311 Foundations of Computing I Lecture 13 Number Theory Autumn 2012 CSE
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Tuesday, 28 April Number-Theoretic Algorithms Chapter 31.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Tuesday, 26 November Number-Theoretic Algorithms Chapter 31.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 7 Tuesday, 11/6/01 Number-Theoretic Algorithms Chapter.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring
Great Theoretical Ideas in Computer Science.
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)
Module :MA3036NI Cryptography and Number Theory Lecture Week 7
1 Cryptography and Network Security Third Edition by William Stallings Lecture slides by Lawrie Brown Chapter 4 – Finite Fields.
CS 312: Algorithm Analysis Lecture #3: Algorithms for Modular Arithmetic, Modular Exponentiation This work is licensed under a Creative Commons Attribution-Share.
CS 312: Algorithm Analysis
CS 312: Algorithm Design & Analysis Lecture #34: Branch and Bound Design Options for Solving the TSP: Tight Bounds This work is licensed under a Creative.
RSA Parameter Generation Bob needs to: - find 2 large primes p,q - find e s.t. gcd(e, Á (pq))=1 Good news: - primes are fairly common: there are about.
Prabhas Chongstitvatana 1 Primality Testing Is a given odd integer prime or composite ? No known algorithm can solve this problem with certainty in a reasonable.
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers Miller-Rabin test.
CS 312: Algorithm Analysis Lecture #8: Non-Homogeneous Recurrence Relations This work is licensed under a Creative Commons Attribution-Share Alike 3.0.
MA/CSSE 473 Day 11 Primality testing summary Data Encryption RSA.
CS 312: Algorithm Design & Analysis Lecture #12: Average Case Analysis of Quicksort This work is licensed under a Creative Commons Attribution-Share Alike.
CS 312: Algorithm Design & Analysis Lecture #2: Asymptotic Notation This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
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.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Lecture 6.1: Misc. Topics: Number Theory CS 250, Discrete Structures, Fall 2011 Nitesh Saxena.
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.
Notation Intro. Number Theory Online Cryptography Course Dan Boneh
CSE 311 Foundations of Computing I Lecture 14 Euclid’s Algorithm Mathematical Induction Autumn 2012 CSE
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.
Great Theoretical Ideas in Computer Science for Some.
CS 312: Algorithm Analysis
CS 312: Algorithm Analysis Lecture #31: Linear Programming: the Simplex Algorithm, part 2 This work is licensed under a Creative Commons Attribution-Share.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
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.
Great Theoretical Ideas in Computer Science.
Great Theoretical Ideas In Computer Science COMPSCI 102 Fall 2010 Lecture 16October 27, 2010Duke University Modular Arithmetic and the RSA Cryptosystem.
Cryptography Lecture 14 Arpita Patra © Arpita Patra.
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 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
CS 312: Algorithm Analysis Lecture #9: Recurrence Relations - Change of Variable Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer,
CS 312: Algorithm Design & Analysis Lecture #29: Network Flow and Cuts This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
CS 312: Algorithm Analysis Lecture #30: Linear Programming: Intro. to the Simplex Algorithm This work is licensed under a Creative Commons Attribution-Share.
Great Theoretical Ideas in Computer Science.
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.
Number-Theoretic Algorithms
MA/CSSE 473 Day 07 Extended Euclid's Algorithm Modular Division
B504/I538: Introduction to Cryptography
Probabilistic Algorithms
MA/CSSE 473 Day 06 Euclid's Algorithm.
Advanced Algorithms Analysis and Design
Numerical Algorithms x x-1 Numerical Algorithms
Number-Theoretic Algorithms (UNIT-4)
CSE 311 Foundations of Computing I
Number Theory and Modular Arithmetic
Numerical Algorithms x x-1
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Number Theory (Chapter 7)
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
CSE 311: Foundations of Computing
Material in the textbook on
Presentation transcript:

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 Commons Attribution-Share Alike 3.0 Unported License Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick

Announcements  HW #2 Due Now  Homework: Required to show your work  Project #1  Today we’ll work through the rest of the math  Early: this Wednesday, 1/16  Due: Friday, 1/18  Bonus help session on Proof by Induction  Tuesday, 4pm, 1066 TMCB

Practice Key points: Represent exponent in binary Break up the problem into factors (one per binary digit) Use the substitution rule

Practice Key points: Represent exponent in binary Break up the problem into factors (one per binary digit) Use the substitution rule

Objectives  Part 1:  Introduce Fermat’s Little Theorem  Understand and analyze the Fermat primality tester  Part 2:  Discuss GCD and Multiplicative Inverses, modulo N  Prepare to Introduce Public Key Cryptography  This adds up to a lot of ideas!

Part 1: Primality Testing

Fermat’s Little Theorem p = 3, a = 2 p = 7, a = 4 How do you wish you could use this theorem?

Fermat’s Little Theorem p = 3, a = 2 p = 7, a = 4 How do you wish you could use this theorem?

Logic Review a  b (a implies b) Which is equivalent to the above statement?  b  a  ~a  ~b  ~b  ~a

Logic Review a  b (a implies b) Which is equivalent to the above statement?  b  aThe Converse  ~a  ~bThe Inverse  ~b  ~aThe Contrapositive

Contrapositive of Fermat’s Little Theorem

First Prime Number Test

False Witnesses  If primality(N) returns “possibly prime”, then N might or might not be prime, as the answer indicates  Consider 15:  4 14 mod 15 = 1  but 15 clearly isn’t prime!  4 is called a false witness of 15  Given a non-prime N, we call a number a where a n-1 mod N = 1 a “false witness” (to the claim that N is prime)

Relatively Prime  Two numbers, a and N, are relatively prime if their greatest common divisor is 1.  3 and 5?  4 and 8?  4 and 9?  Consider the Carmichael numbers:  They pass the test (i.e., a n-1 mod N = 1) for all a relatively prime to N.

False Witnesses  Ignoring Carmichael numbers,  How common are false witnesses?  Lemma: If a n-1 mod n = 1 for some a relatively prime to n, then it must hold for at least half the choices of a < n

State of Affairs  Summary:  If n is prime, then a n-1 mod n = 1 for all a < n  If n is not prime, then a n-1 mod n = 1 for at most half the values of a < n  Allows us to put a bound on how often our primality() function is wrong.

False Witnesses  Notes:  Less than 3.3% of the possible witnesses for n < 1000 are false.  Consider 651,693,055,693,681.  Have % chance of picking a false witness!

Correctness  Question #1: Is the “Fermat test” correct?  No  Question #1’: How correct is the Fermat test?  The algorithm is ½-correct with one-sided error.  The algorithm has 0.5 probability of saying “yes N is prime” when N is not prime.  But when the algorithm says “no N is not prime”, then N must not be prime (by contrapositive of Fermat's Little Theorem)

Amplification  Repeat the test  Decrease the probability of error: C = Composite; P = Prime  Amplification of stochastic advantage 1 st run2 nd runError? Cn/a PC PP

Amplification  Repeat the test  Decrease the probability of error: C = Composite; P = Prime  Amplification of stochastic advantage 1 st run2 nd runError? Cn/a PC PP

P(Correct)  k trials gives 1/(2 k ) probability of being incorrect when the answer is "prime“ P(Correct) =

Modified Primality Test function primality2(N) Input: Positive integer N Output: yes/no for i = 1 to k do: a = uniform(1..N-1) if (modexp(a, N-1, N) == 1): // possibly prime; do nothing else: return “not prime” return yes

2. Greatest Common Divisor

Greatest Common Divisor  Euclid’s rule:  gcd(x, y) = gcd (x mod y, y) = gcd (y, x mod y)  Can compute gcd(x,y) for large x, y by modular reduction until we reach the base case! function Euclid (a,b) Input: Two integers a and b with a  b  0 (n-bit integers) Output: gcd(a,b) if b=0: return a return Euclid(b, a mod b)

Example  gcd(25, 11)

Example  gcd(25, 11)

3 Questions  1. Is it Correct?  2. How long does it take?  3. Can we do better?

Analysis function Euclid (a,b) Input: Two integers a and b with a  b  0 (n-bit integers) Output: gcd(a,b) if b=0: return a return Euclid(b, a mod b)

Bezout’s Identity

Extended Euclid Algorithm function extended-Euclid (a, b) Input: Two positive integers a & b with a  b  0 (n-bits) Output: Integers x, y, d such that d = gcd(a, b) and ax + by = d if b=0: return (1,0,a) (x’, y’, d) = extended-Euclid(b, a mod b) return (y’, x’ – floor(a/b)y’, d)

Example  Note: there’s a great worked example of how to use the extended-Euclid algorithm on Wikipedia here: _algorithm  And another linked from the reading column on the schedule for today

Multiplicative Inverses  In the rationals, what’s the multiplicative inverse of a?  In modular arithmetic (modulo N), what is the multiplicative inverse?

Multiplicative Inverses  In the rationals, what’s the multiplicative inverse of a?  In modular arithmetic (modulo N), what is the multiplicative inverse?

Finding Multiplicative Inverses (Modulo N)

Multiplicative Inverses

Next  RSA

Assignment  Read and Understand: Section 1.4  HW #3: 1.9, 1.18, 1.20  Finish your project #1 early!  Submit on Wednesday by 5pm