Material in the textbook on pages

Slides:



Advertisements
Similar presentations
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Advertisements

Cryptography and Network Security
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
Having Proofs for Incorrectness
Chapter 8 Introduction To Number Theory. Prime Numbers Prime numbers only have divisors of 1 and Prime numbers only have divisors of 1 and self. self.
Primality Testing Patrick Lee 12 July 2003 (updated on 13 July 2003)
Introduction to Modern Cryptography Lecture 6 1. Testing Primitive elements in Z p 2. Primality Testing. 3. Integer Multiplication & Factoring as a One.
COM 5336 Cryptography Lecture 7a Primality Testing
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
1 Fingerprint 2 Verifying set equality Verifying set equality v String Matching – Rabin-Karp Algorithm.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
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 numbers.
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
implementations in a functional language
Theory I Algorithm Design and Analysis (9 – Randomized algorithms) Prof. Dr. Th. Ottmann.
Chapter 8 – Introduction to Number Theory Prime Numbers
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 numbers.
1 Gentle Introduction to Programming Session 2: Functions.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.

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.
Chapter 14 Randomized algorithms Introduction Las Vegas and Monte Carlo algorithms Randomized Quicksort Randomized selection Testing String Equality Pattern.
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.
Approximation Algorithms Pages ADVANCED TOPICS IN COMPLEXITY THEORY.
PRIMES is in P Manindra Agrawal NUS Singapore / IIT Kanpur.
11 -1 Chapter 11 Randomized Algorithms Randomized Algorithms In a randomized algorithm (probabilistic algorithm), we make some random choices.
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
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.
ENCRYPTION TAKE 2: PRACTICAL DETAILS David Kauchak CS52 – Spring 2015.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
9/22/15UB Fall 2015 CSE565: S. Upadhyaya Lec 7.1 CSE565: Computer Security Lecture 7 Number Theory Concepts Shambhu Upadhyaya Computer Science & Eng. University.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
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.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
PRIMES is in P Manindra Agrawal Neeraj Kayal Nitin Saxena Dept of CSE, IIT Kanpur.
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.
RSA Encryption Greg Gronn Laura Trimmer. RSA Encryption  Requires two 30 digit prime numbers to create an encoding/decryption key.  Goal: analyze different.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Randomness and Computation: Some Prime Examples
Lecture #5 מבוא מורחב.
High order procedures Primality testing The RSA cryptosystem
CSE565: Computer Security Lecture 7 Number Theory Concepts
Probabilistic Algorithms
Great Theoretical Ideas in Computer Science
Handbook of Applied Cryptography - CH4, from 4.1~4.3
Randomness and Computation: Some Prime Examples
Cryptography and Network Security
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Introduction to Number Theory
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Number Theory (Chapter 7)
Higher-Order Procedures
CS 154, Lecture 6: Communication Complexity
Lecture #5 מבוא מורחב.
CS220 Programming Principles
Material in the textbook on
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
CS21 Decidability and Tractability
This Lecture Substitution model
Lecture 6 - Recursion.
Presentation transcript:

Material in the textbook on pages Lecture 5 Material in the textbook on pages 50-53 (1.2.6) 56-66 (1.3.1, 1.3.2) Of second edition מבוא מורחב

Primality Testing - II n is a prime iff its only divisors are 1 and n Iff it has no divisors between 2 and (sqrt n) (define (divides? a b) (= (remainder b a) 0)) (define (find-smallest-divisor n i) (cond ((> i (sqrt n)) n) ((divides? i n) i) (else (find-smallest-divisor n (+ i 1))))) (define (prime? n) (= n (find-smallest-divisor n 2))) מבוא מורחב

(Prime? 47) (= 47 (find-smallest-divisor 47 2)) (= 47 (cond (divides? 2 47) 2) (else (find-smallest-divisor 47 3)))) (= 47 (find-smallest-divisor 47 3)) (= 47 (find-smallest-divisor 47 4)) (= 47 (find-smallest-divisor 47 5)) (= 47 (find-smallest-divisor 47 6)) (= 47 (find-smallest-divisor 47 7)) (= 47 47) #t מבוא מורחב

Analysis Correctness: If n is not a prime, then n=a * b for a,b>1. Then at least one of them is n. So n must have a divisor smaller then n. Time complexity: first test - (n) second test -  (n) . For a number n, we test at most n numbers to see if they divide n. If n is a 800 digit number, that’s very bad. Absolutely infeasible. מבוא מורחב

The Fermat Primality Test Fermat’s little theorem: If n is a prime number then: an = a (mod n) for every 0 < a < n, integer The Fermat Test: Do 400 times: Pick a random a < n and compute an (mod n) If  a then for sure n is not a prime. If all 400 tests passed, declare that n is a prime. מבוא מורחב

Computing ab (mod m) fast. (define (expmod a b m) ; computes ab (mod m) (cond ((= b 0) 1) ((even? b) (remainder (expmod (remainder (* a a) m) (/ b 2) m) m)) (else (remainder (* a (expmod a (- b 1) m)) m))))

Implementing Fermat test (define (test a n)(= (expmod a n n) a)) (define (one-test n) (test (+ 1 (random (- n 1))) n)) (define (many-tests n t); calls one-test t times (cond ((= t 0) true) ((one-test n) (many-test n (- t 1))) (else false))) מבוא מורחב

Time complexity To test if n is a prime. We run 400 tests. Each takes about log(n) multiplcations. T(n) = O(log n) מבוא מורחב

Correctness – I (prime numbers) Fermat’s theorem: Every prime will always pass the test. It therefore follows that if n is a prime then for every a, test(a n) is true, hence we always pass the test, And we declare n to be a prime. For a prime n: We are always right. מבוא מורחב

Correctness II – Carmichael numbers. Definition: A Carmichael number, is a number such that n is Composite, and n always passes the test. For every a, an = a (mod n) If n is a Carmichael number we always pass the test, hence we always declare that n is prime. For a Carmichael number n: We are always wrong. מבוא מורחב

Correctness III – any other number A fact: If n is not prime and not a Carmichael number then: for at least half of the choices of a, an <> a (mod n). Hence, if we chose a at random, then with probability half the test fails and we declare that n is composite. The probability all 100 tests fail is at most 2-400 For such n: We are wrong with probability at most 2-400 מבוא מורחב

Correctness Suppose we do the test t=400 times. If n is a prime we are never wrong. If n is a Carmichael number, we are always wrong If n is a composite number and not a Carmichael number we are wrong with probability at most 2-400 . Error probability smaller than the chance the hardware is faulty. מבוא מורחב

A probabilistic algorithm An algorithm that uses random coins, and for every input gives the right answer with a good probability. Even though Carmichael numbers are very rare Fermat test is not good enough. There are inputs on which it is wrong. There are modifications of Fermat’s test, that for every input give the right answer, with a high probability. We want an algorithm that is good for every input, because the input can be chosen by an adversary, and he will chose the wrost input for us. We therefore can not assume any distribution over the inputs, certainly not that they are chosen at random. Given the input, however, we chose random coins, and we control the probability distribution we use. מבוא מורחב

Types so far Numbers: 1, 7, 1.2 Boolean: #t , #f Strings: “this is a string” Procedures: (< 2 3), (even? 7), (+ 6 3), (define (f x) (if (< x 0) “x is negative” “x is not negative”)) מבוא מורחב

Procedures have types A procedure may have requirements regarding the number of its arguments, may expect each argument to be of a certain type. The procedure + expects numbers as its arguments. Can not be applied on strings. The procedure < expects at least one argument. Will not accept strings as arguments. (< “abc” “xyz”) מבוא מורחב

Procedures have types The type of a procedure is a contract: If the operands have the specified types, the procedure will result in a value of the specified type otherwise, its behavior is undefined maybe an error, maybe random behavior מבוא מורחב

Example The type of the integer-add procedure is two arguments, both numbers result value of integer-add is a number number, number  number (+ 7 “xx”) - causes an error. מבוא מורחב

number, number, number number Your turn The following expressions evaluate to values of what type? (lambda (a b c) (if (> a 0) (+ b c) (- b c))) (lambda (p) (if p "hi" "bye")) (* 3.14 (* 2 5)) number, number, number number Boolean string number

Types (summary) type: a set of values every value has a type procedure types (types which include ) indicate number of arguments required type of each argument type of result of the procedure מבוא מורחב

Can procedures get and return procedures? Can a procedure return a procedure as its value? Can a procedure get a procedure as an argument? Can this be useful? מבוא מורחב

Consider the following three sums 1 + 2 + … + 100 = (100 * 101)/2 1 + 4 + 9 + … + 1002 = (100 * 101 * 201)/6 1 + 1/32 + 1/52 + … + 1/1012 = p2/8 In mathematics they are all captured by the notion of a sum: מבוא מורחב

Let’s have a look at the three programs (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ 1 a) b)))) (define (sum-squares a b) (if (> a b) 0 (+ (square a) (sum-squares (+ 1 a) b)))) (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) מבוא מורחב

Let’s check this new procedure out! (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? (number  number, number, number number, number)  number procedure procedure procedure מבוא מורחב

Higher order procedures A higher order procedure: takes a procedure as an argument or returns one as a value Examples: 1. (define (sum-integers1 a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b)) 2. (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b)) 3. (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b)) מבוא מורחב

Does it work? (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (sum square 1 (lambda (x) (+ x 1)) 100) (+ (square 1) (sum square ((lambda (x) (+ x 1)) 1) (lambda (x) (+ x 1)) 100)) (+ 1 (sum square 2 (lambda (x) (+ x 1)) 100)) (+ 1 (+ (square 2) (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (+ 9 (sum square 4 (lambda (x) (+ x 1)) 100))) מבוא מורחב

Integration as a procedure Integration under a curve f is given roughly by dx (f(a) + f(a + dx) + f(a + 2dx) + … + f(b)) a b dx f (define (integral f a b) (* (sum f a (lambda (x) (+ x dx)) b) dx)) (define dx 1.0e-3) (define atan (lambda (a) (integral (lambda (x) (/ 1 (+ 1 (square x)))) 0 a)))

A moment of reflection It is nice that procedures can be treated as any other value. It can help abstract our thinking as with the sum example. Sometime we would actually send a program rather than execute it. E.g., if the data is not under our control. In fact, it happens quite a lot with web (and other highly distributed) settings. It is nice we can send a mobile agent free to Wonder around and execute somewhere else. What does it cost us? מבוא מורחב

The syntactic sugar “Let” Suppose we wish to implement the function f(x,y) = x(1+x*y)2 + y(1-y) + (1+x*y)(1-y) We can also express this as a = 1+x*y b = 1-y f(x,y) = xa2 + yb + ab

The syntactic sugar “Let” (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y))) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))

The syntactic sugar “Let” (Let ((<var1> <exp1>) (<var2> <exp2>) .. (<varn> <expn>)) <body>) ((lambda (<var1> ….. <varn>) <body>) <exp1> <exp2> … <expn>)