NOTE: This is the archived 2022-23 version of these notes, and may be out of date. Current CSC110/111 students should visit the current notes page, https://www.teach.cs.toronto.edu/~csc110y/fall/notes/.

7.4 Modular Arithmetic

In this section, we’ll explore some properties of modular arithmetic that will be useful in the next chapter, when we study cryptographic algorithms based on modular arithmetic. First, recall the definition of modular equivalence from 7.1 Introduction to Number Theory.

Let a,b,nZ, and assume n0. We say that a is equivalent to b modulo n when nab. In this case, we write ab(modn).

This definition captures the idea that a and b have the same remainder when divided by n. You should think of this congruence relation as being analogous to numeric equality, with a relaxation. When we write a=b, we mean that the numeric values of a and b are literally equal. When we write ab(modn), we we mean that if you look at the remainders of a and b when divided by n, those remainders are literally equal.

We will next look at how addition, subtraction, and multiplication all behave in an analogous fashion under modular arithmetic. The following proof is a little tedious because it is calculation-heavy; the main benefits here are practicing reading and using a new definition, and getting comfortable with this particular notation.

For all a,b,c,d,nZ, if n0, ac(modn), and bd(modn), then:

  1. a+bc+d(modn)
  2. abcd(modn)
  3. abcd(modn)

We will only show how to translate and prove (2), and leave (1) and (3) as exercises. a,b,c,d,nZ, (n0(nac)(nbd))n(ab)(cd).

Let a,b,c,d,nZ. Assume that n0, nac, and that nbd. This means we want to prove that n(ac)(bd).

By the Divisibility of Linear Combinations Theorem, since n(ac) and n(bd), it divides their difference:

n(ac)(bd)n(ab)(cd)

Modular division

The above example stated that addition, subtraction, and multiplication all preserve modular equivalence—but what above division? The following statement is a “divide by k” property, but is actually False: A good exercise is to disprove this statement! a,b,k,nZ, n0akbk(modn)ab(modn)

For the real numbers, division xy has a single “gap”: division by y is undefined when y=0. As we’ll see in the next theorem, division in modular arithmetic has many such “gaps”, but we can predict exactly where these gaps will occur.

(Modular inverse) Let nZ+ and aZ. If gcd(a,n)=1, then there exists pZ such that ap1(modn). (We say that p is a modular inverse of a modulo n.)

nZ+,aZ, gcd(a,n)=1(pZ, ap1(modn))

Let nZ+ and aZ. Assume gcd(a,n)=1.

Since gcd(a,n)=1, by the GCD Characterization Theorem we know that there exist integers p and q such that pa+qn=gcd(a,n)=1.

Rearranging this equation, we get that pa1=qn, and so (by the definition of divisibility, taking k=q), npa1.

Then by the definition of modular equivalence, pa1(modn).

From this theorem about modular inverses, we can build up a form of division for modular arithmetic. To gain some intuition, first think about division ab as the solution to an equation of the form ax=b. We’ll turn this into a statement about modular equivalence now.

Let nZ+ and aZ. If gcd(a,n)=1, then for all bZ, there exists kZ such that akb(modn).

This statement is quite complex! Remember that we focus on translation to examine the structure of the statement, so that we know how to set up a proof. We aren’t going to expand every single definition for the sake of expanding definitions.

nZ+, aZ, gcd(a,n)=1(bZ, kZ, akb(modn)).

So this is saying that under the given assumptions, b is “divisible” by a modulo n. This comes after the theorem about modular inverses, so that should be useful. The conclusion is “there exists a kZ such that…” so that I know that at some point I’ll need to define a variable k in terms of a, b, and/or n, which satisfies the modular equivalence statement.

I notice that the hypothesis here (gcd(a,n)=1) matches with the hypothesis from the previous theorem, so that seems to be something I can use. That gives me a pZ such that ap1(modn)

Wait, I can multiply both sides by b, right?!

Let nZ+ and aZ. Assume gcd(a,n)=1, and let bZ. We want to prove that there exists kZ such that akb(modn).

First, using the previous Modular Inverses theorem, since we assmed gcd(a,n)=1, we know that there exists pZ such that ap1(modn).

Second, we know from statement (3) of our first example above that multiplication preserves modular equivalence, and so we know apbb(modn).

Then we let k=pb, and we have that akb(modn).

These two theorems bring together elements from all of our study of proofs so far. We have both types of quantifiers, mixed with a larger implication. We used the GCD Characterization Theorem for a key step in our proof. This illustrates the power of separating ideas into different statements and using each one to prove the next, just like we separate code into different functions in our programs!