Modular Inverses
Note
This post assumes a basic knowledge of modular arithmetic and the notation for it. In this article, we will use the following notation to denote as the remainder when is divided by .
We will also less frequently use the notation to denote the same thing.
Division in Modular Arithmetic
For modular arithmetic, the operations of addition, subtraction, and multiplication are pretty easy to define and understand. You simply perform the operation and then perform the modulo operator on the output.
However, this doesn't apply to division, since division frequently leads to fractions/decimals, which is not what we are looking for when considering remainders. However, what if we want to solve this problem:
In typical arithmetic, we would divide both sides by to solve for , but that doesn't work here since doesn't make sense as a remainder for after division if we assume that must be a whole number.
Divison is a pretty weird thing in modular arithmetic, although it can be applied in some cases. We actually can do division in modular arithmetic with respect to to solve the equation under a few conditions:
- Both sides of the equation are divisible by the divisor.
- The divisor is coprime with .
Condition (1) Met | Condition (1) Not Met | |
---|---|---|
Condition (2) Met | You can divide safely | You have to use modular inverses |
Condition (2) Not Met | You have to divide everything by | The equation has no solutions |
Here are examples of equations that would belong in each box:
Condition (1) Met | Condition (1) Not Met | |
---|---|---|
Condition (2) Met | ||
Condition (2) Not Met |
For instance, the given bottom right equation has no integer solutions. For the upper right equation, we can divide both sides by 3 to solve:
We will primarily go over the upper right condition in this article. At the end, we will briefly discuss the lower left condition. The other two conditions are easily solved.
Modular Inverse Basics
Definition
The modular inverse of a number is defined as such that
If we knew the modular inverse of with respect to , we could multiply that on both sides of our equation to cancel out the and obtain our whole number answer.
There are many ways we can find modular inverses of a number, and the most simple of these is to simply try every possible number from to . For instance, we could calculate for all values from to .
In this case, we can relatively quickly find that the modular inverse of is since , which is above a multiple of . Therefore, we can solve our equation as follows:
Therefore, we can conclude that all numbers that leave a remainder of when divided by satisfy the original equation.
However, not all numbers have a modular inverse. For instance, does not have a modular inverse with respect to . You can try it out and you will quickly see that there are no values such that .
Theorem
The modular inverse of with respect to only exists if and share no common factors besides or, more formally, .
We can prove this by first assuming that there exists a number and its modular inverse with respect to such that . Then, we can write the following equation:
This is equivalent to writing the following equation for some integer :
We can rearrange this to obtain the following:
However, note that we previously assumed that , but that implies that the left hand side of the above equation has the common factor . However, the only integer factors of the right hand side are , making this statement impossible. Therefore, we have reached a contradiction and we can conclude that there exists no modular inverse of a number if .
Calculating Modular Inverses with Euler's Theorem
While we introduced the brute-force method above for calculating modular inverses, we can actually solve for the modular inverse directly with Euler's theorem.
Theorem
If and are coprime, then the following is true:
where is the Euler totient function and is defined as the number of positive integers such that and are coprime. If is prime, then . If is not prime and has the prime factorization
then can be calculated as
By cleverly using this fact, we can actually calculate the modular inverse of as follows:
By substituting the second equation into the first one, we obtain the following relation:
In this case, we actually divided both sides by , which is valid since and are coprime (if there exists a modular inverse) and both sides were divisible by .
By using this fact, we can actually calculate directly the modular inverse. For instance, if we wanted to calculate without brute force, we could do the following:
However, as you can probably guess, this method, although direct, can require some intense calculation with very large powers for large . However, for small , it can be much faster, especially when used for calculating several modular inverses with respect to . Additionally, this method is also much more reasonable when done using a computer, which can churn these out very quickly. When combined with binary exponentiation, calculating large exponents is not that daunting.
However, there is another method that can be used to calculate modular inverses, which we will discuss next.
Calculating Modular Inverses with Extended Euclidean Algorithm
Another method of calculating modular inverses that doesn't require huge exponents is with the extended euclidean algorithm.
Extended Euclidean Algorithm
You may have heard of the Euclidean algorithm, which is a computational method for determining the of two numbers. For those who haven't or need a quick refresher, it is described below.
Theorem
Let the two numbers be and , with . If , then the Otherwise,
As an example, we can compute .
The extended Euclidean algorithm is a bit different, although it also calculates the .
Theorem
Bézout's identity states there always exists integers and such that the following is true:
Given the numbers and , the extended Euclidean algorithm gives us a method to calculate both the coefficients and given in Bézout's identity, as well as . This is why it's called the extended Euclidean algorithm. While the normal Euclidean algorithm just gives us , the extended version also tells us how to form that through a linear combination of and .
To show how it works, we can use an example with and . As mentioned before, this will give us , as well as and in the equation:
To do this, we first write as the sum of a multiple of and a remainder.
Next, this may seem pretty arbitrary, but we repeat the same process, this time writing as the sum of a multiple of the previous remainder, , and another remainder.
We repeat this process again until we obtain as a remainder.
Once we obtain as the remainder, we can stop. From this process, the last non-zero remainder that we obtained is actually . Since in this case it was , .
Note that this process is actually just running the Euclidean algorithm, it's just that we kept a bunch of extra information as well. We can summarize this process below:
To determine and , we need to do a bit of rearranging to isolate the remainder term in each of the equations as follows. We're simply just subtracting a term from the right side and placing it on the right side.
Now that we have this, we are actually all set to solve for and ! Remember, we are trying to find and such that:
To do so, we look at the last equation, which expresses as a linear combination of and , not the desired and .
However, the equation right above it is an expression for .
We can substitute this in to obtain the following:
This expression now expresses as a linear combination of and . Once again, we can use the expression above it to substitute, this time for .
We can repeat this step one last time and obtain the following:
From this, we can determine that and !
Here all of the calculations sorted:
- Express each term as the sum of a multiple of the other term and a remainder.
- Rearrange each of those equations except the last to obtain an expression for the remainder.
- Starting from the last equation, substitute in the expression directly on top and combine like terms.
For another worked out example, check out this video:
Applying to Linear Diophantine Equations
As a quick side note, we can also apply this to solve linear diophantine equations of two variables, where diophantine means that the equation only has integer solutions. We won't get too deep into the details, but if we have an equation like the following, we can solve it pretty easily.
Since , we can use the extended Euclidean algorithm to find the values of and that make . Then, we can scale these values up by to get the solution for our original equation. This works for any value on the right side that we need to find, as long as it is is a multiple of the . It can actually be shown that this will always produce the solution for us if it exists since if the right side is not a multiple of the , there are no solutions.
Applying to Modular Inverses
To see how this can be useful for what this article is about, let's consider what happens when and are coprime. By definition then, , meaning that
If we consider this equation , then we obtain the following:
Therefore, and !
While this method may appear longer than the previous one, it does not involve any exponents and therefore does not require as large of calculations. For example, this method can actually be used to calculate the modular inverse of extremely large numbers with just a four-function calculator, which is especially useful when doing things such as decrypting RSA ciphers by hand (the reason why I bring this up is because of the Codebusters event I did in Science Olympiad).
Example
Calculate the modular inverse of with respect to .
Clearly, exponentiating would not lead to a good time, as . This could be done with a computer rather quickly if you use binary lifting exponentiation, but doing so by hand would almost definitely lead to a bad time. Instead, we should use the extended Euclidean algorithm to find the modular inverse.
We first write each term as the sum of multiples of the other term and a remainder:
From this exhausting amount of calculations, we can determine that , so the modular inverse of does in fact exist. We can proceed with the next step, to rearrange each of the equations:
Finally, we back-substitute each of the expressions one-by-one into the one below it, starting from the bottom.
Therefore, and . From this, we can conclude that the modular inverse of is with respect to . We can multiply this out to see that this in fact works. While this set of equations was exhausting to go through, it works and only requires basic arithmetic operations.
Chart Method
While the above gives a great amount of intuition for why the algorithm works and makes it easy to memorize, it is not that great for computation since you have to rewrite a ton of stuff. As a result, it is faster to use a chart to organize everything.
We can construct a chart as follows for the process of .
Row | a | b | r | q | x | y |
---|---|---|---|---|---|---|
1 | 56 | 46 |
To perform the first step of the algorithm, we have to fill in the columns . The recurrence relation to calculate the values of the chart are listed below, where a term like represents the value in the column in the row .
and represent our two numbers, represents the remainder () and represents the truncated quotient of the two . After this, we transfer the value of to for the next row, and the value of to for the next row. Note that this is equivalent to the first step from the previously mentioned procedure, just in the form of a chart.
Row | a | b | r | q | x | y |
---|---|---|---|---|---|---|
1 | 56 | 46 | 10 | 1 | ||
2 | 46 | 10 | 6 | 4 | ||
3 | 10 | 6 | 4 | 1 | ||
4 | 6 | 4 | 2 | 1 | ||
5 | 4 | 2 | 0 | 2 |
From this, we can find that the value of the is the last non-zero value of , which is . Next, to fill in the values of and , we first start off with the values of and at the second to last row.
Row | a | b | r | q | x | y |
---|---|---|---|---|---|---|
1 | 56 | 46 | 10 | 1 | ||
2 | 46 | 10 | 6 | 4 | ||
3 | 10 | 6 | 4 | 1 | ||
4 | 6 | 4 | 2 | 1 | 1 | -1 |
5 | 4 | 2 | 0 | 2 | N/A | N/A |
To compute the values for and for the row above it, we set the transfer the from below to the above. Then, to compute the above , we take the below value of and subtract from it the below value of multiplied by the upper value for . For instance, the new value of here would be
We can do this for the entire grid to get the following:
Row | a | b | r | q | x | y |
---|---|---|---|---|---|---|
1 | 56 | 46 | 10 | 1 | -9 | 11 |
2 | 46 | 10 | 6 | 4 | 2 | -9 |
3 | 10 | 6 | 4 | 1 | -1 | 2 |
4 | 6 | 4 | 2 | 1 | 1 | -1 |
5 | 4 | 2 | 0 | 2 | N/A | N/A |
We can verify that these values work, as , as we expected.
Dealing With Nonexistent Modular Inverses
As we mentioned before, if shares any factors with , the modular inverse of with respect to does not exist. This corresponds to the bottom left portion of the chart shown in the beginning of this post. However, there may still be equations that we need to solve, such as the following:
In this case, we do not have a modular inverse for as . However, the reason why this modular inverse does not exist is actually that this equation has multiple solutions modulo . For instance, and both work for the above case!
The way we solve a problem like this is that if we want to divide by , we first want to find and then divide everything by that value, including . For instance, in this case, , so we divide everything by .
This is our new equation, and this gives us our solutions for ! Notice how they match up with the two that were mentioned earlier. However, it is not always the case that , so it will not directly give you the solution as it did here. However, in those cases, you have divided out the shared factors, which means that the new and are now coprime! This means you can apply the modular inverse technique mentioned earlier to then solve that new equation.
However, all of this assumed that the right side of the equation was also divisible by . However, it can be shown that if this is not the case, then the equation has no solutions, which means we do not have to worry about this case.
As a final example, we will solve the following equation:
We can first find from inspection and reduce the equation to the following;
Then, we just need to find . To do this, we will apply the Euler's theorem method.
Therefore, we can multiply both sides of the equation by to obtain our answer:
If we wanted to, we could convert this back to and obtain
With that, we're done!
Summary
All in all, division in modular arithmetic can be pretty messy, and it is pretty important to carefully consider what you're doing before you do anything. Additionally, when calculating modular inverses, it is very important to keep good bookkeeping and to constantly check that the equations being written still satisfy the relations they're supposed to. For instance, periodically checking while you're doing the extended Euclidean algorithm to make sure the values still sum to the properly while substituting is pretty important for avoiding mistakes.