2.33k likes | 6.52k Views
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.
E N D
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.
Divisors • DEF: Let a, b and c be integers such that • a = b ·c . • Then b and c are said to divide (or are factors) of a, while a is said to be a multiple of b (as well as of c). The pipe symbol “|” denotes “divides” so the situation is summarized by: • b | a c | a .
Divisors.Examples A: • 77 | 7: false bigger number can’t divide smaller positive number • 7 | 77: true because 77 = 7 ·11 • 24 | 24: true because 24 = 24 ·1 • 0 | 24: false, only 0 is divisible by 0 • 24 | 0: true, 0 is divisible by every number (0 = 24 ·0)
Formula for Number of Multiples up to given n • How many positive multiples of 15 are less than 100? 15, 30, 45, 60, 75, 80, 95. Therefore the answer is 6. Q: How many positive multiples of 15 are less than 1,000,000?
Formula for Number of Multiples up to Given n In general: The number of d-multiples less than N is given by: |{m Z+ |d |m and m N }| = N/d
Prime Numbers DEF: A number n 2 prime if it is only divisible by 1 and itself. A number n 2 which isn’t prime is called composite. Q: Which of the following are prime? 0,1,2,3,4,5,6,7,8,9,10
Primality Testing • Prime numbers are very important in encryption schemes. Essential to be able to verify if a number is prime or not. It turns out that this is quite a difficult problem. First try: • boolean isPrime(integer n) • if ( n < 2 ) return false • for(i = 2 to n /2) • if( i |n ) // “divides”! not disjunction • return false • return true
Primality Testing LEMMA: If n is a composite, then its smallest prime factor is Proof (by contradiction). Suppose the smallest prime factor is > . Then by the fundamental theorem of arithmetic we can decompose n = pqx where p and q are primes > and x is some integer. Therefore implying that n>n, which is impossible showing that the original supposition was false and the theorem is correct.
Greatest Common DivisorRelatively Prime DEF Let a,b be integers, not both zero. The greatest common divisor of a and b (or gcd(a,b) ) is the biggest number d which divides both a and b. Equivalently: gcd(a,b)is smallest number which divisibly by any x dividing both a and b. DEF: a and b are said to be relatively primeif gcd(a,b) = 1, so no prime common divisors.
Greatest Common DivisorRelatively Prime A: • gcd(11,77) = 11 • gcd(33,77) = 11 • gcd(24,36) = 12 • gcd(24,25) = 1. Therefore 24 and 25 are relatively prime. NOTE: A prime number are relatively prime to all other numbers which it doesn’t divide.
Euclidean Algorithm Euclidean Algorithm m , n gcd(m,n) integer euclid(pos. integer m, pos. integer n) x = m, y = n while(y > 0) r = x mod y x = y y = r return x
Euclidean Algorithm.Example gcd(244,117): By definition 244 and 117 are rel. prime.
Modular Arithmetic There are two types of “mod” (confusing): • the modfunction • 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
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 • 113 mod 24 • -29 mod 7
mod function A: Compute • 113 mod 24: • -29 mod 7
Modular arithmeticharder examples Q: Compute the following. • 3071001 mod 102 • (-45 · 77) mod 17
Modular arithmeticharder examples A: Use the previous identities to help simplify: • Using multiplication rules, before multiplying (or exponentiating) can reduce modulo 102: 3071001 mod 102 3071001(mod 102) 11001(mod 102) 1(mod 102). Therefore, 3071001 mod 102 = 1.
Modular arithmeticharder examples A: Use the previous identities to help simplify: • Repeatedly reduce after each multiplication: (-45·77) mod 17 (-45·77)(mod 17) (6·9)(mod 17) 54(mod 17) 3(mod 17). Therefore (-45·77) mod 17 = 3.
Modular arithmeticharder examples A: Use the previous identities to help simplify: • Similarly, before taking sum can simplify modulo 11: Therefore, the answer is 0.
Pseudo Random number generation • Linear Congruential method • xn+1 = (axn + c) mod m
Simple Encryption Variations on the following have been used to encrypt messages for thousands of years. • Convert a message to capitals. • Think of each letter as a number between 1 and 26. • Apply an invertible modular function to each number. • Convert back to letters (0 becomes 26).
Encryption example Let the encryption function be f (a) = (3a + 9) mod 26 Encrypt “Stop Thief” • STOP THIEF (capitals) • 19,20,15,16 20,8,9,5,6 • 14,17,2,5 17,7,10,24,1 • NQBE QGJXA
Decryption example Decryption works the same, except that you apply the inverse function. EG: Find the inverse of f (a) = (3a + 9) mod 26 If we didn’t have to deal with mod 26, inverse would be g (a) = 3-1 (a - 9) We’ll see that since gcd(3,26) = 1, the inverse of 3 is actually well defined modulo 26 and is the number 9. This gives: g (a) = 9(a - 9) mod 26 = (9a – 3) mod 26
Public Key Cryptography • How can Alice send a secret message to Bob if Bob cannot send a secret key to Alice? ALICE BOB My public key is: 13890580304018329082310291802198210923810830129823019128092183021398301292381320498068029809347849394598178479388287398457923893848928823748283829929384020010924380915809283290823823
RSA • Rivest – Shamir – Adelman • n = pq. p, q are large primes • Choose e relatively prime to (p-1)(q-1) • Find d, k such that de + k(p-1)(q-1) = 1 by Euclid’s Algorithm • Publish e as the encryption key, d is kept private as the decryption key
Message protocol • Bob • Precompute p, q, n, e, d • Publish e, n • Alice • Read e, n from Bob’s public site • To send message M, compute C = Me mod n • Send C to Bob • Bob • Compute Cd to decode message M
RSA Cryptography m emod N N = 4559, e = 13. Smiley Transmits: “Last name Smiley” • L A S T N A M E S M I L E Y • 1201 1920 0014 0113 0500 1913 0912 0525 • 120113mod 4559, 192013mod 4559, … • 2853 0116 1478 2150 3906 4256 1445 2462
RSA Cryptography N = 4559, d = 3397 • 2853 0116 1478 2150 3906 4256 1445 2462 • 28533397mod 4559, 01163397mod 4559, … • 1201 1920 0014 0113 0500 1913 0912 0525 • L A S T N A M E S M I L E Y
Decryption • de = 1 + k(p-1)(q-1) • Cd (Me)d= Mde = M1 + k(p-1)(q-1) (mod n) • Cd M (Mp-1)k(q-1) M (mod p) • Cd M (Mq-1)k(p-1) M (mod q) • Hence Cd M (mod pq)
Representations of Integers • Example:What is the base 8 expansion of (12345)10 ? • First, divide 12345 by 8: • 12345 = 81543 + 1 • 1543 = 8192 + 7 • 192 = 824 + 0 • 24 = 83 + 0 • 3 = 80 + 3 • The result is: (12345)10 = (30071)8.
Number Systems EG: Convert 646 to Oggian (base-5):
( )2 Addition of Integers carry 1 1 1 • How do we (humans) add two integers? • Example: 7583 + 4932 1 2 5 1 5 carry 1 1 Binary expansions: (1011)2 +(1010)2 1 0 1 0 1
2’s Complement • Fixes the non-uniqueness of zero problem • Adding mixed signs still easy • No cycle overflow (pre-computed) • Java’s approach (under the hood) • Same fixed length k , sign convention, and definition of positive numbers as with 1’s complement • Represent numbers with -2(k-1)x < 2(k-1) • EG. Java’s byte ranges from -128 to +127