Home DSA Shor's Algorithm — Quantum Factoring

Shor's Algorithm — Quantum Factoring

Where developers are forged. · Structured learning · Free forever.
📍 Part of: Quantum Algorithms → Topic 4 of 4
Learn Shor's algorithm — how quantum period finding factors integers in polynomial time, why it breaks RSA and ECC, and the timeline for quantum cryptographic threats.
🔥 Advanced — solid DSA foundation required
In this tutorial, you'll learn:
  • Shor factors n-bit integers in O(n³) quantum operations vs sub-exponential classical — exponential speedup.
  • Reduces factoring to period finding: find r where a^r ≡ 1 (mod N), then GCD gives factors.
  • QFT performs period finding in O(n²) gates — the quantum core of the speedup.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚡ Quick Answer
RSA's security depends on integer factorisation being hard. Shor's algorithm factors n-bit numbers in O(n³) quantum operations — polynomial time. Classically, the best algorithm takes sub-exponential time (~2^(n^(1/3))). For a 2048-bit RSA key, that is the difference between roughly 10^50 classical operations and ~10^9 quantum operations. Shor's algorithm is why the entire internet is preparing to switch to post-quantum cryptography.

Peter Shor published his factoring algorithm in 1994, six years before the first few-qubit quantum computers were built. Its impact was immediate and lasting: it demonstrated that quantum computers, when sufficiently powerful, would break the cryptographic foundation of the internet.

Shor's algorithm reduces integer factorisation to period finding in a modular arithmetic sequence — a problem that the Quantum Fourier Transform (QFT) solves exponentially faster than any classical algorithm. The QFT is the quantum version of the Fast Fourier Transform, computing all N frequency components simultaneously via quantum interference.

The Reduction: Factoring → Period Finding

To factor N: 1. Choose random a coprime to N 2. Find the period r of f(x) = a^x mod N — the smallest r such that a^r ≡ 1 (mod N) 3. If r is even: p = gcd(a^(r/2)-1, N) and q = gcd(a^(r/2)+1, N) are factors 4. If r is odd or gcd gives 1 or N: try different a

Classical period finding requires O(exp(n^(1/3))) steps. Quantum period finding (QFT-based) requires O(n²) steps — exponential speedup.

shor_classical.py · PYTHON
12345678910111213141516171819202122232425262728293031
from math import gcd
from random import randrange

def classical_period_finding(a, N):
    """Classical (slow) period finding — O(sqrt(N)) in practice."""
    x = 1
    for r in range(1, N):
        x = (x * a) % N
        if x == 1:
            return r
    return None

def shor_classical_sim(N):
    """Shor's algorithm with classical period finding (slow part)."""
    if N % 2 == 0: return 2, N//2
    for _ in range(20):  # try multiple random a values
        a = randrange(2, N)
        g = gcd(a, N)
        if g > 1: return g, N//g  # lucky — found factor directly
        r = classical_period_finding(a, N)
        if r is None or r % 2 != 0: continue
        p = gcd(a**(r//2) - 1, N)
        q = gcd(a**(r//2) + 1, N)
        if 1 < p < N: return p, N//p
        if 1 < q < N: return q, N//q
    return None

# Factor small numbers
for N in [15, 21, 35, 77, 143]:
    result = shor_classical_sim(N)
    print(f'{N} = {result}')
▶ Output
15 = (3, 5)
21 = (3, 7)
35 = (5, 7)
77 = (7, 11)
143 = (11, 13)

The Quantum Part — QFT Period Finding

The quantum speedup comes from the Quantum Fourier Transform (QFT). Classically, finding the period r in a^x mod N requires O(r) steps — potentially O(N). The QFT finds r using O(n²) quantum gates by: 1. Creating superposition over all x values simultaneously 2. Applying the modular exponentiation function to all x at once 3. Applying QFT to extract the period r from the frequency domain

The QFT step runs in O(n²) quantum gates vs O(n × 2^n) for classical FFT on the same input — exponential speedup.

Cryptographic Impact and Timeline

| Algorithm | Problem | Quantum Attack | Status | |---|---|---|---| | RSA-2048 | Factor n=p×q | Shor: O(n³) | Broken by CRQC | | ECC P-256 | ECDLP | Shor variant | Broken by CRQC | | AES-128 | Symmetric | Grover: 2^64 | Weakened | | AES-256 | Symmetric | Grover: 2^128 | Still secure | | CRYSTALS-Kyber | Lattice (ML-KEM) | None known | PQ-secure |

CRQC = Cryptographically Relevant Quantum Computer. Required: ~4000 logical qubits (millions of physical). Current hardware: ~1000 noisy physical qubits. Timeline estimates: 2030-2040 for CRQC.

⚠️
Harvest Now, Decrypt LaterState actors may be recording encrypted internet traffic today, storing it for when CRQCs become available — decrypting it years later. Sensitive data with long confidentiality requirements (government secrets, medical records) is already at risk. This is why NIST standardised post-quantum algorithms in 2024 and major protocols (TLS, SSH, Signal) are actively migrating.

🎯 Key Takeaways

  • Shor factors n-bit integers in O(n³) quantum operations vs sub-exponential classical — exponential speedup.
  • Reduces factoring to period finding: find r where a^r ≡ 1 (mod N), then GCD gives factors.
  • QFT performs period finding in O(n²) gates — the quantum core of the speedup.
  • Breaks RSA and ECC completely on a fault-tolerant quantum computer. AES weakened but not broken.
  • Timeline: CRQC likely 2030-2040. Migration to post-quantum (CRYSTALS-Kyber, Dilithium) should start now — harvest-now-decrypt-later attacks are already a concern.

Interview Questions on This Topic

  • QHow does Shor's algorithm reduce factoring to period finding?
  • QWhat is the quantum speedup of Shor's algorithm compared to classical factoring?
  • QWhich symmetric algorithms are safe against Shor's algorithm?
  • QWhat is 'harvest now, decrypt later' and why does it matter?

Frequently Asked Questions

When will quantum computers break RSA?

Best current estimates: breaking RSA-2048 requires ~4000 logical qubits (fault-tolerant), which corresponds to millions of physical qubits with current error rates. IBM's roadmap targets 1000+ physical qubits by 2025. Most estimates put a CRQC at 2030-2040. However, given the 'harvest now' threat, organisations should begin post-quantum migration immediately for sensitive long-lived data.

🔥
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousGrover's Search Algorithm — Quantum Speedup
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged