discrete math for computer science csc 281 · 1 discrete math for computer science csc 281 dr.yuan...

70
1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian [email protected] Module Basic Structures: Number Theory

Upload: others

Post on 06-Dec-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

1

Discrete Math for Computer Science

CSC 281

Dr.Yuan Tian

[email protected]

Module

Basic Structures: Number Theory

Page 2: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Importance of Number Theory

Before the dawn of computers, many viewed number theory as

last bastion of “pure math” which could not be useful and

must be enjoyed only for its aesthetic beauty.

No longer the case. Number theory is crucial for encryption

algorithms. Of utmost importance to everyone from Bill

Gates, to the CIA, to Osama Bin Laden.

E.G., of great importance in “Computer Security”.

Page 3: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 3

Importance of Number Theory

The encryption algorithms depend heavily on modular

arithmetic. We need to develop various machinery (notations

and techniques) for manipulating numbers before can

describe algorithms in a natural fashion.

First we start with divisors.

Page 4: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

4

The Integers and Division

Of course, you already know what the integers are, and what division

is…

However: There are some specific notations, terminology, and

theorems associated with these concepts which you may not know.

These form the basics of number theory.

– Vital in many important algorithms today (hash functions,

cryptography, digital signatures; in general, on-line security).

Page 5: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

5

The divides operator

New notation: 3 | 12

– To specify when an integer evenly divides another integer

– Read as “3 divides 12”

The not-divides operator: 5 | 12

– To specify when an integer does not evenly divide another integer

– Read as “5 does not divide 12”

Page 6: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

6

Divides, Factor, Multiple

Let a,bZ with a0.

Defn.: a|b “a divides b” : ( cZ: b=ac)

“There is an integer c such that c times a equals b.”

– Example: 312 True, but 37 False.

Iff a divides b, then we say a is a factor or a divisor of b, and b is a multiple of a.

Ex.: “b is even” :≡ 2|b. Is 0 even? Is −4?

Page 7: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

7

Results on the divides operator

• If a | b and a | c, then a | (b+c)

– Example: if 5 | 25 and 5 | 30, then 5 | (25+30)

• If a | b, then a | bc for all integers c

– Example: if 5 | 25, then 5 | 25*c for all ints c

• If a | b and b | c, then a | c

– Example: if 5 | 25 and 25 | 100, then 5 | 100

(“common facts” but good to repeat for background)

Page 8: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

8

Divides Relation

Theorem: a,b,c Z:

1. a|0

2. (a|b a|c) a | (b + c)

3. a|b a|bc

4. (a|b b|c) a|c

Corollary: If a, b, c are integers, such that a | b and a | c, then

a | mb + nc whenever m and n are integers.

Page 9: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

9

Proof of (2)

Show a,b,c Z: (a|b a|c) a | (b + c).

Let a, b, c be any integers such that a|b and a|c, and show that a | (b + c).

By defn. of | , we know s: b=as, and t: c=at.

Let s, t, be such integers.

Then b+c = as + at = a(s+t).

So, u: b+c=au, namely u=s+t. Thus a|(b+c). QED

Page 10: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Divides Relation

Corollary: If a, b, c are integers, such that a | b and a | c, then

a | mb + nc whenever m and n are integers.

Proof:

From previous theorem part 3 (i.e., a|b a|be) it follows that

a | mb and a | nc ; again, from previous theorem part 2 (i.e.,

(a|b a|c) a | (b + c)) it follows that a | mb + nc

Page 11: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

11

The Division “Algorithm”

Theorem:

Division Algorithm --- Let a be an integer and d a positive

integer. Then there are unique integers q and r, with 0 ≤r < d,

such that a = dq+r.

It’s really a theorem, not an algorithm…

Only called an “algorithm” for historical reasons.

• q is called the quotient

• r is called the remainder

• d is called the divisor

• a is called the dividend

Page 12: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

12

What are the quotient and remainder when 101 is divided

by 11?

•q is called the quotient

•r is called the remainder

•d is called the divisor

•a is called the dividend

101 = 11 9 + 2

We write:

q = 9 = 101 div 11

r = 2 = 101 mod 11

a d q r

Page 13: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

13

If a = 7 and d = 3, then q = 2 and r = 1, since 7 = (2)(3) + 1.

If a = −7 and d = 3, then q = −3 and r = 2, since −7 = (−3)(3) + 2.

So: given positive a and (positive) d, in order to get r we repeatedly

subtract d from a, as many times as needed so that what remains, r, is

less than d.

Given negative a and (positive) d, in order to get r we repeatedly

add d to a, as many times as needed so that what remains, r, is

positive (or zero) and less than d.

Page 14: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 14

Modular Arithmetic

There are two types of “mod” (confusing):

the mod function

– Inputs a number a and a base b

– Outputs a mod b a number between 0 and b –1 inclusive

– This is the remainder of ab

– Similar to Java’s % operator.

the (mod) congruence

– Relates two numbers a, a’ to each other relative some base b

– a a’ (mod b) means that a and a’ have the same remainder when dividing by b

Page 15: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Modular arithmetic

If a and b are integers and m is a positive integer, then

“a is congruent to b modulo m” if m divides a-b

– Notation: a ≡ b (mod m)

– Rephrased: m | a-b

– Rephrased: a mod m = b mod m

– If they are not congruent: a ≡ b (mod m)

Example: Is 17 congruent to 5 modulo 6?

– Rephrased: 17 ≡ 5 (mod 6)

– As 6 divides 17-5, they are congruent

Example: Is 24 congruent to 14 modulo 6?

– Rephrased: 24 ≡ 14 (mod 6)

– As 6 does not divide 24-14 = 10, they are not congruent

Note: this is a different use of “” than the meaning “is defined as” used before.

Note “=“ sign.

Page 16: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 16

mod function

Similar to Java’s “%” operator except that answer is always positive.

E.G.

-10 mod 3 = 2, but in Java –10%3 = -1.

Q: Compute

1. 113 mod 24

2. -29 mod 7

Page 17: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 17

mod function

A: Compute

1. 113 mod 24:

2. -29 mod 7

11324

6

Page 18: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 18

mod function

A: Compute

1. 113 mod 24:

2. -29 mod 7

4

11324

17

96

Page 19: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 19

mod function

A: Compute

1. 113 mod 24:

2. -29 mod 7

4

11324

17

96

297

Page 20: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 20

mod function

A: Compute

1. 113 mod 24:

2. -29 mod 7

4

11324

17

96

5

297

35

6

Page 21: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 21

(mod) congruence

Formal Definition

DEF: Let a,a’ be integers and b be a positive integer. We say that a is congruent to a’ modulo b (denoted by a a’ (mod b) ) iff b | (a – a’ ).

Equivalently: a mod b = a’ mod b

Q: Which of the following are true?

1. 3 3 (mod 17)

2. 3 -3 (mod 17)

3. 172 177 (mod 5)

4. -13 13 (mod 26)

Page 22: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 22

(mod) congruence

A:

1. 3 3 (mod 17) True. any number is congruent to itself (3-3 = 0, divisible by all)

2. 3 -3 (mod 17) False. (3-(-3)) = 6 isn’t divisible by 17.

3. 172 177 (mod 5) True. 172-177 = -5 is a multiple of 5

4. -13 13 (mod 26) True: -13-13 = -26 divisible by 26.

Page 23: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

L9 23

(mod) congruence

Identities

The (mod) congruence is useful for manipulating expressions involving the mod function. It lets us view modular arithmetic relative a fixed base, as creating a number system inside of which all the calculations can be carried out.

a mod b a (mod b)

Suppose a a’ (mod b) and c c’ (mod b) Then:

– a+c (a’+c’ )(mod b)

– ac a’c’ (mod b)

– a k a’ k (mod b)

Page 24: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

24

Time-keeping on a clock gives an

example of modular arithmetic.

(mod 12 in the US;

or mod 24, using the 24hr clock.

Naturally imposed by the periodicity of

earth’s rotation.)

Page 25: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Spiral Visualization of mod

≡ 3

(mod 5)

≡ 2

(mod 5)

≡ 1

(mod 5)

≡ 0

(mod 5)

≡ 4

(mod 5) 0 1

2 3

4

5

6

7 8

9

10

11

12 13

14

15

16

17 18

19

20

21

22

Example shown:

modulo-5

arithmetic

So, e.g., 19 is congruent to 9 modulo 5.

The spiral/circular view is useful

to keep in mind when doing

modular arithmetic!

Congruence classes

modulo 5.

Collapses infinite

set of numbers into

5 classes.

Where is -1?

Where is -7?

Page 26: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

More on congruences

Theorem: Let a and b be integers, and let m be a positive integer.

Then a ≡ b (mod m) if and only if a mod m = b mod m

Theorem:

Let m be a positive integer. The integers a and b are congruent modulo m if

and only if there is an integer k such that a = b + km

Example: 17 and 5 are congruent modulo 6, so

17 = 5 + 2*6

5 = 17 - 2*6

Page 27: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

27

Even even more on congruence

Theorem: Let m be a positive integer. If a ≡ b (mod m) and c ≡ d (mod m),

then a+c ≡ (b+d) (mod m) and ac ≡ bd (mod m)

Example

– We know that 7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5)

– Thus, 7+11 ≡ (2+1) (mod 5), or 18 ≡ 3 (mod 5)

– Thus, 7*11 ≡ 2*1 (mod 5), or 77 ≡ 2 (mod 5)

Page 28: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

28

Cryptology

(secret messages)

Page 29: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

29

The Caesar cipher

Julius Caesar used the following procedure to encrypt messages

A function f to encrypt a letter is defined as: f(p) = (p+3) mod 26

– Where p is a letter (0 is A, 1 is B, 25 is Z, etc.)

Decryption: f-1(p) = (p-3) mod 26

This is called a substitution cipher

– You are substituting one letter with another

Page 30: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Caesar Cipher

The earliest known substitution cipher used by Julius Caesar

Replacement of each letter with the 3rd letter down from the alphabet

- E(p)=(p+3) mod 26, p : a plaintext letter

- example:

plaintext: a r e y o u r e a d y

ciphertext: DUH BRX UHDGB

What’s the key?

Page 31: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Caesar Cipher

Can define transformation as: a b c d e f g h i j k l m n o p q r s t u v w x y z

D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

Mathematically give each letter a number

Then Caesar cipher is generalized as below:

– C = E(p) = (p + k) mod (26)

– p = D(C) = (C – k) mod (26)

a b c d e f g h i j k l m

0 1 2 3 4 5 6 7 8 9 10 11 12

n o p q r s t u v w x y z

13 14 15 16 17 18 19 20 21 22 23 24 25

Page 32: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Cryptanalysis of Caesar Cipher

Three important characteristics

- The encryption / decryption algorithm are known

- There are only 25 keys to try

- The language is known and easily readable

A brute force search easily performed

- given a ciphertext, just try all the 25 possible keys

- e.g., break ciphertext "GCUA VQ DTGCM"

Page 33: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

33

The Caesar cipher

Encrypt “go cavaliers” – Translate to numbers: g = 6, o = 14, etc.

• Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18

– Apply the cipher to each number: f(6) = 9, f(14) = 17, etc. • Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21

– Convert the numbers back to letters 9 = j, 17 = r, etc. • Full sequence: jr wfdydolhuv

Decrypt “jr wfdydolhuv” – Translate to numbers: j = 9, r = 17, etc.

• Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21

– Apply the cipher to each number: f-1(9) = 6, f-1(17) = 14, etc. • Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18

– Convert the numbers back to letters 6 = g, 14 = 0, etc. • Full sequence: “go cavaliers”

Page 34: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

34

Rot13 encoding

A Caesar cipher, but translates letters by 13 instead of 3

– Then, apply the same function to decrypt it, as 13+13=26

Rot13 stands for “rotate by 13”

Example: >echo Hello World | rot13

Uryyb Jbeyq

> echo Uryyb Jbeyq | rot13

Hello World

Page 35: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Primes and Greatest Common Divisor

Page 36: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

36

Prime numbers

A positive integer p is prime if the only positive factors of p are 1 and p

– If there are other factors, it is composite

– Note that 1 is not prime!

• It’s not composite either – it’s in its own class

An integer n is composite if and only if there exists an integer a such that

a | n and 1 < a < n

Page 37: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Fundamental theorem of arithmetic

Fundamental Theorem of Arithmetic:

Every positive integer greater than 1 can be uniquely written as a prime or as

the product of two or more primes where the prime factors are written in

order of non-decreasing size

Examples

– 100 = 2 * 2 * 5 * 5

– 182 = 2 * 7 * 13

– 29820 = 2 * 2 * 3 * 5 * 7 * 71

In a fundamental sense, primes are the building blocks of the natural numbers.

Page 38: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Fundamental theorem of arithmetic: Strong Induction

[from before]

Show that if n is an integer greater than 1, then n can be written as the

product of primes.

1 - Hypothesis P(n) - n can be written as the product of primes.

2 – Base case – P(2) 2 can be written a 2 (the product of itself)

3 – Inductive Hypothesis - P(j) is true for 2 ≤j ≤k, j integer.

4 – Inductive step?

a) k+1 is prime – in this case it’s the product of itself;

b) k+1 is a composite number and it can be written as the product of two positive integers a and b, with 2 ≤a ≤ b ≤ k+1. By the inductive hypothesis, a and b can be written as the product of primes, and so does k+1 ,

QED

What’s missing?

Uniqueness proof,

soon…

Page 39: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Composite factors

Theorem: If n is a composite integer, then n has a prime divisor

less than or equal to the square root of n

Proof

– Since n is composite, it has a factor a such that 1<a<n

– Thus, n = ab, where a and b are positive integers greater than 1

– Either a≤n or b≤n (Otherwise, assume a > n and b > n, then

ab > n*n > n. Contradiction.)

– Thus, n has a divisor not exceeding n

– This divisor is either prime or a composite

• If the latter, then it has a prime factor (by the FTA)

– In either case, n has a prime factor less than n

QED

Page 40: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

40

Showing a number is prime

E.g., show that 113 is prime.

Solution

– The only prime factors less than 113 = 10.63 are 2, 3, 5, and 7

– None of these divide 113 evenly

– Thus, by the fundamental theorem of arithmetic, 113 must be prime

How?

Page 41: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

41

Showing a number is composite

Show that 899 is composite.

Solution

– Divide 899 by successively larger primes, starting with 2

– We find that 29 and 31 divide 899

Page 42: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

On a linux system or in cygwin, enter “factor 899”

> factor 899

899: 29 31

>factor 89999999999999999

89999999999999999: 7 7 13 6122449 23076923

Page 43: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Greatest common divisor

The greatest common divisor of two integers a and b is the largest

integer d such that d | a and d | b

– Denoted by gcd(a,b)

Examples

– gcd (24, 36) = 12

– gcd (17, 22) = 1

– gcd (100, 17) = 1

Page 44: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

44

Relative primes

Two numbers are relatively prime if they don’t have any common

factors (other than 1)

– Rephrased: a and b are relatively prime if

gcd (a,b) = 1

gcd (25, 16) = 1, so 25 and 16 are relatively prime

Page 45: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

45

Pairwise relative prime

A set of integers a1, a2, … an are pairwise relatively prime if, for all pairs of numbers, they are relatively prime

– Formally: The integers a1, a2, … an are pairwise relatively prime if gcd(ai, aj) = 1 whenever 1 ≤ i < j ≤ n.

Example: are 10, 17, and 21 pairwise relatively prime?

– gcd(10,17) = 1, gcd (17, 21) = 1, and gcd (21, 10) = 1

– Thus, they are pairwise relatively prime

Example: are 10, 19, and 24 pairwise relatively prime?

– Since gcd(10,24) ≠ 1, they are not

Page 46: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

More on gcd’s

Given two numbers a and b, rewrite them as:

– Example: gcd (120, 500)

• 120 = 23*3*5 = 23*31*51

• 500 = 22*53 = 22*30*53

Then compute the gcd by the following formula:

– Example: gcd(120,500) = 2min(3,2) 3min(1,0) 5min(1,3) = 22 30 51 = 20

nn b

n

bba

n

aapppbpppa ...,... 2121

2121

),min(),min(

2

),min(

1 ...),gcd( 2211 nn ba

n

babapppba

Page 47: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Least common multiple

The least common multiple of the positive integers a and b is the smallest positive integer that is divisible by both a and b.

– Denoted by lcm (a, b)

Example: lcm(10, 25) = 50

What is lcm (95256, 432)?

– 95256 = 233572, 432=2433

– lcm (233572, 2433) = 2max(3,4)3max(5,3)7max(2,0)

= 24 35 72 = 190512

),max(),max(

2

),max(

1 ...),lcm( 2211 nn ba

n

babapppba

Page 48: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

lcm and gcd theorem

Theorem: Let a and b be positive integers.

Then a*b = gcd(a,b) * lcm (a, b)

Example: gcd (10,25) = 5, lcm (10,25) = 50

So, 10*25 = 5*50

Example: gcd (95256, 432) = 216, lcm (95256, 432) = 190512

So, 95256*432 = 216*190512

How do we find the gcd?

Two algs.:

1) Try all #s up to smallest

2) Factor #s.

(likely)

Exp. in # of digits!

Page 49: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Euclid’s Algorithm for GCD

Finding GCDs by comparing prime factorizations can be

difficult when the prime factors are not known! And, no

fast alg. for factoring is known. (except …)

Euclid discovered: For all ints. a, b

gcd(a, b) = gcd((a mod b), b).

How can this be useful? (assume a>b)

Sort a, b so that a>b, and then (given b>1)

(a mod b) < a, so problem is simplified.

Euclid of

Alexandria

325-265 B.C.

On quantum computer!

Page 50: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Theorem: Let a =bq+r, where a, b, q, and r are integers.

Then gcd(a,b) = gcd(b,r)

Suppose a and b are the natural numbers whose gcd has to be

determined. And suppose the remainder of the division of a by b is r.

Therefore a = qb + r where q is the quotient of the division.

Any common divisor of a and b is also a divisor of r. To see why this is

true, consider that r can be written as r = a − qb. Now, if there is a

common divisor d of a and b such that a = sd and b = td, then

r = (s−qt)d. Since all these numbers, including s−qt, are whole

numbers, it can be seen that r is divisible by d. (Also, by corollary

on slide 6.)

Similarly, any common divisor of b and r is also a divisor of a. Note that

a = qb +r. Hence a common divisor of b and r also divides a.

It follows that gcd(a,b) = gcd(b,r). QED

Page 51: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

51

Euclidean Algorithm

procedure procedure (a,b:positive integers)

x := a

y := b

while y 0

begin

r := x mod y

x := y

y := r

end { gcd(a, b) is x }

Lemma: Let a = bq + r, where a, b, q, and r are

integers. Then gcd(a, b) = gcd(b, r)

What about the “y=0” case?

Arises when r = 0. So, y

divides x. But “x:=y” and

“y:=0”, so return x. Also

note that gcd(a,0) = a.

Do we need a >= b? hmm…

Page 52: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Euclid’s Algorithm Example

gcd(372,164) = gcd(164, 372 mod 164).

– 372 mod 164 = 372164 372/164 = 372164·2 = 372328 = 44.

gcd(164,44) = gcd(44, 164 mod 44).

– 164 mod 44 = 16444 164/44 = 16444·3 = 164132 = 32.

gcd(44,32) = gcd(32, 44 mod 32)

= gcd(32,12) = gcd(12, 32 mod 12)

= gcd(12,8) = gcd(8, 12 mod 8)

= gcd(8,4) = gcd(4, 8 mod 4)

= gcd(4,0) = 4.

So, we repeatedly swap the numbers. Largest first. “mod” reduces

them quickly!

Complexity? Guess… O(log b) divisions. Linear in #digits of b! Compare to direct search for divisor.

(Lame`’s thm. Section 4.3)

2000+ yr alg. makes

E-commerce possible!

Page 53: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

– Ron Rivest, Adi Shamir and Len Adleman

– Created in 1978; RSA Security sells related products

– Most widely used public-key algorithm

– Block cipher: plaintext and ciphertext are integers

RSA

Page 54: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Algorithm

• Plaintext encrypted in blocks, each block binary value less than n

• In practice, block size i bits where 2i < n ≤2i+1; n is 1024 bits

• Encryption of plaintext M:

C = Me mod n

• Decryption of ciphertext C:

M = Cd mod n

= (Me)d mod n = Med mod n

• Sender A and receiver B know n; Sender A knows e; Receiver B

knows d

• PUb = {e, n}, PRb = {d, n}

Page 55: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Conditions

• Euler’s theorem: aφ (n) ≡ 1(mod n) where a and n are relatively

prime

• alternative form is: aφ(n)+1 ≡ a(mod n) and in this form a and n

are NOT required to be relatively prime

• Compare this to our condition above:

Med ≡ M(mod n) This is true if φ (n)+1 = ed .

• This implies: ed(modφ (n)) ≡ 1 e and d are multiplicative

inverses in modular arithmetic φ (n)

• A number e has a multiplicative inverse in mod φ (n) if e and φ

(n) are relatively prime

• choose a number e such that it is relatively prime to φ (n) and

less than φ (n) gcd(e,φ (n)) = 1

Page 56: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Conditions

• For an attacker, it should be hard to calculate φ(n)

• for the person generating the keys, it should be easy to

calculate φ(n)

• if n= pq, where p and q are (very large) prime numbers, then:

φ(n) = φ(pq)= φ(p) * φ(q)= (p-1)(q-1)

Page 57: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Key Generation

• Select two large prime numbers p, q

- Calculate n = p * q, used as modulus.

- Calculate φ(n) = (p-1)(q-1), the number of positive integers less

than n and relatively prime to n.

• Select either e or d and calculate the other

- Select integer e, gcd[φ(n), e] = 1; 1 < e < φ(n)

- Calculate d de mod φ(n) = 1, multiplicative inverse of e

• Public key, PU= { e, n}

• Private key, PR = { d, n }

Page 58: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Key Generation

• For p * q to be chosen - The value of n = pq can be known to any opponent by

exhaustive method.

- To prevent above, p and q must be large numbers.

- On the other hand, the method to find large primes must be

reasonably efficient.

- At present, no useful techniques to yield or determine a prime

number.

- The techniques that have been developed are to test whatever

the chosen number is prime.

- Almost invariably, the tests are probabilistic.

Page 59: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Requirements of the RSA Algorithm

1. Possible to find values of e, d, n such that Med mod n = M for all

M < n

2. Easy to calculate Me mod n and Cd mod n for all values of M < n

3. Infeasible to determine d given e and n

• Requirement 1 met if e and d are relatively prime

• Choose primes p and q, and calculate:

n = pq

1 < e < φ(n)

ed ≡ 1 (mod φ(n)) or d ≡ e1(mod � φ(n))

•n and e are public; p, q and d are private

•The first two requirements are easily met.

•The third requirement can be met for a large value of e and n.

Page 60: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

The RSA Algorithm

Page 61: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Key Generation

1. Select primes: p=17 & q=11

2. Compute n = pq =17× 11=187

3. Compute φ(n) =(p–1)(q-1)=16× 10=160

4. Select e such that it is relatively prime to 160 and less

than 160. Lets choose e = 7.

5. Choose d such that de ≡ 1 (mod 160) and d < 160

d = 23. Since 23 x 7 = 161 = 10 x 160 + 1 (extended Euclid’s

algorithm can be used to calculate d)

Publish public key PU={7,187}

Keep secret private key PR={23,187}

Page 62: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Example

Given a plaintext input of M=88(< n=187)

with PU={7,187} PR={23,187}

For encryption, calculate c=88e mod 187:

887 mod 187 = [(884 mod 187) × (882 mod 187)

× (881 mod 187)] mod 187

881 mod 187 = 88

882 mod 187 = 7744 mod 187 = 77

884 mod 187 = 59,969,536 mod 187 = 132

887 mod 187 = (88 × 77 × 132) mod 187 = 894,432 mod

187 = 11

Page 63: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Example of RSA Algorithm

Page 64: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

RSA Example

For decryption, calculate M=1123 mod 187:

1123 mod 187 = [(111 mod 187) × (112 mod 187) × (114 mod

187) × (118 mod 187) × (118 mod 187)] mod

187

111 mod 187 = 11

112 mod 187 = 121

114 mod 187 = 214,358,881 mod 187 = 33

118 mod 187 = (11× 121 × 55 × 33 × 33) mod 187

= 79,720,245 mod 187 = 88

Page 65: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Computational Efficiency of RSA

• Encryption and decryption require exponentiation

• Very large numbers; using properties of modular arithmetic makes it

easier:

[(a mod n) * (b mod n)] mod n = (a * b) mod n

• Choosing e

• Values such as 3, 17 and 65537 are popular: make exponentiation faster

• Small e vulnerable to attack: add random padding to each M

• Choosing d

• Small d vulnerable to attack

• Decryption using large d made faster using Chinese Remainder

Theorem and Fermat's Theorem

• Choosing p and q

• p and q must be very large primes

• Choose random odd number and test if its prime (probabilistic test)

Page 66: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

Security of RSA

• Brute-Force attack: choose large d (but makes algorithm slower)

• Mathematical attacks:

1. Factor n into its two prime factors

2. Determine �φ(n) directly, without determining p or q

3. Determine d directly, without determining � φ(n)

• Factoring n is considered fastest approach; hence used as

measure of RSA security

• Timing attacks: practical, but countermeasures easy to add (e.g.

random delay). 2 to 10% performance penalty

• Chosen ciphertext attack: countermeasure is to use padding

(Optimal Asymmetric Encryption Padding)

Page 67: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

67

Why RSA Works

Theorem (Correctness of RSA): (Me)d ≡ M (mod n).

Proof:

By the definition of d, we know that de ≡ 1 [mod (p−1)(q−1)].

– Thus by the definition of modular congruence,

k: de = 1 + k(p−1)(q−1).

– So, the result of decryption is

Cd ≡ (Me)d = Mde = M1+k(p−1)(q−1) (mod n)

Page 68: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

68

Proof cont.

We had:

Cd ≡ (Me)d = Mde = M1+k(p−1)(q−1) (mod n)

Assuming that M is not divisible by either p or q,

– Which is nearly always the case when p and q are very large

– Fermat’s Little Theorem tells us that

Mp−1≡1 (mod p) and Mq−1≡1 (mod q)

Thus, we have that the following two congruences hold:

First: Cd ≡ M · (Mp−1)k(q−1) ≡ M·1k(q−1) ≡ M (mod p)

Second: Cd ≡ M·(Mq−1)k(p−1) ≡ M·1k(p−1) ≡ M (mod q)

Hmm. System of modular equations…

Page 69: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

69

Proof cont.

We had:

Cd ≡ (Me)d = Mde = M1+k(p−1)(q−1) (mod n)

Assuming that M is not divisible by either p or q,

– Which is nearly always the case when p and q are very large

– Fermat’s Little Theorem tells us that

Mp−1≡1 (mod p) and Mq−1≡1 (mod q)

Thus, we have that the following two congruences hold:

First: Cd ≡ M · (Mp−1)k(q−1) ≡ M·1k(q−1) ≡ M (mod p)

Second: Cd ≡ M·(Mq−1)k(p−1) ≡ M·1k(p−1) ≡ M (mod q)

Hmm. System of modular equations…

Page 70: Discrete Math for Computer Science CSC 281 · 1 Discrete Math for Computer Science CSC 281 Dr.Yuan Tian ytian@ksu.edu.sa Module Basic Structures: Number Theory

70

Proof cont.

Thus, we have that the following two congruences hold:

First: Cd ≡ M · (Mp−1)k(q−1) ≡ M·1k(q−1) ≡ M (mod p)

Second: Cd ≡ M·(Mq−1)k(p−1) ≡ M·1k(p−1) ≡ M (mod q)

Hmm. System of modular equations…

And since gcd(p,q) = 1, we can use the Chinese Remainder Theorem

to show that therefore Cd ≡ M (mod pq), since

If Cd ≡ M (mod pq) then s: Cd = s p q + M,

so Cd ≡ M (mod p) and also Cd ≡ M (mod q).

Thus, M is a solution to these two congruences,

so (by CRT) it’s the only solution.

QED