CS3023- Security Principles and Cryptography
theory and Practices
Number Theory
Prepared by
Balachander S
PhD - Full Time Scholar
RA2113003011020
Introduction to Number Theory
•Number theory is about integers and their properties.
•We will start with the basic principles of
• divisibility,
• greatest common divisors,
•modular arithmetic
•and look at some relevant algorithms.
Divisors
• say a non-zero number b divides a if for some
m
have a=mb (a,b,m all integers)
• that is b divides into a with no remainder
• denote this b|a
• and say that b is a divisor of a
• eg. all of 1,2,3,4,6,8,12,24 divide 24
• eg. 13 | 182; –5 | 30; 17 | 289; –3 | 33; 17 | 0
Primes
•A positive integer p greater than 1 is called prime if the only
positive factors of p are 1 and p.
•A positive integer that is greater than 1 and is not prime is called
composite.
•The fundamental theorem of arithmetic:
•Every positive integer can be written uniquely as the product of
primes, where the prime factors are written in order of increasing
size.
Examples
15 = 3*5
48 = 2*2*2*2*3 = 24*3
17 = 17
Properties of
Divisibility
• If a|1, then a = ±1.
• If a|b and b|a, then a = ±b.
• Any b /= 0 divides 0.
• If a | b and b | c, then a | c
– e.g. 11 | 66 and 66 | 198 ,then 11 |
198
• If b|g and b|h, then b|(mg + nh)
for arbitrary integers m and n
e.g. b = 7; g = 14; h = 63; m = 3; n = 2
hence 7|14 and 7|63
Division Algorithm
•Let a be an integer and d a positive integer.
•Then there are unique integers q and r, with
0 ≤ r < d, such that a = dq + r.
•In the above equation,
• d is called the divisor,
• a is called the dividend,
• q is called the quotient, and
• r is called the remainder.
•Example:
•When we divide 17 by 5, we have
•17 = 5⋅3 + 2.
• 17 is the dividend,
• 5 is the divisor,
• 3 is called the quotient, and
• 2 is called the remainder.
•Another example:
•What happens when we divide -11 by 3 ?
•Note that the remainder cannot be negative.
•-11 = 3⋅(-4) + 1.
• -11 is the dividend,
• 3 is the divisor,
• -4 is called the quotient, and
• 1 is called the remainder.
Division
Algorithm
• if divide a by n get integer quotient q and
integer remainder r such that:
– a = qn + r where 0 <= r < n; q = floor(a/n)
• remainder r often referred to as a residue
Greatest Common Divisor
(GCD)
⮚ a common problem in number theory
⮚ GCD (a,b) of a and b is the largest integer that
divides evenly into both a and b
● eg GCD(60,24) = 12
⮚ define gcd(0, 0) = 0
⮚ often want no common factors (except 1)
define such numbers as relatively prime
● eg GCD(8,15) = 1
● hence 8 & 15 are relatively prime
•Let a and b be integers, not both zero.
•The largest integer d such that d | a and d | b is called the greatest common
divisor of a and b.
•The greatest common divisor of a and b is denoted by gcd(a, b).
•Example 1: What is gcd(48, 72) ?
•The positive common divisors of 48 and 72 are
1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24.
•Example 2: What is gcd(19, 72) ?
•The only positive common divisor of 19 and 72 is
1, so gcd(19, 72) = 1.
Greatest Common Divisors
•Using prime factorizations:
•a = p1a1 p2a2 … pnan , b = p1b1 p2b2 … pnbn ,
•where p1 < p2 < … < pn and ai, bi ∈ N for 1 ≤ i ≤ n
•gcd(a, b) = p1min(a1, b1 ) p2min(a2, b2 ) … pnmin(an, bn )
Relatively prime Integers
•Definition:
•Two integers a and b are relatively prime if
gcd(a, b) = 1.
•Examples:
•Are 15 and 28 relatively prime?
•Yes, gcd(15, 28) = 1.
•Are 55 and 28 relatively prime?
•Yes, gcd(55, 28) = 1.
•Are 35 and 28 relatively prime?
•No, gcd(35, 28) = 7.
•Definition:
•The integers a1, a2, …, an are pairwise relatively prime if gcd(ai, aj) = 1
whenever 1 ≤ i < j ≤ n.
•Examples:
•Are 15, 17, and 27 pairwise relatively prime?
•No, because gcd(15, 27) = 3.
•Are 15, 17, and 28 pairwise relatively prime?
•Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and gcd(17, 28) = 1.
Modular Arithmetic
• define modulo operator “a mod n” to
be remainder when a is divided by n
– where integer n is called the modulus
• b is called a residue of a mod n
– since with integers can always write: a = qn + b
– usually chose smallest positive remainder as residue
• ie. 0 <= b <= n-1
– process is known as modulo reduction
• eg. -12 mod 7 = -5 mod 7 = 2 mod 7 = 9 mod 7
• a & b are congruent if: a mod n = b mod n
– when divided by n, a & b have same remainder
– eg. 100 ≡ 34 (mod 11) - (Notation)
Modular Arithmetic Operations
• can perform arithmetic with residues
• uses a finite number of values, and loops back
from either end
Zn = {0, 1, . . . , (n – 1)}
• modular arithmetic is when do addition &
multiplication and modulo reduce
answer
• can do reduction at any point, ie
– a+b mod n = [a mod n + b mod n] mod n
Modular Arithmetic Operations
1.[(a mod n) + (b mod n)] mod n
= (a + b) mod n
2.[(a mod n) – (b mod n)] mod n
= (a – b) mod n
3.[(a mod n) x (b mod n)] mod n
= (a x b) mod n
e.g.
[(11 mod 8) + (15 mod 8)] mod 8 = 10 mod 8 = 2 (11 + 15) mod 8 = 26 mod 8 = 2
[(11 mod 8) – (15 mod 8)] mod 8 = –4 mod 8 = 4 (11 – 15) mod 8 = –4 mod 8 = 4
[(11 mod 8) x (15 mod 8)] mod 8 = 21 mod 8 = 5 (11 x 15) mod 8 = 165 mod 8 = 5
Modulo 8 Addition
Example
+ 0 1 2 3 4 5 6 7
0 0 1 2 3 4 5 6 7
1 1 2 3 4 5 6 7 0
2 2 3 4 5 6 7 0 1
3 3 4 5 6 7 0 1 2
4 4 5 6 7 0 1 2 3
5 5 6 7 0 1 2 3 4
6 6 7 0 1 2 3 4 5
7 7 0 1 2 3 4 5 6
Modulo 8
Multiplication
* 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7
2 0 2 4 6 0 2 4 6
3 0 3 6 1 4 7 2 5
4 0 4 0 4 0 4 0 4
5 0 5 2 7 4 1 6 3
6 0 6 4 2 0 6 4 2
7 0 7 6 5 4 3 2 1
Modular Arithmetic Properties
Euclidean Algorithm
•The Euclidean Algorithm finds the greatest common divisor of two integers a and b.
•For example, if we want to find gcd(287, 91), we divide 287 by 91:
•287 = 91⋅3 + 14
•We know that for integers a, b and c,
if a | b and a | c, then a | (b + c).
•Therefore, any divisor of 287 and 91 must also be a divisor of 287 - 91 ⋅3 = 14.
•Consequently, gcd(287, 91) = gcd(91, 14).
Euclidean
Algorithm
• an efficient way to find the GCD(a,b)
• uses theorem that:
– GCD(a,b) = GCD(b, a mod b)
• Euclidean Algorithm to compute GCD(a,b) is:
Euclid(a,b)
if (b=0) then return a;
else return Euclid(b, a mod b);
Example
GCD(1970,1066)
1970 = 1 x 1066 + 904 gcd(1066, 904)
1066 = 1 x 904 + 162 gcd(904, 162)
904 = 5 x 162 + 94 gcd(162, 94)
162 = 1 x 94 + 68 gcd(94, 68)
94 = 1 x 68 + 26 gcd(68, 26)
68 = 2 x 26 + 16 gcd(26, 16)
26 = 1 x 16 + 10 gcd(16, 10)
16 = 1 x 10 + 6 gcd(10, 6)
10 = 1 x 6 + 4 gcd(6, 4)
6 = 1 x 4 + 2 gcd(4, 2)
4 = 2 x 2 + 0 gcd(2, 0)
Extended Euclidean
Algorithm
• calculates not only GCD but x & y:
ax + by = d = gcd(a, b)
• useful for later crypto computations
• follow sequence of divisions for GCD but
assume at each step i, can find x &y:
r = ax + by
• at end find GCD value and also x & y
• if GCD(a,b)=1 these values are inverses
Finding
Inverses
EXTENDED EUCLID(m, b)
1. (A1, A2, A3)=(1, 0, m);
(B1, B2, B3)=(0, 1, b)
2. if B3 = 0
return A3 = gcd(m, b); no inverse
3. if B3 = 1
return B3 = gcd(m, b); B2 = b–1 mod m
4. Q = A3 div B3
5. (T1, T2, T3)=(A1 – Q B1, A2 – Q B2, A3 – Q B3)
6. (A1, A2, A3)=(B1, B2, B3)
7. (B1, B2, B3)=(T1, T2, T3)
8. goto 2
Inverse of 550 in
GF(1759)
Q A1 A2 A3 B1 B2 B3
— 1 0 1759 0 1 550
3 0 1 550 1 –3 109
5 1 –3 109 –5 16 5
21 –5 16 5 106 –339 4
1 106 –339 4 –111 355 1
THANK YOU