1. Cryptography and
Cryptography and
Network Security
Network Security
Chapter 8
Chapter 8
Fifth Edition
Fifth Edition
by William Stallings
by William Stallings
Lecture slides by Lawrie Brown
Lecture slides by Lawrie Brown
2. Chapter 8 –
Chapter 8 – Introduction to
Introduction to
Number Theory
Number Theory
The Devil said to Daniel Webster: "Set me a task I can't carry out, and
The Devil said to Daniel Webster: "Set me a task I can't carry out, and
I'll give you anything in the world you ask for."
I'll give you anything in the world you ask for."
Daniel Webster: "Fair enough. Prove that for n greater than 2, the
Daniel Webster: "Fair enough. Prove that for n greater than 2, the
equation a
equation an
n
+ b
+ bn
n
= c
= cn
n
has no non-trivial solution in the integers."
has no non-trivial solution in the integers."
They agreed on a three-day period for the labor, and the Devil
They agreed on a three-day period for the labor, and the Devil
disappeared.
disappeared.
At the end of three days, the Devil presented himself, haggard, jumpy,
At the end of three days, the Devil presented himself, haggard, jumpy,
biting his lip. Daniel Webster said to him, "Well, how did you do at
biting his lip. Daniel Webster said to him, "Well, how did you do at
my task? Did you prove the theorem?'
my task? Did you prove the theorem?'
"Eh? No . . . no, I haven't proved it."
"Eh? No . . . no, I haven't proved it."
"Then I can have whatever I ask for? Money? The Presidency?'
"Then I can have whatever I ask for? Money? The Presidency?'
"What? Oh, that—of course. But listen! If we could just prove the
"What? Oh, that—of course. But listen! If we could just prove the
following two lemmas—"
following two lemmas—"
—
—The Mathematical Magpie
The Mathematical Magpie, Clifton Fadiman
, Clifton Fadiman
3. Prime Numbers
Prime Numbers
prime numbers only have divisors of 1 and self
prime numbers only have divisors of 1 and self
they cannot be written as a product of other numbers
they cannot be written as a product of other numbers
note: 1 is prime, but is generally not of interest
note: 1 is prime, but is generally not of interest
eg. 2,3,5,7 are prime, 4,6,8,9,10 are not
eg. 2,3,5,7 are prime, 4,6,8,9,10 are not
prime numbers are central to number theory
prime numbers are central to number theory
list of prime number less than 200 is:
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
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
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
131 137 139 149 151 157 163 167 173 179 181 191
193 197 199
193 197 199
4. Prime Factorisation
Prime Factorisation
to
to factor
factor a number
a number n
n is to write it as a
is to write it as a
product of other numbers:
product of other numbers: n=a x b x c
n=a x b x c
note that factoring a number is relatively
note that factoring a number is relatively
hard compared to multiplying the factors
hard compared to multiplying the factors
together to generate the number
together to generate the number
the
the prime factorisation
prime factorisation of a number
of a number n
n is
is
when its written as a product of primes
when its written as a product of primes
eg.
eg. 91=7x13 ; 3600=2
91=7x13 ; 3600=24
4
x3
x32
2
x5
x52
2
5. Relatively Prime Numbers & GCD
Relatively Prime Numbers & GCD
two numbers
two numbers a, b
a, b are
are relatively prime
relatively prime if have
if have
no common divisors
no common divisors apart from 1
apart from 1
eg. 8 & 15 are relatively prime since factors of 8 are
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
1,2,4,8 and of 15 are 1,3,5,15 and 1 is the only
common factor
common factor
conversely can determine the greatest common
conversely can determine the greatest common
divisor by comparing their prime factorizations
divisor by comparing their prime factorizations
and using least powers
and using least powers
eg.
eg. 300
300=2
=21
1
x3
x31
1
x5
x52
2
18=2
18=21
1
x3
x32
2
hence
hence
GCD(18,300)=2
GCD(18,300)=21
1
x3
x31
1
x5
x50
0
=6
=6
6. Fermat's Theorem
Fermat's Theorem
a
ap-1
p-1
= 1 (mod p)
= 1 (mod p)
where
where p
p is prime and
is prime and gcd(a,p)=1
gcd(a,p)=1
also known as Fermat’s Little Theorem
also known as Fermat’s Little Theorem
also have:
also have: a
ap
p
= a (mod p)
= a (mod p)
useful in public key and primality testing
useful in public key and primality testing
7. Euler Totient Function
Euler Totient Function ø(n)
ø(n)
when doing arithmetic modulo n
when doing arithmetic modulo n
complete set of residues
complete set of residues is:
is: 0..n-1
0..n-1
reduced set of residues
reduced set of residues is those numbers
is those numbers
(residues) which are relatively prime to n
(residues) which are relatively prime to n
eg for n=10,
eg for n=10,
complete set of residues is {0,1,2,3,4,5,6,7,8,9}
complete set of residues is {0,1,2,3,4,5,6,7,8,9}
reduced set of residues is {1,3,7,9}
reduced set of residues is {1,3,7,9}
number of elements in reduced set of residues is
number of elements in reduced set of residues is
called the
called the Euler Totient Function ø(n)
Euler Totient Function ø(n)
8. Euler Totient Function
Euler Totient Function ø(n)
ø(n)
to compute ø(n) need to count number of
to compute ø(n) need to count number of
residues to be excluded
residues to be excluded
in general need prime factorization, but
in general need prime factorization, but
for p (p prime)
for p (p prime) ø(p)=p-1
ø(p)=p-1
for p.q (p,q prime)
for p.q (p,q prime) ø(p.q)=(p-1)x(q-1)
ø(p.q)=(p-1)x(q-1)
eg.
eg.
ø(37) = 36
ø(37) = 36
ø(21) = (3–1)x(7–1) = 2x6 = 12
ø(21) = (3–1)x(7–1) = 2x6 = 12
9. Euler's Theorem
Euler's Theorem
a generalisation of Fermat's Theorem
a generalisation of Fermat's Theorem
a
aø(n)
ø(n)
= 1 (mod n)
= 1 (mod n)
for any
for any a,n
a,n where
where gcd(a,n)=1
gcd(a,n)=1
eg.
eg.
a
a=3;
=3;n
n=10; ø(10)=4;
=10; ø(10)=4;
hence 3
hence 34
4
= 81 = 1 mod 10
= 81 = 1 mod 10
a
a=2;
=2;n
n=11; ø(11)=10;
=11; ø(11)=10;
hence 2
hence 210
10
= 1024 = 1 mod 11
= 1024 = 1 mod 11
also have:
also have: a
aø(n)+1
ø(n)+1
= a (mod n)
= a (mod n)
10. Primality Testing
Primality Testing
often need to find large prime numbers
often need to find large prime numbers
traditionally
traditionally sieve
sieve using
using trial division
trial division
ie. divide by all numbers (primes) in turn less than the
ie. divide by all numbers (primes) in turn less than the
square root of the number
square root of the number
only works for small numbers
only works for small numbers
alternatively can use statistical primality tests
alternatively can use statistical primality tests
based on properties of primes
based on properties of primes
for which all primes numbers satisfy property
for which all primes numbers satisfy property
but some composite numbers, called pseudo-primes,
but some composite numbers, called pseudo-primes,
also satisfy the property
also satisfy the property
can use a slower deterministic primality test
can use a slower deterministic primality test
11. Miller Rabin Algorithm
Miller Rabin Algorithm
a test based on prime properties that result from
a test based on prime properties that result from
Fermat’s Theorem
Fermat’s Theorem
algorithm is:
algorithm is:
TEST (
TEST (n
n) is:
) is:
1. Find integers
1. Find integers k
k,
, q
q,
, k
k > 0,
> 0, q
q odd, so that
odd, so that (
(n
n–1)=2
–1)=2k
k
q
q
2. Select a random integer
2. Select a random integer a
a, 1<
, 1<a
a<
<n
n–1
–1
3.
3. if
if a
aq
q
mod
mod n
n = 1
= 1 then
then return (“
return (“inconclusive
inconclusive");
");
4.
4. for
for j
j = 0
= 0 to
to k
k – 1
– 1 do
do
5.
5. if
if (
(a
a2
2j
j
q
q
mod
mod n
n =
= n
n-1
-1)
)
then
then return(“
return(“inconclusive
inconclusive")
")
6. return (“composite")
6. return (“composite")
12. Probabilistic Considerations
Probabilistic Considerations
if Miller-Rabin returns “composite” the
if Miller-Rabin returns “composite” the
number is definitely not prime
number is definitely not prime
otherwise is a prime or a pseudo-prime
otherwise is a prime or a pseudo-prime
chance it detects a pseudo-prime is <
chance it detects a pseudo-prime is < 1
1
/
/4
4
hence if repeat test with different random a
hence if repeat test with different random a
then chance n is prime after t tests is:
then chance n is prime after t tests is:
Pr(n prime after t tests) = 1-4
Pr(n prime after t tests) = 1-4-t
-t
eg. for t=10 this probability is > 0.99999
eg. for t=10 this probability is > 0.99999
could then use the deterministic AKS test
could then use the deterministic AKS test
13. Prime Distribution
Prime Distribution
prime number theorem states that primes
prime number theorem states that primes
occur roughly every (
occur roughly every (ln n
ln n) integers
) integers
but can immediately ignore evens
but can immediately ignore evens
so in practice need only test
so in practice need only test 0.5 ln(n)
0.5 ln(n)
numbers of size
numbers of size n
n to locate a prime
to locate a prime
note this is only the “average”
note this is only the “average”
sometimes primes are close together
sometimes primes are close together
other times are quite far apart
other times are quite far apart
14. Chinese Remainder Theorem
Chinese Remainder Theorem
used to speed up modulo computations
used to speed up modulo computations
if working modulo a product of numbers
if working modulo a product of numbers
eg.
eg. mod M = m
mod M = m1
1m
m2
2..m
..mk
k
Chinese Remainder theorem lets us work
Chinese Remainder theorem lets us work
in each moduli m
in each moduli mi
i separately
separately
since computational cost is proportional to
since computational cost is proportional to
size, this is faster than working in the full
size, this is faster than working in the full
modulus M
modulus M
15. Chinese Remainder Theorem
Chinese Remainder Theorem
can implement CRT in several ways
can implement CRT in several ways
to compute
to compute A(mod M)
A(mod M)
first compute all
first compute all a
ai
i = A mod m
= A mod mi
i separately
separately
determine constants
determine constants c
ci
i below, where
below, where M
Mi
i = M/m
= M/mi
i
then combine results to get answer using:
then combine results to get answer using:
16. Primitive Roots
Primitive Roots
from Euler’s theorem have
from Euler’s theorem have a
aø(n)
ø(n)
mod n=1
mod n=1
consider
consider a
am
m
=1 (mod n), GCD(a,n)=1
=1 (mod n), GCD(a,n)=1
must exist for
must exist for m =
m = ø(n)
ø(n) but may be smaller
but may be smaller
once powers reach m, cycle will repeat
once powers reach m, cycle will repeat
if smallest is
if smallest is m =
m = ø(n)
ø(n) then
then a
a is called a
is called a
primitive root
primitive root
if
if p
p is prime, then successive powers of
is prime, then successive powers of a
a
"generate" the group
"generate" the group mod p
mod p
these are useful but relatively hard to find
these are useful but relatively hard to find
18. Discrete Logarithms
Discrete Logarithms
the inverse problem to exponentiation is to find
the inverse problem to exponentiation is to find
the
the discrete logarithm
discrete logarithm of a number modulo p
of a number modulo p
that is to find
that is to find i
i such that
such that b = a
b = ai
i
(mod p)
(mod p)
this is written as
this is written as i = dlog
i = dloga
a b (mod p)
b (mod p)
if
if a
a is a primitive root then it always exists,
is a primitive root then it always exists,
otherwise it may not, eg.
otherwise it may not, eg.
x = log
x = log3
3 4 mod 13 has no answer
4 mod 13 has no answer
x = log
x = log2
2 3 mod 13 = 4 by trying successive powers
3 mod 13 = 4 by trying successive powers
whilst exponentiation is relatively easy, finding
whilst exponentiation is relatively easy, finding
discrete logarithms is generally a
discrete logarithms is generally a hard
hard problem
problem
20. Summary
Summary
have considered:
have considered:
prime numbers
prime numbers
Fermat’s and Euler’s Theorems &
Fermat’s and Euler’s Theorems & ø(n)
ø(n)
Primality Testing
Primality Testing
Chinese Remainder Theorem
Chinese Remainder Theorem
Primitive Roots & Discrete Logarithms
Primitive Roots & Discrete Logarithms
Editor's Notes
#1:Lecture slides by Lawrie Brown for “Cryptography and Network Security”, 5/e, by William Stallings, Chapter 8 – “Introduction to Number Theory”.
#2:Opening quote. A number of concepts from number theory are essential in the design of public-key cryptographic algorithms, which this chapter will introduce.
#3:A central concern of number theory is the study of prime numbers. Indeed, whole books have been written on the subject. An integer p>1 is a prime number if and only if its only divisors are 1 and itself. Prime numbers play a critical role in number theory and in the techniques discussed in this chapter. Stallings Table 8.1 (excerpt above) shows the primes less than 2000. Note the way the primes are distributed. In particular note the number of primes in each range of 100 numbers.
#4:The idea of "factoring" a number is important - finding numbers which divide into it. Taking this as far as can go, by factorising all the factors, we can eventually write the number as a product of (powers of) primes - its prime factorisation. Note also that factoring a number is relatively hard compared to multiplying the factors together to generate the number.
#5:Have the concept of “relatively prime” if two number share no common factors other than 1.
Another common problem is to determine the "greatest common divisor” GCD(a,b) which is the largest number that divides into both a & b.
#6:Two theorems that play important roles in public-key cryptography are Fermat’s theorem and Euler’s theorem.
Fermat’s theorem (also known as Fermat’s Little Theorem) as listed above, states an important property of prime numbers. See Stallings section 8.2 for its proof.
#7:Now introduce the Euler’s totient function ø(n), defined as the number of positive integers less than n & relatively prime to n. Note the term “residue” refers to numbers less than some modulus, and the “reduced set of residues” to those numbers (residues) which are relatively prime to the modulus (n). Note by convention that ø(1) = 1. Stallings Table 8.2 lists the first 30 values of ø(n). The value ø(1) is without meaning but is defined to have the value 1.
#8:To compute ø(n) need to count the number of residues to be excluded. In general you need use a complex formula on the prime factorization of n, but have a couple of special cases as shown.
#9:Euler's Theorem is a generalization of Fermat's Theorem for any number n. See Stallings section 8.2 for its proof. As is the case for Fermat's theorem, an alternative form of the theorem is also useful. Again, similar to the case with Fermat's theorem, the first form of Euler's theorem requires that a be relatively prime to n, but this form does not.
#10:For many cryptographic functions it is necessary to select one or more very large prime numbers at random. Thus we are faced with the task of determining whether a given large number is prime. There is no simple yet efficient means of accomplishing this task.
Traditionally sieve for primes using trial division of all possible prime factors of some number, but this only works for small numbers.
Alternatively can use repeated statistical primality tests based on properties of primes, and then for certainty, use a slower deterministic primality test, such as the AKS test.
#11:The algorithm shown is due to Miller and Rabin is typically used to test a large number for primality. See Stallings section 8.3 for its proof, which relies on some properties of primes that result from Fermat’s theorem.
#12:How can we use the Miller-Rabin algorithm to determine with a high degree of confidence whether or not an integer is prime? If Miller-Rabin returns “composite” the number is definitely not prime, otherwise it is either a prime or a pseudo-prime. The chance it detects a pseudo-prime is < 1/4
So if apply test repeatedly with different values of a, the probabiility that the number is a pseudo-prime can be made as small as desired, eg after 10 tests have chance of error < 0.00001
If really need certainty, then would now expend effort to run a deterministic primality proof such as AKS. Prior to 2002, there was no known method of efficiently proving the primality of very large numbers. All of the algorithms in use, including the most popular (Miller-Rabin), produced a probabilistic result. In 2002, Agrawal, Kayal, and Saxena [AGRA02] developed a relatively simple deterministic algorithm that efficiently determines whether a given large number is a prime. The algorithm, known as the AKS algorithm, does not appear to be as efficient as the Miller-Rabin algorithm. Thus far, it has not supplanted this older, probabilistic technique.
#13:It is worth noting how many numbers are likely to be rejected before a prime number is found using the Miller-Rabin test, or any other test for primality. A result from number theory, known as the prime number theorem, states that primes near n are spaced on the average one every (ln n) integers. Since you can ignore even numbers, on average need only test 0.5 ln(n) numbers of size n to locate a prime. eg. for numbers round 2^200 would check 0.5ln(2^200) = 69 numbers on average. This is only an average, can see successive odd primes, or long runs of composites.
#14:One of the most useful results of number theory is the Chinese remainder theorem (CRT), so called because it is believed to have been discovered by the Chinese mathematician Sun-Tse in around 100 AD. In essence, the CRT says it is possible to reconstruct integers in a certain range from their residues modulo a set of pairwise relatively prime moduli. Thus it is very useful in speeding up some operations in the RSA public-key scheme, since it allows you to do perform calculations modulo factors of your modulus, and then combine the answers to get the actual result. Since the computational cost is proportional to size, this is faster than working in the full modulus sized modulus.
#15:One of the useful features of the Chinese remainder theorem is that it provides a way to manipulate (potentially very large) numbers mod M, in terms of tuples of smaller numbers. This can be useful when M is 150 digits or more. However note that it is necessary to know beforehand the factorization of M. See worked examples in Stallings section 8.4.
#16:Consider the powers of an integer modulo n. By Eulers theorem, for every relatively prime a, there is at least one power equal to 1 (being ø(n)), but there may be a smaller value. If the smallest value is m = ø(n) then a is called a primitive root. If n is prime, then the powers of a primitive root “generate” all residues mod n. Such generators are very useful, and are used in a number of public-key algorithms, but they are relatively hard to find.
#17:Stallings Table 8.3 shows all the powers of a, modulo 19 for all positive a < 19. The length of the sequence for each base value is indicated by shading. Note the following:
All sequences end in 1.
The length of a sequence divides %(19) = 18. That is, an integral number of sequences occur in each row of the table.
Some of the sequences are of length 18. In this case, it is said that the base integer a generates (via powers) the set of nonzero integers modulo 19. Each such integer is called a primitive root of the modulus 19.
#18:Discrete logarithms are fundamental to a number of public-key algorithms, including Diffie-Hellman key exchange and the digital signature algorithm (DSA).
Discrete logs (or indices) share the properties of normal logarithms, and are quite useful. As with ordinary positive real numbers, the logarithm function is the inverse of exponentiation. The logarithm of a number is defined to be the power to which some positive base (except 1) must be raised in order to equal that number. If working with modulo arithmetic, and the base is a primitive root, then an integral discrete logarithm exists for any residue.
However whilst exponentiation is relatively easy, finding discrete logs is not, in fact is as hard as factoring a number. This is an example of a problem that is "easy" one way (raising a number to a power), but "hard" the other (finding what power a number is raised to giving the desired answer). Problems with this type of asymmetry are very rare, but are of critical usefulness in modern cryptography.
#19:Stallings Table 8.4, which is directly derived from Table 8.3, shows the sets of discrete logarithms that can be defined for modulus 19.