1 chapter 7– introduction to number theory instructor: 孫宏民 [email protected]...

35
1 Chapter 7– Introduction to Number Theory Instructor: 孫孫孫 [email protected] Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572- 3694

Post on 19-Dec-2015

243 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

1

Chapter 7– Introduction to Number Theory

Instructor: 孫宏民[email protected]

Room: EECS 6402, Tel:03-5742968, Fax : 886-3-572-3694

Page 2: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

2

Prime Numbers

• prime numbers only have divisors of 1 and self – they cannot be written as a product of other numbers – note: 1 is prime, but is generally not of interest

• eg. 2,3,5,7 are prime, 4,6,8,9,10 are not• prime numbers are central to number theory• list of prime number less than 200 is:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

Page 3: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

3

Prime Factorisation

• to factor a number n is to write it as a product of other numbers: n=a × b × c

• note that factoring a number is relatively hard compared to multiplying the factors together to generate the number

• the prime factorisation of a number n is when its written as a product of primes – eg. 91=7×13 ; 3600=24×32×52

Page 4: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

4

Relatively Prime Numbers & GCD

• two numbers a, b are relatively prime if have no common divisors apart from 1 – eg. 8 & 15 are relatively prime since factors of

8 are 1,2,4,8 and of 15 are 1,3,5,15 and 1 is the only common factor

• conversely can determine the greatest common divisor by comparing their prime factorizations and using least powers– eg. 300=21×31×52 18=21×32 hence GCD(18,300)=21×31×50=6

Page 5: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

5

Fermat's Theorem

• ap-1 mod p = 1 – where p is prime and gcd(a,p)=1

• also known as Fermat’s Little Theorem

• useful in public key and primality testing

Page 6: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

6

Euler Totient Function ø(n)

• when doing arithmetic modulo n • complete set of residues is: 0..n-1 • reduced set of residues is those numbers

(residues) which are relatively prime to n – eg for n=10, – complete set of residues is {0,1,2,3,4,5,6,7,8,9} – reduced set of residues is {1,3,7,9}

• number of elements in reduced set of residues is called the Euler Totient Function ø(n)

Page 7: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

7

Euler Totient Function ø(n)

• to compute ø(n) need to count number of elements to be excluded

• in general need prime factorization, but– for p (p prime) ø(p) = p-1 – for p.q (p,q prime) ø(p.q) = (p-1)(q-1)

• eg.– ø(37) = 36– ø(21) = (3–1)×(7–1) = 2×6 = 12

Page 8: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

8

Euler's Theorem

• a generalisation of Fermat's Theorem • aø(n)mod N = 1

– where gcd(a,N)=1

• eg.– a=3;n=10; ø(10)=4; – hence 34 = 81 = 1 mod 10– a=2;n=11; ø(11)=10;– hence 210 = 1024 = 1 mod 11

Page 9: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

9

Primality Testing

• often need to find large prime numbers • traditionally sieve using trial division

– ie. divide by all numbers (primes) in turn less than the square root of the number

– only works for small numbers

• alternatively can use statistical primality tests based on properties of primes – for which all primes numbers satisfy property – but some composite numbers, called pseudo-primes,

also satisfy the property

Page 10: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

10

Miller Rabin Algorithm

• a test based on Fermat’s Theorem• algorithm is:

TEST (n) is:1. Find integers k, q, k > 0, q odd, so that (n–1)=2kq2. Select a random integer a, 1<a<n–13. if aq mod n = 1 then return (“maybe prime");4. for j = 0 to k – 1 do

5. if (a2jq mod n = n-1) then return(" maybe prime ")

6. return ("composite")

Page 11: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

11

Probabilistic Considerations

• if Miller-Rabin returns “composite” the number is definitely not prime

• otherwise is a prime or a pseudo-prime

• chance it detects a pseudo-prime is < ¼

• hence if repeat test with different random a then chance n is prime after t tests is:– Pr(n prime after t tests) = 1-4-t

– eg. for t=10 this probability is > 0.99999

Page 12: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

12

Prime Distribution

• prime number theorem states that primes occur roughly every (ln n) integers

• since can immediately ignore evens and multiples of 5, in practice only need test 0.4 ln(n) numbers of size n before locate a prime– note this is only the “average” sometimes

primes are close together, at other times are quite far apart

Page 13: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

13

Algebraic System

• Binary Operation: Given a nonempty set S and a function op : S×SS, then op is a binary operation on S.

• Examples: S= N and op = × :the multiple of integer S= N and op = +: the addition of integer.

• Algebratic Systems: (S, op1, op2, …, opn), where S is a nonenmpty set and there are at least one binary operation on S.

• Examples: (R, +, ×) and (Z, +, ×)

Page 14: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

14

• Properties of Algebratic Systems– Closure: a op b S, where a and b S.– Associative: (a op b) op c= a op (b op c) for (S, op),

where a, b, and c S.– Communicative: a op b = b op a for (S, op) for a and

b S.– (Z, -) have no communicative property.– Identity: For (S, op), eS, aS, such that a op e=

e op a= a. – Example: For (Z, +), e=0, for (Z, ×), e= 1.– Inverses: For (S, op), aS, bS, such that a op b

= b op a = e.– Symbol : a-1 or -a.

Page 15: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

15

– Example: For (Z, +), the inverse of a is -a. – Example: For (R/{0}, ×), the inverse of a is a-1 while,

for (Z, ×), there is no inverse for any integer.– Distribution: For (S, +, *), a*(b+c)= a*b+a*c, where a,

b, and cS.– Semigroup (G,*): An algebratic system (G, *) with the

following properties: Closure, association, and an identity.

– Theorem: For a semigroup (G, *), the identity is unique.

Page 16: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

16

• Groups (G, *) : A semigroup (G, *) with inverses.• Examples:(Z, +), (R/{0}, *) are groups.• Abelian (Commutative) Groups: the group with

communitative property• Theorem: For a group (G, *), the inverse of an element in

G is unique.

• Field (F, +, *): • (F, +) is a commutative group.• (F, *) is a semigroup and (F-{0}, *) is a

commutative group, where 0 is the identity for the operation +.

Page 17: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

17

Finite Fields

• Finite Group (G, *): A group (G, *) with finite elements in G.

• Example: ({0, 1, …, N-1}, +N) is a finite group, where N is an integer.

• Cyclic Group (G, *): For a group (G, *), there exists an element a such that G= {an|nZ}, where an =a*a* …*a (n-1 times).

• a:primitive root (with the order n=|G|).

• Example: ({1, …, 6}, *7) is a cyclic group with the primitive root 3. [{3, 2, 6, 4, 5, 1}, & order= 6]

Page 18: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

18

• Generator with order m: am=1.• Finite Fields: A field (F, +, *) with finite elements

in F.• Example:GF(P)= ({0, 1, …, P-1}, +P, *P) for a

prime number P. [The first finite fields].

Page 19: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

19

Some Famous Finite Fields [P is a prime number]

• GF(P) or ZP.• GF(Pn): Given an irreducible polynomial Q(x) of

degree n over GF(P).• GF(2n) for P= 2.• Example: Q(x)= x3+x+1 over GF(2)• (x+1)+ (x)= 1.• (x+1)*x2= x2+x+1.

Page 20: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

20

Congruences

• Given integers a, b, and n 0, a, is congruent to b modulo n, written

a b mod n

if and only if

a b = kn

for some integer k.

Ex. 41 93 mod13. 18 10 mod8.

Page 21: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

21

• If a b mod n, then b is called a residue of a modulo n (conversely, a is a residue of b modulo n).

• A set of n integers {r1, …, rn} is called a complete set of residues modulo n if, for every integer a, there is exactly one ri in the set such that a ri mod n.

• For any modulus n, the set of integers {0, 1,…, n1} forms a complete set of residues modulo n.

Page 22: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

22

Computing Inverses

• Unlike ordinary integer arithmetic, modular arithmetic sometimes permits the computation of multiplicative inverse

• That is, given an integer a in the range [0, n1], it may be possible to find a unique integer x in the range [0, n1] such that ax mod n = 1.

• Ex. 3 and 7 are multiplicative inverses mod 10 because 21 mod 10 = 1.

• Thm. If gcd(a, n) = 1, then (ai mod n) (aj mod n) for each i, j such that 0 i < j < n.

Page 23: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

23

• This property implies that each ai mod n (i = 0, ..., n1) is a distinct residue mod n, and that the set {ai mod n}i=0, ..., n1

• is a permutation of the complete set of residues {0, ..., n 1}.

• This property does not hold when a and n have a common factor.

• If gcd(a, n) = 1, then there exists an integer x, 0 < x < n, such that ax mod n = 1.

Page 24: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

24

• Ex. n = 5 and a = 3:30 mod 5 = 031 mod 5 = 332 mod 5 = 133 mod 5 = 434 mod 5 = 2.

• Ex. n = 4 and a = 2:20 mod 4 = 021 mod 4 = 222 mod 4 = 023 mod 4 = 2.

Page 25: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

25

Solving for Inverse

• Euler's generalization of Fermat's theorem gives us an algorithm for solving the equation

ax mod n = 1,where gcd(a, n) = 1.Since a(n) mod n = 1, we may compute x as

ax a(n) , orx = a(n)1 mod n.

If n is prime, this is simplyx = a(n1)1 mod n = an2 mod n.

Page 26: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

26

• Ex. Let a = 3 and n = 7. Then x = 35 mod 7 = 5.• Ex. Let a = 2 and n = 15. Then x = 27 mod 15 =

8.• With this approach, to compute x, you have to

know (n).

Page 27: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

27

Another Approach

• x can also be computed using an extension of Euclid's algorithm for computing the greatest common divisor.

• This is more suitable for computers to do.Euclid's algorithm for computing greatest common divisor :gcd(a, n)g0 ng1 ai 1while gi 0 gi+1 gi1 mod gi

i i + 1return gi1

Page 28: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

28

Extended Euclid's Algorithm • Extended Euclid's algorithm for computing inverse (loop invariant: gi =

uin + via):• inv(a, n) g0 n; g1 a; u0 1; v0 0; u1 0; v1 1; i 1 while gi 0 y gi1 div gi

gi+1 gi1 y gi

ui+1 ui1 y ui

vi+1 vi1 y vi

i i + 1 x vi1

if x 0 return x else return x + n

Page 29: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

29

Example

• Ex. To solve 3x mod 7 = 1 using the algorithm, we have

• Because v2 = 2 is negative, the solution is x = 2 + 7 = 5.

i gi ui vi y

0 7 1 0

1 3 0 1 2

2 1 1 2 3

3 0

Page 30: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

30

Chinese Remainder Theorem

• used to speed up modulo computations

• working modulo a product of numbers – eg. mod M = m1m2..mk

• Chinese Remainder theorem lets us work in each moduli mi separately

• since computational cost is proportional to size, this is faster than working in the full modulus M

Page 31: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

31

Chinese Remainder Theorem

• can implement CRT in several ways

• to compute (A mod M) can firstly compute all (a i mod mi) separately and then combine results to get answer using:

McaAk

iii mod

1

kimMMC iiii 1for mod1

Page 32: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

32

Example

• Ex. Solve the equation "3x mod 10 = 1".

Since 10 = 2 5, d1 = 2 and d2 = 5. We first find solutions x1 and x2, as follows:

3x mod 2 = 1 mod 2 = 1 x1 = 1

3x mod 5 = 1 mod 5 = 1 x2 = 2

Then we apply the Chinese Remainder Theorem to find a common solution x to the equations:

Page 33: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

33

x mod 2 = x1 = 1

x mod 5 = x2 = 2 .

We find y1 and y2 satisfying

(10/2) y1 mod 2 = 1 y1 = 1

(10/5) y2 mod 5 = 1 y2 = 3Thus, we have

x = ((10/2) y1 x1 + (10/5) y2 x2) mod 10 = (511 + 232) mod 10 = 7.

Page 34: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

34

Primitive Roots

• from Euler’s theorem have aø(n)mod n=1 • consider ammod n=1, GCD(a,n)=1

– must exist for m= ø(n) but may be smaller– once powers reach m, cycle will repeat

• if smallest is m= ø(n) then a is called a primitive root

• if p is prime, then successive powers of a "generate" the group mod p

• these are useful but relatively hard to find

Page 35: 1 Chapter 7– Introduction to Number Theory Instructor: 孫宏民 hmsun@cs.nthu.edu.tw hmsun@cs.nthu.edu.tw Room: EECS 6402, Tel:03- 5742968, Fax : 886-3-572-3694

35

Discrete Logarithms or Indices

• the inverse problem to exponentiation is to find the discrete logarithm of a number modulo p

• that is to find x where ax = b mod p • written as x=loga b mod p or x=inda,p(b)• if a is a primitive root then always exists,

otherwise may not– x = log3 4 mod 13 (x st 3x = 4 mod 13) has no answer – x = log2 3 mod 13 = 4 by trying successive powers

• whilst exponentiation is relatively easy, finding discrete logarithms is generally a hard problem