EECS 70 Discrete Mathematics and Probability TheorySpring 2014 Anant Sahai Note 5Modular ArithmeticIn several settings, such as error-correcting codes and cryptography, we sometimes wish to work over asmaller range of numbers. Modular arithmetic is useful in these settings, since it limits numbers to a prede-fined range {0,1,.. .,N − 1}, and wraps around whenever you try to leave this range — like the hand of aclock (where N = 12) or the days of the week (where N = 7).Example: Calculating the time When you calculate the time, you automatically use modular arithmetic.For example, if you are asked what time it will be 13 hours from 1 pm, you say 2 am rather than 14.Let’s assume our clock displays 12 as 0. This is limiting numbers to a predefined range, {0, 1,2,. .., 11}.Whenever you add two numbers in this setting, you divide by 12 and provide the remainder as the answer.If we wanted to know what the time would be 24 hours from 2 pm, the answer is easy. It would be 2 pm.This is true not just for 24 hours, but for any multiple of 12 hours. What about 25 hours from 2 pm? Sincethe time 24 hours from 2 pm is still 2 pm, 25 hours later it would be 3 pm. Another way to say this is thatwe add 1 hour, which is the remainder when we divide 25 by 12.This example shows that under certain circumstances it makes sense to do arithmetic within the confinesof a particular number (12 in this example). That is, we only keep track of the remainder when we divideby 12, and when we need to add two numbers, instead we just add the remainders. This method is quiteefficient in the sense of keeping intermediate values as small as possible, and we shall see in later notes howuseful it can be.More generally we can define x mod m (in words x modulo m) to be the remainder r when we divide x bym. i.e. if x mod m = r, then x = mq + r where 0 ≤ r ≤ m − 1 and q is an integer. Thus 5 = 29 mod 12 and3 = 13 mod 5.ComputationIf we wish to calculate x+y mod m, we would first add x+y and the calculate the remainder when we dividethe result by m. For example, if x = 14 and y = 25 and m = 12, we would compute the remainder when wedivide x +y = 14 + 25 = 39 by 12, to get the answer 3. Notice that we would get the same answer if we firstcomputed 2 = x mod 12 and 1 = y mod 12 and added the results modulo 12 to get 3. The same holds forsubtraction: x − y mod 12 is −11 mod 12, which is 1. Again, we could have directly obtained this as 2 − 1by first simplifying x mod 12 and y mod 12.This is even more convenient if we are trying to multiply: to compute xy mod 12, we could first computexy = 14 × 25 = 350 and then compute the remainder when we divide by 12, which is 2. Notice that we getthe same answer if we first compute 2 = x mod 12 and 1 = y mod 12 and simply multiply the results modulo12.More generally, while carrying out any sequence of additions, subtractions or multiplications mod m, weget the same answer even if we reduce any intermediate results mod m. This can considerably simplify thecalculations.EECS 70, Spring 2014, Note 5 1Set RepresentationThere is an alternate view of modular arithmetic which helps understand all this better. For any integer mwe say that x and y are congruent modulo m if they differ by a multiple of m, or in symbols,x = y mod m ⇔ m divides (x − y).Note that you may also see this written as x ≡ y mod m. For example, 29 and 5 are congruent modulo 12because 12 divides 29 − 5. We can also write 22 = −2 mod 12. Notice that x and y are congruent modulo miff they have the same remainder modulo m.What is the set of numbers that are congruent to 0 mod 12? These are all the multiples of 12:{... ,−36,−24, −12,0,12, 24,36,. ..}. What about the set of numbers that are congruent to 1 mod 12?These are all the numbers that give a remainder 1 when divided by 12: {.. .,−35, −23,−11,1,13,25,37,...}.Similarly the set of numbers congruent to 2 mod 12 is {... ,−34,−22, −10,2,14, 26,38,. ..}. Notice in thisway we get 12 such sets of integers, and every integer belongs to one and only one of these sets.In general if we work modulo m, then we get m such disjoint sets whose union is the set of all integers. Wecan think of each set as represented by the unique element it contains in the range (0,. .., m − 1). The setrepresented by element i would be all numbers z such that z = mx + i for some integer x. Observe that all ofthese numbers have remainder i when divided by m; they are therefore congruent modulo m.We can understand the operations of addition, subtraction and multiplication in terms of these sets. Whenwe add two numbers, say x = 2 mod 12 and y = 1 mod 12, it does not matter which x and y we pick from thetwo sets, since the result is always an element of the set that contains 3. The same is true about subtractionand multiplication. It should now be clear that the elements of each set are interchangeable when computingmodulo m, and this is why we can reduce any intermediate results modulo m.Here is a more formal way of stating this observation:Theorem 5.1: If a = c mod m and b = d mod m, then a + b = c + d mod m and a · b = c · d mod m.Proof: We know that c = a + k · m and d = b + ` · m, so c + d = a + k · m + b + ` · m = a + b + (k + `) · m,which means that a + b = c + d mod m. The proof for multiplication is similar and left as an exercise. 2What this theorem tells us is that we can always reduce any arithmetic expression modulo m into a naturalnumber smaller than m. As an example, consider the expresion (13 + 11) · 18 mod 7. Using the aboveTheorem several times we can write:(13 + 11) · 18 = (6 + 4) · 4 mod 7= 10 · 4 mod 7= 3 · 4 mod 7= 12 mod 7= 5 mod 7.In summary, we can always do basic arithmetic (multiplication, addition, subtraction, and division) calcula-tions modulo m by reducing intermediate results modulo m.ExponentiationAnother standard operation in arithmetic algorithms (this is used heavily in primality testing and RSA) israising one number to a power modulo another number. I.e., how do we compute xymod m, where x, y,mEECS 70, Spring 2014, Note 5 2are natural numbers and …
View Full Document