Basic Number Theory
Lecture 08
1
Importance of Number Theory
Number theory is crucial for encryption algorithms
and hence required to security.
Of utmost importance to everyone from Bill Gates,
to the CIA, to Kim.
The encryption algorithms depend heavily on
modular arithmetic.
Machinery (notations and techniques) for
manipulating numbers
2
Divisors
DEF: Let a, b and c be integers such that
a = b ·c
- b and c are factors of a
-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.
3
Divisors. Examples
Which of the following is true?
1. 77 | 7 b|a a is divisible by b
2. 7 | 77
3. 24 | 24
4. 0 | 24
5. 24 | 0
4
Divisors. Examples
1. 77 | 7: false bigger number can’t divide smaller
positive number
2. 7 | 77:
3. 24 | 24:
4. 0 | 24:
5. 24 | 0:
5
Divisors. Examples
1. 77 | 7: false bigger number can’t divide smaller
positive number
2. 7 | 77: true because 77 = 7 · 11
3. 24 | 24:
4. 0 | 24:
5. 24 | 0:
6
Divisors. Examples
1. 77 | 7: false bigger number can’t divide smaller
positive number
2. 7 | 77: true because 77 = 7 · 11
3. 24 | 24: true because 24 = 24 · 1
4. 0 | 24:
5. 24 | 0:
7
Divisors. Examples
1. 77 | 7: false bigger number can’t divide smaller
positive number
2. 7 | 77: true because 77 = 7 · 11
3. 24 | 24: true because 24 = 24 · 1
4. 0 | 24: false, only 0 is divisible by 0
5. 24 | 0:
8
Divisors. Examples
1. 77 | 7: false bigger number can’t divide smaller
positive number
2. 7 | 77: true because 77 = 7 · 11
3. 24 | 24: true because 24 = 24 · 1
4. 0 | 24: false, only 0 is divisible by 0
5. 24 | 0: true, 0 is divisible by every number (0 = 24 · 0)
9
Multiples up to given n
How many positive multiples of 15 are less
than 100?
Just list them:
15, 30, 45, 60, 75, 90
Therefore the answer is 6.
Q: How many positive multiples of 15 are less
than 1,000,000?
10
Multiples up to given n
A: Listing is too much of a hassle. 1,000,000/15.
In general: The number of d-multiples less than N
is given by:
|{m Z+ | d |m and m N }| = N/d
11
Divisor Theorem
THM: Let a, b, and c be integers. Then:
1. a|b a|c a|(b + c )
2. a|b a|bc
3. a|b b|c a|c
EG:
12
Divisor Theorem
THM: Let a, b, and c be integers. Then:
1. a|b a|c a|(b + c )
2. a|b a|bc
3. a|b b|c a|c
EG:
1. 17|34 17|170 17|204 (34+170)
13
Divisor Theorem
THM: Let a, b, and c be integers. Then:
1. a|b a|c a|(b + c )
2. a|b a|bc
3. a|b b|c a|c
EG:
1. 17|34 17|170 17|204
2. 17|34 17|340 (34*10)
14
Divisor Theorem
THM: Let a, b, and c be integers. Then:
1. a|b a|c a|(b + c )
2. a|b a|bc
3. a|b b|c a|c
EG:
1. 17|34 17|170 17|204
2. 17|34 17|340
3. 6|12 12|144 6 | 144
15
Divisor Theorem
In general, such statements are proved by starting
from the definitions and manipulating to get the
desired results.
EG. Proof of no. 2 (a|b a|bc ):
Suppose a|b.
then there exist m such that b = am.
X both sides by c to get bc = amc = a (mc ).
Consequently, bc has been expressed as a times
the integer mc so by definition of “|”, a|bc •
16
Prime Numbers
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
17
Prime Numbers
A: 0, and 1 not prime since not positive and greater
or equal to 2
2 is prime as 1 and 2 are only factors
3 is prime as 1 and 3 are only factors.
4,6,8,10 not prime as non-trivially divisible by 2.
5, 7 prime.
9 = 3 · 3 not prime.
Last example shows that not all odd numbers are
prime.
18
Fundamental Theorem of Arithmetic
Any number n 2 is expressible as a unique
product of 1 or more prime numbers.
Note: prime numbers are considered to be
“products” of 1 and prime.
We’ll need induction and some more number
theory tools to prove this.
Q: Express each of the following number as a
product of primes: 22, 100, 12, 17
19
Fundamental Theorem of Arithmetic
22 = 2·11,
100 = 2·2·5·5,
12 = 2·2·3,
17 = 17.1
20
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 -1)
if( i |n ) // “divides”
return false
return true
Q: What is the running time of this algorithm? 21
Primality Testing
A: Assuming divisibility testing is a basic operation –
then above primality testing algorithm is O (n).
Q: What is the running time in terms of the input size k ?
22
Primality Testing
A: Consider n = 1,000,000. The input size is k = 7
because n was described using only 7 digits. In
general we have
n = O (10k ). Therefore, running time is O (10k ).
Q: Can we improve the algorithm?
23
Primality Testing
A:
• Don’t try number bigger than n/2
• After trying 2, don’t try any other even
numbers, because know n is odd by this point.
• In general, try only smaller prime numbers
• In fact, only need to try to divide by prime
numbers no larger than n as we’ll see next:
24
Primality Testing
LEMMA: If n is a composite, then its smallest prime factor
is n
Proof (by contradiction). 1<a<n
n=ab
a<=n or b<=n
if a> n and b> n then ab> n
25
Primality Testing
EG: Test if 139 and 143 are prime.
List all primes up to n and check if they divide the
numbers.
2: Neither is even
3: Sum of digits trick: 1+3+9 = 13, 1+4+3 = 8 so neither divisible by
3
5: Don’t end in 0 or 5
7: 140 divisible by 7 so neither div. by 7
11: Alternating sum trick: 1-3+9 = 7 so 139 not div. By 11. 1-4+3 =
0 so 143 is divisible by 11.
STOP! Next prime 13 need not be examined since bigger than .n
Conclude: 139 is prime, 143 is composite.
26
Division
Remember long division?
q the
d the
3 quotient
divisor 31 117
93
a the r the
dividend 24 remainder
117 = 31·3 + 24
a = dq + r
27
Division
THM: Let a be an integer, and d be a positive
integer. There are unique integers q, r with
r {0,1,2,…,d-1} satisfying
a = dq + r
28
GCD and Relatively 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.
DEF: a and b are said to be relatively prime if
gcd(a,b) = 1, so no prime common divisors.
29
GCD and Relatively Prime
1. gcd(11,77) = 11
2. gcd(33,77) = 11
3. gcd(24,36) = 12
4. gcd(24,25) = 1. Therefore 24 and 25 are
relatively prime.
30
GCD and Relatively Prime
EG: More realistic. Find gcd(98,420).
Find prime decomposition of each number and
find all the common factors:
98 = 2·49 = 2·7·7
420 = 2·210 = 2·2·105 = 2·2·3·35 = 2·2·3·5·7
Underline common factors: 2·7·7, 2·2·3·5·7
Therefore, gcd(98,420) = 14
31
Least Common Multiple
DEF: The least common multiple of a, and b
(lcm(a,b) ) is the smallest number m which is
divisible by both a and b.
Q: Find the lcm’s:
1. lcm(10,100)
2. lcm(7,5)
3. lcm(9,21)
32
Least Common Multiple
A:
1. lcm(10,100) = 100
2. lcm(7,5) = 35
3. lcm(9,21) = 63
THM: lcm(a,b) = ab / gcd(a,b)
33
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
34
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
35
mod function
A: Compute
1. 113 mod 24: 24 113
2. -29 mod 7
36
mod function
A: Compute 4
1. 113 mod 24: 24 113
96
17
2. -29 mod 7
37
mod function
A: Compute 4
1. 113 mod 24: 24 113
96
17
2. -29 mod 7
7 29
38
mod function
A: Compute 4
1. 113 mod 24: 24 113
96
17
2. -29 mod 7
5
7 29
35
6
39
(mod) congruence Formal Definition
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)
40
(mod) congruence
• A:
• 3 3 (mod 17) True.
any number is congruent to itself (3-3 = 0,
divisible by all)
• 3 -3 (mod 17) False.
(3-(-3)) = 6 isn’t divisible by 17.
• 172 177 (mod 5) True.
172-177 = -5 is a multiple of 5
• -13 13 (mod 26) True.
-13-13 = -26 divisible by 26.
41
Modular arithmetic harder examples
Q: Compute the following.
1. 3071001 mod 102
23
i
10 mod 11
2.
i 4
42
Modular arithmetic harder examples
A: Use the previous identities to help simplify:
1. Using multiplication rules, before multiplying (or
exponentiating) can reduce modulo 102:
3071001 mod 102 3071001 (mod 102)
11001 (mod 102) (307 1 mod 102)
1 (mod 102).
Therefore, 3071001 mod 102 = 1.
43
Modular arithmetic harder examples
A: Use the previous identities to help simplify:
2. Similarly, before taking sum can simplify
modulo 11:
23 i 23 i
10 mod 11 10 (mod 11)
i 4 i 4
23 i
(1) (mod 11) 10 -1 mod 11
i 4
(1 1 1 1 ... 1 1)(mod 11)
0(mod 11)
Therefore, the answer is 0. 44
Simple Encryption
Variations on the following have been used to
encrypt messages for thousands of years.
1. Convert a message to capitals.
2. Think of each letter as a number between
1 and 26.
3. Apply an invertible modular function to
each number.
4. Convert back to letters (0 becomes 26).
45
Caesar Cipher
• earliest known substitution cipher
• by Julius Caesar
• first attested use in military affairs
• replaces each letter by 3rd letter on
• example: f (a) = (a+3) mod 26
P m e e t m e a f t e r t h e t o g a p a r t y
C P H H W P H D I WH U WK H WR J D S D U WB
46
Caesar Cipher
• can define transformation as:
abcdefghijklmnopqrstuvwxyz
DEFGHIJKLMNOPQRSTUVWXYZABC
• mathematically give each letter a number
abcdefghij k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
• then have Caesar cipher as:
c = E(p) = (p + k) mod (26)
p = D(c) = (c – k) mod (26)
47
Monoalphabetic Cipher
• rather than just shifting the alphabet
• could shuffle (jumble) the letters arbitrarily
• each plaintext letter maps to a different random
ciphertext letter
• hence key is 26 letters long
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: DKVQFIBJWPESCXHTMYAUOLRGZN
Plaintext: ifwewishtoreplaceletters
Ciphertext: WIRFRWAJUHYFTSDVFSFUUFYA
48
Monoalphabetic Cipher
• only have 26 possible ciphers
• A maps to A,B,..Z
• could simply try each in turn
• a brute force search
• given ciphertext, just try all shifts of letters (1 to 25 in
the previous case)
• do need to recognize when have plaintext
49
Example Cryptanalysis
• given ciphertext:
UZQSOVUOHXMOPVGPOZPEVSGZWSZOPFPESXUDBMETSXAIZ
VUEPHZHMDZSHZOWSFPAPPDTSVPQUZWYMXUZUHSX
EPYEPOPDZSZUFPOMBZWPFUPZHMDJUDTMOHMQ
• count relative letter frequencies
• guess P & Z are e and t
• guess ZW is th and hence ZWP is the
• proceeding with trial and error finally get:
it was disclosed yesterday that several informal but
direct contacts have been made with political
representatives of the viet cong in moscow
50
Thank You
51