Modular Arithmetic and Cryptography
Modular arithmetic treats integers by their remainders. This turns infinite integer questions into finite arithmetic on residue classes, which is why it appears in hashing, pseudorandom generators, checksums, calendars, cyclic schedules, and public-key cryptography.
The main shift is to stop asking for the exact integer and ask only for its class modulo . Once this is done carefully, addition, multiplication, inverses, and exponentiation can be performed with small representatives. Cryptography then uses the fact that some modular operations are easy in one direction but hard to reverse without hidden information.
Definitions
For integers and a positive integer , is congruent to modulo , written
if . Equivalently, and have the same remainder when divided by .
The residue class of modulo is the set
There are exactly residue classes modulo , represented by . Arithmetic modulo can be performed on any representatives and then reduced back to this range.
A multiplicative inverse of modulo is an integer such that
The inverse, when it exists, is unique modulo . The set of invertible residue classes modulo is often denoted .
Euler's phi function counts the positive integers not exceeding that are relatively prime to . If for distinct primes and , then
Key results
Congruence is compatible with addition and multiplication. If
then
Proof sketch: and . Therefore divides and also divides .
An inverse of modulo exists if and only if . This follows from Bezout's identity: there are integers such that
exactly when . Reducing modulo gives , so is an inverse of modulo .
The Chinese remainder theorem says that if are pairwise relatively prime, then the system
has a unique solution modulo .
Fast modular exponentiation uses repeated squaring. If the exponent has binary expansion
then
Reducing modulo after each multiplication keeps the numbers small and uses only modular multiplications.
RSA, in simplified mathematical form, chooses primes , sets , chooses relatively prime to , and finds with . Encryption sends to , and decryption computes . The practical security rests on the difficulty of factoring large into and .
Visual
Integers modulo 5 split into five residue classes:
[0]: ..., -10, -5, 0, 5, 10, ...
[1]: ..., -9, -4, 1, 6, 11, ...
[2]: ..., -8, -3, 2, 7, 12, ...
[3]: ..., -7, -2, 3, 8, 13, ...
[4]: ..., -6, -1, 4, 9, 14, ...
| Operation | Modular rule | Example modulo |
|---|---|---|
| reduce | replace by remainder | |
| add | add then reduce | |
| multiply | multiply then reduce | |
| inverse | solve | |
| exponentiate | repeated squaring | |
| cancel | only invertible factors | can cancel mod , not mod |
Worked example 1: Solve a linear congruence
Problem. Solve
Method.
- Compute the gcd:
- A congruence has a solution only if . Here , so solutions exist.
- Divide the congruence by :
- Find the inverse of modulo . Since , the inverse is .
- Multiply both sides by :
- Convert back to solutions modulo . The numbers congruent to modulo are
Checked answer. The seven incongruent solutions modulo are . Substituting gives , and adding to preserves the congruence because is divisible by .
Worked example 2: Fast modular exponentiation
Problem. Compute without expanding .
Method.
- Write the exponent in binary:
- Repeatedly square modulo :
- Multiply the powers corresponding to :
- Reduce step by step:
Checked answer. . The computation used five squarings and a few multiplications instead of forming a huge integer.
Code
def egcd(a, b):
if b == 0:
return (abs(a), 1 if a >= 0 else -1, 0)
g, x1, y1 = egcd(b, a % b)
return (g, y1, x1 - (a // b) * y1)
def inv_mod(a, m):
g, x, _ = egcd(a, m)
if g != 1:
raise ValueError("no inverse")
return x % m
def modexp(base, exp, mod):
result = 1
base %= mod
while exp:
if exp & 1:
result = (result * base) % mod
base = (base * base) % mod
exp >>= 1
return result
print(inv_mod(3, 7))
print(modexp(7, 23, 33))
The modexp routine is the repeated-squaring algorithm used in real modular arithmetic libraries, although production cryptographic code must also handle side-channel concerns and padding schemes.
Common pitfalls
- Treating and as the same statement. The first names a remainder; the second is a relation.
- Cancelling a factor that is not invertible modulo .
- Forgetting that a linear congruence may have zero, one, or several solutions modulo .
- Allowing intermediate powers to grow unnecessarily instead of reducing after each multiplication.
- Thinking RSA security depends on the algorithm being secret. The method is public; the factorization of is the hidden part.
- Using textbook RSA directly for real messages. Practical cryptography requires padding, randomness, validated parameters, and vetted libraries.
When reducing expressions modulo , reduce early and often. The congruence rules allow replacing a large number by any congruent representative, so can become and then . This does not change the residue class, but it keeps arithmetic small and reduces mistakes.
Linear congruences require a gcd check before cancellation. The congruence has no solution because divides , so actually it does have solutions after reducing to ; but has none because . This distinction is exactly why blind division by modulo is invalid.
The Chinese remainder theorem is constructive. To solve and with , one can search modulo for small examples or build the solution using inverses. The uniqueness is modulo , meaning every solution differs by a multiple of .
In cryptography examples, separate the mathematics from the protocol. Modular exponentiation explains how RSA transforms numbers. Real encryption additionally needs message encoding, padding, randomization, parameter validation, and resistance to timing attacks. A discrete mathematics course usually studies the number-theoretic core, not the full engineering standard.
Finally, distinguish congruence classes from their representatives. The class contains . Writing identifies a class, not a single integer. If a problem asks for solutions modulo , one representative from each class is enough.
When checking modular computations, substitute the answer into the original congruence rather than only checking the simplified version. If came from reducing a congruence modulo , verify several lifted representatives modulo . This catches mistakes in the number of solution classes and in the modulus after division by a gcd.
It is also helpful to keep a table of units for small moduli. Modulo , only have inverses. Modulo a prime , every nonzero residue has an inverse. This difference explains why fields such as behave much more like ordinary arithmetic than composite-modulus systems such as .
For worked cryptography exercises, use intentionally small primes only to understand the mechanism. Small RSA examples are insecure precisely because factoring is easy. The lesson is the algebraic loop: choose relatively prime exponents, use an inverse modulo , and rely on modular exponentiation for encryption and decryption. Do not infer practical parameter sizes or security practices from classroom-sized numbers.
In modular arithmetic, the modulus is part of the statement. The claim is incomplete until the modulus is named. Changing the modulus changes the residue classes, the invertible elements, and the number of distinct solutions.
For that reason, carry the modulus through every displayed step instead of mentioning it only at the beginning.
This habit makes invalid cancellation easier to notice.
Connections
- Number theory basics supplies divisibility, gcds, primes, and Bezout coefficients.
- Algorithms and complexity explains why repeated squaring is efficient.
- Relations frames congruence modulo as an equivalence relation.
- Equivalence relations and partial orders studies residue classes as equivalence classes.
- Finite-state machines and computation connects modular arithmetic to finite memory and cyclic state.