Ch02 Unlocked
Ch02 Unlocked
Mathematics of
Cryptography
Part I: Modular Arithmetic, Congruence,
and Matrices
1
Chapter 2
Objectives
To review integer arithmetic, concentrating on divisibility
and finding the greatest common divisor using the Euclidean
algorithm
To understand how the extended Euclidean algorithm can be
used to solve linear Diophantine equations, to solve linear
congruent equations, and to find the multiplicative inverses
To emphasize the importance of modular arithmetic and
the modulo operator, because they are extensively used in
cryptography
To emphasize and review matrices and operations on residue
matrices that are extensively used in cryptography
To solve a set of congruent equations using residue matrices
2
2-1 INTEGER ARITHMETIC
4
2.1.2 Binary Operations
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.
5
2.1.2 Continued
Example 2.1
6
2.1.3 Integer Division
a=q×n+r
7
2.1.3 Continued
Example 2.2
Assume that a = 255 and n = 11. We can find q = 23 and R = 2
using the division algorithm.
Figure 2.3 Example 2.2, finding the quotient and the remainder
8
2.1.3 Continued
Figure 2.4 Division algorithm for integers
2.# 9
2.1.3 Continued
Example 2.3
When we use a computer or a calculator, r and q are negative
when a is negative. How can we apply the restriction that r
needs to be positive? The solution is simple, we decrement the
value of q by 1 and we add the value of n to r to make it
positive.
2.# 10
2.1.3 Continued
Figure 2.5 Graph of division algorithm
11
2.1.4 Divisbility
a=q×n
12
2.1.4 Continued
Example 2.4
2.# 13
2.1.4 Continued
Properties
2.# 14
2.1.4 Continued
Example 2.5
2.# 15
2.1.4 Continued
Example 2.6
2.# 16
2.1.4 Continued
Note
2.# 17
2.1.4 Continued
Figure 2.6 Common divisors of two integers
18
2.1.4 Continued
Note Greatest Common Divisor
19
Note Euclidean Algorithm
21
2.1.4 Continued
Figure 2.7 Euclidean Algorithm
Note
Note
23
2.1.4 Continued
Example 2.7
Find the greatest common divisor of 2740 and 1760.
Solution
We have gcd (2740, 1760) = 20.
24
2.1.4 Continued
Example 2.8
Find the greatest common divisor of 25 and 60.
Solution
We have gcd (25, 60) = 5.
25
2.1.4 Continued
Extended Euclidean Algorithm
Given two integers a and b, we often need to find other two
integers, s and t, such that
26
2.1.4 Continued
Figure 2.8.a Extended Euclidean algorithm, part a
2.# 27
2.1.4 Continued
Figure 2.8.b Extended Euclidean algorithm, part b
28
2.1.4 Continued
Example 2.9
Given a = 161 and b = 28, find gcd (a, b) and the values of s
and t.
Solution
We get gcd (161, 28) = 7, s = −1 and t = 6.
29
2.1.4 Continued
Example 2.10
Given a = 17 and b = 0, find gcd (a, b) and the values of s
and t.
Solution
We get gcd (17, 0) = 17, s = 1, and t = 0.
30
2.1.4 Continued
Example 2.11
Solution
We get gcd (0, 45) = 45, s = 0, and t = 1.
31
2.1.4 Continued
Linear Diophantine Equation
Note
32
2.1.4 Continued
Linear Diophantine Equation
Note
Particular solution:
x0 = (c/d)s and y0 = (c/d)t
Note
General solutions:
x = x0 + k (b/d) and y = y0 − k(a/d)
where k is an integer
2.# 33
2.1.4 Continued
Example 2.12
Solution
2.# 34
2.1.4 Continued
Example 2.13
(0, 20), (1, 16), (2, 12), (3, 8), (4, 4), (5, 0).
2.# 35
2-2 MODULAR ARITHMETIC
37
2.1.4 Continued
Example 2.14
Find the result of the following operations:
a. 27 mod 5 b. 36 mod 12
c. −18 mod 14 d. −7 mod 10
Solution
a. Dividing 27 by 5 results in r = 2
b. Dividing 36 by 12 results in r = 0.
c. Dividing −18 by 14 results in r = −4. After adding the
modulus r = 10
d. Dividing −7 by 10 results in r = −7. After adding the
modulus to −7, r = 3.
38
2.2.2 Set of Residues
39
2.2.3 Congruence
40
2.2.3 Continued
Figure 2.11 Concept of congruence
41
2.2.3 Continued
Residue Classes
A residue class [a] or [a]n is the set of integers congruent
modulo n.
42
2.2.3 Continued
Figure 2.12 Comparison of Z and Zn using graphs
2.# 43
2.2.3 Continued
Example 2.15
2.# 44
2.2.4 Operation in Zn
2.# 45
2.2.4 Continued
Example 2.16
Perform the following operations (the inputs come from Zn):
a. Add 7 to 14 in Z15.
b. Subtract 11 from 7 in Z13.
c. Multiply 11 by 7 in Z20.
Solution
2.# 46
2.2.4 Continued
Example 2.17
Perform the following operations (the inputs come from
either Z or Zn):
a. Add 17 to 27 in Z14.
b. Subtract 43 from 12 in Z13.
c. Multiply 123 by −10 in Z19.
Solution
2.# 47
2.2.4 Continued
Properties
48
2.2.4 Continued
Figure 2.14 Properties of mode operator
49
2.2.4 Continued
Example 2.18
50
2.2.4 Continued
Example 2.19
51
2.2.4 Continued
Example 2.20
52
2.2.5 Inverses
53
2.2.5 Continue
Additive Inverse
Note
Solution
The six pairs of additive inverses are (0, 0), (1, 9), (2, 8), (3, 7),
(4, 6), and (5, 5).
2.# 55
2.2.5 Continue
Multiplicative Inverse
In Zn, two numbers a and b are the multiplicative inverse of
each other if
Note
2.# 56
2.2.5 Continued
Example 2.22
Find the multiplicative inverse of 8 in Z10.
Solution
There is no multiplicative inverse because gcd (10, 8) = 2 ≠ 1.
In other words, we cannot find any number between 0 and 9
such that when multiplied by 8, the result is congruent to 1.
Example 2.23
Find all multiplicative inverses in Z10.
Solution
There are only three pairs: (1, 1), (3, 7) and (9, 9). The
numbers 0, 2, 4, 5, 6, and 8 do not have a multiplicative
inverse.
57
2.2.5 Continued
Example 2.24
58
2.2.5 Continued
Note
59
2.2.5 Continued
Figure 2.15 Using extended Euclidean algorithm to
find multiplicative inverse
2.# 60
2.2.5 Continued
Example 2.25
Find the multiplicative inverse of 11 in Z26.
Solution
2.# 61
2.2.5 Continued
Example 2.26
Find the multiplicative inverse of 23 in Z100.
Solution
2.# 62
2.2.5 Continued
Example 2.27
Find the inverse of 12 in Z26.
Solution
2.# 63
2.2.6 Addition and Multiplication Tables
Figure 2.16 Addition and multiplication table for Z10
2.# 64
2.2.7 Different Sets
Figure 2.17 Some Zn and Zn* sets
Note
66
2-3 MATRICES
67
2.3.1 Definition
68
2.3.1 Continued
69
2.3.2 Operations and Relations
Example 2.28
70
2.3.2 Continued
Example 2. 29
71
2.3.2 Continued
Example 2. 30
72
2.3.2 Continued
Example 2. 31
2.# 73
2.3.3 Determinant
Note
75
2.3.3 Continued
Example 2. 33
76
2.3.4 Inverses
Note
2.# 77
2.3.5 Residue Matrices
Example 2. 34
2.# 78
2 LINEAR CONGRUENCE
79
2.4.1 Single-Variable Linear Equations
80
Approach: The idea is based on the following observations:
A solution exists if and only if b is divisible by gcd (a, n) i.e b mod gcd (a, n)=0.
The number of solutions for x (mod n) is gcd(a, n).
Proof:
1.Given, ax=b (mod n)
⇒ there exist a number y such that ax = b + ny
ax - ny = b — (1)
This is a linear Diophantine equation, and is solvable if and only if gcd(a, n) divides b.
2.Now, using the Extended Euclidean Algorithm, u and v can be found
such that au + nv = gcd (a, n) = d(say)
⇒ au + nv = d
⇒ au = d (mod n) — (2)
1.Assuming b mod d=0, so that solution of Equation 1 exists,
Multiplying both sides of Equation 2 by B/d, (possible since B/d is an integer),
au*(b/d)=d*(b/d) (mod n) or a*(u*bB/d)=b (mod n).
Thus, u*b/d is a solution of Equation 1.
2.Let x0 be u*b/d.
Thus, the d solutions of Equation 1 will be x0, x0+(n/d), x0+2*(n/d), …, x0+(d-1)*(n/d)
81
2.4.1 Continued
Example 2.35
Solve the equation 10 x ≡ 2(mod 15).
Solution
First we find the gcd (10 and 15) = 5. Since 5 does not divide 2,
we have no solution.
Example 2.36
Solve the equation 14 x ≡ 12 (mod 18).
Solution
82
2.4.1 Continued
Example 2.37
Solution
First we change the equation to the form ax ≡ b (mod n). We
add −4 (the additive inverse of 4) to both sides, which give
3x ≡ 2 (mod 13). Because gcd (3, 13) = 1, the equation has only
one solution, which is x0 = (2 × 3−1) mod 13 = 18 mod 13 = 5.
We can see that the answer satisfies the original equation:
3 × 5 + 4 ≡ 6 (mod 13).
83
2.4.2 Single-Variable Linear Equations
84
2.4.2 Continued
Example 2.38
Solve the set of following three equations:
Solution
The result is x ≡ 15 (mod 16), y ≡ 4 (mod 16), and z ≡ 14 (mod
16). We can check the answer by inserting these values into
the equations.
85
𝟑𝟑 𝟓𝟓 𝟕𝟕 𝟏𝟏 𝟎𝟎 𝟎𝟎
𝟏𝟏 𝟒𝟒 𝟏𝟏𝟏𝟏 𝟎𝟎 𝟏𝟏 𝟎𝟎 Mod 16
𝟐𝟐 𝟕𝟕 𝟑𝟑 𝟎𝟎 𝟎𝟎 𝟏𝟏
15 14 11 3
9 5 0 4
5
Mod 16 = 15 16 14
1 11 9
86
Programming Exercise
Extended Euclidean Algorithm:
Extended Euclidean algorithm also finds integer coefficients x and y
such that:
ax + by = gcd(a, b)
Examples:
Input: a = 30, b = 20
Output: gcd = 10
x = 1, y = -1
(Note that 30*1 + 20*(-1) = 10)
Input: a = 35, b = 15
Output: gcd = 5
x = 1, y = -2
(Note that 35*1 + 15*(-2) = 5)
The extended Euclidean algorithm updates results of gcd(a, b) using the results
calculated by recursive call gcd(b%a, a). Let values of x and y calculated by the
recursive call be x1 and y1. x and y are updated using the below expressions.
x = y1 - ⌊b/a⌋ * x1 y = x1
87
// Java program to demonstrate working of extended
// Euclidean Algorithm
import java.util.*;
import java.lang.*;
class GFG
{
// extended Euclidean Algorithm
public static int gcdExtended(int a, int b, int x, int y)
{
// Base Case
if (a == 0)
{
x = 0;
y = 1;
return b;
}
int x1=1, y1=1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, x1, y1);
// Driver Program
public static void main(String[] args)
{
int x=1, y=1;
int a = 35, b = 15;
int g = gcdExtended(a, b, x, y);
System.out.print("gcd(" + a + " , " + b+ ") = " + g);
}
} 88
Programming Exercise
Solve Linear Congruences ax ≡ b (mod n) for values of x in range [0, N-1]
Explanation: The values of X satisfying the condition AX=B (mod N) are {3, 9, 15}.
(15*15)%18 = 225%18 = 9
(3*15)%18 = 45%18 = 9
(9*15)%18 = 135%18 = 9
Input: A=9, B=21, N=30
Output: 9, 19, 20
89
# Python3 program for the above approach
# Base Case
if a == 0 :
return b, 0, 1
return gcd, x, y
90
# Function to give the distinct
# solutions of ax = b (mod n)
def linearCongruence(A, B, N):
A =A %N
B =B %N
u =0
v =0
# No solution exists
if (B % d != 0):
print(-1)
return
# Driver Code
# Input
A = 15
B =9
N = 18
# Function Call
linearCongruence(A, B, N)
91