Modular Arithmetic in Cryptology
Modular Arithmetic in Cryptology
Examples
17 mod 5 = 2 7 mod 11 = 7
20 mod 3 = 2 11 mod 11 = 0
-3 mod 11 = 8 -1 mod 11 = 10
25 mod 5 = 0 -11 mod 11 = 0
• Two numbers (a ) and (b ) are said to be
“congruent modulo n” if
(a mod n) = (b mod n) a ≡ b(mod n)
• The difference between a and b will be a
multiple of n
So ( a-b = kn ) for some value of k
Example
4 9 1419 -1 -6 mod 5
73 4(mod 23); 21 -9(mod 10)
If a 0 (mod n), then n|a.
Properties of Congruences
1. a b (mod n) if n|(a-b)
2. a b (mod n) implies b a (mod n)
3. a b (mod n) and b c (mod n) imply a c (mod n)
Proof of 1.
If n|(a-b), then (a-b) = kn for some k. Thus, we can write
a = b + kn. Therefore,
(a mod n) = (remainder when b + kn is divided by n) =
(remainder when b is divided by n) = (b mod n).
Examples
23 8 (mod 5) because 23 -8 =15 = 5x3
-11 5 (mod 8) because -11-5 =-16 = 8x(-2)
81 0 (mod 27) because 81-0=81 = 27x3
Properties of Modular Arithmetic
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
Proof of 1.
Let (a mod n) = Ra and (b mod n) = Rb. Then, we can write
a = Ra + jn for some integer j and b = Rb + kn for some integer k.
(a + b) mod n = (Ra + jn + Rb + kn) mod n
= [Ra + Rb + (k + j) n] mod n
= (Ra + Rb) mod n
= [(a mod n) + (b mod n)] mod n
Examples
11 mod 8 = 3; 15 mod 8 = 7
[(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
Exponentiation
• Exponentiation is done by repeated
multiplication, as in ordinary arithmetic.
7
To find (11 mod13) do the followings
11 121 4(mod13)
2
11 (11 ) 4 3(mod13)
4 2 2 2
Z n {0,1,..., (n 1)}
This set is referred to as the set of residues, or
residue classes (mod n). That is, each
integer in Zn represents a residue class.
Properties of Modular Arithmetic
We can label the residue classes (mod n) as:
[0],[1],[2],...,[n-1], where
[r] = {a: a is an integer, a ≡ r (mod n)}.
Example:
The residue classes (mod 4) are
[0] = {..., -16,-12,-8,-4,0,4,8,12,16, ...}
[1] = {..., -15,-11,-7,-3,1,5,9,13,17, ...}
[2] = {..., -14,-10,-6,-2,2,6,10,14,18, ...}
[3] = {..., -13,-9,-5,-1,3,7,11,15,19, ...}
Properties of Modular Arithmetic
Property Expression
Cummitative Laws (w + x) mod n = (x + w) mod n
(w x x) mod n = (x x w) mod n
Associative Laws [(w + x) + y] mod n = [w + (x + y)] mod n
[(w x x) x y] mod n = [w x (x x y)] mod n
Distributive Law [w x (x + y)] mod n = [(w x x) + (w x y)] mod n
Identities (0 + w) mod n = w mod n
(1 x w) mod n = w mod n
Additive Inverse (-w) For each w Zn, there exists a z such that w + z ≡ 0 mod n
Modular Arithmetic
Step 1) 1 = 4 – (1 * 3) = 4 – 3
Step 2) 1 = 4 – (11 – (2 * 4)) = 3 * 4 - 11
Step 3) 1 = 3 * (15 – 11) – 11 = 3 * 15 – 4 * 11
Step 4) 1 = 3 * 15 – 4(26 – (1*15)
Step 5 ) 1 = 7 * 15 – 4 * 26 = 105 – 104 >>check
Modular Arithmetic
• Finding Inverses in Zn
– So, what is the inverse of 15 in mod 26?
– 1 = 7 * 15 – 4 * 26 converts to
– 1 7 * 15 mod 26
– 7 is the inverse of 15 in mod 26
– Can you use the same result to show that 11
is its own inverse in mod 15?
Modular Arithmetic
• Using the Extended Euclidean Algorithm
– Formalizing the backward steps we get this
formula:
• y0 = 0
• y1 = 1
• yi = (yi-2 – [yi-1 * qi-2]); i > 1
– Related to the “Magic Box” method
Modular Arithmetic
Step 0 26 = 1 * 15 + 11 y0 = 0
Step 1 15 = 1 * 11 + 4 y1 = 1