Chapter 2Why Post-Quantum Cryptography?

Factoring and RSA

August 25, 20267 min readbeginner

The first of the two arithmetic problems that carried public-key cryptography from 1977 into the 2020s is integer factoring.

The first of the two arithmetic problems that carried public-key cryptography from 1977 into the 2020s is integer factoring. This note builds it from the forward direction, shows why the backward direction stops being possible, and then assembles a complete RSA key pair small enough that you can check every step with a pencil.

01.The forward direction is one multiplication

Pick two prime numbers and multiply them.

Take p=11p = 11 and q=13q = 13. Then

n  =  p⋅q  =  11⋅13  =  143.n \;=\; p \cdot q \;=\; 11 \cdot 13 \;=\; 143.

That is the entire forward direction. One multiplication.

Scale it up and nothing about the difficulty changes. Real RSA uses primes of about 300300 decimal digits each, giving nn of about 600600 digits. Multiplying two 300300-digit numbers is something a laptop does in microseconds. The work grows roughly with the square of the number of digits, which is comfortably polynomial, so clause 1 of the trapdoor contract is satisfied without effort.

02.The backward direction, at pencil scale

Now the reverse. Somebody hands you 143143 and asks which two primes multiply to give it.

Try the primes in order.

Is 143143 divisible by 22? No, it is odd.

By 33? The digits sum to 1+4+3=81 + 4 + 3 = 8, which is not a multiple of 33, so no.

By 55? It does not end in 00 or 55, so no.

By 77? 7⋅20=1407 \cdot 20 = 140, leaving a remainder of 33, so no.

By 1111? 11⋅13=14311 \cdot 13 = 143 exactly. Found it.

Five trial divisions. The backward direction was easy here, and it is important to be clear about why: because 143143 is a tiny number, not because the method is good.

03.Why the backward direction stops being possible

Trial division works by testing every prime up to n\sqrt{n}, because if nn has a factor larger than n\sqrt{n} it must also have one smaller. For 143143, that means testing primes up to 143≈12\sqrt{143} \approx 12, and there are five of them.

Count what that same strategy costs at cryptographic size. For nn around 20482048 bits, n\sqrt{n} is around 210242^{1024}, and the number of primes below that is roughly

nln⁡n  ≈  21024710  ≈  10305.\frac{\sqrt{n}}{\ln \sqrt{n}} \;\approx\; \frac{2^{1024}}{710} \;\approx\; 10^{305}.

The observable universe contains something like 108010^{80} atoms. The number of trial divisions is larger than that by more than two hundred orders of magnitude.

Mathematicians have of course done far better than trial division. The best general method known is the general number field sieve, a descendant of ideas going back to Fermat and Gauss, and its running time is roughly

exp⁡ ⁣(c (log⁡n)1/3(log⁡log⁡n)2/3).\exp\!\Bigl(c\,(\log n)^{1/3} (\log \log n)^{2/3}\Bigr).

That is the subexponential middle band from the previous note. It is a colossal improvement on trial division, and it is still nowhere near enough. The largest RSA modulus ever publicly factored is around 829829 bits, and that took thousands of core-years of computation. Deployed keys are 20482048 or 30723072 bits, and each extra bit makes the gap worse.

So the asymmetry is real. Multiplying is microseconds. Factoring is beyond the reach of every classical machine that exists or is planned.

04.Where the trapdoor lives

Now build the actual scheme, and watch where the secret enters.

Bob picks two primes pp and qq and computes n=pqn = pq. He also computes a second quantity from them,

φ(n)  =  (p−1)(q−1),\varphi(n) \;=\; (p - 1)(q - 1),

which counts how many numbers below nn share no factor with it. With p=11p = 11 and q=13q = 13,

φ(143)  =  10⋅12  =  120.\varphi(143) \;=\; 10 \cdot 12 \;=\; 120.

Bob then picks a public exponent ee that shares no common factor with φ(n)\varphi(n). Take e=7e = 7.

Finally he finds the number dd satisfying

e⋅d  ≡  1(modφ(n)).e \cdot d \;\equiv\; 1 \pmod{\varphi(n)}.

This dd is called the private exponent, and finding it is quick once you know φ(n)\varphi(n). Here we need 7d≡1(mod120)7d \equiv 1 \pmod{120}, and d=103d = 103 works, since 7⋅103=721=6⋅120+17 \cdot 103 = 721 = 6 \cdot 120 + 1.

Bob publishes the pair (n,e)=(143,7)(n, e) = (143, 7) and tells nobody pp, qq, φ(n)\varphi(n) or dd.

05.The worked example, end to end

Alice wants to send the message m=9m = 9. She has Bob's public key and nothing else.

Encryption. She computes c=me mod nc = m^e \bmod n, that is 97 mod 1439^7 \bmod 143. Doing it by repeated squaring so the numbers stay small:

92  =  81,9^2 \;=\; 81, 94  =  812  =  6561  =  45⋅143+126  ≡  126(mod143),9^4 \;=\; 81^2 \;=\; 6561 \;=\; 45 \cdot 143 + 126 \;\equiv\; 126 \pmod{143}, 97  =  94⋅92⋅91  ≡  126⋅81⋅9(mod143).9^7 \;=\; 9^4 \cdot 9^2 \cdot 9^1 \;\equiv\; 126 \cdot 81 \cdot 9 \pmod{143}.

Taking those last two multiplications one at a time, 126⋅81=10206=71⋅143+53126 \cdot 81 = 10206 = 71 \cdot 143 + 53, so that part is 5353. Then 53⋅9=477=3⋅143+4853 \cdot 9 = 477 = 3 \cdot 143 + 48.

So c=48c = 48, and Alice sends 4848.

What Eve sees. She sees n=143n = 143, e=7e = 7 and c=48c = 48. To recover mm she would need dd, to get dd she would need φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1), and to get that she would need pp and qq separately. She is back at factoring.

Decryption. Bob computes m=cd mod nm = c^d \bmod n, that is 48103 mod 14348^{103} \bmod 143, and gets 99 back.

That cdc^d returns the original mm is guaranteed by a classical result called Euler's theorem, which this book does not derive. The part that matters here is structural rather than computational: dd was built out of φ(n)\varphi(n), and φ(n)\varphi(n) was built out of pp and qq. The factorisation is the trapdoor.

06.The three clauses, checked

Look back at the contract from the previous note and confirm RSA satisfies all three.

Easy forward. Computing me mod nm^e \bmod n is repeated squaring, about log⁡2e\log_2 e multiplications. Fast.

Hard backward without the secret. Recovering mm from cc without dd is believed to require factoring nn. Infeasible at deployed sizes.

Easy backward with the secret. Computing cd mod nc^d \bmod n is the same repeated squaring as encryption. Fast.

And the security statement is unusually crisp. Anybody who can factor nn can derive dd and read everything. That is why "RSA is secure" and "factoring is hard" are treated as the same sentence.

Which is also why RSA has a single point of failure. There is no fallback if factoring turns out to be easy.

07.One caution about this example

Everything above is textbook RSA, and textbook RSA should never be used for anything real. Notice that Alice encrypting m=9m = 9 always produces c=48c = 48, every time, with no randomness anywhere. Eve can encrypt guesses under the public key and compare. If the message is one of a small set, say a yes or a no, she simply tries both.

Notice also what happens with m=100m = 100: encryption returns 100100 unchanged. Some messages are fixed points, and a real scheme has to avoid handing one to an attacker.

Deployed RSA fixes this by padding the message with structured randomness before exponentiating, under a scheme called OAEP. The padding is not optional decoration. But it is also not what this chapter is about, because a quantum computer does not attack the padding. It attacks nn.

The next note builds the second classical problem, which has a completely different flavour and, as it turns out, exactly the same fatal weakness.

FeedbackBook mode
post-quantum-cryptographycryptographymathematics