Skip to main content

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 mm. 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 a,ba,b and a positive integer mm, aa is congruent to bb modulo mm, written

ab(modm),a\equiv b\pmod m,

if m(ab)m\mid(a-b). Equivalently, aa and bb have the same remainder when divided by mm.

The residue class of aa modulo mm is the set

[a]m={bZ:ba(modm)}.[a]_m=\{b\in\mathbb{Z}:b\equiv a\pmod m\}.

There are exactly mm residue classes modulo mm, represented by 0,1,,m10,1,\dots,m-1. Arithmetic modulo mm can be performed on any representatives and then reduced back to this range.

A multiplicative inverse of aa modulo mm is an integer bb such that

ab1(modm).ab\equiv1\pmod m.

The inverse, when it exists, is unique modulo mm. The set of invertible residue classes modulo mm is often denoted UmU_m.

Euler's phi function ϕ(n)\phi(n) counts the positive integers not exceeding nn that are relatively prime to nn. If n=pqn=pq for distinct primes pp and qq, then

ϕ(n)=(p1)(q1).\phi(n)=(p-1)(q-1).

Key results

Congruence is compatible with addition and multiplication. If

ab(modm),cd(modm),a\equiv b\pmod m,\qquad c\equiv d\pmod m,

then

a+cb+d(modm),acbd(modm).a+c\equiv b+d\pmod m,\qquad ac\equiv bd\pmod m.

Proof sketch: m(ab)m\mid(a-b) and m(cd)m\mid(c-d). Therefore mm divides (a+c)(b+d)(a+c)-(b+d) and also divides acbd=a(cd)+d(ab)ac-bd=a(c-d)+d(a-b).

An inverse of aa modulo mm exists if and only if gcd(a,m)=1\gcd(a,m)=1. This follows from Bezout's identity: there are integers s,ts,t such that

as+mt=1as+mt=1

exactly when gcd(a,m)=1\gcd(a,m)=1. Reducing modulo mm gives as1(modm)as\equiv1\pmod m, so ss is an inverse of aa modulo mm.

The Chinese remainder theorem says that if m1,,mkm_1,\dots,m_k are pairwise relatively prime, then the system

xai(modmi)(1ik)x\equiv a_i\pmod{m_i}\quad(1\le i\le k)

has a unique solution modulo M=m1m2mkM=m_1m_2\cdots m_k.

Fast modular exponentiation uses repeated squaring. If the exponent has binary expansion

e=jbj2j,e=\sum_j b_j2^j,

then

ae=bj=1a2j.a^e=\prod_{b_j=1} a^{2^j}.

Reducing modulo mm after each multiplication keeps the numbers small and uses only O(loge)O(\log e) modular multiplications.

RSA, in simplified mathematical form, chooses primes p,qp,q, sets n=pqn=pq, chooses ee relatively prime to ϕ(n)\phi(n), and finds dd with ed1(modϕ(n))ed\equiv1\pmod{\phi(n)}. Encryption sends MM to CMe(modn)C\equiv M^e\pmod n, and decryption computes Cd(modn)C^d\pmod n. The practical security rests on the difficulty of factoring large nn into pp and qq.

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, ...
OperationModular ruleExample modulo 77
reducereplace by remainder31331\equiv3
addadd then reduce5+61145+6\equiv11\equiv4
multiplymultiply then reduce452064\cdot5\equiv20\equiv6
inversesolve ab1ab\equiv13153^{-1}\equiv5
exponentiaterepeated squaring3823^8\equiv2
cancelonly invertible factorscan cancel 33 mod 77, not 22 mod 66

Worked example 1: Solve a linear congruence

Problem. Solve

14x21(mod35).14x\equiv21\pmod{35}.

Method.

  1. Compute the gcd:
gcd(14,35)=7.\gcd(14,35)=7.
  1. A congruence axb(modm)ax\equiv b\pmod m has a solution only if gcd(a,m)b\gcd(a,m)\mid b. Here 7217\mid21, so solutions exist.
  2. Divide the congruence by 77:
2x3(mod5).2x\equiv3\pmod5.
  1. Find the inverse of 22 modulo 55. Since 23=61(mod5)2\cdot3=6\equiv1\pmod5, the inverse is 33.
  2. Multiply both sides by 33:
x94(mod5).x\equiv9\equiv4\pmod5.
  1. Convert back to solutions modulo 3535. The numbers congruent to 44 modulo 55 are
4,9,14,19,24,29,34(mod35).4,9,14,19,24,29,34\pmod{35}.

Checked answer. The seven incongruent solutions modulo 3535 are x4,9,14,19,24,29,34(mod35)x\equiv4,9,14,19,24,29,34\pmod{35}. Substituting x=4x=4 gives 144=5621(mod35)14\cdot4=56\equiv21\pmod{35}, and adding 55 to xx preserves the congruence because 145=7014\cdot5=70 is divisible by 3535.

Worked example 2: Fast modular exponentiation

Problem. Compute 723(mod33)7^{23}\pmod{33} without expanding 7237^{23}.

Method.

  1. Write the exponent in binary:
23=16+4+2+1.23=16+4+2+1.
  1. Repeatedly square modulo 3333:
717(mod33),724916(mod33),74162=25625(mod33),78252=62531(mod33),716312=9614(mod33).\begin{aligned} 7^1&\equiv7\pmod{33},\\ 7^2&\equiv49\equiv16\pmod{33},\\ 7^4&\equiv16^2=256\equiv25\pmod{33},\\ 7^8&\equiv25^2=625\equiv31\pmod{33},\\ 7^{16}&\equiv31^2=961\equiv4\pmod{33}. \end{aligned}
  1. Multiply the powers corresponding to 16,4,2,116,4,2,1:
72371674727425167(mod33).7^{23}\equiv7^{16}7^4 7^2 7 \equiv4\cdot25\cdot16\cdot7\pmod{33}.
  1. Reduce step by step:
425=1001,11616,167=11213.\begin{aligned} 4\cdot25&=100\equiv1,\\ 1\cdot16&\equiv16,\\ 16\cdot7&=112\equiv13. \end{aligned}

Checked answer. 72313(mod33)7^{23}\equiv13\pmod{33}. 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 amodm=ba\bmod m=b and ab(modm)a\equiv b\pmod m as the same statement. The first names a remainder; the second is a relation.
  • Cancelling a factor that is not invertible modulo mm.
  • Forgetting that a linear congruence may have zero, one, or several solutions modulo mm.
  • 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 nn 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 mm, reduce early and often. The congruence rules allow replacing a large number by any congruent representative, so 372(mod11)37^2\pmod{11} can become 42(mod11)4^2\pmod{11} and then 165(mod11)16\equiv5\pmod{11}. 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 6x3(mod9)6x\equiv3\pmod9 has no solution because gcd(6,9)=3\gcd(6,9)=3 divides 33, so actually it does have solutions after reducing to 2x1(mod3)2x\equiv1\pmod3; but 6x4(mod9)6x\equiv4\pmod9 has none because 343\nmid4. This distinction is exactly why blind division by 66 modulo 99 is invalid.

The Chinese remainder theorem is constructive. To solve xa(modm)x\equiv a\pmod m and xb(modn)x\equiv b\pmod n with gcd(m,n)=1\gcd(m,n)=1, one can search modulo mnmn for small examples or build the solution using inverses. The uniqueness is modulo mnmn, meaning every solution differs by a multiple of mnmn.

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 [3]10[3]_{10} contains ,7,3,13,23,\dots,-7,3,13,23,\dots. Writing x3(mod10)x\equiv3\pmod{10} identifies a class, not a single integer. If a problem asks for solutions modulo 1010, 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 x4(mod5)x\equiv4\pmod5 came from reducing a congruence modulo 3535, verify several lifted representatives modulo 3535. 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 1010, only 1,3,7,91,3,7,9 have inverses. Modulo a prime pp, every nonzero residue has an inverse. This difference explains why fields such as Zp\mathbb{Z}_p behave much more like ordinary arithmetic than composite-modulus systems such as Z10\mathbb{Z}_{10}.

For worked cryptography exercises, use intentionally small primes only to understand the mechanism. Small RSA examples are insecure precisely because factoring nn is easy. The lesson is the algebraic loop: choose relatively prime exponents, use an inverse modulo ϕ(n)\phi(n), 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 x2x\equiv2 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