Modular Arithmetic
Modular Arithmetic
Modular Arithmetic
7 mod 11 = 7
11 mod 11 = 0
-1 mod 11 = 10
-11 mod 11 = 0
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
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
11 (11 ) 4 3(mod13)
4
2 2
11 11 4 3 132 2(mod13)
7
Modular Division
What is 5 3 mod 11?
We need to multiply 5 by the inverse of 3 mod 11
When you multiply a number by its inverse, the answer is
1.
Thus the inverse of 2 is since 2* = 1
The inverse of 3 mod 11 is 4 since 3*4=1 mod 11
Thus 5 3 mod 11 = 5*4 mod 11 = 9 mod 11
Euclidean algorithm
gcd(a,b) = gcd(b, b mod a)
int Euclid(int a, int b) {
if (b == 0) return a;
else return Euclid(b, b % a)
}
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.
Expression
Cummitative Laws
(w + x) mod n = (x + w) mod n
(w x x) mod n = (x x w) mod n
Associative Laws
Distributive Law
Identities
(0 + w) mod n = w mod n
(1 x w) mod n = w mod n
Modular Arithmetic
A Multiplication Table in Zn: Summary
The numbers that have inverses in Zn are
relatively prime to n
That is: gcd(x, n) = 1
Modular Arithmetic
A Multiplication Table in Zn: Summary
The results have implications for division:
Some divisions have no answers
3 * x = 2 mod 6 has no solutions => 2/3 has no equivalent
in Z6
Modular Arithmetic
A Multiplication Table in Zn: Summary
The results have implications for division:
Zero divisors exist in some mods:
3 * 2 = 0 mod 6 => 0/3 = 2 and 0/2 = 3 in mod 6
3 * 6 = 0 mod 9 => 0/3 = 6 and 0/6 = 3 in mod 9
Modular Arithmetic
Finding Inverses in Zn
The numbers that have inverses in Zn are
relatively prime to n
We can use the Euclidean Algorithm to see if a
given x is relatively prime to n; then we
know that an inverse does exist.
How can we find the inverse without looking at
all the remainders? A problem for large n.
Modular Arithmetic
Finding Inverses in Zn
The numbers that have inverses in Zn are
relatively prime to n
We can use the Euclidean Algorithm to see if a
given x is relatively prime to n; then we
know that an inverse does exist.
How can we find the inverse without looking at
all the remainders? A problem for large n.
Modular Arithmetic
Finding Inverses in Zn
What is the inverse of 15 in mod 26?
First use the Euclidean Algorithm to determine
if 15 and 26 are relatively prime
26
15
11
4
3
= 1 * 15 + 11
= 1 * 11 + 4
=2*4
+3
= 1*3
+1
=3*1 +0
Modular Arithmetic
Finding Inverses in Zn
What is the inverse of 15 in mod 26? Now we
now they are relatively prime so an inverse
must exist.
We can use the algorithm to work backward to
create 1 (the gcd(26, 15)) as a linear
combination of 26 and 15:
1 = x * 26 + y * 15
Modular Arithmetic
Finding Inverses in Zn
Convert 1 = x * 26 + y * 15 to mod 26 and we
get:
1 mod 26 (y * 15) mod 26
Then if we find y we find the inverse of 15 in
mod 26.
So we start from 1 and work backward
Modular Arithmetic
26
15
11
4
= 1 * 15 + 11 => 11 = 26 (1*15)
= 1 * 11 + 4
=> 4 = 15 (1*11)
=2*4
+ 3 => 3 = 11 (2*4)
= 1*3
+1
=> 1 = 4 (1*3)
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
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
Modular Arithmetic
Step 0
26 = 1 * 15 + 11
y0 = 0
Step 1
15 = 1 * 11 + 4
y1 = 1
Step 2
11 = 2 * 4 + 3
Step 3
4=1*3+1
Step 4
3=3*1+0
Step 5
Note: qi is in red
above
Modular Arithmetic
Using the Extended Euclidean Algorithm
y0 = 0
y1 = 1
yi = (yi-2 [yi-1 * qi-2]); i > 1
Try it for
13 mod 22
17 mod 97
Modular Arithmetic
Using the Extended Euclidean Algorithm
22 = 1 * 13 + 9
13 = 1 * 9 + 4
9=2*4+1
4=4*1+0
Last Step :
y[0]=0
y[1]=1
y[2]=0 - 1 * 1 mod 22 = 21
y[3]=1 - 21 * 1 mod 22 = 2
y[4]=21 - 2 * 2 mod 22 = 17
Modular Arithmetic
Using the Extended Euclidean Algorithm
97 = 5 * 17 + 12
17 = 1 * 12 + 5
12 = 2 * 5 + 2
5=2*2+1
2=2*1+0
Last Step:
x[0]=0
x[1]=1
x[2]=0 - 1 * 5 mod 97 = 92
x[3]=1 - 92 * 1 mod 97 = 6
x[4]=92 - 6 * 2 mod 97 = 80
x[5]=6 - 80 * 2 mod 97 = 40
Modular Arithmetic
Some good sources on the internet:
http://www.antilles.k12.vi.us/math/cryptotut/e
xtended_euclidean_algorithm.htm
http://www.math.umbc.edu/~campbell/NumbTh
y/Class/BasicNumbThy.html
http://www.promys.org/pft/pcmi/PCMI_01.pdf