Professional Documents
Culture Documents
2 0 Lect2-ModuloMath-4
2 0 Lect2-ModuloMath-4
Dr Bobby Yang
1/28
Modular arithmetic – some symbols
used
• Set of real numbers: R
• Set of integers: Z, e.g {· · · , −2, −1, 0, 1, 2, · · · }
• Set of non-negative integers, e.g. {0, 1, 2, 3, · · · }
• Set of positive integers Z+ , e.g. {1, 2, 3, · · · }
• Set of integers < m, Zm , e.g. {1, 2, · · · , m − 1}
• Greatest common divisor, gcd (a, b) = x, where x is the
largest common factor in a and b.
If a and b are relatively prime, then gcd (a, b) = 1
i.e. a and b do not share a common factor other than 1.
2/28
Modular arithmetic
• Modulo operation. Let a, r , m ∈ Z where Z a set of integers
and m > 0,
We write
a ≡ r mod m, i.e.
a = k · m + r where k is integer.
m is called the modulus and r is called the remainder
• Often “modulo”, “mod” are used interchangeably.
• Equivalent class – set of numbers with the same
remainder, e.g.
21 ≡ 3 mod 9
12 ≡ 3 mod 9
3 ≡ 3 mod 9
−6 ≡ 3 mod 9
−15 ≡ 3 mod 9
···
3/28
Equivalence Class
4/28
Example – equivalence class
e.g. Find 6561 mod 7.
Since 6561 = 38 = 34 · 34 = 81 · 81,
and 81 = 11 · 7 + 4,
then 6561 = 38 = 81 · 81 ≡ 4 · 4 = 16 mod 7 = 2 mod 7
5/28
Groups
Definition: A group is a set G together with one binary
operation ◦ on G, satisfying the following axioms:
1. There is closure for all operations ◦, i.e.,
c = a ◦ b, for all a, b, c ∈ G
a ◦ e = e ◦ a = a for all a ∈ G
a ◦ (a−1 ) = (a−1 ) ◦ a = e
6/28
Examples of groups
7/28
Additive group
• Consider the set Z6 = {0, 1, 2, 3, · · · , 5}. Is this an additive
group? i.e. under modulo 6 addition?
• Construct the Cayley table to represent the operation.
+ 0 1 2 3 4 5
0 0 1 2 3 4 5
1 1 2 3 4 5 0
2 2 3 4 5 0 1
3 3 4 5 0 1 2
4 4 5 0 1 2 3
5 5 0 1 2 3 4
• Closure?
• Associative?
• Is there an identity e such that for every a, e + a = a
• Is there an inverse of a, i.e (a−1 ) written as (−a) such that
for every a, (−a) + a = e
8/28
Application – Caesar’s cipher
9/28
Example
E.g. Use Key = 5
1. What is the ciphertext of P = “RETREAT”?
2. What is the plaintext of C=“ANHYTWD”
3. What is the keyspace of this cipher?
4. How many attempts are required at most to break this
cipher?
10/28
Vigenère Cipher
11/28
Vigenère cipher example
• Plaintext P = ATTACKATDAWN, can be written as
{0 19 19 0 2 10 0 19 3 14 22 13}.
• Key, Ks = CIPHER, written as {2 8 15 7 4 17}, m = 6
(length of Ks ),.
• Write out the plaintext and key (used twice) and do
addition (mod 26)
P 0 19 19 0 2 10 0 19 3 0 22 13
Ks 2 8 15 7 4 17 2 8 15 7 4 17
C 2 1 8 7 6 1 2 1 18 7 0 4
• The corresponding ciphertext is thus
C = CBIHGBCBSHAE
• Decryption using the same modulo 26 addition with −Ks
The original plaintext is recovered.
12/28
Multiplicative group
• Consider the set Z6 = {0, 1, 2, 3, · · · , 5}.
• Is this a group under multiplication mod 6?
× 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 1 2 3 4 5
2 0 2 4 0 2 4
3 0 3 0 3 0 3
4 0 4 2 0 4 2
5 0 5 4 3 2 1
• Closure?
• Associative?
• Is there an identity e such that for every a, i.e., e × a = a
• Is there an inverse of a, i.e (a−1 ) such that for every a, i.e.
a−1 × a = e
• For an element a ∈ Zm the inverse a−1 exist iff
gcd (a, m) = 1
• What are the elements in Z6 that form a multiplicative
group?
13/28
Affine cipher
• Let a, b, x, y ∈ Z26
Encryption key: k = (a, b), plaintext: x, ciphertext: y
Encryption yi ≡ a · xi + b mod m
Decryption xi ≡ a−1 · (yi − b) mod m
• Conditions: a−1 exists, i.e. gcd(a, 26) = 1
• Keyspace = 11 × 26 = 286
• Attacks: Brute force, frequency analysis
14/28
Affine cipher example
• Plaintext, x = AUTUMN, in ordinals, {0, 20, 19, 20, 12, 13},
• Let key be k = (a, b) where a = 5, b = 12. Note, 5−1 ≡ 21
mod 26 (it exists)
• Encrypt: e.g., y1 ≡ 0 × a + 12 = 12 mod 26, etc.
• Ciphertext, y = {12, 8, 3, 8, 20, 25}, i.e. y = MIDIUZ
• Decrypt: e.g., take y2 , I = 8,
x2 ≡ a−1 (y2 − b) = 21(8 − 12) = 20 mod 26, i.e. x2 = U
15/28
Integer Rings
• Definition: The integer ring is a set
Zm = {0, 1, 2, · · · , m − 1} with 2 operations "+" and "×" for
all a, b, c, d ∈ Zm such that:
1. a + b ≡ c mod m,
2. a × b ≡ d mod m
• Properties:
• Closure Add and multiply any two numbers, result is
always in the ring.
• Addition and multiplication is associative and distributive,
i.e., a + (b + c) = (a + b) + c and a × (b × c) = (a × b) × c
and a × (b + c) = (a × b) + (a × c)
• Addition: There is a neutral and inverse element i.e.
a + 0 = 0 + a = a mod m; a + (−a) = 0 mod m
• Multiplication: There is a neutral element and inverse exists
for some, but not all elements , i.e.
a × 1 = 1 × a = 1 mod m, and a × a−1 ≡ 1 mod m
• For an element a ∈ Zm the inverse a−1 exist iff
gcd(a, m) = 1
16/28
Fields
17/28
Hill Cipher
gcd(det(K ), 26) = 1
• Usually pad 3 more symbols and use mod 29, a prime
number.
18/28
Hill cipher example
• Consider the message M = “RETREAT”, i.e.
{17, 4, 19, 17, 4, 0, 19}, Key K = “CAVE”, i.e {2, 0, 21, 4}
• Use the prime field GF29 to represent the letters in the
alphabet, padded with unused symbols, e.g.
{26 =?, 27 = @, 28 = ∗}
• Construct padded message as (4 × 2) and key as (2 × 2)
C ≡ M· K mod 29, i.e.
matrices,
17 4 2 16
19 17 2 0 18 10
M= , K = C =
4 0 21 4 8 0
19 26 4 17
• C = CQSKIAER
19/28
Hill cipher example – contd
• Decryption: P ≡ C · K −1 mod 29,
1
• K−1 = det(K −1 exist as 29 is
) adj(K ) exists, since det(K )
prime, i.e.,
15 0
K −1 =
2 22
2 16 17 4
18 10 15 0 19 17
• Plaintext = 8 0 1 22
=
4 0
4 17 19 26
• What is the key space ?
20/28
Euclidean algorithm
• Uses:
1. test if a, m are relatively prime
2. find multiplicative inverse a−1
• Find gcd (r0 , r1 ), r0 > r1 , let gcd (r0 , r1 ) = g
i.e. r0 = g · x and r1 = g · y where x > y
write r0 = r1 + r2 , i.e. g · (x − y) = r2
i.e. since g divides r0 , r1 , then g also divides r2
• Since r2 < r0 , easier to find gcd (r2 , r1 ) i.e. gcd (r0 − r1 , r1 )
write r2 = r1 + r3 i.e r2 − r1 = r3
if g divides r1 , r2 , then it divides r3
hence recursively, gcd (r3 , r1 ) = gcd (r2 − r1 , r1 ) =
gcd (r0 − 2r1 , r1 ) = · · · = gcd (r0 − mr1 , r1 )
• choose maximum m for least number of steps.
• E.g. Find gcd (27, 21)
21/28
Eucliden algorithm example
• Another approach:
22/28
Extended Euclidean algorithm
r0 = k1 r1 + r2
xg = k1 yg + r2
• i.e. g divides r0 , r1 , r2 .
• Hence, instead of finding gcd (r0 , r1 ), find gcd (r1 , r2 ) since
r1 , r2 are smaller numbers.
• Proceed recursively until rn = 0
23/28
Extended Euclidean algorithm
• Example: find gcd (973, 301)
• Since,
973 = 3 × 301 + 70
301 = 4 × 70 + 21
70 = 3 × 21 + 7
21 = 3 × 7 + 0
• since 7 divides 21, 70, 301, and 973, then
gcd (973, 301) = 7
• We also see gcd (973, 301) = 7 and
7 = 70 − 3 × 21 = 70 − 3 · (301 − 4 × 70)
= 70 − 3 × 301 + 12 × 70 = 13 × 70 − 3 × 301
= 13 · (973 − 3 × 301) − 3 × 301 = 13 × 973 − 42 × 301
= s · 973 + t · 301, where s = 13, t = −42
24/28
Extended Euclidean algorithm example
67 = 5 × 12 + 7
Find gcd(12,7) instead
12 = 1 × 7 + 5
7 = 1×5+2
5 = 2 × 2 + 1 ← gcd (2, 1) = 1
2 = 2×1+0
25/28
Finding inverse, a−1 mod m
• Write: gcd (a, m) = s · a + t · m where s, t are integers
• If gcd (a, m) = 1, then 1 = s · a + t · m
Taking mod m, 1 ≡ s · a mod m
inverse of a mod m, is by definition, a−1 mod m = s
i.e. the inverse a−1 mod(m) exists iff gcd (a, m) = 1
• Use extended Euclidean algorithm in reverse to find s, t
26/28
Finding inverse
• E.g. Find the inverse 12−1 mod 67.
1 = 5−2×2
From finding gcd (67, 12) = 5 − 2(7 − 1 × 5)
= 3×5−2×7
67 = 5 × 12 + 7 = 3 × (12 − 7) − 2 × 7
12 = 1 × 7 + 5 = 3 × 12 − 5 × 7
7 = 1×5+2 = 3 × 12 − 5(67 − 5 × 12)
5 = 2×2+1 = 28 × 12 − 5 × 67
1 ≡ 28 × 12 mod 67
• i.e. 12−1 = 28
27/28
Summary
• Groups are sets which
1 has one operation,
2 is closed,
3 associative,
4 has a neutral element (identity) e, such that a ◦ e = a, has
inverse such the a ◦ a−1 = e
• For multiplicative group mod m, an inverse a−1 exist iff
gcd(a,m) = 1
?
• Euclidean algorithm used to test gcd (a, m) = 1
• Extended Euclidean algorithm used to find a mod m
−1
28/28