MathsNumber Theory

Modular Arithmetic
calculator.

Add, multiply, divide, invert or raise numbers to a power mod n, with the working.

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.

a\displaystyle a≡b(modn)\displaystyle {}\equiv b \pmod{n}⟺n|(a−b)\displaystyle {}\iff n \mid (a - b)

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.

(a×b)modn\displaystyle (a \times b) \bmod n=((amodn)×(bmodn))modn\displaystyle {}= \bigl((a \bmod n) \times (b \bmod n)\bigr) \bmod n

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.

a13\displaystyle a^{13}=a8×a4×a1\displaystyle {}= a^{8} \times a^{4} \times a^{1}

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.

Open this example in the tool

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.

Open this example in the tool

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.

Open this example in the tool

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.

Questions people ask

You multiply by the inverse of the divisor instead. Mod 7 the inverse of 2 is 4, because 2 × 4 leaves 1, so 3 ÷ 2 is 3 × 4, which leaves 5. Some books avoid the division sign and write the inverse with a small −1 for this reason.

Sources