Shor’s algorithm, introduced by Peter Shor in 1994 (algorithms for quantum computation: discrete logarithms and factoring), is the breakthrough quantum algorithm that factors large integers in polynomial time, a task widely believed to be intractable for classical computers. Given an integer , the algorithm finds its prime factors in
time on a quantum computer. This is exponentially faster than the best classical algorithms, such as the general number field sieve, which runs in sub-exponential time
, where
.
The discovery of Shor’s algorithm showed that a large quantum computer would be capable of breaking widely used public-key cryptosystems, most notably RSA, whose security relies on the assumed hardness of integer factorization. This result initiated both the global effort to build scalable quantum computers and the rapid development of post-quantum cryptography, which seeks cryptographic protocols secure even against quantum attackers.
RSA Cryptosystem
Since Shor’s algorithm was primarily designed to break the RSA public-key cryptosystem. For completeness, we first review the core principles of RSA. The RSA cryptosystem, along with its variants, is one of the most widely deployed methods for securely transmitting messages over public channels, such as the Internet, where it protects confidentiality and enables digital signatures. Its security relies fundamentally on a single, long-standing computational assumption: that factoring a large composite integer into its prime factors is extraordinarily difficult for classical computers.
It is worth emphasizing that testing whether a number is prime is computationally feasible, whereas factoring a large composite number into its prime factors remains extraordinarily difficult. RSA cryptography leverages precisely this asymmetry: while anyone can efficiently verify the primality of the factors used in key generation, recovering those factors from the public modulus is, for classical computers, practically intractable. This one-way computational difficulty forms the cornerstone of RSA’s security, enabling secure encryption and decryption of messages.
The RSA cryptosystem operates as follows:
- Alice prepares two very large prime numbers
and
, which she keeps secret. She computes
and publishes
. She also chooses a number, called the public exponent (or encoding exponent),
, which is required to be relatively prime to
, that is,
. Alice then computes the multiplicative inverse of
modulo
, namely a number
satisfying
She keeps secret, thus called the secret exponent (or decoding exponent).
- When Bob wants to send a message to Alice, he first encodes the message as an integer
with
. He encrypts it as
- Upon receiving
, Alice decrypts it using
where is the multiplicative inverse of the public exponent
modulo
.
To see that the RSA cryptosystem correctly recovers the message using
, we need to check
.
From , there exists an integer
such that
. Hence
.
Suppose is not a multiple of
. Fermat’s little theorem gives
.
If is a multiple of
, then
.
Similarly,
when is not a multiple of
, and
if
is a multiple of
.
We now verify by considering several cases.
- If
is a multiple of both
and
, then it is automatically a multiple of
, and
holds trivially.
- If
is a multiple of
but not of
, then
,
and
.
Hence,
.
By the Chinese remainder theorem,
.
We are using a basic consequence of the Chinese remainder theorem: if two integers and
satisfy
and
, with
, then
. In particular, if
and
are prime, it is easier to see
and
implies
.
- If
is a multiple of
but not of
, the argument is symmetric, giving
.
- If
is not a multiple of either
or
, then Fermat’s little theorem gives
.
Again, by the Chinese remainder theorem,
.
This completes the proof of .
We see from the above analysis that if one could factor efficiently, then the RSA system would be completely compromised. Indeed, factoring
reveals the primes
and
, from which one can compute
, determine the public exponent
, and then recover the secret exponent
. With
in hand, any encrypted message can be decrypted.
Shor’s Algorithm
Definition (The Factoring Problem)
The input to Shor’s algorithm is an odd composite integer , where
and
are distinct large primes. The goal is to find the prime factors
and
.
Shor’s algorithm is a hybrid quantum-classical algorithm that factors in polynomial time on a quantum computer. It consists of two main phases:
- Classical reduction: Transform factoring
into an instance of the order-finding problem (see this post).
- Quantum order-finding: Solve the order-finding problem efficiently using a quantum computer.
The classical reduction (which succeeds with probability for random choices) is as follows:
1. Choose a random integer and compute
:
- If
, then
is a non-trivial factor of
. Factoring complete.
- If
, continue.
2. Find the multiplicative order of
modulo
, i.e., the smallest positive integer
such that
.
3. If is odd, return to the first step with a new
.
4. If is even, compute
:
- If
, return to the first step.
- If
, proceed.
5. Compute the factors:,
.
At least one of these is a non-trivial factor of .
The only step that is computationally infeasible on a classical computer is step 2: finding the order .
Shor’s breakthrough contribution is a quantum algorithm that performs this order-finding step in polynomial time using quantum phase estimation on the unitary operator .
Once is obtained quantumly, the remaining classical post-processing immediately yields the prime factors
and
. Thus, on a sufficiently large fault-tolerant quantum computer, Shor’s algorithm solves the integer factorization problem efficiently, rendering RSA and related cryptosystems insecure.
Remark (Why the Reduction Works)
Assume that is even and that
(the cases explicitly filtered out by the algorithm). Since
is the order of
modulo
, we have
.
This implies,
so , or equivalently,
.
Because and
are distinct primes, the prime factors of the product
can be distributed in only four logical ways:
divides
and
divides
divides
and
divides
divides
(i.e.,
divides
)
divides
(i.e.,
divides
)
Cases 1 and 2 are precisely what we want: they yield non-trivial factors via the gcd computations and
(or vice versa).
Cases 3 and 4, however, must be excluded:
- Case 3 is impossible: If
divides
, then
.
Butis the order of
modulo
, so
would have to divide
, which is impossible unless
, a contradiction.
- Case 4 is impossible under our assumption: If
divides
, then
.
This situation is explicitly detected and rejected in the algorithm (step 5).
Thus, whenever the algorithm reaches step 6, we are guaranteed to be in Case 1 or Case 2, and the two gcd operations recover the prime factors and
of
.
Example
Let us illustrate Shor’s algorithm with the integer .
- Choose a random number: Let
. Compute
.
Since, we proceed to the next step.
- Find the period
: Using a quantum computer, we determine the period of the function
.
The result is, since
.
- Use the period to find the factors:
- Since
is even, compute
.
- Compute the gcd:
, which is one factor of
.
- Similarly,
, and
, which is the other factor.
- Since
Thus, we have successfully factored .

