← Back to section · Foundations of Cryptography

The Algebraic Structure of Modular Arithmetic: Residue Classes, the Unit Group and Euler's Theorem

Modular arithmetic is more than «taking remainders». Viewed structurally, congruence is an equivalence relation, the residue classes form a ring, the elements coprime to the modulus form a multiplicative group, and Euler's theorem is a corollary of Lagrange's theorem — the shared language behind RSA and the discrete logarithm problem.

Published 22 November 2026 · 5 min read · PQ Frontier

The Algebraic Structure of Modular Arithmetic: Residue Classes, the Unit Group and Euler's Theorem
AI-generated site artwork
Video explainer · Mathematical foundations: the language of cryptography

Most people first meet modular arithmetic as shorthand for “divide and keep the remainder”. That view suffices for exercises, but it hides what modular arithmetic really is: a complete algebraic structure. This article takes the structural view — not starting from hard problems, but from the structure itself — to see how congruence folds the infinite integers into a finite ring and group, and where Euler’s theorem truly sits in that picture. Once this chain is clear, RSA and the discrete logarithm problem become two dialects of one language.

Congruence: an equivalence relation, a clean folding

We say a is congruent to b modulo n, written a ≡ b (mod n), when n divides a − b, i.e. both leave the same remainder on division by n. Congruence satisfies the three properties of an equivalence relation — reflexive, symmetric, transitive — so it partitions all integers cleanly into n disjoint classes, the residue classes, with every integer in exactly one. The crucial step is compatibility: congruence respects addition and multiplication. If a ≡ a′ and b ≡ b′, then a + b ≡ a′ + b′ and ab ≡ a′b′. This licenses defining operations on the classes themselves: pick any representatives, compute, then reduce — the result does not depend on the choice. The residue classes with these two operations form the ring Z_n (a ring in the sense of the previous article). Big-integer arithmetic in computers, check digits, hash bucketing — all of it is work inside Z_n.

One more structural fact is worth remembering: when m and n are coprime, the ring Z_{mn} is fully isomorphic to the direct product Z_m × Z_n (this is the ring-theoretic statement of the Chinese remainder theorem). Arithmetic modulo a composite can therefore be split into arithmetic modulo two smaller numbers and recombined — RSA’s use of the Chinese remainder theorem to accelerate private-key operations rests on exactly this. That is the value of the structural viewpoint: many engineering tricks, traced to their source, are a structure theorem.

The unit group: coprime elements form a group of their own

Not every element of the ring Z_n can be divided by: a has a multiplicative inverse modulo n if and only if a is coprime to n. These “reversible” elements are closed under multiplication, contain the identity 1, and all have inverses — they form a group of their own, Z_n^*, the group of units modulo n. Its size is counted by Euler’s totient: φ(n) is the number of integers between 1 and n coprime to n. If n factors as n = ∏ p_i^{e_i}, then φ(n) = n ∏ (1 − 1/p_i) — an elegant formula, but note: to use it, you must first factor n.

Group theory supplies a basic fact (a corollary of Lagrange’s theorem): in a finite group, the order of every element divides the order of the group, so raising any element to the power |G| returns the identity. Apply this to Z_n^* and Euler’s theorem falls out immediately: for every a coprime to n, a^φ(n) ≡ 1 (mod n). Euler’s theorem is thus not an isolated number-theoretic coincidence but a necessary consequence of group structure; Fermat’s little theorem is merely the special case where n is prime (then φ(p) = p − 1).

A worked example: the unit group modulo 15

Put the abstract definitions to a small test. Take n = 15. The integers coprime to 15 are 1, 2, 4, 7, 8, 11, 13, 14 — eight of them, matching the formula φ(15) = 15 × (1 − 1/3) × (1 − 1/5) = 8. Take a = 2 and raise it repeatedly: 2¹ = 2, 2² = 4, 2³ = 8, 2⁴ = 16 ≡ 1 (mod 15), so the order of 2 is 4, which divides the group order 8, exactly as group theory predicts. Euler’s theorem says 2⁸ ≡ 1 (mod 15), and the check is easy: 2⁴ ≡ 1, and squaring both sides gives it. Note that the order 4 is smaller than φ(15) = 8 — Euler’s theorem provides a universally valid exponent bound, but not necessarily the smallest one. The function that pushes “the true order divides φ(n)” to its limit is the Carmichael function, giving the smallest exponent λ(n) with a^λ(n) ≡ 1 for all a; some RSA standards use it when generating private parameters, because a smaller exponent means faster private-key operations.

Reduce the exponent first: the mathematics behind fast exponentiation

Euler’s theorem has another corollary used every day: when computing a^k mod n with a coprime to n, one may first reduce the exponent k modulo φ(n) and then exponentiate, because the powers of a cycle with period φ(n) (or one of its divisors). Combined with the square-and-multiply method (write the exponent in binary, square repeatedly, multiply when needed), even exponents with hundreds of decimal digits finish almost instantly. This is why RSA encryption and decryption and Diffie–Hellman agreement are feasible in practice: modular exponentiation itself is never expensive — what is expensive is reversing it, recovering a discrete logarithm or factoring the modulus. Cryptographic one-wayness is often born exactly like this: not because the forward computation has any magic, but because structure theorems pave the forward road and no such road exists backwards.

Orders and primitive roots: the language of discrete logarithms

Every group element a has an order: the smallest positive integer k with a^k ≡ 1. Orders describe the behaviour of repeated multiplication precisely. When the modulus is a prime p, Z_p^* happens to be cyclic: there exists a primitive root g whose powers g^0, g^1, …, g^{p−2} exhaust the whole group. Every non-zero residue is therefore some power of g, and “find that exponent” is the discrete logarithm problem. The security of Diffie–Hellman and the construction of ElGamal rest entirely on this asymmetry — exponentiation is easy, recovering the exponent is hard — and the foundations of that language are exactly the residue classes and unit group of this article.

Primitive roots are not rare: modulo a prime p there are exactly φ(p − 1) of them. But verifying that a given number is a primitive root requires the factorisation of p − 1 — the same pattern again: structure theorems give the count, concrete verification still needs factoring. Engineers choosing Diffie–Hellman groups therefore favour “safe primes” p = 2q + 1 (with q also prime), precisely so that the factor structure of p − 1 is simple and transparent and every element’s order has only a few possible values, preventing small-subgroup attacks from leaking information about the exponent through the cracks.

The hardness of φ(n) is RSA’s floor

Finally, back to RSA. RSA takes n = pq, a product of two large primes, with φ(n) = (p − 1)(q − 1); the private exponent is computed from the public exponent by inversion using φ(n). Knowing φ(n) lets one recover p and q (it reduces to solving a quadratic), and conversely factoring n yields φ(n) — the two problems are equivalent in difficulty. This explains the shape of RSA’s security reduction: breaking it is at least as hard as factoring. The whole chain now compresses into one sentence: congruence gives the ring, coprimality gives the group, the group’s order gives Euler’s theorem, and the difficulty of computing Euler’s function is the floor RSA stands on. Why that floor can be pulled away by a quantum algorithm is the story of Shor’s algorithm, told elsewhere on this site.

Tags: Modular ArithmeticEuler's TheoremNumber Theory