We worked with the factorial in the proof of Fermat’s Little Theorem without ever needing to calculate it. It turns out we can calculate it, using a clever trick.
However, we’ll need a fact first:
Let be a prime, and let be an integer with the property that . Then either or .
If , then , ie . In other words, .
But then, either (in which case ), or (in which case ).
This theorem is not true for some composite moduli! For example, .
I regard this as more evidence that prime moduli behave very nicely indeed!
This means that if we have not congruent to modulo a prime , then the inverse of (modulo ) is different to .
Indeed, if then .
Now, this allows us to do this:
[Wilson’s Theorem] We have if and only if is prime.
I’ll show firstly that if is composite, we don’t get .
Indeed, suppose that has a factor such that . Then we certainly have , and so . However, if and , then , which gives a contradiction.
Now I’ll show that if is prime we do get .
Given that is prime, the product consists of one representative of each invertible residue class.
We can pair each up with its inverse; each element gets paired with another, except for and . So, the product consists of a lot of pairs of inverses (whose product modulo is ), together with the odd ones out and : so the product is as claimed.
Here are some examples:
, and so is composite.
, and so is prime. Indeed, and are inverses, and and , and and , and and .
You could use this as a way of testing if a number is prime.
As a matter of fact, it’s not a good way of doing it: if we want to check a large number , it’s quicker to do trial division to see if has any factors, than it is to multiply lots of numbers together.
But this result was psychologically important in the development of modern fast primality tests: it was the first evidence that there are ways of investigating whether a number is prime or not by looking at how arithmetic modulo behaves.
Public Key Cryptography
In this section, we’ll show off a major modern application of all the ideas above. The aim is to talk about (one small but key part of) modern cryptography.
Cryptography is the study of how to send messages in a form which cannot be read except by the intended recipients. To encrypt the messages is to put them in a form which cannot be read easily; to decrypt the messages is to take such messages and recover them in readable form.
The literature of cryptography usually talks about three people:
Alice who wishes to send a private message to Bob,
Bob who wishes to receive a private message from Alice, and
Eve who wishes to find out what Alice is telling Bob.
Alice and Bob are of course named so that the message goes from to . Eve is so named because she is an eavesdropper, or perhaps because she is evil.
In the olden days, Alice and Bob would have come up with some kind of system depending on a shared secret key with which you could encrypt and decrypt a message. Perhaps you’ve seen many of these techniques already! For example, you could substitute the letters of the alphabet according to some agreed system: then the key would describe that system and would be a list of facts like , , etc.
The big disadvantage with that is that Alice and Bob have to exchange the key somehow at the beginning: if Eve can spy on that conversation, she has the key and can decrypt Alice’s message just as easily as Bob can.
The problem with this old-time approach is that the same secret is used to encrypt and decrypt the message, so needs exchanging.
Suppose instead there was a type of encryption with a key for encryption and another key for decryption, such that, even if you know exactly how to encrypt a message, it is very hard indeed to work out how to decrypt it.
That suggests the following plan:
Bob comes up with a system of encrypting and decrypting of that sort.
Bob takes the key which tells you how to decrypt messages, the private key, and keeps it to himself, never telling anyone.
Bob takes the key which tells you how to encrypt messages, the public key, and shares it with everyone who wants it, with no secrecy whatsoever. In particular, he sends Alice a postcard telling her his public key. Of course Eve finds it out quickly, but Bob doesn’t care.
Alice uses Bob’s public key to encrypt a message for Bob.
Alice sends Bob the encrypted message.
Bob uses his private key to decrypt it, and read Alice’s message.
So the only question is, how can we come up with such a system, where being able to encrypt things doesn’t help you decrypt things very much?
The approach we’ll describe was the first one to be thought of, in the 1970s. It is known as RSA after its American inventors Rivest, Shamir and Adleman. (A British mathematician, Cocks, invented it a few years earlier, but he was working in secret for the government, so this was not known for many years). RSA is still in very widespread use on the internet.
The secret of RSA is to work modulo , where and are (different) primes. We’re going to need to do modular arithmetic mod , including exponentiation. So we’ll need to see what Fermat-Euler says:
Let and be different primes. Then the number , of integers from to coprime to , is given by
There are integers between and . Those that are not coprime to are either multiples of or of .
Of these, of them are multiples of (namely ).
Also, of them are multiples of (namely ).
Lastly, one of them (namely ) is a multiple of and of .
Hence are not coprime to , and so
As a result of that, we know (from the Fermat-Euler Theorem that, for all coprime to , we have and indeed for all .
So, Bob chooses two fairly large primes and , and keeps them secret. He also chooses a number which is coprime to .
He also calculates the inverse to , modulo , by using Euclid’s algorithm.
His public key consists of and , so he sends that to Alice (and Eve); his private key consists of and . He shreds any evidence of what and are.
Alice represents her message as a number between and . It is overwhelmingly likely that her choice will be coprime to . She calculates and sends it on to Bob.
Bob receives this number from Alice, and raises it to the power modulo . He thus obtains something congruent to
Now, because , we have for some . As a result,
Hence, using his private key, Bob can recover what was from being told .
The idea is that it should be very hard for anyone else to work out from and ; we did this using Euclid’s algorithm, but we needed to know more than just : we needed to know .
So the security of this approach depends (among other things) on it being difficult to factorise the number : if factorising large numbers were easy, we could get and for ourselves from Bob’s public key. Currently, we know of no way to do this fast enough: we know how to generate primes that are hundreds of digits long, but not to factorise a product of two of them.