Modular Arithmetic I (Discrete Math)
Modular Arithmetic I (Discrete Math)
Lecture 9: Oct 4
This Lecture
Modular arithmetic is an arithmetic about remainders. It is very useful in coding theory and cryptography. In this lecture we will focus on additions and multiplications, while in the next lecture we will talk about divisions. This lecture is short. We will talk about:
Basic rule of modular addition and modular multiplication Applications: Fast exponentiation and fast division test
Given any b, we can divide the integers into many blocks of b numbers. For any a, there is a unique position for a in this line. q = the block where a is in r = the offset in this block
-b
2b
kb
a (k+1)b
Modular Arithmetic
Def: a | b (mod n) iff n|(a - b) iff a mod n = b mod n.
Be careful, a mod n means the remainder when a is divided by n. a | b (mod n) means a and b have the same remainder when divided by n. e.g. 12 | 2 (mod 10) 107 | 207 (mod 10) 7 | 3 (mod 2) 7 | -1 (mod 2) 13 | -1 (mod 7) -15 | 10 (mod 5) Fact: a | a mod n (mod n) 12 mod 10 = 2 207 mod 10 = 7 7 mod 2 = 1 -1 mod 2 = 1 -1 mod 7 = 6 -15 mod 5 = 0 as a and a mod n have the same remainder mod n
Modular Addition
Lemma: If a | c (mod n), and b | d (mod n) then a+b | c+d (mod n).
When you try to understand a statement like this, first think about the familiar cases, e.g. n=10 or n=2. When n=2, it says that if a and c have the same parity, and b and d have the same parity, then a+b and c+d have the same parity. When n=10, it says that if a and c have the same last digit, and b and d have the same last digit, then a+b and c+d have the same last digit. And the lemma says that the same principle applied for all n.
Modular Addition
Lemma: If a | c (mod n), and b | d (mod n) then a+b | c+d (mod n).
Example 1 13 | 1 (mod 3), 25 | 1 (mod 3) => 12 + 25 (mod 3) | 1 + 1 (mod 3) | 2 (mod 3) Example 2 87 | 2 (mod 17), 222 | 1 (mod 17) => 87 + 222 (mod 17) | 2 + 1 (mod 17) | 3 (mod 17) Example 3 101 | 2 (mod 11), 141 | -2 (mod 11) => 101 + 141 (mod 11) | 0 (mod 11) In particular, when computing a+b mod n, we can first replace a by a mod n and b by b mod n, so that the computation is faster.
Modular Addition
Lemma: If a | c (mod n), and b | d (mod n) then a+b | c+d (mod n). Proof
a | c (mod n) => a = c + nx for some integer x b | d (mod n) => b = d + ny for some integer y To show a+b | c+d (mod n), it is equivalent to showing that n | (a+b-c-d). Consider a+b-c-d. a+b-c-d = (c+nx) + (d+ny) c d = nx + ny. It is clear that n | nx + ny. Therefore, n | a+b-c-d. We conclude that a+b | c+d (mod n).
Modular Multiplication
Lemma: If a | c (mod n), and b | d (mod n) then ab | cd (mod n).
Example 1 9876 | 6 (mod 10), 17642 | 2 (mod 10) => 9876 * 17642 (mod 10) | 6 * 2 (mod 10) | 2 (mod 10) Example 2 10987 | 1 (mod 2), 28663 | 1 (mod 2) => 10987 * 28663 (mod 2) | 1 (mod 2) Example 3 1000 | -1 (mod 7), 1000000 | 1 (mod 7) => 1000 * 1000000 (mod 7) | -1 * 1 (mod 7) | -1 (mod 7) In particular, when computing ab mod n, we can first replace a by a mod n and b by b mod n, so that the computation is faster.
Modular Multiplication
Lemma: If a | c (mod n), and b | d (mod n) then ab | cd (mod n). Proof
a | c (mod n) => a = c + nx for some integer x b | d (mod n) => b = d + ny for some integer y To show ab | cd (mod n), it is equivalent to showing that n | (ab-cd). Consider ab-cd. ab-cd = (c+nx) (d+ny) cd = cd + dnx + cny + n2xy cd = n(dx + cy + nxy). It is clear that n | n(dx + cy + nxy). Therefore, n | ab-cd. We conclude that ab | cd (mod n).
This Lecture
Basic rule of modular addition and modular multiplication Applications: Fast exponentiation and fast division test
Fast Exponentiation
20736 * 20736 mod 713
1444
mod 713
= 144 * 144 * 144 * 144 mod 713 = 20736 * 144 * 144 mod 713 = 59 * 144 * 144 mod 713 = 8496 * 144 mod 713 = 653 * 144 mod 713 = 94032 mod 713 = 629 mod 713
Repeated Squaring
1442 mod 713 = 59
Note that 50 = 32 + 16 + 2 14450 mod 713 = 14432 14416 1442 mod 713 = 648 485 59 mod 713 = 242
1444 mod 713 = 1442 1442 mod 713 = 59 59 mod 713 = 629 1448 mod 713 = 1444 1444 mod 713 = 629 629 mod 713 = 639 14416 mod 713 = 1448 1448 mod 713 = 639 639 mod 713 = 485 14432 mod 713 = 14416 14416 mod 713 = 485 485 mod 713 = 648
Suppose we are given the decimal representation of a big number N. To test if N is divisible by a small number n, of course we can do a division to check. But can we do faster? If n = 2, we just need to check whether the last digit of N is even or not. If n = 10, we just need to check whether the last digit of N is 0 or not. If n = 5, we just need to check whether the last digit of N is either 5 or 0 or not. What about when n=3? When n=7? When n=11?
Example 1. 9333234513171 is divisible by 9. 9+3+3+3+2+3+4+5+1+3+1+7+1 = 45 is divisible by 9. Example 2. 128573649683 is not divisible by 9. 1+2+8+5+7+3+6+4+9+6+8+3 = 62 is not divisible by 9.
= (di) (10 mod 9) (10 mod 9) (10 mod 9) mod 9 i terms = (di) (1 mod 9) (1 mod 9) (1 mod 9) mod 9 = di mod 9
= (dk10k mod 9 + dk-110k-1 mod 9 + + d110 mod 9 + d0 mod 9) mod 9 = (dk mod 9 + dk-1 mod 9 + + d1 mod 9 + d0 mod 9) mod 9 = (dk + dk-1 + + d1 + d0) mod 9 By previous slide
Quick Summary
Need to know how to apply the basic rules effectively. Understand the principle of fast division tests. Repeated squaring will be useful later.