Powers modulo a prime return to on a fixed rhythm, and the problem is to explain that rhythm well enough to compute enormous powers in a handful of steps and to build a secret code on them.
The previous lesson made remainders into a number system and ended by noticing that powers in it repeat. This lesson uses two of its results: has an inverse modulo exactly when , found by running Euclid's algorithm backwards, and the Chinese remainder theorem, by which a number modulo is determined by its remainders modulo and . It also uses Euler's totient from an earlier lesson, the count of integers from to coprime to (the letter here is the totient again, not the golden ratio it stood for in the lesson on linear recurrences).
Powers modulo a prime
Here are the powers of every non-zero class modulo , each entry the previous one times , reduced.
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 4 | 1 | 2 | 4 | 1 |
| 3 | 3 | 2 | 6 | 4 | 5 | 1 |
| 4 | 4 | 2 | 1 | 4 | 2 | 1 |
| 5 | 5 | 4 | 6 | 2 | 3 | 1 |
| 6 | 6 | 1 | 6 | 1 | 6 | 1 |
The last column is all s: every not divisible by has . And each row returns to for the first time after , , , , or steps, all divisors of . The powers of cycle through , while those of and pass through every non-zero class before coming home.
Modulo likewise . For a composite the obvious analogue fails: , not .
Fermat's little theorem
Pierre de Fermat stated the pattern, without proof, in a letter to Bernard Frénicle de Bessy dated 18 October 1640. Leibniz wrote one down in unpublished notes before 1683, and Euler published the first in 1736.
Theorem (Fermat). If is prime and does not divide , then .
Consider the numbers . None is , since by Euclid's lemma a prime dividing divides or , and it divides neither. No two are congruent either: if , multiply both sides by the inverse of , which exists because , to get . So they are the classes in some order, and multiplying all of them together both ways gives
The left side is . Every factor of is coprime to , so is invertible modulo , and cancelling it leaves . For and the products reduce to , a shuffle of to , exactly as the proof says.
Multiplying by gives , true for every integer , since when both sides are . The converse is false. The composite satisfies , and satisfies for every coprime to it, the first of the numbers Robert Carmichael described in 1910. Failing the test does prove compositeness: , so is not prime, and no factor was needed to show it.
Euler's generalisation
Modulo the powers of run , back to after four steps, and , counting . Fermat's proof used only that multiplying by keeps the classes distinct and non-zero, and that their product can be cancelled. Modulo a composite the classes coprime to behave the same way, and Euler saw in 1763 that the proof goes through.
Theorem (Euler). If , then .
The proof is Fermat's with new classes. Let be the integers from to coprime to . Each is coprime to too, since a prime dividing and would divide or . The products are distinct modulo , since is invertible. So they are the reshuffled, and multiplying everything gives , where is the product of the . Being coprime to , cancels. Modulo with , the products reduce to , the same four classes shuffled.
For a prime, and Euler's theorem is Fermat's. The formula from inclusion and exclusion, times for each prime dividing , supplies the exponent: , and for distinct primes .
The hypothesis cannot be dropped: powers of are even, so none is . And is an exponent that always works, not always the smallest: modulo , is already .
Reducing huge exponents
If , powers of repeat with period : writing with gives
Euler's theorem always provides such an , namely . The base is reduced modulo and the exponent modulo , and confusing the two is the commonest error here.
The last digit of a number is its remainder modulo . For , with and , the last digit is that of , which is .
Example. Find the last two digits of .
Work modulo , where and . Since , the task is . Build it up: , then , then . So in fact the period is , and . The last two digits are .
Now you. Find the last two digits of .
Answer
Again reduce the exponent modulo : the task is . Then , , , and . So , and the last two digits are .
Repeated squaring
Reducing the exponent needs , and RSA needs with hundreds of digits long, and multiplying by one step at a time would take multiplications, more than there are atoms in the universe. Squaring doubles the exponent with each multiplication, , , and so on, reduced modulo each time. Any exponent is a sum of powers of , its binary expansion, so multiply together the squares matching its bits.
An exponent with binary digits needs squarings, and if it has ones, further multiplications to combine them: at most in all. For a bit exponent, around in size, that is at most multiplications.
Example. Compute by repeated squaring, and count the multiplications.
In binary . Square six times, reducing modulo : , , , , , . Then combine: , and . So , in multiplications instead of , as Fermat predicts for the prime .
Now you. Compute by repeated squaring, and count the multiplications.
Answer
. The squares are , , , , . Then , and . So , in multiplications.
The RSA cryptosystem
Every cipher before 1976 needed a key agreed in secret, since whoever could lock could unlock. That year Whitfield Diffie and Martin Hellman proposed separating the two: a public key anyone can use to encrypt, and a private key that only its owner uses to decrypt. Ron Rivest, Adi Shamir and Leonard Adleman at MIT found a way to do it in 1977, named RSA from their initials. Clifford Cocks at GCHQ, the British signals intelligence agency, had found the same system in 1973, but it stayed classified until 1997.
To make keys, choose two primes and and let , so . Choose an exponent coprime to , and compute its inverse modulo with extended Euclid, so that . Publish and keep secret. A message is a number . Anyone encrypts it as , and the key holder decrypts by computing , which will turn out to be .
The classic small key takes and . Then and . Take , which is prime and does not divide . Euclid's algorithm runs , , , and backwards
so . Check: . The public key is and the private key is .
Example. Encrypt the message with the public key , and decrypt it.
Here , so square four times modulo : , , and . Then , in five multiplications. Decryption computes . In binary , twelve digits with five ones, so it takes squarings and multiplications, and the result is again.
Now you. Build a key from and with . Find , encrypt , and decrypt the result.
Answer
and . Euclid gives and , so and . To encrypt: , , , so . To decrypt, and the squares of are , , , . Then , and , the message.
Why decryption works
Since , write for some integer . Decrypting computes , and when Euler's theorem finishes it:
Since , the remainder is itself. For the key above, .
If shares a factor with , Euler's theorem says nothing, and the Chinese remainder theorem steps in: check modulo and separately. Suppose but does not. Modulo both and are . Modulo , Fermat gives , and , so . The two numbers agree modulo and modulo , so by the uniqueness half of the theorem they agree modulo . (If both primes divide , then .) With the key above, encrypts to , and .
Secure only while factoring is hard
An attacker sees , and . The route to is through , and finding is exactly as hard as factoring . The primes give , and conversely , so knowing it gives , and with the primes are the roots of . For the toy key, , and has roots and .
Multiplying two primes takes an instant, and no known method on an ordinary computer undoes it in time polynomial in the number of digits. That asymmetry is the whole lock. The best general method, the number field sieve, factored RSA-250, a 250 digit (829 bit) challenge number, in February 2020, after roughly 2700 core-years of computation by Fabrice Boudot, Paul Zimmermann and four colleagues. Recommended keys are bits and up, digits or more, far beyond that reach. Nobody has proved that factoring is hard, so the security is an empirical bet, and in 1994 Peter Shor gave an algorithm that factors in polynomial time on a large quantum computer, which no machine built so far is anywhere near big enough to run.
The integers are one kind of discrete structure, rich enough to protect the traffic of the internet. Another is the network (cities and roads, bridges over a river, people and friendships), where all that matters is what is joined to what, and the next lesson models it with graphs.