z e e p l e a r n s t o p a i n t - wilkes...

29
ZEEP LEARNS TO PAINT (Adapted from an idea by Danny Hills, MIT) HOW TO PAINT 1. Buy paint 2. Open the can of paint 3. Dip the hairy end of the brush into the paint 4. Rub the paint from the brush onto the wall 5. If the can is empty, go to step 1, otherwise, go back to step 3 HOW TO BUY SOMETHING 1. Walk to the store 2. Ask the person in the store for something 3. Pay for it 4. Bring it home HOW TO TELL IF A CAN IS EMPTY 1. Dip brush in can 2. If paint gets on the brush the answer is “NO”, otherwise the answer is “YES”

Upload: others

Post on 15-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

ZEEP LEARNS TO PAINT (Adapted from an idea by Danny Hills, MIT)

HOW TO PAINT

1. Buy paint2. Open the can of paint3. Dip the hairy end of the brush into the paint4. Rub the paint from the brush onto the wall5. If the can is empty, go to step 1, otherwise, go back to step 3

HOW TO BUY SOMETHING

1. Walk to the store2. Ask the person in the store for something3. Pay for it4. Bring it home

HOW TO TELL IF A CAN IS EMPTY

1. Dip brush in can2. If paint gets on the brush the answer is “NO”, otherwise the answer is “YES”

Page 2: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

1 / 7

1

7

1 0

8

142857

1 03 02 06 04 05 0* * 1 0

28000 5 6

GowiithStats

Go

Page 3: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 4: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 5: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 6: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 7: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

In the computer science subfield of algorithmic information theory, a Chaitin constant (Chaitin omega number) or halting probability is a real number that informally represents the probability that a randomly constructed program will halt.

function TorF = halt(a) if halt(me ) while true disp(' ha ha ') endend

Page 8: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 9: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Array Sorting Algorithms Algorithm Time Complexity Space Complexity Best Avg Worst Worst Quicksort Mergesort Timsort Heappsort Bubble Sort Insertion Sort Selection Sort Tree Soort Shell Sort Bucket Sort Radix Sort Counting Sort Cubesort

from https://www.bigocheatsheet.com

Page 10: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 11: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id
Page 12: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

zerosperms

zeros --> 120zersozeorszeosrzesorzesrozreoszresozroeszrosezrsoezrseozoreszorsezoerszoesrzoserzosrezsroezsreozsorezsoerzseorzseroezrosezrsoezorsezosrezsor

perms(recursive) 174

function Rperms(s,init) n=length(s); if n == init disp(s); else Rperms(s,init+1); for i=(init+1):n x = s(init); s(init) = s(i); s(i)=x;

Rperms(s,init+1);

x = s(i); s(i) = s(init); s(init)=x; end end end

string

clock

function permutations(s) Rperms(s,1);end

Page 13: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

usebinarynary --> 126iaryinryinayinarbarybnrybnaybnarbirybiaybiarbinybinrbinaearyenryenayenareiryeiayeiareinyeinreinaebryebayebarebny

combinations

101string is

clock -disp(combnk('zeros',3))

ros eos ers ero ers zos zro zes zeo zer

4

function combinations (s,k) clc slen = length(s); n = slen; lim= 2^n;

for i=1:lim+1 binStr = []; n = slen; decN = i; comboset = []; while (decN > 0) binStr = [mod(decN,2) binStr]; if mod(decN,2) == 1 if n > 0 comboset = [s(n) comboset]; endif endif decN = floor(decN/2); n = n-1; endwhile

if length(comboset) == k disp(comboset); endif endfor endfunction

Page 14: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

8 3 5 1GCD

function numOut = MYgcd(a,b) if (a < b) temp = a; a = b; b = temp; end while (b ~= 0) r=mod(a,b); a=b; b=r; end numOut=a;end

function numOut = rGCD(a,b) if mod(a,b) == 0 numOut = b; else numOut = rGCD(b, mod(a,b)); endend

%% Bezout's identityfunction vOut = egcd(a, b)%% will output gcd(a,b), s, and t such that gcd(a,b) = as + bt if a == 0 vOut = [b, 0, 1]; else k=egcd(mod(b,a),a); g = k(1); x = k(2); y = k(3); vOut = [g, y - floor(b/a)*x, x]; endend

2 8 0

35 = 8(4) + 38 = 3(2) + 23 = 2(1) + 12 = 1(2) + 0

1 = 8(-13) + 35(3)

NOTE: MatLab/Octave contains a very robust version of gcd. Use an internet search for MatLab gcd to see the specifics.

Page 15: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Definition: ax = b(mod m) is called a linear congruence.

Theorem: Let d = gcd(a, m) = a(s) + m(t). If d | b, then thr solutions to ax = b(mod m) are given by

x = sb + m u for all integers u d d

Example: Find the solutions 3x = 4(mod 7)gcd(3 ,7) :7 = 3(2) + 13 = 1(3) + 0

1 = 3(-2) + 7(1) Now s = -2, t = 1, b = 4, d = 1, m = 7 and x = (sb)/d + (m/d)u

x = -8 + 7ux = 6,13,20 ...

3 7

Solve

4

a b m

Page 16: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Definition: The inverse of a modulo m, a, has the property aa = 1(mod m)

Theorem: The inverse of a modulo m exists iff a and m are relatively prime.

Theorem: ax = b(mod m) is equivalent to x = ab(mod m) if a and m are relatively prime.

Example: Find the inverse of 3 modulo 7gcd(3 ,7) :7 = 3(2) + 13 = 1(3) + 0

1 = 3(-2) + 7(1) Now s = -2, t = 1, b = 1, d = 1, m = 7 and x = (sb)/d + (m/d)u

x = -2 + 7u, so 3 inverse modulo 7 is x and x = 5,12,19,26 ...

3 71a b m

Solve

Page 17: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Theorem: ax = b(mod m) is equivalent to x = ab(mod m) if a and m are relatively prime.

Example: 5x = 3(mod 11) is equivalent to x = 27(mod 11)

Theorem: (ca)x = (cb)(mod (cm)) is equivalent to ax = b(mod m)

gcd(5 ,11) :11 = 5(2) + 15 = 1(5) + 0

1 = 5(-2) + 11(1) Now s = -2, t = 1, b = 1, d = 1, m = 11 and x = (sb)/d + (m/d)u

x = -2 + 11u, so 5 inverse modulo 11 is x and x = 9,20,31,42 ...

Therefore 5x = 3(mod 11) is equivalent to x = 27(mod 11)

5 1 13

a b m

Solve

Page 18: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Chinese Remainder Theorem: Let m and n be relatively prime. Then the system x = a(mod m) x = b(mod n)

has a solution. Furthermore, all solutions are congruent to ant + bms modulo mn where ms + nt = 1.

Example: Find soluutions to x = 2(mod 3) 4 x = 5(mod 9) x = 3(mod 5) 2 x = 6(mod 20)

gcd(3 ,5) :5 = 3(1) + 23 = 2(1) + 12 = 1(2) + 0

1 = 3(2) + 5(-1)

Now s = 2, t = -1, b = 3, a = 2, m = 3,n = 5 and x = (ant + bms) (mod mn) So x = 8( mod 15 )

5

3

3

nb

m

Solve

a

2

Page 19: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Chinese Remainder Theorem: Let m, n, and k be pairwise relatively prime. Then the system

x = a(mod m) x = b(mod n) x = c(mod k)

Example: Find soluutions to x = 3(mod 5) x = 1(mod 7) x = 6(mod 8)

gcd(5 ,7) :7 = 5(1) + 25 = 2(2) + 12 = 1(2) + 0

1 = 5(3) + 7(-2)

Now s = 3, t = -2, b = 1, a = 3, m = 5,n = 7 and x = (ant + bms) (mod mn) So x = -27( mod 35 ) = 8( mod 35 )

7

5

1

nb

m

Solve

a

3

c k

6 8

x = 78( mod 280 )

final answer

Page 20: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Fermat's Little Theorem: If p a and p is a prime number. Then a = 1(mod p)

Examples: Find the solution to 2 ^ 50 (mod 17) 4 ^ 532 (mod 11) 7 ^ 329 (mod 50)

So 2 ^ 16 = 1 ( mod 17)2 ^ 50 = (2 ^ 16 ) ^ 3 x 2 ^ 2

So 2 ^ 50 is congruent to (1 ^ 3 x 4) (mod 17)

OR 2 ^ 50 is congruent to 4 (mod 17)

a

power

2 1 7

Solve

5 0

m

23571 11 3

Page 21: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Cryptography: The study of techniques for secure transmission of information in the presence of adversaries.

How can Alice send secret messages to Bob without Eve being able to read them?

Page 22: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Private key cryptography:

Sender and receiver agree on a secret key that both use to encrypt/decrypt their message. eg Caesar Cipher

• Alice wants to send Bob a briefcase with a secret message; they both have a common key to lock/unlock the briefcase.

• Alice locks the briefcase with the shared key and Bob unlocks the briefcase with the shared key.

Page 23: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

Public key cryptography:

A public key is used to encrypt the message, and a private key is used to decrypt the message

• Alice puts the message in the briefcase and locks it with her own key, A.

• Bob gets the locked briiefcase, adds his lock, B, and sends it back to Alice.

• Alice gets the double locked box, removes A, and sends it back to Bob.

• Bob opens the briefcase using his own key.

Page 24: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

• Bob has two keys, public and private

• Everyone knows Bob's public key, but only he knows his private key.

• Alice encrypts a message using Bob's public key

• Bob decrpts the message using his private key.

• The public key can encrypt, but not decrypt

• Therefore, no one can read the message but Bob

RSA: (Rivest, Shamir, Adlemann)

Page 25: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

• In the RSA system, private key consists of two very large prime numbers p , q

• Public key consists of a number n , which is the product of p , q and another number e - relatively prime with (p - 1)(q - 1) (!(N )) Euler's totient function,

• Encrypt messages using n , e , but to decrypt, must know p , q

• In theory, can extract p , q from n using prime factorization which is intractable for very large numbers

• The security of RSA relies on inherent computational difficulty of prime factorization

Page 26: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

• To send message to Bob, Alice first represents message as a sequence of numbers Call this number representing message M

• Alice then uses Bob’s public key n , e to perform encryption as: C = M e (mod n ) , C is called the ciphertext

Example:

• Encrypt message ”STOP” using RSA with n = 2537, e = 13 • Convert each letter to a number in [0, 25]: S = 18, T = 19, O = 14, P = 15 • Group the sequence into blocks of 4 digits: M = 18 19 14 15 and encrypt each block as C = (M ^ 13 )(mod 2537) • For first block, (1819 ^13) (mod 2537) = 2081; for second block (1415 ^13) (mod 2537) = 2182 • Ciphertext: 2081 2182

Page 27: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

• The decryption key d is the inverse of e modulo (p - 1)(q - 1): de = 1 (mod(p - 1)(q - 1)) • As seen earlier, d can be computed reasonably efficiently if we know (p - 1)(q - 1) • However, since adversaries do not know p , q , they cannot computed with reasonable computational effort!

• Using the Chinese remainder theorem and another theorem called Fermat’s Little Theorem, it can be shown that: (M ^ e ) ^ d = M (mod n ) • Since the ciphertext C is just M ^ e , (C ^ d )(mod n ) allows decrypting the message • Since Bob can compute d using p , and q , Bob can easily decrypt the message, but no one else can! • Decrypt the cipher text 0981 0461 for the RSA cipher with p = 43, q = 59, and e = 13.

Page 28: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

• First we need to compute d, the inverse of e modulo (p - 1)(q - 1) Here (p - 1)(q - 1) = 2436 Therefore we solve 13x = 1 (mod 2436)

13s + 2436t = 1 s = 937 t = -5

• Decrypt 0981 0461 using p = 43, q = 59, n = 2537, and e = 13. Recall: Solution to this sytem is given by:

Here, s = 937, b , d = 1, m = 2436, thus solution: e = 937 ( 0981 ^ 937 )(mod 2537) = 0704; (0461 ^ 937) (mod 2537) = 1115. Thus, decrypted message is 0704 1115, or in English, ”HELP”

Page 29: Z E E P L E A R N S T O P A I N T - Wilkes Universitymathcs.wilkes.edu/~rpryor/mth232/AlgorithmsNumberTheory.pdf · Z E E P L E A R N S T O P A I N T ( A d a p te d f r o m a n id

The encryption function used in RSA is a trapdoor function

The trapdoor function is easy to compute in one direction, but very difficult in reverse direction without additional knowledge

Encryption direction is easy because just requires exponentiation and mod Decryption without private key is very hard because requires prime factorization Therefore, security of RSA depends on difficulty of prime factorization