Modular Exponentiation and the Euler-Fermat Theorem
How can a computer compute ? It must do this without ever writing a million-digit number. This practical question forces us to confront something beautiful. Modular arithmetic hides a piece of elegant structure inside it. The multiplicative group of integers modulo is finite. So every element raised to a high enough power must cycle back. It returns to where it started. Euler's theorem makes this precise. It shows that whenever and are coprime. Here is The Euler Totient Function. This single congruence is the engine behind an entire computational and cryptographic industry. The idea matters for a key reason. It converts a statement about counting into a statement about periodicity. Counting means asking how many integers below are coprime to . Periodicity means asking how quickly powers of return to . That bridge — from counting to cycling — is one of the recurring miracles of elementary number theory. It generalizes the older, more restrictive Fermat's Little Theorem from prime moduli to arbitrary moduli. The big picture is this: , the set of residues coprime to under multiplication, is a finite abelian group of order . Euler's theorem is nothing more than Lagrange's theorem in disguise. It is also a direct consequence of a clever rearrangement argument. That argument predates group theory itself. This article builds the theorem from first principles. It uses only Modular Arithmetic and the definition of . It gives a complete, self-contained proof by the classical "permutation of residues" method. This is the same style of argument Euler himself would have recognized. We then turn to the algorithmic payoff. Fast modular exponentiation works by repeated squaring. This lets us compute in about multiplications instead of multiplications. Each multiplication stays small by reducing modulo at every step. The article closes by pointing toward where this machinery becomes indispensable. That place is the RSA Cryptosystem. Its security and correctness rest entirely on Euler's theorem. This theorem applies to a modulus , a product of two large primes. Everything proved here — the theorem and the fast exponentiation algorithm — is exactly the toolkit that public-key cryptography needs.
- Euler's theorem: if then , generalizing Fermat's Little Theorem.
- Proof strategy: multiplication by permutes the residues coprime to ; multiply them all and cancel.
- Fermat's Little Theorem is the special case prime, where .
- Fast modular exponentiation (repeated squaring) computes in modular multiplications.
- The order of modulo always divides — a consequence of Lagrange's theorem for the group .
- This pair of tools (theorem + algorithm) is the exact computational backbone of RSA encryption and decryption.
- Key idea: finiteness of the coprime residues forces every coprime power sequence to be eventually periodic with period dividing .
Where this lives today
Euler's theorem and fast modular exponentiation together form the computational core of modern public-key cryptography. In the RSA Cryptosystem, a modulus is chosen as a product of two large primes, and encryption/decryption exponents are chosen so that ; correctness of decryption is a direct application of Euler's theorem (with some extra care for messages not coprime to , handled via the Chinese Remainder Theorem). Without fast modular exponentiation, computing for a 2048-bit exponent would be entirely infeasible; repeated squaring makes it routine, requiring only a few thousand modular multiplications rather than an astronomically large number. Beyond RSA, the same machinery underlies the Diffie–Hellman key exchange, Rabin cryptosystem, and various digital signature schemes, all of which perform exponentiation in a finite group of known or partially known order. In primality testing, Fermat's Little Theorem (the prime-modulus special case) is the seed idea behind the Fermat primality test and its refinements (Miller–Rabin), which rely on fast modular exponentiation to test candidate primes with hundreds of digits in milliseconds. In computer algebra and coding theory, modular exponentiation appears in polynomial evaluation over finite fields, and the order-divides-group-order corollary generalizes to the structure theory of finite fields and cyclic groups used in error-correcting codes. Even outside cryptography, Euler's theorem is a standard tool in competition mathematics and in analytic number theory for controlling the multiplicative order of residues, feeding into deeper results about the distribution of primes and the structure of .
Ask the tutor: How does Euler's theorem, combined with the Chinese Remainder Theorem, guarantee that RSA decryption recovers the original message even when the message shares a factor with the modulus?
Let be an integer. Define
Let denote the set of residue classes modulo that have a representative coprime to ; this set has exactly elements, and it is closed under multiplication modulo (if then ). For integers and with and , Euler's theorem asserts
The order of modulo , written , is the smallest positive integer such that ; Euler's theorem guarantees this order exists (since itself is one such exponent), and a companion fact proved below shows .
Intuition
Think of the coprime residues modulo as points arranged in a cycle, and think of multiplication by as a shuffle of these points. Because is invertible modulo (this is exactly what buys us), the shuffle is a bijection: no two points collide, and no point is left out. A finite set shuffled bijectively by repeatedly applying the same rule must eventually return every point to its original position — you cannot shuffle a finite deck forever without repeating a configuration. Euler's theorem pins down exactly how many shuffles it takes in the worst case: applications of "multiply by " always return every coprime residue to itself, though the true minimal period (the order of ) may be a proper divisor of . A second way to see it: the coprime residues modulo form a group under multiplication, of size . The powers of trace out a subgroup — a smaller cycle sitting inside the big cycle. Lagrange's theorem, a purely group-theoretic fact, says the size of any subgroup divides the size of the whole group. Since the size of the subgroup generated by is precisely the order of , this order must divide , and raising to the -th power is just going around the big cycle a whole number of times, landing back at the identity element .
The central theorem
Let be a positive integer and let be an integer with . Then
Let and let be an integer with . Let be the distinct residues modulo in that are coprime to (there are of them by definition of the totient). Then the residues
are again exactly the numbers , in some order.
We use the technique of exhibiting a bijection between finite sets by showing the map is injective (hence, by finiteness, a permutation), and checking the outputs land in the same set as the inputs. Step 1: each is coprime to . Since and , and since whenever (a standard fact: if a prime divided , then since is prime it would divide or , contradicting one of the two coprimality hypotheses), we get . Reducing modulo does not change this gcd, since and gcd with is a function of the residue class alone (if then for some integer , so any common divisor of and also divides , and vice versa). Hence is one of the numbers in coprime to , i.e., it is one of . Step 2: the map is injective. Suppose for some . Then . Since , Euclid's lemma (in the form: if and then , which follows from Bézout's identity for integers , giving , and divides both terms on the right) implies . Since , we have , so forces , and hence (as the are listed as distinct residues). Step 3: conclude it is a bijection onto the same finite set. We have a map from the finite set to itself (Step 1) that is injective (Step 2). An injective map from a finite set to itself is automatically surjective (a basic pigeonhole fact: if it missed some element, the images of the distinct inputs would occupy at most slots among the outputs, forcing two inputs to share an output, contradicting injectivity). Hence the map is a bijection of the set onto itself — that is, a permutation, exactly as claimed.
∎(Proof of Euler's Theorem, using the Lemma.) We use the technique of multiplying all elements of a permuted finite set and cancelling the common factor — the classical argument due to Euler. By the Lemma, the numbers are a rearrangement of . Therefore their products modulo agree:
Since each is congruent to modulo , and a product of numbers congruent term-by-term modulo is congruent modulo (an immediate induction on the number of factors, using that , itself immediate from ), we may replace by throughout:
The left side factors as
Let . We have shown . We now claim . Each satisfies , and a product of integers each coprime to is itself coprime to (if a prime divided the product , then since is prime it would divide one of the factors , contradicting ). Hence . Since and , Euclid's lemma (as invoked above, via Bézout) gives
This completes the proof.
∎If is a prime and is any integer with , then
For a prime , every one of the integers is coprime to (none of them share the factor , since is prime and each is smaller than and nonzero), while itself is not coprime to . Hence . The hypothesis is exactly (since the only divisors of the prime are and ). Applying Euler's Theorem with gives .
∎If , then exists and divides .
Existence: Euler's theorem exhibits as a positive integer with , so the set of such positive exponents is nonempty; by well-ordering it has a least element, which is by definition. Call it . Divisibility: apply the division algorithm to write with . Then
using from the definition of . So with . Since is the smallest positive exponent with this property, cannot be a positive integer less than satisfying ; hence . Thus , i.e., .
∎Fast modular exponentiation
Euler's theorem tells us , but in applications (and especially in cryptography) we need to compute for specific, often astronomically large, exponents — we cannot simply reduce modulo unless we already know , and even then we must still exponentiate efficiently. The naive approach of multiplying by itself times is computationally infeasible once has, say, hundreds of digits. The algorithm of repeated squaring (also called binary exponentiation, or exponentiation by squaring) solves this by exploiting the binary representation of .
Let be integers with and . Write the binary expansion of as
Define the sequence , and for (so ). Then
and this value can be computed using at most modular multiplications (each performed on numbers already reduced modulo ), i.e. multiplications, as opposed to multiplications for naive repeated multiplication.
(Proof by induction on , then assembly of the binary sum — the technique of "repeated squaring".) Claim: for all . We induct on . Base case : . Inductive step: suppose . Since congruences are preserved under multiplication (as used earlier), squaring both sides gives . Reducing modulo does not change the residue class, so . This completes the induction. Assembling . By the binary expansion, , so by the laws of exponents,
(Terms with contribute and drop out.) Using the Claim, for each appearing in the product, and since a product of congruent terms is congruent (as before), we get
Reducing the right side modulo at the end (or, equivalently, at each multiplication step, which yields the same residue class by the same congruence-preservation fact) gives , as claimed. This proves correctness. Complexity count. Computing requires squarings (one modular multiplication each), where is the position of the leading bit of (since ). Forming the product requires at most further modular multiplications (one for each additional term beyond the first factor included). Hence the total number of modular multiplications is at most , which is . Crucially, every multiplication is immediately followed by a reduction modulo (in computing each and each partial product), so every number ever multiplied is already less than in absolute value; the multiplicands never grow beyond roughly before reduction, regardless of how large is. This is what makes the algorithm practical even when has hundreds or thousands of digits.
∎Worked examples
Example 1 (using Euler's theorem to reduce a large exponent, non-prime modulus). Compute . First, since and is divisible by neither nor . Compute : using the standard product formula (a consequence of the multiplicativity of , itself provable from the definition, though we take it as known here per the prerequisites) . By Euler's theorem, . Now reduce the exponent: write , so exactly, with remainder . Then
Let's double check independently by directly computing small powers of mod : , (since ). So actually the true order of modulo is , a proper divisor of , consistent with the Corollary above (, and indeed ). Since is even, , confirming the answer . This illustrates an important subtlety: Euler's theorem always gives a valid exponent () that works, but the true period (the order) can be much smaller, and knowing the order lets us reduce even faster — though Euler's theorem alone, without knowing the order, already suffices to get the correct final answer via the division .
Example 2 (fast modular exponentiation by hand, asymmetric numbers). Compute using repeated squaring, and cross-check with Euler's theorem. First, , so (using multiplicativity of on the coprime factors ). Since (as shares no factor with or ), Euler's theorem gives . Binary expansion of the exponent. . Check: . ✓. So in binary, , i.e. bits . Building the squaring tower : . . . Since and , . . Since , , so . . Since , , so . (Sanity check on the cycle: and — this recurrence is plausible since has some order dividing , and squaring is periodic; we don't need this observation for the computation, we just continue mechanically.) (identical computation to , since ). (identical computation to ). So the tower is:
Assembling the answer. We need for (bits equal to at positions , matching ✓). So
Compute step by step, reducing modulo after each multiplication: . : , . Running product: . . : , . Running product: . . : , . Running product: . (computed above). Running product: . So . Cross-check via Euler's theorem. We know . Write , so . We can verify independently: note , using bits at . We already have ; we need (already computed above). So
(as before). ; , remainder . Running product: . ; , . Running product: . Both routes give , confirming the computation and simultaneously illustrating how Euler's theorem can be combined with repeated squaring to shrink the exponent before squaring, cutting the work further.
Consider , so . Take , which is not coprime to (indeed ). If Euler's theorem held without the coprimality hypothesis, we would expect
But , and , so , not . In fact for every (an easy induction: , and if then ), so is never congruent to for any positive exponent — the sequence of powers of mod is eventually constant at , never reaching . This shows the hypothesis is not a technical nicety but is essential: without it, need not even lie in the multiplicative structure where "returning to " makes sense, since may not be invertible modulo at all (an element has a multiplicative inverse mod exactly when , by Bézout's identity), and the permutation argument in the Lemma breaks down completely: multiplication by a non-invertible is not injective on residues mod , so it cannot permute any set of representatives.
Scope, edge cases & common traps
| Situation | What happens | Correct handling |
|---|---|---|
| , arbitrary | Euler's theorem applies directly | |
| Theorem does not apply; conclusion can fail | Factor out common divisors first, or use other tools (e.g. CRT-style case analysis) | |
| prime | Euler's theorem reduces to Fermat's Little Theorem | |
| Exponent larger than | Reduce only when | , valid because |
| True order of smaller than | Reduction modulo the true order is even more efficient | Order always divides , but need not equal it |
| Computing without factoring | Practically hard for large (this hardness is the basis of RSA) | Only feasible when the factorization of is known |
The mathematician behind it
Origins · 1 minLeonhard Euler and how this was discovered
Leonhard Euler was among the most prolific mathematicians in history, contributing foundational results across nearly every branch of mathematics then in existence, from analysis and mechanics to graph theory and number theory. His work on number theory, though only a fraction of his total output, includes the generalization of Fermat's Little Theorem that now bears his name, introduced in a paper of 1763 and refined further in subsequent decades; he also introduced the totient function itself as the natural tool needed to state the generalization correctly for composite moduli.
Why does Euler's theorem require instead of working for all integers ?
The proof relies on multiplication by being a bijection on the set of residues coprime to . This bijectivity itself relies on being invertible modulo , which by Bézout's identity happens exactly when . If shares a factor with , multiplication by can collapse distinct residues onto the same image (it fails to be injective), so the permutation argument collapses. Concretely, as shown in the counterexample, gives forever, never reaching , because is a zero divisor modulo rather than a unit. The coprimality condition is precisely the condition that makes a genuine element of the finite group .
How is Fermat's Little Theorem related to Euler's theorem?
Fermat's Little Theorem is the special case of Euler's theorem where the modulus is a prime . Since every integer from to is automatically coprime to a prime , the totient satisfies , and Euler's theorem becomes , which is exactly Fermat's statement. Historically the order ran the other way: Fermat stated the prime case first (around 1640), and Euler later realized the same permutation argument works for any modulus once replaces , producing the fully general theorem in 1763.
Can Euler's theorem be used to find , the modular inverse of ?
Yes. If , Euler's theorem gives , which can be rewritten as . This shows is a multiplicative inverse of modulo , computable via fast modular exponentiation in multiplications. In practice, the Extended Euclidean Algorithm is often faster since it avoids needing to know , but the Euler-theorem approach is conceptually clean and is exactly how RSA decryption exponents relate to encryption exponents via inverses modulo .
Does repeated squaring change the mathematical value of , or just how fast we compute it?
It only changes the computational route, never the value. The theorem proved above shows the repeated-squaring procedure computes the same residue class as direct exponentiation followed by reduction mod ; this is guaranteed by the laws of exponents ( over the set bits of ) together with the fact that reducing modulo at intermediate steps never changes the final congruence class, since congruence is preserved under multiplication. The entire benefit is algorithmic efficiency: replacing up to multiplications with about multiplications, while keeping every intermediate number bounded roughly by rather than growing without bound.
What happens if the exponent is negative or ?
Euler's theorem as stated requires , which already excludes whenever (since ). For negative exponents, must first be computed (which requires , guaranteeing an inverse exists by Bézout's identity), and then for is computed as using the same repeated-squaring algorithm applied to the inverse. So negative exponents are handled by first inverting, not by any modification of the core theorem.
Why is factoring relevant to computing , and why does this matter for cryptography?
The formula requires knowing the distinct prime factors of . For a general large , factoring is believed to be computationally hard (no known efficient classical algorithm), so is hard to compute without factoring first. RSA exploits exactly this asymmetry: the public modulus is published, but can only be efficiently computed by someone who knows the prime factors , kept secret. Euler's theorem is what makes the decryption exponent work correctly once is known, while the difficulty of finding from alone is what keeps the scheme secure.
Is the converse of Euler's theorem true — if for some , must ?
Yes, and this direction is easy: if for some positive integer , then , so for some integer , giving . This is an integer combination of and equal to , so by Bézout's identity (or its converse direction, which is elementary: any common divisor of and must divide ) we conclude . So coprimality is not just sufficient but also necessary for to have any finite multiplicative order modulo ; this is proved directly here, not merely asserted.