Lecture 18

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:

Proposition

Let pp be a prime, and let aa be an integer with the property that a21(modp)a^2\equiv 1\pmod{p}. Then either a1(modp)a\equiv1\pmod{p} or a1(modp)a\equiv-1\pmod{p}.

Proof

If a21(modp)a^2\equiv 1\pmod{p}, then a210(modp)a^2-1\equiv 0\pmod{p}, ie (a1)(a+1)0(modp)(a-1)(a+1)\equiv 0\pmod{p}. In other words, p(a1)(a+1)p\mid(a-1)(a+1).

But then, either pa1p\mid a-1 (in which case a1(modp)a\equiv1\pmod{p}), or pa+1p\mid a+1 (in which case a1(modp)a\equiv-1\pmod{p}).

This theorem is not true for some composite moduli! For example, 123252721(mod8)1^2\equiv 3^2\equiv 5^2\equiv 7^2\equiv 1\pmod{8}.

I regard this as more evidence that prime moduli behave very nicely indeed!

This means that if we have aa not congruent to ±1\pm 1 modulo a prime pp, then the inverse of aa (modulo pp) is different to aa.

Indeed, if aa1a\equiv a^{-1} then 1aa1a21\equiv aa^{-1}\equiv a^2.

Now, this allows us to do this:

Theorem

[Wilson’s Theorem] We have (n1)!1(modn)(n-1)!\equiv -1\pmod{n} if and only if nn is prime.

Proof

I’ll show firstly that if nn is composite, we don’t get (n1)!1(modn)(n-1)!\equiv-1\pmod{n}.

Indeed, suppose that nn has a factor aa such that 1<a<n1<a<n. Then we certainly have a(n1)!a\mid(n-1)!, and so (n1)!0(moda)(n-1)!\equiv 0\pmod{a}. However, if (n1)!1(modn)(n-1)!\equiv -1\pmod{n} and ana|n, then (n1)!1(moda)(n-1)!\equiv -1\pmod{a}, which gives a contradiction.

Now I’ll show that if nn is prime we do get (n1)!1(modn)(n-1)!\equiv-1\pmod{n}.

Given that nn is prime, the product (n1)!=12(n1)(n-1)! = 1\cdot 2\cdot\cdots\cdot(n-1) 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 11 and 1-1. So, the product consists of a lot of pairs of inverses (whose product modulo nn is 11), together with the odd ones out 11 and 1-1: so the product is 1-1 as claimed.

Here are some examples:

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 NN, it’s quicker to do trial division to see if NN 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 NN is prime or not by looking at how arithmetic modulo NN 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 and Bob are of course named so that the message goes from AA to BB. 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 AQA\mapsto Q, BJB\mapsto J, 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:

  1. Bob comes up with a system of encrypting and decrypting of that sort.

  2. Bob takes the key which tells you how to decrypt messages, the private key, and keeps it to himself, never telling anyone.

  3. 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.

  4. Alice uses Bob’s public key to encrypt a message for Bob.

  5. Alice sends Bob the encrypted message.

  6. 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 pqpq, where pp and qq are (different) primes. We’re going to need to do modular arithmetic mod pqpq, including exponentiation. So we’ll need to see what Fermat-Euler says:

Proposition

Let pp and qq be different primes. Then the number φ(pq)\varphi(pq), of integers from 11 to pqpq coprime to pqpq, is given by φ(pq)=(p1)(q1).\varphi(pq) = (p-1)(q-1).

Proof

There are pqpq integers aa between 11 and pqpq. Those that are not coprime to pqpq are either multiples of pp or of qq.

Of these, qq of them are multiples of pp (namely p,2p,,pqp,2p,\ldots,pq).

Also, pp of them are multiples of qq (namely q,2q,,pqq,2q,\ldots,pq).

Lastly, one of them (namely pqpq) is a multiple of pp and of qq.

Hence q+p1q+p-1 are not coprime to pqpq, and so φ(pq)=pqqp+1=(p1)(q1).\varphi(pq) = pq-q-p+1 = (p-1)(q-1).

As a result of that, we know (from the Fermat-Euler Theorem that, for all aa coprime to pqpq, we have a(p1)(q1)1(modpq),a^{(p-1)(q-1)} \equiv 1 \pmod{pq}, and indeed ak(p1)(q1)1(modpq).a^{k(p-1)(q-1)} \equiv 1 \pmod{pq}. for all kk.

So, Bob chooses two fairly large primes pp and qq, and keeps them secret. He also chooses a number ee which is coprime to (p1)(q1)(p-1)(q-1).

He also calculates the inverse dd to ee, modulo (p1)(q1)(p-1)(q-1), by using Euclid’s algorithm.

His public key consists of pqpq and ee, so he sends that to Alice (and Eve); his private key consists of pqpq and dd. He shreds any evidence of what pp and qq are.

Alice represents her message as a number mm between 11 and pqpq. It is overwhelmingly likely that her choice will be coprime to pqpq. She calculates me(modpq)m^e\pmod{pq} and sends it on to Bob.

Bob receives this number mem^e from Alice, and raises it to the power dd modulo pqpq. He thus obtains something congruent to (me)d=mde.(m^e)^d = m^{de}.

Now, because de1(modφ(pq))de\equiv 1\pmod{\varphi(pq)}, we have de=1+kφ(pq)de=1+k\varphi(pq) for some kk. As a result, (me)d=mde=m1+kφ(pq)=m(mφ(pq))km1km(modpq).(m^e)^d {}= m^{de} {}= m^{1+k\varphi(pq)} {}= m(m^{\varphi(pq)})^k {}\equiv m1^k {}\equiv m\pmod{pq}.

Hence, using his private key, Bob can recover what mm was from being told mem^e.

The idea is that it should be very hard for anyone else to work out dd from pqpq and ee; we did this using Euclid’s algorithm, but we needed to know more than just pqpq: we needed to know (p1)(q1)(p-1)(q-1).

So the security of this approach depends (among other things) on it being difficult to factorise the number pqpq: if factorising large numbers were easy, we could get pp and qq 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.