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 read
Intuition
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. That is three separate jobs, and this page covers the tools for all three. Primes are the raw material: only resists factoring because and are hard to find. Fermat’s and Euler’s theorems are what make modular exponentiation predictable instead of chaotic. They are the reason always lands back on , which is exactly the fact RSA decryption depends on. And fast exponentiation is what makes any of this usable at real key sizes: an RSA exponent can run to hundreds of digits, and multiplying a number by itself that many times, one step at a time, is not something a computer finishes in a human lifetime. Binary decomposition turns that into a few hundred squarings instead.
Mechanism
A prime number has exactly two positive divisors, and itself. It cannot be written as a product of two smaller positive integers. This makes primes the multiplicative building blocks of every integer, a fact strong enough to have its own theorem.
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 .
Worked example
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.
Mechanism
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.
Worked example
Answer3^6 = 1 (mod 7)
Take , . Fermat predicts .
- .
- : , and . So .
- , confirming the theorem.
Mechanism
Computing directly and then reducing works fine for small numbers, but the source also shows a second route that reduces at every step instead of at the end: , , and then Both routes agree. The second one is the more important habit: it keeps every intermediate value small, and it generalises to exponents far too large to compute directly. That generalisation is square-and-multiply.
Mechanism
For a modulus and exponent the size RSA and Diffie-Hellman actually use, computing in full before reducing is not an option: 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.
Worked example
Answer5^117 mod 19 = 1
- Write in binary: , i.e. . So .
- .
- .
- .
- (since ).
- .
- (since ).
- (since ).
- Combine the terms needed for : .
- . : , so . Thus .
Aside
This reaches the answer by computing powers of two from the bottom up () and multiplying together the ones the binary expansion calls for. The tracer below implements the same idea the other way round, from the top bit down: it keeps a single running accumulator, squaring it every step and multiplying in whenever the current bit is . Both are square-and-multiply; they only differ in which end of the exponent they start from. Type base , exponent , modulus into the tracer to see the second route 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.
Formula
Fermat's little theorem
- a prime number
- any integer not divisible by p
Mechanism
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 .
Worked example
Answerphi(37) = 36
- is prime.
- Every integer from through shares no factor with a prime, so all of them are coprime to .
- .
Mechanism
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 .
Worked example
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 , all of them, multiples of and included, and then states “there are 24 numbers on the list.” The printed list does not match that claim: it is not the coprime set, just the full run of consecutive integers. The same pattern repeats for below. In both cases the final count is correct; only the printed enumeration is wrong. The coprime lists above are the ones that actually 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.
Worked example
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 always 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 .
Mechanism
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 .
Worked example
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.
Source
Week 4 Number Theory PDF