Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Fermat, Euler and RSA

Powers modulo a prime return to 1 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: [a] has an inverse modulo n exactly when gcd(a,n)=1, found by running Euclid's algorithm backwards, and the Chinese remainder theorem, by which a number modulo pq is determined by its remainders modulo p and q. It also uses Euler's totient φ(n) from an earlier lesson, the count of integers from 1 to n coprime to n (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 7, each entry the previous one times a, reduced.

aa1a2a3a4a5a6
1111111
2241241
3326451
4421421
5546231
6616161

The last column is all 1s: every a not divisible by 7 has a6≡1(mod7). And each row returns to 1 for the first time after 1, 3, 6, 3, 6 or 2 steps, all divisors of 6. The powers of 2 cycle through 2,4,1, while those of 3 and 5 pass through every non-zero class before coming home.

Modulo 11 likewise 210=1024=93×11+1. For a composite the obvious analogue fails: 39=19683≡3(mod10), not 1.

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 p is prime and p does not divide a, then ap-1≡1(modp).

Consider the p-1 numbers a,2a,3a,…,(p-1)a. None is ≡0(modp), since by Euclid's lemma a prime dividing ka divides k or a, and it divides neither. No two are congruent either: if ia≡ja, multiply both sides by the inverse of a, which exists because gcd(a,p)=1, to get i≡j. So they are the classes 1,2,…,p-1 in some order, and multiplying all of them together both ways gives

a×2a×⋯×(p-1)a≡1×2×⋯×(p-1)(modp)

The left side is ap-1(p-1)!. Every factor of (p-1)! is coprime to p, so (p-1)! is invertible modulo p, and cancelling it leaves ap-1≡1. For p=7 and a=3 the products 3,6,9,12,15,18 reduce to 3,6,2,5,1,4, a shuffle of 1 to 6, exactly as the proof says.

Multiplying by a gives ap≡a(modp), true for every integer a, since when p∣a both sides are 0. The converse is false. The composite 341=11×31 satisfies 2340≡1(mod341), and 561=3×11×17 satisfies a560≡1 for every a coprime to it, the first of the numbers Robert Carmichael described in 1910. Failing the test does prove compositeness: 290≡64(mod91), so 91 is not prime, and no factor was needed to show it.

Euler's generalisation

Modulo 10 the powers of 3 run 3,9,7,1, back to 1 after four steps, and 4=φ(10), counting 1,3,7,9. Fermat's proof used only that multiplying by a keeps the classes 1,…,p-1 distinct and non-zero, and that their product can be cancelled. Modulo a composite n the classes coprime to n behave the same way, and Euler saw in 1763 that the proof goes through.

Theorem (Euler). If gcd(a,n)=1, then aφ(n)≡1(modn).

The proof is Fermat's with new classes. Let r1,…,rφ(n) be the integers from 1 to n coprime to n. Each ari is coprime to n too, since a prime dividing n and ari would divide a or ri. The products are distinct modulo n, since a is invertible. So they are the ri reshuffled, and multiplying everything gives aφ(n)R≡R, where R is the product of the ri. Being coprime to n, R cancels. Modulo 10 with a=3, the products 3,9,21,27 reduce to 3,9,1,7, the same four classes shuffled.

For a prime, φ(p)=p-1 and Euler's theorem is Fermat's. The formula from inclusion and exclusion, φ(n)=n times 1-1p for each prime p dividing n, supplies the exponent: φ(100)=100×12×45=40, and for distinct primes φ(pq)=(p-1)(q-1).

The hypothesis gcd(a,n)=1 cannot be dropped: powers of 2 are even, so none is ≡1(mod10). And φ(n) is an exponent that always works, not always the smallest: modulo 100, 320 is already ≡1.

Reducing huge exponents

If am≡1(modn), powers of a repeat with period m: writing k=mq+s with 0≤s<m gives

ak=(am)qas≡1qas=as(modn)

Euler's theorem always provides such an m, namely φ(n). The base is reduced modulo n and the exponent modulo φ(n), and confusing the two is the commonest error here.

The last digit of a number is its remainder modulo 10. For 32026, with φ(10)=4 and 2026=4×506+2, the last digit is that of 32, which is 9.

Example. Find the last two digits of 32026.

Work modulo 100, where gcd(3,100)=1 and φ(100)=40. Since 2026=40×50+26, the task is 326(mod100). Build it up: 35=243≡43, then 310≡432=1849≡49, then 320≡492=2401≡1. So in fact the period is 20, and 326≡36=729≡29. The last two digits are 29.

Now you. Find the last two digits of 132026.

Answer

Again reduce the exponent modulo 40: the task is 1326(mod100). Then 132=169≡69, 134≡692=4761≡61, 135≡61×13=793≡93, 1310≡932=8649≡49 and 1320≡492≡1. So 1326≡136=134×132≡61×69=4209≡9, and the last two digits are 09.

Repeated squaring

Reducing the exponent needs φ(n), and RSA needs ak(modn) with k hundreds of digits long, and multiplying by a one step at a time would take k-1 multiplications, more than there are atoms in the universe. Squaring doubles the exponent with each multiplication, a2, a4=(a2)2, a8 and so on, reduced modulo n each time. Any exponent is a sum of powers of 2, its binary expansion, so multiply together the squares matching its 1 bits.

An exponent with b binary digits needs b-1 squarings, and if it has w ones, w-1 further multiplications to combine them: at most 2(b-1) in all. For a 2048 bit exponent, around 10616 in size, that is at most 4094 multiplications.

Example. Compute 3100(mod101) by repeated squaring, and count the multiplications.

In binary 100=11001002=64+32+4. Square six times, reducing modulo 101: 32=9, 34=81, 38≡812=6561≡97, 316≡972≡(-4)2=16, 332≡256≡54, 364≡542=2916≡88. Then combine: 364×332≡88×54=4752≡5, and 5×34=405≡1. So 3100≡1, in 6+2=8 multiplications instead of 99, as Fermat predicts for the prime 101.

Now you. Compute 545(mod101) by repeated squaring, and count the multiplications.

Answer

45=1011012=32+8+4+1. The squares are 52=25, 54=625≡19, 58≡361≡58, 516≡3364≡31, 532≡961≡52. Then 52×58=3016≡87, 87×19=1653≡37 and 37×5=185≡84. So 545≡84, in 5+3=8 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 p and q and let n=pq, so φ(n)=(p-1)(q-1). Choose an exponent e coprime to φ(n), and compute its inverse d modulo φ(n) with extended Euclid, so that ed≡1(modφ(n)). Publish (n,e) and keep d secret. A message is a number 0≤m<n. Anyone encrypts it as c≡me(modn), and the key holder decrypts by computing cd(modn), which will turn out to be m.

The classic small key takes p=61 and q=53. Then n=3233 and φ(n)=60×52=3120. Take e=17, which is prime and does not divide 3120. Euclid's algorithm runs 3120=183×17+9, 17=1×9+8, 9=1×8+1, and backwards

1=9-8=2×9-17=2×3120-367×17

so 17-1≡-367≡2753(mod3120). Check: 17×2753=46801=15×3120+1. The public key is (3233,17) and the private key is d=2753.

Example. Encrypt the message m=65 with the public key (3233,17), and decrypt it.

Here 17=100012=16+1, so square four times modulo 3233: 652=4225≡992, 654≡9922≡1232, 658≡12322≡1547 and 6516≡15472≡789. Then c≡789×65=51285≡2790, in five multiplications. Decryption computes 27902753(mod3233). In binary 2753=1010110000012, twelve digits with five ones, so it takes 11 squarings and 4 multiplications, and the result is 65 again.

Now you. Build a key from p=7 and q=13 with e=5. Find d, encrypt m=10, and decrypt the result.

Answer

n=91 and φ(n)=72. Euclid gives 72=14×5+2 and 5=2×2+1, so 1=5-2×(72-14×5)=29×5-2×72 and d=29. To encrypt: 102=100≡9, 104≡81, 105≡810≡82, so c=82. To decrypt, 29=16+8+4+1 and the squares of 82 are 822≡81, 824≡9, 828≡81, 8216≡9. Then 9×81=729≡1, 1×9=9 and 9×82=738≡10, the message.

Why decryption works

Since ed≡1(modφ(n)), write ed=1+kφ(n) for some integer k≥0. Decrypting computes cd≡(me)d=med, and when gcd(m,n)=1 Euler's theorem finishes it:

med=m×(mφ(n))k≡m×1k=m(modn)

Since 0≤m<n, the remainder is m itself. For the key above, ed=46801=1+15×3120.

If m shares a factor with n, Euler's theorem says nothing, and the Chinese remainder theorem steps in: check modulo p and q separately. Suppose p∣m but q does not. Modulo p both med and m are 0. Modulo q, Fermat gives mq-1≡1, and ed=1+k(p-1)(q-1), so med=m×(mq-1)k(p-1)≡m. The two numbers agree modulo p and modulo q, so by the uniqueness half of the theorem they agree modulo pq=n. (If both primes divide m, then m=0.) With the key above, m=61 encrypts to 610, and 6102753≡61(mod3233).

Secure only while factoring is hard

An attacker sees n, e and c. The route to d is through φ(n), and finding φ(n) is exactly as hard as factoring n. The primes give (p-1)(q-1), and conversely φ(n)=pq-(p+q)+1, so knowing it gives p+q=n-φ(n)+1, and with pq=n the primes are the roots of t2-(p+q)t+n=0. For the toy key, p+q=3233-3120+1=114, and t2-114t+3233=0 has roots 61 and 53.

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 2048 bits and up, 617 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.