Key exchange and encryption [01F2]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
Although one may (and should!) learn elementary number theory for love of the game, it actually has some “practical” applications. One of these is cryptography. We will see how modular arithmetic may be used to agree upon a private key over an insecure line, via the Diffie–Hellman protocol; another application is Shamir’s three-pass protocol, which uses Fermat’s Little Theorem to privately send messages without exchanging keys.
The Diffie–Hellman key-exchange protocol [01F4]
- October 29, 2026
- Marcelo Fiore, Jon Sterling
First some pre-mathematical intuition:
Illustration Intuition for the Diffie–Hellman key exchange protocol [01F3]
- October 29, 2026
In the illustration, paint colours are used as a metaphor for numerical data; the mixing of paint is a metaphor for applying efficiently computable arithmetic operations to numbers that are prohibitively difficult to undo. It is important, above, that mixing of paints is a commutative operation, since otherwise Alice and Bob would not end up with the same secret colour. Modular arithmetic provides one source of such operations: modular exponentiation is easy to compute, but the inverse operation (discrete logarithm) is very hard to compute.
The Diffie–Hellman protocol, formally [01F7]
- October 29, 2026
- Marcelo Fiore, Jon Sterling
In this protocol, Alice and Bob will agree upon a shared secret key by communicating in public.
- In public, Alice and Bob agree upon an integer c and a prime p.
- Privately, Alice chooses her own prive key \mathsf {priv}_{A} and Bob separately decides upon his own private key \mathsf {priv}_{B}.
- Individually, Alice and Bob define public keys as follows by modular exponentiation using their private keys: \begin {aligned} \mathsf {pub}_{X} \coloneqq {\mathopen {}\left [c^{\mathsf {priv}_{X}}\right ]\mathclose {}}_p \quad {\mathopen {}\left (X\in {\mathopen {}\left \{A,B\right \}\mathclose {}}\right )\mathclose {}} \end {aligned}
- In public, Alice and Bob then transmit to each other their public keys.
- In private, Alice and Bob both compute their own copies of the shared secret key as follows: \begin {aligned} K_A&\coloneqq {\mathopen {}\left [\mathsf {pub}_{B}^{\mathsf {priv}_{A}}\right ]\mathclose {}}_p \\ K_B&\coloneqq {\mathopen {}\left [\mathsf {pub}_{A}^{\mathsf {priv}_{B}}\right ]\mathclose {}}_p \end {aligned}
Finally, we show that Alice’s and Bob’s computations of the shared secret agree in the sense that K_A=K_B\text {.}
Proof.
- October 29, 2026
- Marcelo Fiore, Jon Sterling
We calculate:
\begin {aligned} K_A &\equiv \mathsf {pub}_{B}^{\mathsf {priv}_{A}} &&\mod p \\ &\equiv {\mathopen {}\left (c^{\mathsf {priv}_{B}}\right )\mathclose {}}^{\mathsf {priv}_{A}} &&\mod p \\ &\equiv c^{\mathsf {priv}_{B}\cdot \mathsf {priv}_{A}} &&\mod p \\ &\equiv c^{\mathsf {priv}_{A}\cdot \mathsf {priv}_{B}} &&\mod p \\ &\equiv {\mathopen {}\left (c^{\mathsf {priv}_{A}}\right )\mathclose {}}^{\mathsf {priv}_{B}} &&\mod p \\ &\equiv \mathsf {pub}_{A}^{\mathsf {priv}_{B}} &&\mod p \\ &\equiv K_B &&\mod p \end {aligned}Therefore, K_A=K_B.
Why exponentiation modulo a prime? [01F8]
- October 29, 2026
- Jon Sterling
We have already commented that modular exponentiation is easy to compute but very difficult to invert. But why don’t we do exponentiation modulo a non-prime number?
The protocol itself does not depend on the modulus being prime. To break Diffie–Hellman, you might try to compute discrete logarithms; discrete logarithms over a prime modulus is very very hard; if your modulus is composite, then the Chinese Remainder Theorem can be used to reduce the discrete logarithm problem to the prime factors of the modulus. So Diffie–Hellman over a prime modulus has only one avenue of attack: computing the discrete logarithm of a big prime. In contrast, Diffie–Hellman over a composite modulus has two avenues of attack: computing the discrete logarithm of a big number, or factoring the big number and then computing the discrete logarithms of some little primes.
Encryption and decryption via modular exponentiation [01F9]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
We now consider some other additional cryptographic protocols that are based on having separate keys for encryption and for decryption. In the Diffie–Hellman protocol, each actor had just one private key, whereas we will now consider protocols in which the private keys are split into two: an encryption key and a decryption key. The relationship between encryption and decryption keys will then be governed by Fermat’s little theorem.
We are going to use modular exponentiation to model encryption and decryption. In particular, we will be considering pairs of integers {\mathopen {}\left (e,d\right )\mathclose {}} such that k^{e\cdot d} \equiv k \mod p; such a pair is an encryption-decryption pair modulo p. To find a good source of such encryption-decryption pairs, we must return to Fermat’s Little Theorem and develop some of its consequences.
Theorem Fermat’s Little Theorem [01BK]
- September 29, 2026
- Marcelo Fiore, Jon Sterling
For all natural numbers i and primes p, we have the following:
- i^p \equiv i \mod p
- i^{p-1}\equiv 1 \mod p unless i is a multiple of p
For simplicity, we had previously established Fermat’s Little Theorem only for naturals. But it holds, in fact, for all integers, which we shall need:
Corollary Fermat’s Little Theorem for integers [01FC]
- October 31, 2026
- Jon Sterling
For all integers k and primes p, we have the following:
- k^p\equiv k \mod p
- k^{p-1}\equiv 1 \mod p unless k is a multiple of p.
Proof.
- October 31, 2026
- Jon Sterling
This follows straightaway from Fermat’s Little Theorem, because every integer k is congruent to some natural i modulo p: indeed, just add p to k enough times to get into the naturals.
Then we have: k^p \equiv i^p \equiv i \equiv k \mod p and k^{p-1}\equiv i^{p-1}\equiv 1\mod p under the stated assumptions.
Corollary A useful consequence of Fermat’s Little Theorem [01FB]
- October 31, 2026
- Jon Sterling
For every natural number c, integer k, and prime p, the following identity holds modulo p:
k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv k \mod pProof.
- October 31, 2026
- Jon Sterling
We proceed depending on whether p\mid k.
- Suppose that p\mid k. Then k \equiv 0\mod p by definition, so k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv 0 \equiv k\mod p\text {.}
- Suppose on the other hand that p\nmid k. Then by Fermat’s Little Theorem for integers, we have k^{p-1}\equiv 1 \mod p. We therefore deduce k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv k \mod p by elementary equational reasoning as follows: \begin {aligned} k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} &= k\cdot k^{c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \\ &= k\cdot {\mathopen {}\left (k^{p-1}\right )\mathclose {}}^c \\ &\equiv k \cdot 1^c &&\mod p \\ &= k \end {aligned}
Lemma A good condition for encryption-decryption key pairs [01FD]
- October 31, 2026
- Jon Sterling
By Corollary [01FB], for prime p every pair {\mathopen {}\left (e,d\right )\mathclose {}} of positive integers such that e\cdot d \equiv 1 \mod p-1 is an encryption-decryption key pair modulo p in the sense that \forall k:\mathbb {Z}\mathpunct {.} k^{e\cdot d} \equiv k \mod p\text {.}
Proof.
- October 31, 2026
- Jon Sterling
That e\cdot d is congruent to 1 modulo p-1 is equivalent to the existence of some c:\mathbb {Z} such that e\cdot d = 1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}:
\begin {aligned} &e\cdot d \equiv 1 \mod p - 1 \\ &\Longleftrightarrow {\mathopen {}\left (p-1\right )\mathclose {}}\mid {\mathopen {}\left (e\cdot d - 1\right )\mathclose {}} \\ &\Longleftrightarrow \exists c:\mathbb {Z}\mathpunct {.} c\cdot {\mathopen {}\left (p-1\right )\mathclose {}} = e\cdot d - 1 \\ &\Longleftrightarrow \exists c:\mathbb {Z}\mathpunct {.} 1+ c\cdot {\mathopen {}\left (p-1\right )\mathclose {}} = e\cdot d \end {aligned}Because d,e are positive, we know that e\cdot d - 1 is non-negative and hence any such c is also non-negative, so we have c\in \mathbb {N}.
Now, we fix an integer k to check that k^{e\cdot d} \equiv k\mod p, which follows below from Corollary [01FB]:
k^{e\cdot d} = k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}}\equiv k \mod p\text {.}{}Lemma A good source of encryption-decryption key pairs [01FE]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
Let p be prime and let e be a positive integer coprime with p-1. Define d \coloneqq {\mathopen {}\left [\operatorname {lc}_2{\mathopen {}\left (p-1,e\right )\mathclose {}}\right ]\mathclose {}}_{p-1}\text {.}
Then for all integers k we have k^{e\cdot d}\equiv k \mod p.
Proof.
- October 31, 2026
- Marcelo Fiore, Jon Sterling
That e and p-1 are coprime means that their greatest common divisor is 1. Meanwhile, we recall the representation of greatest common divisors as integer linear combinations, so that we have:
\begin {aligned} 1 &= \gcd {\mathopen {}\left (p-1,e\right )\mathclose {}} \\ &= {\mathopen {}\left (p-1\right )\mathclose {}}\cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot \operatorname {lc}_2{\mathopen {}\left (p-1,e\right )\mathclose {}} \\ &\equiv {\mathopen {}\left (p-1\right )\mathclose {}}\cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot d &&\mod p-1 \\ &\equiv 0 \cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot d &&\mod p - 1 \\ &\equiv e\cdot d &&\mod p-1 \end {aligned}As e\cdot d\equiv 1 \mod p -1, we deduce via Lemma [01FD] that k^{e\cdot d}\equiv k for all integers k.
Example Shamir’s three-pass protocol [01F6]
- October 29, 2026
- Marcelo Fiore, Jon Sterling
Whereas Diffie-Hellman is a way for two actors to agree upon a secret key that neither of them started with, Shamir’s three-pass protocol is a way for one actor to securely send secret information to another. Here is how Alice would send a secret integer k<p to Bob without anyone else being able to intercept it:
- In public, Alice and Bob agree upon a prime p.
- In private, Alice choose a key pair (e_A,d_A) satisfying d_A\cdot e_A\equiv 1 \mod p-1. Bob similarly chooses a key pair (e_B,d_B) satisfying the same property.
- Alice encrypts k as the modular exponentiation {\mathopen {}\left [k^{e_A}\right ]\mathclose {}}_p, and sends it to Bob.
- Bob then encrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B}\right ]\mathclose {}}_p , and the result back to Alice.
- Alice decrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B\cdot d_A}\right ]\mathclose {}}_p and sends it back to Bob.
- Finally, Bob decrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B\cdot d_A\cdot d_B}\right ]\mathclose {}}_p \equiv k^{e_A\cdot e_B\cdot d_A\cdot d_B} \equiv {\mathopen {}\left (k^{e_A\cdot d_A}\right )\mathclose {}}^{e_B\cdot d_B} \equiv k\mod p\text {.} Because k<p, Bob’s final decrypted value is equal to k.
Lemma Encryption/decryption in RSA [01FF]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
Let p,q be distinct primes and let d,e be positive integers such that e\cdot d \equiv 1 \mod {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}. Then for all integers k, we have k^{e\cdot d} \equiv k \mod p\cdot q\text {.}
With what you are expected to know at this point, you would not be able to prove this theorem. I will cheat and use the Chinese Remainder Theorem, which we regrettably will not teach you in this course.
Proof.
- October 31, 2026
- Marcelo Fiore, Jon Sterling
By our assumptions that d,e are positive and that e\cdot d\equiv 1 \mod {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}, we may choose a natural number c such that e\cdot d = 1 + c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}.
Because any two distinct primes are coprime, to verify k^{e\cdot d}\equiv k \mod p\cdot q it suffices by the Chinese Remainder Theorem to show that k^{e\cdot d}\equiv k \mod p and k^{e\cdot d}\equiv k \mod q. To that end, we make a few observations:
- If r\mid k, then k^{e\cdot d} \equiv k\mod r for any natural number r. This is immediate as e,d are positive.
- If p\nmid k then k^{e\cdot d}\equiv k \mod p, and similarly if q\nmid k then k^{e\cdot d}\equiv k\mod q. Without loss of generality, we consider just the first case, where we calculate using Fermat’s little theorem for integers: \begin {aligned} k^{e\cdot d} &= k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}} \\ &= k\cdot {\mathopen {}\left (k^{p-1}\right )\mathclose {}}^{c\cdot {\mathopen {}\left (q-1\right )\mathclose {}}} \\ &\equiv k\cdot 1&&\mod p\\ &= k \end {aligned}
We now conclude that both k^{e\cdot d}\equiv k \mod p and k^{e\cdot d}\equiv k \mod q hold by cases on whether p\mid k and whether q\mid k.