Crypto Maths - Part 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Cryptography

Mathematics of Cryptography: Part 1

Ashutosh Bhatia
BITS Pilani
[email protected]
Set of Integers and CRYPTO
 The set of integers, denoted by Z, contains all integral numbers (with
no fraction) from negative infinity to positive infinity
𝑍 = . . . , −2, −1, 0, 1, 2, . . .

 In cryptography, we are interested in three binary operations applied


to the set of integers. A binary operation takes two inputs and creates
one output.
( +, - and ×)
Divisors
 An integer 𝑏 ≠ 0 is a divisor of 𝑎 if 𝑎 = 𝑚𝑏, where 𝑎, 𝑏 and 𝑚 are integers.
 Notation: 𝑏|𝑎 is used to define that b is a divisor of s or b divides a.
 Properties:
 If 𝑎|1, then 𝑎 = ±1
 If 𝑎|𝑏 and 𝑏|𝑎 then 𝑏 = ±𝑎
 Any 𝑏 ≠ 0 divides 0
 For any integer 𝑎, 1/a
 If 𝑟| 𝑎 + 𝑏 and 𝑟|𝑎 then 𝑟|𝑏 (prove it !)
 If 𝑏|𝑔 and 𝑏|h then 𝑏|(𝑚𝑔 + 𝑛ℎ) for arbitrary integers m and n (prove it !)
Quotients and Remainders
 Given any positive integer 𝑛 and an integer 𝑎, we get a quotient and remainder 𝑟 such
that 𝑎 = 𝑞𝑛 + 𝑟, 0 ≤ 𝑟 ≤ 𝑛 and q = 𝑎/𝑛 , where 𝑥 is the largest integer less than
or equal to x.
 Exercise: a = -5 and n=2
 For any integer 𝑎 and 𝑛 ≥ 1, 𝑟 and 𝑞 are unique.
 Remainder 𝑟 is also known as residue and defined to be a mod n.
 𝑎 = 𝑞𝑛 + 𝑟 can be rewritten as 𝑎 = 𝑎/𝑛 𝑛 + 𝑎 𝑚𝑜𝑑 𝑛
Greatest Common Divisor (GCD)
 Greatest Common Divisor (GCD) of two integers 𝑎 and 𝑏 denoted as gcd(𝑎, 𝑏) is defined as
positive integer that satisfies gcd 𝑎, 𝑏 = max{𝑘: 𝑘 𝑎 & 𝑘 𝑏}
 GCD Theorem: Let 𝑎; 𝑏 be nonzero integers. Let 𝑔 = gcd(a,b). Then there exist integers 𝑠
and 𝑡 and such that 𝑔 = 𝑎𝑠 + 𝑏𝑡 (Homework)

 Euclidean Algorithm : gcd (a, 0) = a; gcd (a, b) = gcd (b, r), where r is the remainder of
dividing a by b

 Let 𝑔 = gcd(a,b). An integer n can be expressed in the form 𝑎𝑠 + 𝑏𝑡 with 𝑠, 𝑡 ∈ 𝑍, if and


only if 𝑛 is a multiple of 𝑔 .

 The extended Euclidean algorithm can calculate the gcd (a, b) and at the same time
calculate the value of s and t.
Euclidean Algorithm
• an efficient way to find the GCD(a,b)

• Euclidean Algorithm to compute GCD(a,b) is:

EUCLID(a,b)

1. A = a; B = b
2. if B = 0 return A = gcd(a, b)
3. R = A mod B
4. A = B
5. B = R
6. goto 2
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
Modular Arithmetic
 If 𝑎 = 𝑞𝑛 + 𝑟, then 𝑎 𝑚𝑜𝑑 𝑛 = ?
 𝑎 𝑚𝑜𝑑 𝑛 is always an integer 𝑘 such that 0 ≤ 𝑘 ≤ 𝑛 − 1.
 Hence, we can have arithmetic in the set of integers 0,1,2, … … 𝑛 − 1 in
which all arithmetic operators are modulo 𝑛.
 Therefore, the result will always be in the set 0,1,2, … … 𝑛 − 1 .
 The set 0,1,2, … … 𝑛 − 1 is referred as residue modulo 𝑛 and it is denoted
by 𝑍𝑛 .
Modular Arithmetic: Properties
1. a + b mod n = a mod n + b mod n mod n
2. a − b mod n = a mod n − b mod n mod n
3. ab mod n = a mod n ) (b mod n mod n (𝐻𝑜𝑚𝑒𝑤𝑜𝑟𝑘)
4. Commutative, Associate, Distributive
5. Identities
1. 0 + 𝑎 𝑚𝑜𝑑 𝑛 = 𝑎 𝑚𝑜𝑑 𝑛
2. 1 ∗ 𝑎 𝑚𝑜𝑑 𝑛 = 𝑎 𝑚𝑜𝑑 𝑛
Congruent Modulo Operator (≡)
 An integer 𝑎 is congruent to and integer 𝑏 modulo 𝑛, denoted by

 𝑎 ≡ b mod n, iff 𝑎 𝑚𝑜𝑑 𝑛 = 𝑏 𝑚𝑜𝑑 𝑛, i.e they have the same


remainders.
 It is wrong to interpret 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 as 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 , instead it is
semantically equivalent to 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛, nevertheless parenthesis is
generally not used.
Properties of (≡)
𝑖𝑓𝑓
1. 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 𝑛 | (𝑎 − 𝑏)
2. If 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 𝒂𝒏𝒅 𝑏 ≡ 𝑐 𝑚𝑜𝑑 𝑛 𝒕𝒉𝒆𝒏 𝑎 ≡ 𝑐 𝑚𝑜𝑑 𝑛
3. If 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 then
1. 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 𝑚𝑜𝑑 𝑛
2. 𝑘+𝑎 ≡ 𝑘 + 𝑏 𝑚𝑜𝑑 𝑛
3. 𝑘 ∗ 𝑎 ≡ 𝑘 ∗ 𝑏 𝑚𝑜𝑑 𝑛
4. If 𝑎 ≡ 𝑏 𝑚𝑜𝑑 𝑛 𝒂𝒏𝒅 𝑐 ≡ 𝑑 𝑚𝑜𝑑 𝑛 𝒕𝒉𝒆𝒏 (𝐻𝑜𝑚𝑒𝑤𝑜𝑟𝑘)
1. 𝑎𝑐 ≡ 𝑏𝑑 𝑚𝑜𝑑 𝑛 also 𝑎𝑘 ≡ 𝑏 𝑘 𝑚𝑜𝑑 𝑛
2. (𝑎 + 𝑐) ≡ (𝑏 + 𝑑) 𝑚𝑜𝑑 𝑛
3. (𝑎 − 𝑐) ≡ (𝑏 − 𝑑) 𝑚𝑜𝑑 𝑛
Prime Numbers
 An integer 𝑝 > 1 is a prime number if its only divisors are ±1 and 𝑝
 Any integer can be factored in a unique way as
𝑎 = 𝑝1𝛼1 𝑝2𝛼2 𝑝3𝛼3 ….. 𝑝𝑙𝛼𝑙 = ς𝑝∈𝑃 𝑃𝛼𝑝
where 𝑝1 > 𝑝2 > 𝑝𝑙 are prime numbers and where each 𝛼𝑖 > 0.
 Is the number 211 prime ?
Prime Numbers
 An integer 𝑝 > 1 is a prime number if its only divisors are ±1 and 𝑝
 Any integer can be factored in a unique way as
𝑎 = 𝑝1𝛼1 𝑝2𝛼2 𝑝3𝛼3 ….. 𝑝𝑙𝛼𝑙 = ς𝑝∈𝑃 𝑃𝛼𝑝
where 𝑝1 > 𝑝2 > 𝑝𝑙 are prime numbers and where each 𝛼𝑖 > 0.
 Is the number 211 prime ?
 Only test prime numbers smaller than the number you are testing as possible
factors.
 Suppose one number is a factor of N and that it is smaller than the square-root
of the number N. Then the second factor must be larger than the square-root.
Relative Prime Numbers
 Two integers 𝑎 and b are said to be relatively prime if gcd 𝑎, 𝑏 = 1, i.e they have no
prime factors in common.
 Lemma: If 𝑥 is relatively prime to 𝑛, then 𝑥 + 𝑘𝑛 is relatively prime to 𝑛 where 𝑘 is
any integer. (Prove!)
 Lemma: If 𝑥1 , 𝑥2 , … 𝑥𝑘 are relatively prime to 𝑛, then 𝑥1 . 𝑥2 . … 𝑥𝑘 is also relatively prime
to 𝑛.
More Lemmas
 If gcd 𝑎, 𝑛 = 𝑝 then gcd 𝑎 𝑚𝑜𝑑 𝑛, 𝑛 = 𝑝
 If 𝑎 is relatively prime to 𝑛, then 𝑎 𝑚𝑜𝑑 𝑛 is also relatively prime to 𝑛.
 if 𝑎 is not relatively prime to 𝑛, then 𝑎 𝑚𝑜𝑑 𝑛 is also not relatively prime to 𝑛.
 If 𝑏 𝑚𝑜𝑑 𝑛 = 0 then 𝑎 + 𝑏 𝑚𝑜𝑑 𝑛 = 𝑎 𝑚𝑜𝑑 𝑛
Inverses
When we are working in modular arithmetic, we often need to find the inverse of a number
relative to an operation. We are normally looking for an additive inverse (relative to an
addition operation) or a multiplicative inverse (relative to a multiplication operation).

Additive Inverse:
 𝑎, 𝑏 ∈ 𝑍𝑛, , then 𝑏 would be the additive inverse of 𝑎 if 𝑎 + 𝑏 𝑚𝑜𝑑 𝑛 = 0.
 denoted as −𝑎
 𝑎 + 𝑏 ≡ 0 𝑚𝑜𝑑 𝑛
 In modular arithmetic ∀𝑎 ∈ 𝑍𝑛 , ∃ − 𝑎 ∈ 𝑍𝑛 𝑠. 𝑡. 𝑎 + 𝑏 𝑚𝑜𝑑 𝑛 = 0.
 Find all additive inverse pairs in 𝑍8 .

Multiplicative Inverse:
 𝑎, 𝑏 ∈ 𝑍𝑛, , then 𝑏 would be the multiplicative inverse of 𝑎 if 𝑎 ∗ 𝑏 𝑚𝑜𝑑 𝑛 = 1.
 denoted as −𝑎
 𝑎 ∗ 𝑏 ≡ 1 𝑚𝑜𝑑 𝑛
 In modular arithmetic ~∀ 𝑎 ∈ 𝑍𝑛 , ∃ − 𝑎 ∈ 𝑍𝑛 𝑠. 𝑡. 𝑎 ∗ 𝑏 𝑚𝑜𝑑 𝑛 = 1.
 Find all multiplicative inverse pairs in 𝑍8 .
Example: Addition Modulo 8
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

What is the additive inverse of 4?


Example: Multiplication Modulo 8
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

What is the multiplicative inverse of 4?


Multiplicative Inverse in 𝒁𝒏
 If 𝑎 ∈ 𝑍𝑛 is relatively prime to n, then the number ‘b’ s.t. 1 ≤ 𝑏 ≤ 𝑛 is said
to be the multiplicative inverse of a and is denoted as 𝑎− .

 Lemma: Let 𝑍𝑛 = 0, 1, … … , 𝑛 − 1 and 𝑎 ∈ 𝑍𝑛 s.t. 𝑎 ≠ 0 and a is


relatively prime to n, then ∃𝑎 unique element 𝑏 s.t. 𝑏 ∈ 𝑍𝑛 , 𝑏 ≠ 𝑎,
𝑎𝑛𝑑 𝑎𝑏 ≡ 1 𝑚𝑜𝑑 𝑛 .

 We can prove this by showing that If 𝑎 ∈ 𝑍𝑛 is relatively prime to n and


𝑏1 , 𝑏2 ∈ 𝑍𝑛 then 𝑎𝑏1 𝑚𝑜𝑑 𝑛 ≠ 𝑎𝑏2 𝑚𝑜𝑑 𝑛

 If p is a prime integer then every non-zero element in the set 𝑍𝑝 is


relatively prime to p and each integer 𝑎 ∈ 𝑍𝑝 𝑠. 𝑡 𝑎𝑎−1 ≡ 𝑚𝑜𝑑 𝑝
Extended Euclid Algorithm
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

You might also like