CS 312: Algorithm Analysis Lecture #3: Algorithms for Modular Arithmetic, Modular Exponentiation This work is licensed under a Creative Commons Attribution-Share.

Slides:



Advertisements
Similar presentations
Presented by Alex Atkins.  An integer p >= 2 is a prime if its only positive integer divisors are 1 and p.  Euclid proved that there are infinitely.
Advertisements

Cryptography and Network Security
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.
Public Key Cryptosystems - RSA Receiver Sender Eavesdroppe r p q p q p q p and q prime.
Number Theory and Cryptography
Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Announcements: HW3 updated. Due next Thursday HW3 updated. Due next Thursday Written quiz tomorrow on chapters 1-2 (next slide) Written quiz tomorrow on.
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
Arithmetic.
MATH 224 – Discrete Mathematics
Section 4.3: Fermat’s Little Theorem Practice HW (not to hand in) From Barr Text p. 284 # 1, 2.
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.
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.
CompSci 102 Discrete Math for Computer Science February 16, 2012 Prof. Rodger.
CS 312: Algorithm Analysis Lecture #8: Non-Homogeneous Recurrence Relations This work is licensed under a Creative Commons Attribution-Share Alike 3.0.
CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,
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 Analysis Lecture #1: Algorithms and Efficiency This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
CS 312: Algorithm Design & Analysis Lecture #24: Optimality, Gene Sequence Alignment 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.
CS 312: Algorithm Analysis Lecture #1: Algorithms and Efficiency This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
Information Security Lab. Dept. of Computer Engineering 87/121 PART I Symmetric Ciphers CHAPTER 4 Finite Fields 4.1 Groups, Rings, and Fields 4.2 Modular.
CS Modular Division and RSA1 RSA Public Key Encryption To do RSA we need fast Modular Exponentiation and Primality generation which we have shown.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
CS 312: Algorithm Analysis Lecture #7: Recurrence Relations a.k.a. Difference Equations Slides by: Eric Ringger, with contributions from Mike Jones, Eric.
CS 312: Algorithm Analysis Lecture #8: Non-Homogeneous Recurrence Relations This work is licensed under a Creative Commons Attribution-Share Alike 3.0.
CS 312: Algorithm Analysis
Implementation Issues for Public Key Algorithms
CS 312: Algorithm Analysis Lecture #31: Linear Programming: the Simplex Algorithm, part 2 This work is licensed under a Creative Commons Attribution-Share.
CS 312: Algorithm Analysis Lecture #31: Linear Programming: the Simplex Algorithm, part 2 This work is licensed under a Creative Commons Attribution-Share.
9.1 Primes and Related Congruence Equations 23 Sep 2013.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
Dan Boneh Intro. Number Theory Arithmetic algorithms Online Cryptography Course Dan Boneh.
CSE 311: Foundations of Computing Fall 2013 Lecture 11: Modular arithmetic and applications.
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.
Implementation of Public Key Encryption Algorithms
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
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.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 06 Mathematical Induction Modular Arithmetic Do question 1 on today's quiz (work with another person)
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 #26: 0/1 Knapsack This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
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.
MA/CSSE 473 Day 06 DivisionPrimes Modular Arithmetic.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
Chapter 4 With Question/Answer Animations 1. Chapter Motivation Number theory is the part of mathematics devoted to the study of the integers and their.
CSE 311 Foundations of Computing I Lecture 12 Modular Arithmetic and Applications Autumn 2012 CSE
CSE 311 Foundations of Computing I Lecture 11 Modular Exponentiation and Primes Autumn 2011 CSE 3111.
MA/CSSE 473 Day 07 Extended Euclid's Algorithm Modular Division Fermat's little theorem intro.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Growth of Functions & Algorithms
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Public Key Cryptosystems - RSA
Number Theory (Chapter 7)
Lecture 20 Guest lecturer: Neal Gupta
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Application: Algorithms
Cryptography Lecture 20.
Cryptography Lecture 16.
Presentation transcript:

CS 312: Algorithm Analysis Lecture #3: Algorithms for Modular Arithmetic, Modular Exponentiation 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 #1 Due Now  Always start of class  Always show work  FERPA protects your student record  Need waiver to return graded work without cover sheet

Objectives  Add the Max Rule to your asymptotic analysis toolbox  Review modular arithmetic  Discuss and analyze algorithms for:  modular arithmetic  modular exponentiation

Max. rule  Another useful rule for Asymptotic analysis. O( f(n) + g(n) ) = O( max( f(n), g(n) ) )  Examples:

Max. rule  Another useful rule for Asymptotic analysis. O( f(n) + g(n) ) = O( max( f(n), g(n) ) )  Examples:

Goal for Ch. 1  Appreciate the role of theoretical analysis in the security of RSA.  Requires: Solve, analyze, and use (!) two important and related problems:  Factoring: Given a number N, express it as a product of its prime numbers  Primality Testing: Given a number N, determine whether it is prime  Which one is harder?

Algorithms for Integer Arithmetic

 Addition  Multiplication  Division

Algorithms for Integer Arithmetic

Modular Arithmetic

Congruency

An important distinction  Congruency  Equality, using the modulus operator

Properties  Associativity:  Commutativity:  Distributivity:

Substitution Rule

Useful Consequence x y  (x mod z) y (mod z) x y mod z = (x mod z) y mod z  Example:

Useful Consequence x y  (x mod z) y (mod z) x y mod z = (x mod z) y mod z  Example:

Modular Addition

Modular Multiplication

Goal: Modular Exponentiation  We need to compute x y mod N for values of x, y, and N that are several hundred bits long.  Can we do so quickly?

Sequential Exponentiation Describe a simple algorithm for doing exponentiation:

Analysis of Sequential Exponentiation function seqexp (x, y) Input: An n-bit integer x and a non-negative integer exponent y (arbitrarily large) Output: x y if y=0: return 1 r = x for i = 1 to y-1 do r = r x return r

Modular Exponentiation, Take I

New Ideas  Represent y (the exponent) in binary  Then break down x y into factors using the non-zero bits of y  Also: compute the factors using repeated squaring  Reduce factors using substitution rule

New Ideas  Represent y (the exponent) in binary  Then break down x y into factors using the non-zero bits of y  Also: compute the factors using repeated squaring  Reduce factors using substitution rule

Modular Exponentiation, Take II Right shift Multiplication Recursive call

Analysis of Modular Exponentiation  Each multiplication is  (n 2 )  Each modular reduction is  (n 2 )  There are log(y)=m of them  Thus, modular exponentiation is in  (n 2 log y) =  (n 2 m) function modexp (x, y, N) if y=0: return 1 z = modexp(x, floor(y/2), N) if y is even: return z 2 mod N else: return x z 2 mod N

Modular Exponentiation (II), Iterative Formulation

Modular Exponentiation  x y mod N  Key Insights: 1.Exponent y can be represented in binary 2.Problem can be factored into one factor per binary digit 3.Each factor can be reduced mod N (substitution rule)

Example We’re employing same insights and a little more cleverness than the algorithm.

Example worked by Strictly Tracing the Algorithm 2^125 mod 127 modexp(2,125,127) x=2, y=125, N=127 i=125, r=1, z = 2 mod 127 = 2 r = 1*2 mod 127 = 2 z = 2^2 mod 127 = 4 i = 62 z = 4^2 mod 127 = 16 i = 31 r = 2 * 16 mod 127 = 32 z = 16^2 mod 127 = 2 * 128 mod 127 = 2 i = 15 r = 32 * 2 mod 127 = 64 z = 2^2 mod 127 = 4 i = 7 r = 64 * 4 mod 127 = 2 * 128 mod 127 = 2 z = 4^2 mod 127 = 16 i = 3 r = 2 * 16 mod 127 = 32 z = 16^2 mod 127 = 2 * 128 mod 127 = 2 i = 1 r = 32 * 2 mod 127 = 64 z = 2^2 mod 127 = 4 i = 0 return r=64 function modexp (x, y, N) Input: Two n-bit integers x and N, an integer exponent y (arbitrarily large) Output: x y mod N if y = 0: return 1 i = y; r = 1; z = x mod N while i > 0 if i is odd: r = r z mod N z = z 2 mod N i = floor(i/2) return r

Example #2 function modexp (x, y, N) Input: Two n-bit integers x and N, an integer exponent y (arbitrarily large) Output: x y mod N if y = 0: return 1 i = y; r = 1; z = x mod N while i > 0 if i is odd: r = r z mod N z = z 2 mod N i = floor(i/2) return r Strictly tracing the algorithm.

Example #2 function modexp (x, y, N) Input: Two n-bit integers x and N, an integer exponent y (arbitrarily large) Output: x y mod N if y = 0: return 1 i = y; r = 1; z = x mod N while i > 0 if i is odd: r = r z mod N z = z 2 mod N i = floor(i/2) return r

Example Needed: two volunteers: Volunteer A: use our final modexp() to compute it. Volunteer B: compute 3 20 then reduce mod 10

Efficiency  The key point is that x y mod N is easy  modexp is in  (n 2 log y)  In fact, it requires about 1.5 log 2 y multiplications for typical y  seqexp required y-1 multiplications  When x, y, and N are 200 digit numbers  Assume 1 multiplication of two 200 digit numbers takes seconds  modexp typically takes about 1 second  seqexp would require times the Age of the Universe!  Only works when y is an integer.

Assignment  Read: Section 1.4  HW #2:  Problem 1.25 using modexp,  Then redo 1.25 but replace 125 with 126 for the exponent  Implement modular exponentiation now as a step toward finishing Project #1

Next  Primality Testing