Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer 2: Factoring from a Random ResidueResearch Paper
Motivation
Shor's 1997 paper (SIAM J. Comput. 26(5), arXiv:quant-ph/9508027) gives a polynomial-time quantum algorithm for factoring integers. The quantum computer does not factor directly: it finds the multiplicative order of an element modulo . The step from order finding to factoring is classical and randomized, and goes back to Miller's 1976 work on primality testing (G. L. Miller, Riemann's hypothesis and tests for primality, J. Comput. System Sci. 13 (1976)). Every account of Shor's algorithm, and every resource estimate for breaking RSA with a quantum computer, depends on this reduction succeeding with a constant probability per trial. This mission formalizes that probability bound as Shor states it on p. 1498 of the published paper.
Setting
Let be an odd integer with prime factorization
so is the number of distinct prime factors of , all odd. The unit group consists of the residues coprime to ; it has elements, where is Euler's totient function.
For a unit the order is the least positive integer with . For each the local order is the order of , taken modulo the full prime power, not modulo . For a positive integer , denotes the exponent of the largest power of dividing .
The reduction is: choose uniformly at random from , obtain its order (from the quantum subroutine), and compute
The procedure yields a nontrivial factor at when is even and . In Lean this event is ShorAlgorithms.Reduction.successEvent n u for u : (ZMod n)ˣ, and is localOrder n u p for p ∈ n.primeFactors.
Formalization targets
Goal: the success probability
It is stated for every odd . For a prime power () the bound is , so the statement says nothing there; it is informative exactly when is not a prime power, as the paper remarks. The constant is sharp: for exactly of the units succeed, so in place of would be false.
Milestones, in the order the page uses them
- Success criterion. If is even and , then .
- Order is the lcm. .
- Failure forces agreement. For odd , if the procedure fails at , then .
- At most half per odd prime power. For an odd prime and , at most units modulo have order with a prescribed 2-adic valuation.
- All agree rarely. The units for which number at most .
Significance
The result. The bound turns an order-finding oracle into a factoring algorithm: when is odd and not a prime power, each trial succeeds with probability at least , so independent trials all fail with probability at most . Even numbers and prime powers are split classically, as the paper notes, so the bound completes the reduction from factoring to order finding. The same criterion — a square root of other than splits — underlies the Miller–Rabin test and several classical factoring methods.
Formalizing it. The mathematics is classical and proved; the paper gives a sketch of one paragraph. This mission writes out the sketch as machine-checked statements over Mathlib's ZMod, including the probabilistic step, which in the paper is an informal appeal to the Chinese remainder theorem and "50% probability of agreeing with the previous ones". Mathlib already has the needed ingredients (cyclicity of for odd , ZMod.chineseRemainder, ZMod.card_units_eq_totient), but not the reduction or its probability bound.
Difficulty
The success criterion (milestone 1) is elementary. The substance is the counting. The obvious route — treating the as independent and each "equal to the previous one with probability " — needs both a precise product decomposition of the unit group modulo into the unit groups modulo , compatible with the local orders, and the count in a cyclic group of even order of the elements whose order has a given 2-adic valuation. The informal phrase "at most a 50% probability of agreeing with the previous ones" hides a conditioning argument over coordinates that has to be done by an explicit cardinality bound. A second pitfall is milestone 3: its converse direction and its forward direction use oddness of in different places, and modulo a power of the argument breaks because .
Formalization scope
- Sample space. Uniform on
(ZMod n)ˣ; probabilities are stated in cleared-denominator form, in , with the count asNat.cardof a subtype. Non-units have no multiplicative order and are not sampled. - The gcd. is represented by its least nonnegative residue
.val, which is at least for a unit when , so the natural-number subtraction inval - 1never truncates. is natural-number division, used only underEven r. - .
n.primeFactors.card, at least for , sok - 1does not truncate. Since is odd this equals the page's "number of distinct odd prime factors". - Local orders. The order of the image of in
ZMod (p ^ n.factorization p)under the reduction homomorphism. - Hypotheses. The goal assumes exactly odd and . It does not assume "not a prime power": that clause in the paper describes when the bound is useful. Milestones 1 and 2 do not assume odd, because they do not need it; milestones 3 and 5 do.
- No trivialization. Counting over all of
ZMod ninstead of the units would put non-units (with junk order ) into the denominator; the goal counts over(ZMod n)ˣand divides by . The goal's constant is the paper's , which is attained, so it cannot be weakened into a triviality without changing the theorem. - Welcome contributions. A reusable counting lemma for elements of prescribed 2-adic order in a finite cyclic group; the transfer of
ZMod.chineseRemainderto unit groups and to local orders; and proofs of the milestones in any order.
Selected references
- P. W. Shor, Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer, SIAM J. Comput. 26(5):1484–1509, 1997. https://doi.org/10.1137/S0097539795293172 (preprint arXiv:quant-ph/9508027, https://arxiv.org/abs/quant-ph/9508027)
- G. L. Miller, Riemann's hypothesis and tests for primality, J. Comput. System Sci. 13(3):300–317, 1976. https://doi.org/10.1016/S0022-0000(76)80043-8
- D. E. Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 2nd ed., Addison-Wesley, 1981.
- G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, 5th ed., Oxford University Press, 1979 (Theorem 121, Chinese remainder theorem).