Number Theory
Number Theory
Number Theory:
Divisibility, Prime Numbers,
Greatest Common Divisor,
Relative Primality
Groups, Rings and Fields
2
Sources
• The Basics of Abstract Algebra, Paul E. Bland, Freeman
• Introduction to Cryptography with Java Applets, David Bishop, Jones &
Bartlett
• Practical Cryptography, Niels Ferguson and Bruce Schneier, Wiley
• Concrete Mathematics, Graham, Knuth, and Patashnik, Addison-Wesley
• Network Security: Private Communications in a Public World, Second Edition
Charlie Kaufman, Radia Perlman, Mike Speciner, Prentice-Hall
• Applied Cryptography: Protocols, Algorithms and Source Code in C, Second
Edition, Bruce Schneier, Wiley
• Cryptography and Network Security: Principles and Practices, William
Stallings, Prentice-Hall
• and a number of web sites...
3
Divisibility and Divisors
• We say that m divides n (or n is divisible by m) if:
–m>0
and:
n
– the ratio is an integer.
m
• This property underlies all number theory, so we have a
notation for it:
m|n
and we say that m is a divisor of n
4
Divisibility and Divisors
• Here are some relations:
1) If a|1, then a = ± 1
2) If a|b and b|a, then a = ± b
3) Any b 0 divides 0
4) If b|g and b|h, then b|(mg + nh) for arbitrary integers m and n
5) If a|b and b|c, then a|c
6) If n is a positive number > 1, and d is the smallest divisor of n that
is greater than 1, then d is prime.
5
Prime Numbers
• A positive integer p is called prime if it has just two
divisors: 1 and p
• A positive integer that has three or more divisors is known
as a composite.
• Every integer > 1 is either prime or composite, but not both.
– Note:
• 2 is a prime
• 1 is not a prime
• The sequence of primes starts:
2,3,5,7,11,13,17,19,23,29,31,37,41,...
6
Prime Numbers
• Primes are important because they form the fundamental
building blocks of all the positive integers:
– Any positive integer n can be written as a product of primes:
m
n p1 p2 pm pk ( p1 p2 pm )
k 1
7
Prime Numbers
• There are an infinite number of primes
– *Euclid's proof:
• Assume that there are a finite number of primes
• Call the list of primes p1, p2, ... pk, where k is the number of primes
• Define the number n, the product of all primes, plus 1:
n p1 p2 pk 1
• Consider the smallest divisor of n, d, greater than 1 :
– d must be prime, because if it had divisors, they would also divide n
– But none of the primes in our finite list is a divisor of n (if you divide n by
any of those primes, you will always get a remainder of 1)
– So d is a prime not in our list
• This is a contradiction, so there must be an infinite number of primes
Pn n ln n
9
Generating Small Prime Numbers
• One simple way of calculating primes is to use the Sieve of
Eratosthenes*:
1) Write down all integers from 2 through x
2) Circle 2, marking it prime, and cross out all other multiples of 2
3) Repeatedly circle the smallest uncircled, uncrossed number and
cross out all its other multiples
4) When every number has been circled or crossed out, the circled
numbers are the primes
10
Greatest Common Divisor (GCD)
• The greatest common divisor of two integers m and n is the largest
integer that divides them both:
gcd(m, n) = max{k | k|m and k|n}
– Euclid's algorithm to calculate gcd(m,n), for given values 0 m n
uses the recurrence:
gcd(0, n) n;
gcd(m, n) gcd(n mod m, m), for m 0
• So, for example, gcd(12, 18) = gcd(6,12) = gcd(0,6) = 6
– Because any common divisor of m and n must also be a common
divisor of both m and the number:
n mod m n n / m m
where a is the floor function, the smallest integer less than or equal to a
11
Euclid's Algorithm for GCD
package primes;
public static void main(String[] args)
/** {
* Class to calculate the greatest common divisor (gcd) int m = 18;
* of two integers using Euclid's algorithm int n = 12;
*/ System.out.println("GCD of " + m + " and " +
n + " is " + calculate(m, n));
public class GCD }
{ }
public static int calculate(int m, int n)
{
int g;
if (m < 0)
m = -m;
if (n < 0)
n = -n;
if ((m + n) == 0)
An implementation in Java
throw new java.lang.IllegalArgumentException(
"m and n cannot add to zero");
g = n;
while (m > 0)
{
g = m;
m = n % m;
n = g;
}
return g;
}
12
Euclid's Algorithm for GCD
• Euclid's algorithm can be generalized for an array of integer
values:
/**
* Calculates the gcd of an array of numbers
*/
public static int calculate(int[] v)
{
This function is added to the
int g = v[0];
for (int i = 1; i < v.length; i++)
previous Java class.
{
g = calculate(g, v[i]);
if (g == 1)
return 1;
}
return g;
}
13
Relative Primality
• Two integers m and n are relatively prime (also known as coprimes)
when their gcd(m,n) = 1
– That is, they have no common factor other than 1
For example:
• 14 and 15 are relatively prime, despite the fact that neither one is a prime
• 6 and 35 are relatively prime
• 6 and 27 are not relatively prime because they are both divisible by 3.
14
Groups
• A group, G, is a set of elements with an associated binary
operation, . It is sometimes denoted {G, }
– For each ordered pair (a, b) of elements in G, there is an associated
element (a b), such that the following axioms hold:
15
Groups
• A finite group is a group with a finite number of elements,
otherwise, a group is an infinite group.
• A group is said to be an abelian group if it satisfies the following
condition:
5) Commutativ e : a b b a for all a, b G
17
Rings
• A ring, R, denoted by {R, +, }, is a set of elements with two binary operations,
called addition (+) and multiplication ( ), such that, for a, b, c in R:
addition and multiplication are abstract operations here
1)-5) R is an abelian group with respect to addition; for this case of an additive group, we
denote the identity element as 0, and the inverse of a as -a.
6) Closure under multiplication:
If a and b belong to R, then a b is also in R
Note that we often write
7) Associativity of multiplication:
a b as simply ab
a (b c) = (a b) c for all a, b, c, in R
8) Distributive Laws:
a (b + c) = a b + a c for all a, b, c, in R
(a + b) c = a c + b c for all a, b, c, in R
18
Commutative Rings
• A ring is commutative if it satisfies the following additional condition:
9) Commutativity of multiplication:
a b = b a for all a, b, c, in R
19
Integral Domains
• An integral domain is a commutative ring that obeys the following:
20
Fields
• A field, F, denoted by {F, +, }, is a set of elements with two binary
operations, called addition and multiplication, such that, for all a, b, c
in F, the following apply:
Again, addition and multiplication are abstract operations
21
Fields
• A field is a set in which we can do addition, subtraction,
multiplication, and division without leaving the set.
• Division is defined:
a/b = a(b-1)
Examples:
• The set of rational numbers, Q; the set of real numbers, R, the set of
complex numbers, C.
• The set of all integers, Z, is not a field, because only the elements 1 and
-1 have multiplicative inverses in the integers.
22
Groups, Rings, and Fields
Groups
Abelian Groups
Rings
Commutative Rings
Integer Domains
Fields
23
Summary
• Whew!
• I realize it's a quite a bit of new stuff, and much of it is
fairly abstract.
• However, I think we need some mathematical background
to understand modern cryptographic algorithms.
• There's more: Modular Arithmetic, which is a very
important topic for modern cryptography.
24