✖

Shor’s algorithm: how a quantum computer would break the RSA cipher

Shor’s algorithm is a procedure by which a quantum computer would quickly factorise a large number into primes. That would bring down the RSA cipher, on which a large part of the internet still rests. The same trick would also break the Diffie–Hellman key exchange and ciphers based on elliptic curves. It was devised by the American mathematician Peter Shor in 1994. The trick is that it turns factorising a number into finding a repeating pattern, and a quantum computer can find that pattern because waves in it can both add up and cancel each other out.

Why factorising is hard

Multiplying two primes is easy: anyone can work out 5 · 11 = 55. The opposite direction is harder: starting from 55 and finding out that it came from 5 · 11. With small numbers it is enough to try dividing by 2, 3, 5, 7 and so on in turn. But with a number of 617 digits, such as those used in RSA, not even all the computers in the world could try a fraction of the possible divisors. Cleverer methods exist and are described in the article on prime factorisation, but even they would run for billions of years on such a number. The security of RSA rests on this.

Shor did not come up with a faster way of trying divisors. He turned it into a completely different problem: finding a period.

Factorising turned into finding a period

Take the number N = 55 that we want to factorise, and some smaller number, say 2. We will work out the powers of two and keep only the remainder after division by 55 from each. The notation 2xmod55 means exactly that remainder:

2, 4, 8, 16, 32, 9, 18, 36, 17, 34, 13, 26, 52, 49, 43, 31, 7, 14, 28, 1, …

Each number is double the previous one, and when it goes over 55, we subtract 55. For example, 2 · 32 = 64 and 64 − 55 = 9. After twenty steps we get a one, 220mod55=1, and from there the whole sequence starts repeating again from two.

A bar chart of the remainders of the powers of two after division by 55, for x from 0 to 44. The bars look irregular, but from x = 20 the whole pattern repeats, and one period is shaded orange. The bars with the value 1 at x = 0, 20 and 40 are red A bar chart of the remainders of the powers of two after division by 55, for x from 0 to 44. The bars look irregular, but from x = 20 the whole pattern repeats, and one period is shaded orange. The bars with the value 1 at x = 0, 20 and 40 are red

The length of the repeating section is called the period, and here it is r = 20. Why does it help us? Because 220 leaves remainder 1 after division by 55, 220 − 1 is divisible by 55. And 220 − 1 can be factorised using the formula a2 − b2 = (a − b)(a + b):

220−1=(210−1)⋅(210+1)=1023⋅1025

So 55 divides the product 1023 · 1025. Yet neither 1023 nor 1025 is divisible by 55; the remainders after division are 33 and 35. How does that fit together? The number 55 = 5 · 11 has to be “split”: five divides one factor and eleven divides the other. If both divided the same factor, it would be divisible by 55. Which part belongs to which factor is revealed by the greatest common divisor (GCD):

gcd(1023,55)=11,gcd(1025,55)=5

And there it is, 55 = 5 · 11. The greatest common divisor can be worked out quickly even for gigantic numbers, using the Euclidean algorithm, so this step is no problem. The whole difficulty of factorising has thus moved into a single step: finding the period.

Sometimes it does not work out. If the period is odd, it cannot be halved and the difference of squares formula cannot be used. At other times one of the factors is divisible by the whole of N and the GCD reveals nothing new. Then you try a number other than 2. For numbers of the kind used in RSA the chance of success is at least one in two, so a few attempts are enough.

Why finding the period is hard

For 55 we found the period by writing out twenty numbers. For a number with 617 digits the period can also have hundreds of digits. The sequence of remainders soon starts jumping about as if at random, and not even the first few billion numbers reveal anything about the period. Writing them out one by one would take longer than the factorisation itself. An ordinary computer therefore gets no help from the conversion to a period. A quantum one does.

What a quantum computer does with the period

An ordinary computer works with bits, each of which is either 0 or 1. A quantum computer has qubits, which can be in a superposition, that is, in a kind of mixture of zero and one at once. With a few thousand qubits the machine holds a mixture of all the numbers x at the same time and, in a single calculation, works out 2xmodN for all of them together.

It sounds like a miracle, but the catch is in the measurement. When you look at the result, you learn only one random x and one remainder, nothing more. If a quantum computer could do only this, it would be useless. Shor’s trick lies in what happens before the measurement.

First the machine looks only at the remainder, say that 8 came out. Then only those x that give remainder 8 stay together, that is, 3, 23, 43, 63 and so on. They lie exactly one period apart, but nobody knows that period yet.

Each of those x carries a little hand inside the machine, like the hand of a clock. A quantum calculation called the quantum Fourier transform tries all possible rotation speeds at once. For each speed it turns the hand of each x in proportion to x and adds the hands up. It works much like waves on water: two wave crests add up into a higher wave, and a crest and a trough cancel out.

At the top, eight clock faces with the hand always turned the same way; placed one after another they make a long red arrow. At the bottom the hand on each following clock face is turned by an eighth of a turn; placed one after another they close into an octagon, and their sum is zero, a red dot At the top, eight clock faces with the hand always turned the same way; placed one after another they make a long red arrow. At the bottom the hand on each following clock face is turned by an eighth of a turn; placed one after another they close into an octagon, and their sum is zero, a red dot

Imagine a speed at which the hand makes exactly one full turn in 20 steps. The numbers 3, 23, 43 and so on differ by 20, so their hands differ by whole turns and all point the same way. They add up to a long arrow, which is the top row of the picture. The same happens for a speed at which the hand goes round twice in 20 steps, three times, and so on. For the other speeds the hands scatter in all directions and cancel out, as at the bottom. The measurement then almost certainly returns one of the speeds with a long arrow, say “3 turns in 20 steps”. From the fraction 320 an ordinary computer can read off the period 20. Occasionally only part of it comes out and the measurement is repeated. The quantum computer does not write out the period; it lets the wrong answers cancel each other out.

The rest of the algorithm, that is, the GCD calculation and the factorisation, runs on an ordinary computer. The quantum part does only the one thing that an ordinary computer cannot do.

How many qubits are needed

A lot. Qubits are fragile, the slightest vibration or heat throws them off, so hundreds of them have to be combined to make one reliable one. A 2019 estimate assumed 20 million qubits and eight hours of computing for a 2048-bit RSA key. In May 2025 Craig Gidney of Google showed that fewer than a million qubits and roughly a week would be enough.

Today’s machines are far from that. Google’s Willow chip from 2024 has 105 qubits, and the largest experimental arrays of atoms have thousands of qubits, but they make too many errors to manage a long calculation. Attempts to run Shor’s algorithm on a real quantum computer have so far got only as far as numbers like 15 and 21. When a machine that could handle RSA will arrive, nobody knows. But because of the “harvest now, decrypt later” attack, described in the article on post-quantum cryptography, ciphers are already being replaced.

What Shor’s algorithm cannot do

A quantum computer is not a miracle machine for everything. Shor’s algorithm works because both factorising a number and finding the exponent in Diffie–Hellman hide a period. Problems without such structure are not sped up much by a quantum computer. The hardest problems from the P versus NP question, such as the travelling salesman problem, are, as far as anyone can tell, not solved quickly even by a quantum computer. Factorising numbers is probably not among them.

Ordinary shared-key ciphers such as AES, which encrypts the data itself, are threatened only by a different quantum procedure, Grover’s algorithm. It speeds up the search for a key roughly as if the key were half as long, and that is solved with a longer key. Only today’s public-key ciphers such as RSA are in real trouble. That is why post-quantum cryptography came about: ciphers built on problems in which nobody has found any period.

Who is Peter Shor

In 1994 Peter Shor worked at Bell Labs in New Jersey, where he devised the algorithm. Today he is a professor at MIT. At the time of the discovery quantum computers existed only on paper, and Shor’s algorithm was one of the main reasons why governments and companies began to build them. For the discovery he received the Nevanlinna Prize in 1998, which the International Mathematical Union awards for mathematics in computer science, and in 2023 also the Breakthrough Prize in Fundamental Physics.

See also