Modular Arithmetic
calculator.
Add, multiply, divide, invert or raise numbers to a power mod n, with the working.
- a mod n
- 9
- b mod n
- 5
- Modulus n
- 12
Start at 9 on a dial with 12 positions and move 5 steps forward: you land on 2. On a 12-hour clock, 5 hours after 9 o'clock is 2 o'clock.
On a dial of 12
A dial with 12 positions and 0 at the top. a points to 9; b points to 5; The answer points to 2.
Working
- 1Reduce both numbers to remainders9 mod 12 = 9, 5 mod 12 = 5
- 2Combine, then reduce mod n9 + 5 = 14 ≡ 2 (mod 12)
- 3Check: take b away again2 − 5 ≡ 9 (mod 12), which is 9
Every + at once, mod 12
| + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 |
| 2 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 1 |
| 3 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 1 | 2 |
| 4 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 1 | 2 | 3 |
| 5 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 1 | 2 | 3 | 4 |
| 6 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 1 | 2 | 3 | 4 | 5 |
| 7 | 7 | 8 | 9 | 10 | 11 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| 8 | 8 | 9 | 10 | 11 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 9 | 9 | 10 | 11 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 10 | 10 | 11 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 11 | 11 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Row a, column b. Your cell is lit.
Numbers that leave 2, mod 12
| Turns | Number |
|---|---|
| −2 | −22 |
| −1 | −10 |
| 0 | 2 |
| 1 | 14 |
| 2 | 26 |
| 3 | 38 |
| 4 | 50 |
How it's worked out
Two whole numbers are congruent modulo n when they leave the same remainder after division by n. The Handbook of Applied Cryptography defines it the other way round: n divides their difference (Definition 2.110). The number n is the modulus. Every answer here is given as the least residue, the one remainder from 0 to n − 1.
So a negative number lands on a positive remainder. −7 sits 2 steps past a multiple of 3, because −7 = −3 × 3 + 2.
Adding, subtracting and multiplying can be reduced at any point, before or after, and the answer is the same (Fact 2.112). The handbook works in Z25, the numbers 0 to 24: 13 + 16 = 4 and 13 × 16 = 8 there. Reducing as you go keeps every number small.
Dividing is different. Dividing by b means multiplying by its inverse, the number that times b leaves 1 (Definition 2.116). An inverse exists only when b and n share no factor other than 1 (Fact 2.117). In Z9 the numbers with an inverse are 1, 2, 4, 5, 7, 8, and the inverse of 4 is 7. The tool finds an inverse with the extended Euclidean algorithm, which writes 1 from a and n.
Powers use square and multiply (Algorithm 2.143). Write the exponent in binary. Square the base once per binary digit, reducing each time, and multiply in the squares where the digit is 1. The handbook's own example, 5 to the power 596 mod 1,234, takes 10 rows and gives 1,013.
Two congruences at once are solved with the Chinese remainder theorem (Fact 2.120). When the two moduli share no factor, exactly one answer exists from 0 up to their product, and every answer differs from it by a multiple of that product.
Worked examples
Which day of the week is 100 days from now?
Grace is booking a trip 100 days ahead and wants to know the weekday. She numbers today as day 0. The days of the week repeat every 7, so she needs 0 + 100 mod 7, which is 2.
Her trip lands 2 weekdays later than today, so a Monday today means a Wednesday then. As a check, counting 100 days back from day 2 lands on day 0, which is today.
The private key in a textbook RSA example
A common classroom key uses the primes 61 and 53, so φ(n) = 3,120, and the public exponent 17. The private exponent is the inverse of 17 mod 3,120.
The extended Euclidean algorithm takes 5 divisions and gives 2,753. The check is 17 × 2,753 = 46,801, which is 15 × 3,120 + 1. The RSA calculator builds the whole key from p and q.
A power far too big to write out
Jun is checking the handbook's example by hand. 5 to the power 596 has hundreds of digits, but he never needs it. 596 in binary has 10 digits, so he squares 5 that many times mod 1,234 and multiplies in the squares where the digit is 1.
The last row gives 1,013, the same as Table 2.4 in the handbook. The tool checks it a second way by splitting the exponent into 298 and 298, working out each half, and multiplying the two results mod 1,234.
Reading your result
"No inverse" is a real answer. It means b and n share a factor. Mod 7, 3 ÷ 4 = 6, because the inverse of 4 is 2. Mod 8 the same division has no answer, since 4 and 8 share the factor 4. Dividing by 4 mod 8 needs an inverse of 4, which exists only when gcd(4, 8) = 1.
A prime modulus makes everything easier. Every number from 1 to p − 1 has an inverse, and Fermat's theorem (Fact 2.127) says a to the power p − 1 leaves 1 when a is not a multiple of p. The tool agrees for the prime 1,000,000,007: 2 to the power 1,000,000,006 leaves 1.
Two congruences may have no answer. With x ≡ 2 mod 3 and x ≡ 3 mod 5 the answer is 8, and it repeats every 15. With x ≡ 1 mod 4 and x ≡ 2 mod 6, no x works: No solution: m1 = 4 and m2 = 6 share the factor 2, but 1 and 2 leave different remainders mod 2.
Inputs can be up to 60 digits. Every step uses whole numbers of any length, so a 60-digit modulus or an exponent with hundreds of binary digits is still exact.
Common mistakes
- Dividing the ordinary way. 3 ÷ 4 is not a whole number, but mod 7 it has an exact answer. Multiply by the inverse of the divisor instead.
- Leaving a negative remainder. Some programming languages give −1 for −7 % 3. In modular arithmetic the least residue is 2, and both stand for the same class.
- Working out the whole power first. A power with a large exponent overflows long before you reduce it. Reduce after every squaring and every multiplication.
- Expecting an inverse that cannot exist. An even number has no inverse mod an even modulus, and no multiple of 5 has one mod 10.
- Combining congruences whose moduli share a factor. The Chinese remainder theorem promises one answer only when the moduli are coprime. Otherwise there may be none, or the answer repeats sooner.