DISCRETE MATH • NUMBER THEORY AND CRYPTOGRAPHY

Solving linear congruences (intro)

Master the foundational technique of modular arithmetic that underpins modern cryptographic systems.

Historical Context & Motivation

The study of linear congruences is among the oldest problems in number theory, arising naturally whenever one asks about divisibility patterns and remainders. Ancient civilizations recognized that certain arithmetic problems—distributing objects into equal groups, scheduling recurring events, or solving puzzles about unknown quantities—reduce to questions about residues modulo an integer. The formal language of congruences, however, took centuries to develop, and its codification transformed number theory from a collection of clever tricks into a rigorous algebraic discipline. Today, solving equations of the form ax ≡ b (mod m) is a gateway skill for understanding modular arithmetic and its profound applications in cryptography, coding theory, and computer science.

~300 CE
Sun Zi's Remainder Problem
The Chinese mathematician Sun Zi posed a problem in the Sunzi Suanjing asking for a number that leaves specific remainders when divided by 3, 5, and 7—one of the earliest known systems of simultaneous linear congruences.
628
Brahmagupta's Kuttaka Method
The Indian mathematician Brahmagupta refined the kuttaka ('pulverizer') algorithm, an iterative procedure closely related to the Extended Euclidean Algorithm, for solving linear Diophantine equations—the integer-valued cousins of linear congruences.
1801
Gauss's Disquisitiones Arithmeticae
Carl Friedrich Gauss published his landmark treatise, introducing the ≡ notation for congruences and systematically developing the theory of modular arithmetic, including existence and uniqueness conditions for solutions of linear congruences.
1977
RSA Cryptosystem
Rivest, Shamir, and Adleman published the RSA algorithm, whose key generation step requires computing modular inverses—precisely the operation at the heart of solving linear congruences. This cemented modular arithmetic as essential infrastructure for digital security.

The central question that linear congruences address is deceptively simple: given integers a, b, and m, can we find an integer x such that ax leaves remainder b upon division by m? Under what conditions does a solution exist, and when it does, how many solutions are there modulo m? These questions lead directly to the concept of modular inverses and the role of the greatest common divisor as the arbiter of solvability.

Core Principles & Definitions

Before solving linear congruences, we need to establish the precise definitions and foundational results that govern their behavior. A congruence a ≡ b (mod m) asserts that m divides the difference a − b, or equivalently, that a and b share the same remainder when divided by m. A linear congruence is an equation of the form ax ≡ b (mod m), where a, b, m are given integers with m > 0, and x is the unknown. The following principles form the backbone of the theory.

1

Congruence as Equivalence

The relation ≡ (mod m) is an equivalence relation on ℤ: it is reflexive, symmetric, and transitive. It partitions the integers into m distinct residue classes {0, 1, 2, …, m−1}.
2

Existence Theorem

The congruence ax ≡ b (mod m) has a solution if and only if gcd(a, m) divides b. This is the single most important criterion: if d = gcd(a, m) does not divide b, no solution exists.
3

Number of Solutions

When a solution exists with d = gcd(a, m), there are exactly d incongruent solutions modulo m. In the special case where gcd(a, m) = 1, the solution is unique modulo m.
4

Modular Inverse

When gcd(a, m) = 1, the integer a has a multiplicative inverse modulo m: an integer a⁻¹ such that a · a⁻¹ ≡ 1 (mod m). The solution to ax ≡ b (mod m) is then x ≡ a⁻¹b (mod m).
5

Extended Euclidean Algorithm

The Extended Euclidean Algorithm computes integers s and t such that as + mt = gcd(a, m). The coefficient s (reduced mod m) yields the modular inverse when gcd(a, m) = 1.
KEY TAKEAWAY
Think of a linear congruence ax ≡ b (mod m) as trying to 'hit a target' on a circular number line with m positions. Each step of size a moves you forward around the circle. The question is: can you land exactly on position b? The gcd(a, m) determines how many positions on the circle are reachable—if d = gcd(a, m), only every d-th position is accessible. So b must be one of those accessible positions (i.e., d | b), or you can never reach it.

Visual Explanation

The following diagram illustrates how the residues of multiples of a behave modulo m, depicting the 'stepping around a clock' metaphor. We visualize ℤ/mℤ as a circular arrangement of m positions and trace where successive multiples of a land, revealing the connection between gcd(a, m) and reachability.

Left: When gcd(a, m) = 3, only positions that are multiples of 3 are reachable (highlighted in cyan). Right: When gcd(a, m) = 1, every position on the circle is reachable (all highlighted in green), guaranteeing a unique solution for any right-hand side b.

The diagram above makes the existence theorem visually immediate. On the left, with a = 3 and m = 9, the multiples of 3 modulo 9 cycle through only {0, 3, 6}, covering exactly 9/gcd(3,9) = 3 distinct residues. Consequently, 3x ≡ b (mod 9) is solvable only when b ∈ {0, 3, 6}—precisely the multiples of d = 3. On the right, with a = 5 and m = 9, the gcd is 1, so stepping by 5 visits every residue class before returning to the start. This is why coprimality of a and m guarantees not merely existence but uniqueness of the solution modulo m. Understanding this geometric picture provides strong intuition for why the Extended Euclidean Algorithm works: it is essentially constructing the precise sequence of steps needed to reach any given target on the circle.

Mathematical Framework

We now formalize the theory. The fundamental objects are the linear congruence equation and the conditions that govern its solution set. Throughout, we let a, b, m ∈ ℤ with m > 0.

LINEAR CONGRUENCE
ax ≡ b (mod m)
This states that m | (ax − b), or equivalently, there exists an integer k such that ax = b + km. The unknowns are x ∈ ℤ.
EXISTENCE CONDITION
ax ≡ b (mod m) has a solution ⟺ gcd(a, m) | b
Let d = gcd(a, m). The congruence is equivalent to the Diophantine equation ax − my = b, which by Bézout's identity has integer solutions if and only if d divides b.
SOLUTION COUNT
If d | b, there are exactly d incongruent solutions modulo m
If x₀ is one solution, the complete solution set is x ≡ x₀ + k(m/d) (mod m) for k = 0, 1, …, d−1. When d = 1, there is exactly one solution modulo m.
REDUCTION TO COPRIME CASE
ax ≡ b (mod m) → (a/d)x ≡ (b/d) (mod m/d) where d = gcd(a, m)
When d > 1 and d | b, divide every coefficient by d. The reduced congruence has gcd(a/d, m/d) = 1, so the modular inverse of a/d modulo m/d exists and yields the unique base solution x₀ modulo m/d.
🔗 Connection to Bézout's Identity
The Extended Euclidean Algorithm applied to (a, m) produces integers s and t with as + mt = d. When d | b, multiply through by b/d to get a(sb/d) + m(tb/d) = b, showing that x₀ = sb/d is a particular solution. Reducing x₀ modulo m/d gives the base solution in the coprime formulation. This algorithmic connection is why the Extended Euclidean Algorithm is the standard computational tool for solving linear congruences.

Solution Algorithm & Classification

With the theory established, we can organize the solution procedure into a systematic algorithm. The flowchart below captures the decision logic, while the subsequent table classifies the different scenarios one may encounter when solving a linear congruence.

The flowchart proceeds top to bottom: compute the gcd, test divisibility, reduce to the coprime case, find the modular inverse via the Extended Euclidean Algorithm, and enumerate all d incongruent solutions.
Classification of solution scenarios for ax ≡ b (mod m)
ScenarioCondition# Solutions (mod m)Example
No solutiongcd(a, m) ∤ b04x ≡ 3 (mod 6): gcd(4,6) = 2 ∤ 3
Unique solutiongcd(a, m) = 113x ≡ 5 (mod 7): gcd(3,7) = 1
Multiple solutionsd = gcd(a,m) > 1 and d | bd6x ≡ 4 (mod 8): gcd(6,8) = 2 | 4 → 2 soln's
Trivial (a ≡ 0)a ≡ 0 (mod m), b ≡ 0 (mod m)m (every residue)0x ≡ 0 (mod 5): every x works

Worked Example

Let us solve the congruence 12x ≡ 18 (mod 30) step by step, demonstrating every stage of the algorithm including the Extended Euclidean computation.

Solve 12x ≡ 18 (mod 30)
1
Step 1 — Compute gcd(a, m)Apply the Euclidean Algorithm to a = 12 and m = 30. We have 30 = 2 × 12 + 6, then 12 = 2 × 6 + 0. Therefore gcd(12, 30) = 6.
d = gcd(12, 30) = 6
2
Step 2 — Check existence conditionWe need d | b, i.e., 6 | 18. Since 18 = 3 × 6, the condition is satisfied. A solution exists, and there will be exactly d = 6 incongruent solutions modulo 30.
6 | 18 ✓ — solutions exist
3
Step 3 — Reduce to coprime caseDivide every coefficient by d = 6: a/d = 12/6 = 2, b/d = 18/6 = 3, m/d = 30/6 = 5. The reduced congruence is 2x ≡ 3 (mod 5). Note that gcd(2, 5) = 1, confirming the coprime condition.
Reduced: 2x ≡ 3 (mod 5)
4
Step 4 — Find modular inverse of a/d mod m/dWe need 2⁻¹ mod 5. Apply the Extended Euclidean Algorithm: 5 = 2 × 2 + 1, so 1 = 5 − 2 × 2, giving s = −2. Reducing modulo 5: −2 ≡ 3 (mod 5). Verify: 2 × 3 = 6 ≡ 1 (mod 5). ✓
2⁻¹ ≡ 3 (mod 5)
5
Step 5 — Compute base solutionMultiply both sides of 2x ≡ 3 (mod 5) by 2⁻¹ = 3: x ≡ 3 × 3 (mod 5) = 9 ≡ 4 (mod 5). The base solution is x₀ = 4.
x₀ ≡ 4 (mod 5)
6
Step 6 — Enumerate all solutions modulo mThe d = 6 solutions modulo 30 are x ≡ x₀ + k(m/d) (mod m) = 4 + 5k (mod 30) for k = 0, 1, 2, 3, 4, 5. Computing: x ∈ {4, 9, 14, 19, 24, 29}. Each can be verified: 12 × 4 = 48 ≡ 18 (mod 30) ✓, 12 × 9 = 108 ≡ 18 (mod 30) ✓, and so on.
x ≡ 4, 9, 14, 19, 24, 29 (mod 30)
Verification Tip
Always verify at least two solutions by direct substitution. For instance, 12 × 24 = 288 = 9 × 30 + 18, so 288 mod 30 = 18. ✓ This catches arithmetic errors in the Extended Euclidean computation, which is the most error-prone step.

Solution Methods Compared

While the Extended Euclidean Algorithm is the general-purpose tool for solving linear congruences, several alternative approaches exist. Each has its own strengths and appropriate contexts. The following comparison helps you choose the right method for a given situation.

Comparison of methods for solving ax ≡ b (mod m)
MethodStrengthsLimitations
Brute-force searchTrivial to implement; no theory needed; guarantees finding all solutions for small moduliO(m) time complexity; completely impractical for large m (e.g., cryptographic sizes ~ 10³⁰⁰)
Extended Euclidean AlgorithmO(log m) time; handles all cases including d > 1; directly produces Bézout coefficientsMulti-step bookkeeping; negative intermediate values require careful sign handling
Euler's theorem (a^{φ(m)−1})Elegant theoretical connection; useful when φ(m) is easily computed (e.g., m prime)Requires gcd(a,m) = 1; computing φ(m) for large composite m requires factorization
Inspection / mental arithmeticFastest for small moduli (m ≤ 20); builds number sense and intuitionNot systematic; error-prone; does not scale
PRACTICAL ADVICE
For homework and exam problems with small moduli (m < 50), inspection or a quick Euclidean computation suffices. For programming tasks—especially in cryptography where moduli are hundreds of digits long—the Extended Euclidean Algorithm is the standard. Languages like Python provide built-in support via pow(a, -1, m) (Python 3.8+), which internally uses exactly this algorithm.

Connections to Advanced Topics

Linear congruences are the simplest equations in modular arithmetic, but they serve as the launching pad for a rich landscape of more advanced results. The table below maps how each idea generalizes, illustrating that mastering the introductory case provides essential scaffolding for the rest of number theory and its applications.

From introductory linear congruences to advanced mathematics
Introductory ConceptAdvanced GeneralizationApplication Domain
Single linear congruence ax ≡ b (mod m)Systems of simultaneous congruences → Chinese Remainder TheoremFault-tolerant computing, secret sharing
Modular inverse a⁻¹ mod mMultiplicative group (ℤ/mℤ)* and Euler's theoremRSA key generation, Diffie-Hellman
Extended Euclidean AlgorithmContinued fractions, lattice basis reductionCryptanalysis, integer programming
Linear congruence in one variablePolynomial congruences and Hensel's lemmap-adic analysis, algebraic number theory
Existence via gcd conditionSolvability in rings, ideal theoryAbstract algebra, algebraic geometry

Perhaps the most immediate next step is the Chinese Remainder Theorem (CRT), which addresses systems of linear congruences with pairwise coprime moduli. Given the system x ≡ a₁ (mod m₁), x ≡ a₂ (mod m₂), …, x ≡ aₖ (mod mₖ), the CRT guarantees a unique solution modulo m₁m₂···mₖ. Each individual congruence in the CRT construction is solved using exactly the techniques covered in this lesson. Beyond the CRT, polynomial congruences like x² ≡ a (mod p) (quadratic residues) and higher-degree congruences build on the linear case through iterative lifting procedures such as Hensel's lemma.

Practice Problems

PROBLEM 1CONCEPTUAL
Explain why the congruence 6x ≡ 5 (mod 9) has no solution, even though both 6 and 5 are less than 9. What specific criterion fails, and what is the underlying reason in terms of the residue classes that multiples of 6 can reach modulo 9?
PROBLEM 2BASIC CALCULATION
Solve the congruence 7x ≡ 3 (mod 11).
PROBLEM 3INTERMEDIATE
Find all solutions to 15x ≡ 9 (mod 24).
PROBLEM 4APPLIED
In a simplified RSA scheme, the public key is (e, n) = (17, 60) and you need to compute the private key d such that ed ≡ 1 (mod φ(n)), where φ(60) = 16. Find d by solving 17d ≡ 1 (mod 16).
PROBLEM 5CRITICAL THINKING
Prove that if p is prime and a is not divisible by p, then ax ≡ b (mod p) always has exactly one solution modulo p. Furthermore, explain how this connects to the fact that ℤ/pℤ is a field.

Summary & Review

A linear congruence ax ≡ b (mod m) asks for integers x whose product with a leaves remainder b upon division by m. The existence theorem states that solutions exist if and only if d = gcd(a, m) divides b, and when they do, there are exactly d incongruent solutions modulo m. The solution procedure involves dividing through by d to obtain a coprime reduced congruence, finding the modular inverse of a/d modulo m/d via the Extended Euclidean Algorithm, and then generating all solutions by adding multiples of m/d.

This technique is the entry point to a vast array of applications: from the Chinese Remainder Theorem for systems of congruences, to RSA cryptography where computing modular inverses is the central operation in key generation. Mastering the interplay between divisibility, residue classes, and modular arithmetic provides essential algebraic fluency for all subsequent work in number theory and discrete mathematics.

Varsity Tutors • Discrete Math • Solving linear congruences (intro)