Primes, Fermat, Euler and fast modular exponentiation
The fundamental theorem of arithmetic, Fermat's little theorem, square-and-multiply exponentiation worked in full on 5^117 mod 19, and Euler's totient function and theorem.
- State the fundamental theorem of arithmetic and factor an integer into its prime powers.
- Apply Fermat's little theorem and verify 3^6 = 1 (mod 7) by direct computation.
- Compute a modular exponentiation by binary decomposition, reproducing every squaring in 5^117 mod 19.
- Compute Euler's totient function directly and via the phi(pq) = (p-1)(q-1) identity, and state Euler's theorem.
22 min readJump to the quiz ↓
Primes are the building blocks, and every integer factors into them in exactly one way
RSA key generation needs two large primes, a count of how many numbers are coprime to their product, and an exponent computed against that count. This page covers the tools for all three. Primes come first: only resists factoring because and are hard to find.
A prime number has exactly two positive divisors, and itself. It cannot be written as a product of two smaller positive integers. The fundamental theorem of arithmetic states that any integer factors into primes in exactly one way: Uniqueness is the load-bearing part: there is no second, different set of prime powers that also multiplies out to .
Try it — worked example
Write 360 as a product of primes.
Show answer and steps
Answer360 = 2^3 x 3^2 x 5
- .
- .
- Combining: . Check: .
Pitfall
The Week 4 number-theory supplement’s own second example, factoring , prints . That is wrong: , not . The correct factorisation is (); the exponent on is , not . The example above is printed correctly in the source and needs no correction.
Fermat’s little theorem makes powers predictable, and square-and-multiply makes them cheap
Fermat’s little theorem states that for a prime and any integer not divisible by , Raising to one less than the prime always lands on , modulo that prime. This is why lands back on in the generalisation below, which is the fact RSA decryption depends on.
Formula
Fermat's little theorem
- a prime number
- any integer not divisible by p
Try it — worked example
Check Fermat’s little theorem for p = 7, a = 3: what is 3^6 mod 7?
Show answer and steps
Answer3^6 = 1 (mod 7)
Take , . Fermat predicts .
- .
- : , and . So .
- , confirming the theorem.
Computing directly and then reducing works for small numbers, but the source also shows a second route that reduces at every step: , , and then Both routes agree. The second is the habit that matters: it keeps every intermediate value small and generalises to exponents far too large to compute directly.
At the size RSA and Diffie-Hellman use, computing in full before reducing is not an option, because the intermediate number would run to thousands of digits. Square-and-multiply fixes this by writing the exponent in binary and building the answer from repeated squarings of , mod , at every step, combining only the squarings that correspond to a bit.
Try it — worked example
Use square-and-multiply to compute 5^117 mod 19.
Show answer and steps
Answer5^117 mod 19 = 1
- Write in binary: , i.e. . So .
- .
- .
- .
- (since ).
- .
- (since ).
- (since ).
- Combine the terms needed for : .
- . : , so . Thus .
That worked example builds the powers of two from the bottom up and multiplies together the ones the binary expansion calls for. The tracer below runs the same idea from the top bit down: one running accumulator, squared every step, with multiplied in whenever the current bit is . Type base , exponent , modulus into it to reach the same answer.
Exam detail
The point of this method is the operation count. Multiplying by itself times is multiplications, infeasible once has hundreds of digits. Square-and-multiply needs one squaring per bit of and one extra multiply per bit, so the cost is modular multiplications: for a 2048-bit RSA exponent, roughly 2048 squarings instead of multiplications. Diffie-Hellman key exchange, covered in Diffie-Hellman key exchange, runs on exactly this operation.
The totient counts the numbers coprime to n, and for a product of two primes it is (p-1)(q-1)
Fermat’s little theorem only counts, implicitly, the nonzero residues mod a prime . Euler’s totient function generalises that count to any modulus: is the number of positive integers less than that are relatively prime to . When is prime, every integer from to is coprime to , so .
Try it — worked example
Find φ(37).
Show answer and steps
Answerphi(37) = 36
- is prime.
- Every integer from through shares no factor with a prime, so all of them are coprime to .
- .
For that is not prime, the direct method is to list every integer below and remove the ones that share a factor with . The supplement demonstrates this on .
Try it — worked example
Find φ(35) by listing the numbers that are not coprime to 35.
Show answer and steps
Answerphi(35) = 24
- , so an integer from to fails to be coprime to exactly when it is a multiple of or of .
- Multiples of below : , six numbers.
- Multiples of below : , four numbers. ( itself is excluded by “below 35”.)
- No overlap between the two lists, since is not below . So integers remain.
- The coprime set is , numbers. .
Pitfall
The Week 4 supplement’s own worked version of this example lists every integer from through , multiples of and included, and then states “there are 24 numbers on the list.” The printed list is just the full run of consecutive integers, not the coprime set. The same happens for below. In both cases the final count is correct; only the printed enumeration is wrong. The coprime lists on this page are the ones that belong to and .
Formula
Totient of a product of two primes
- two distinct primes
- their product
This is the identity RSA key generation runs directly, and the reason listing every integer below n is never actually necessary when n's factorisation is known.
Try it — worked example
Find φ(21) using the product-of-two-primes rule.
Show answer and steps
Answerphi(21) = 12
- , both prime.
- .
- Direct check: the integers from to coprime to (excluding multiples of and of ) are , numbers, agreeing with the formula.
Exam detail
Whenever ‘s prime factorisation is already known, the identity is faster and safer than listing and counting. RSA key generation never enumerates anything: it computes directly from the two primes it just generated. See extended Euclid and modular inverses for how feeds into finding the private exponent .
Euler’s theorem extends Fermat’s to any modulus coprime to the base
Euler’s theorem generalises Fermat’s little theorem from a prime modulus to any modulus, provided the base and modulus are coprime: An equivalent form is . Setting prime recovers Fermat’s theorem exactly, since .
Try it — worked example
Check Euler’s theorem for a = 3, n = 5: what is 3^φ(5) mod 5?
Show answer and steps
Answer3^4 = 1 (mod 5)
Take , . , since is prime.
- , so Euler’s theorem applies.
- .
- : , so .
- , as the theorem predicts.
Recall
Why does Fermat's little theorem count as a special case of Euler's theorem, rather than a separate result?
Euler’s theorem says for any coprime to . When is a prime , every integer from to is automatically coprime to , and . Substituting recovers exactly: Fermat’s statement, with no extra assumption needed beyond being prime.
Check yourself
Source
Week 4 Number Theory PDF