Modular Arithmetic
Let’s learn about the modular arithmetic that is used in many different cryptographic primitives, particularly public-key algorithms such as RSA.
Motivation
Modular arithmetic is a familiar idea dressed in mathematical terminology. This is best illustrated by several examples from everyday use.
Days of the week
When we work out what day of the week something will happen on, we often (unconsciously) make mental calculations such as ‘two days after Tuesday is Thursday.’ We could write this in a pseudo-mathematical way as follows:
When such a calculation takes us beyond the end of a particular week, we will make statements such as ‘three days after Friday is Monday.’ Although this is Monday of the following week, this does not cause us any problem since we treat all Mondays as the same for this purpose. So:
Similarly, we can make statements such as:
We can also say:
We can restate this simple idea by now replacing the days of the week, starting with Monday, by the numbers 0 to 6 (so Monday is 0, Tuesday is 1, and Sunday is 6). It is now possible to write all our previous pseudo-mathematical equations as mathematical equations. In other words:
Computing the days of the week in this manner is an example of modulo 7 arithmetic (often abbreviated as mod 7). It’s like normal arithmetic, except we ‘wrap back around’ when we reach the number 7 by treating 7 as the beginning again (starting back at 0).
Months of the year
Another example of modular arithmetic is when we calculate the months of the year. When we try to work out what month of the year an event will occur, we often make calculations such as ‘three months after January is April’. We can write this as:
Similarly, ‘four months after October is February’ is:
By replacing January to December with the numbers 0 to 11, we can write these calculations as:
This is modulo 12 arithmetic. There are many other examples of this type of ‘unconscious’ use of modular arithmetic. For example, the 24-hour clock provides an example of modulo 24 arithmetic.
Modular numbers
We can perform modular arithmetic using any positive integer as the modulus, where a positive integer means a whole number bigger than 0 (a number such as 2, 18, 754, etc.). When working modulo (mod ), where is some positive integer, which we call the modulus, the only numbers we deal with are:
So, instead of having numbers that go on forever, when working modulo , we only have n different numbers to work with. For example, mod 6 arithmetic only uses the numbers 0, 1, 2, 3, 4, and 5, and mod 359 arithmetic only uses the numbers 0, 1, 2, 3, …, 355, 356, 357, 358. Using modular arithmetic, we will later see that we can add, subtract, and multiply numbers. So long as the answer is less than the modulus, these operations are just the same as if no modulus was used. For example, when working modulo 358, 57 + 101 = 158. However, we need to consider what happens when the answer takes us beyond the modulus value.
Adding multiples of the modulus
First, let’s consider what happens if we add two numbers using modulo and end up with itself. For example, when working modulo 7, what happens when we compute 3 + 4? We know 3 + 4 = 7, but in modulo 7 arithmetic, there is no number 7.
Recall the example based on days of the week. When working modulo 7, we see that asking ‘what is 3 + 4?’ in modulo 7 arithmetic is equivalent to asking ‘what day comes four days after Thursday?’ The answer is Monday, which is day 0, so:
The ‘mod 7’ at the end of the line indicates that we are performing the calculation modulo 7. Unless it is clear from the context, it is important to include this indicator since the answer to ‘what is 3 + 4?’ depends very much on what modulus we are working with.
The important lesson is that when working modulo , adding to a number will not change it. For the days of the week example, if we calculate seven days after Tuesday, it is Tuesday again:
In fact, 14 days after Tuesday, it is Tuesday once again:
Similarly, seven days before Tuesday is also Tuesday:
So, in modulo arithmetic, the number n behaves just like 0 does for the numbers we normally use. This is an important principle of modular arithmetic.
One number modulo another
Consider two numbers and , where and are both positive integers. Although is a positive integer, let’s suppose we want to know what value would take if we considered it a number modulo . This question is often phrased as ‘what is the value of modulo ?’
It is important to recognize that there will be an answer to this question. This is because any number can be expressed uniquely modulo any other number. Since we have just seen that in modulo arithmetic, all multiples of are 0, the answer to our question is that will take the value that is the remainder when we divide by .
To see a simple example, let = 6 and = 5. We know 5 is the same as 0 in modulo 5 arithmetic. Since 6 is just one more than 5, 6 will be the same as 1 in modulo 5 arithmetic. This is just another way of saying that when we divide 6 by 5, we get a remainder of 1. Thus, 6 is equal to 1 when 6 is considered a number modulo 5. In other words:
Similarly, let = 5417 and = 7. We can divide 5417 by 7 using traditional arithmetic to discover that:
Since we know 7 = 0 when working modulo 7, it follows that will also be 0. Thus, 5,417 is equal to 6 when we work modulo 7, which we write as:
Returning to our analogy, if we ask, ‘what day of the week is it 5,417 days after Wednesday?’ we now know that in 5,417 days, it will be 6 days after Wednesday, so it will be Tuesday!
We will also need the following idea later. Suppose we know that a positive integer takes the value modulo . In other words, we know that when we divide by , we get the remainder . This tells us that, for some number us it is possible to write as:
We do not necessarily know what is, but there must be a for which this is true. Returning to our previous example, knowing that 5,417 takes the value 6 modulo 7 allows us to claim that:
The claim given above is for some number (in fact, it is easy to work out, in this case, that = 773).
Terminology and notation
We have kept our terminology as simple as possible. However, other texts may explain modular arithmetic using slightly different language. For example, calculating one number modulo another is sometimes called modular reduction. Thus, we might say that 5,417 reduces to 6 modulo 7. Also, just to make sure it is recognized that a modular reduction has taken place, often the symbol ≡ is used instead of =. Thus, we would write 5417 ≡ 6 mod 7 rather than 5417 = 6 mod 7. This idea is often expressed by stating that 5,417 is congruent to 6 modulo 7.
Note: While 5,417 is congruent to 6 mod 7, it is also congruent to 13 mod 7 and to 20 mod 7. It is congruent to any multiple of 7 with 6 added. However, when asked ‘what number is congruent to 5,417 mod 7?’ there will only be one answer lying between 0 and 6. This is the default answer and usually the most useful one.
Negative modular numbers
We can also work with negative numbers in modular arithmetic. These behave in almost the same way. To start, we can regard any negative multiple of the modulus as equal to 0 modulo . In other words, -7 and -14 are equal to 0 modulo 7. More generally, any negative integer can be uniquely expressed as a number modulo . For example, let’s suppose that we wish to compute -17 mod 10. We divide -17 by 10 and take the remainder. When we do this, we need a positive remainder because our answer modulo 10 must be one of the numbers between 0 and 9. So, we see that:
In other words:
Modular arithmetic operations
We’ll now consider computing basic arithmetic operations modulo .
Addition, subtraction, and multiplication
The good news is that the basic arithmetic operations of addition, subtraction, and multiplication behave just as we would expect them to. In other words, we can add numbers, subtract them, or multiply them, just as we would do with ‘normal’ numbers. We just need to remember to reduce the answer modulo . For example:
Importantly, the division is intentionally left out of this list because it does not work in the same way for normal numbers.
Modular reduction: before or after?
One issue worth clarifying when doing a calculation such as 15 + 20 mod 6 is whether we should reduce 15 and 20 modulo 6 before or after they have been added together. Fortunately, it does not matter since we will still get the same answer.
This is best seen by example:
-
Add then reduce: We first compute and then note . So .
-
Reduce then add: We first compute , and then . Now we add the answers, in other words: .
Get hands-on with 1400+ tech skills courses.