|
| 1 | +# Shor's Algorithm |
| 2 | + |
| 3 | +## Definition |
| 4 | + |
| 5 | +**Shor's Algorithm** is a *quantum* algorithm discovered by Peter Shor in 1994, designed to factor large integers and solve discrete logarithms in **polynomial time** on a quantum computer. This capability poses a significant threat to **classical cryptography** schemes, such as [[RSA]] and [[Diffie-Hellman]], which rely on the infeasibility of factoring large numbers or computing discrete logarithms with classical computers. |
| 6 | + |
| 7 | +Shor’s Algorithm exploits the power of **quantum parallelism** and the **Quantum Fourier Transform (QFT)** to find the period of a function related to the integer in question. By determining this period, it becomes possible to derive non-trivial factors of large numbers—or solve discrete logs—exponentially faster compared to any known classical algorithm. |
| 8 | + |
| 9 | +## Use Cases |
| 10 | + |
| 11 | +Despite often being described in the context of *breaking* cryptography, Shor's Algorithm has broader implications: |
| 12 | + |
| 13 | +- **Cryptanalysis**: It is primarily famous for breaking [[RSA]], [[Elliptic Curves]] **Cryptography (ECC)**, and other discrete-log-based systems if (and when) large-scale quantum computers become practical. |
| 14 | +- [[Post-Quantum Cryptography]] **(PQC) Research**: Shor’s Algorithm directly motivates the field of **post-quantum** or **quantum-resistant** cryptography, prompting the design of new cryptographic schemes believed to be secure against quantum attacks. |
| 15 | +- **Scientific and Academic**: In quantum computing courses and research, Shor’s Algorithm is often used as a central illustration of how quantum algorithms can vastly outperform classical algorithms for certain problems. |
| 16 | + |
| 17 | +## Advantages |
| 18 | + |
| 19 | +1. **Exponential Speed-Up** |
| 20 | + - Classically, factoring an $n$-bit integer grows super-polynomially. Shor's Algorithm can factor an $n$-bit integer in roughly $O(n^3) $ steps (or better with optimizations), showcasing a significant theoretical advantage. |
| 21 | + |
| 22 | +2. **Demonstration of Quantum Superiority** |
| 23 | + - Alongside [[Grover’s Algorithm]] for unstructured search, Shor’s Algorithm remains a cornerstone example of how quantum computers can outperform classical systems for specific tasks. |
| 24 | + |
| 25 | +3. **Driving Innovation in Cryptography** |
| 26 | + - The cryptographic world now intensively researches **lattice-based**, **code-based**, or other **post-quantum** cryptosystems to stay ahead of the quantum threat. |
| 27 | + |
| 28 | +## Challenges |
| 29 | + |
| 30 | +1. **Quantum Hardware Requirements** |
| 31 | + - To factor large numbers (e.g., 2048-bit RSA moduli), you need **thousands to millions** of *logical* qubits with fault tolerance and low error rates—well beyond current quantum hardware capabilities. |
| 32 | + |
| 33 | +2. **Error Correction** |
| 34 | + - Real quantum systems suffer from *noise* and *decoherence*, necessitating robust quantum error correction schemes (e.g., the **surface code**). These in turn require many *physical* qubits to create a single *logical* qubit. |
| 35 | + |
| 36 | +3. **Implementation Complexity** |
| 37 | + - Designing, programming, and stabilizing a large-scale quantum circuit for Shor’s Algorithm is complex. Even with today’s quantum libraries, only small numbers (tens to hundreds of bits) can be factored reliably. |
| 38 | + |
| 39 | +## Applying Shor’s Algorithm to Cryptography |
| 40 | + |
| 41 | +### Challenges in Classical Cryptography |
| 42 | + |
| 43 | +Modern cryptosystems like [[RSA]], [[Diffie–Hellman]] **Key Exchange**, and[[Elliptic Curves]] Cryptography rely on the hardness of factoring large integers or computing discrete logarithms. A classical computer cannot feasibly solve these problems for properly chosen key sizes (e.g., 2048-bit or higher). However, with Shor’s Algorithm: |
| 44 | + |
| 45 | +- **Factoring RSA Moduli**: Breaking RSA requires factoring a large semi-prime $N = p \times q$. Shor’s Algorithm can do this in polynomial time, undermining RSA’s security assumptions. |
| 46 | +- **Discrete Log Problems**: Similar logic applies to discrete log variants (e.g., [[Diffie–Hellman]] and ECC), making them vulnerable to quantum attacks. |
| 47 | + |
| 48 | +### How Shor’s Algorithm Works (High-Level) |
| 49 | + |
| 50 | +1. **Choose a Random $a$** |
| 51 | + - Select an integer $a$ coprime to $N$ (the integer to be factored). |
| 52 | +2. **Quantum Period-Finding** |
| 53 | + - Construct and evaluate the function $f(x) = a^x $mod N$ within a *quantum* circuit. |
| 54 | + - Use the **Quantum Fourier Transform (QFT)** to find the *period* $r$ of this function in *superposition*. |
| 55 | +3. **Classical GCD** |
| 56 | + - Once the period $r$ is found, compute $\gcd(a^{r/2} \pm 1, N)$ to discover non-trivial factors of $N$. |
| 57 | +![[Shor's Algorithm.png]] |
| 58 | + |
| 59 | +## Example: Factoring a Small Number with Shor's Algorithm |
| 60 | + |
| 61 | +Below is a simplified Python-style pseudo-code using [Qiskit](https://qiskit.org/). This example can factor very small numbers (like 15, 21), illustrating core concepts rather than large-scale factorization. More info [here](https://github.com/Qiskit/textbook/blob/main/notebooks/ch-algorithms/shor.ipynb) |
| 62 | + |
| 63 | +```python |
| 64 | +from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer, execute |
| 65 | +from qiskit.circuit.library import QFT |
| 66 | + |
| 67 | +def shor_factor(N): |
| 68 | + """ |
| 69 | + Pseudo-code function to demonstrate the structure |
| 70 | + of a Shor's Algorithm circuit for a small integer N. |
| 71 | + For large N, a significantly more complex circuit and |
| 72 | + quantum resources are required. |
| 73 | + """ |
| 74 | + |
| 75 | + # 1. Choose a random 'a' coprime to N |
| 76 | + a = find_random_coprime(N) # Placeholder function |
| 77 | + |
| 78 | + # 2. Determine number of qubits needed for the range [0 .. 2^m-1] ~ N^2 |
| 79 | + m = 4 # For demonstration, a small circuit |
| 80 | + |
| 81 | + # Quantum registers: 'x' for superposition, 'f' to store a^x mod N |
| 82 | + x = QuantumRegister(m, name='x') |
| 83 | + f = QuantumRegister(m, name='f') |
| 84 | + c = ClassicalRegister(m, name='c') |
| 85 | + circuit = QuantumCircuit(x, f, c) |
| 86 | + |
| 87 | + # 3. Initialize x register to superposition (Hadamard transforms) |
| 88 | + for qubit in x: |
| 89 | + circuit.h(qubit) |
| 90 | + |
| 91 | + # 4. Implement the modular exponentiation a^x mod N on the 'f' register |
| 92 | + # (Pseudo-code: in reality, this is done via repeated modular multiplications) |
| 93 | + circuit = apply_mod_exp(circuit, x, f, a, N) |
| 94 | + |
| 95 | + # 5. Apply Quantum Fourier Transform (QFT) to the x register |
| 96 | + qft_dagger = QFT(num_qubits=m, do_swaps=False).inverse() |
| 97 | + circuit.append(qft_dagger.to_instruction(), x[:]) |
| 98 | + |
| 99 | + # 6. Measure x register |
| 100 | + circuit.measure(x, c) |
| 101 | + |
| 102 | + # 7. Run on a simulator |
| 103 | + simulator = Aer.get_backend('aer_simulator') |
| 104 | + result = execute(circuit, simulator, shots=1024).result() |
| 105 | + counts = result.get_counts() |
| 106 | + |
| 107 | + # Use classical post-processing on the measurement result (period-finding) |
| 108 | + # Try GCD steps with a^(r/2) ± 1 to find non-trivial factors of N |
| 109 | + # ... |
| 110 | + return possible_factors |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + # Example usage: |
| 115 | + N = 15 # For demonstration (factors: 3 and 5) |
| 116 | + factors = shor_factor(N) |
| 117 | + print("Candidate factors:", factors) |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +### Original Paper: |
| 122 | +https://arxiv.org/abs/quant-ph/9508027 |
0 commit comments