Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Shor's reduction: a random unit x mod odd n yields a nontrivial factor with probability ≥ 1 − 1/2^{k−1}

Proved
ShorAlgorithms.Reduction.factor_prob_ge

by mikedeng1 · Sep 29, 2026 · Mathlib 0df444a (Lean v4.33.1)

factoringnumber-theoryorder-findingp2o-batch-p200bp2o-gran-per-chapterp2o-plan-paperp2o-v1probability

Let n>1n > 1n>1 be an odd integer with kkk distinct (necessarily odd) prime factors, and let φ(n)\varphi(n)φ(n) be the number of units modulo nnn. Choose xxx uniformly at random from (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times(Z/nZ)×, let rrr be its multiplicative order, and compute gcd⁡(xr/2−1,n)\gcd(x^{r/2} - 1, n)gcd(xr/2−1,n). The procedure yields a nontrivial factor of nnn when rrr is even and 1<gcd⁡(xr/2−1,n)<n1 < \gcd(x^{r/2}-1, n) < n1<gcd(xr/2−1,n)<n. Then

Pr⁡x[the procedure yields a nontrivial factor of n]  ≥  1−12k−1,\Pr_{x}\bigl[\text{the procedure yields a nontrivial factor of } n\bigr] \;\ge\; 1 - \frac{1}{2^{k-1}},xPr​[the procedure yields a nontrivial factor of n]≥1−2k−11​,

that is, (1−2−(k−1)) φ(n)≤#{x∈(Z/n)×:success at x}\bigl(1 - 2^{-(k-1)}\bigr)\,\varphi(n) \le \#\{x \in (\mathbb{Z}/n)^\times : \text{success at } x\}(1−2−(k−1))φ(n)≤#{x∈(Z/n)×:success at x}.

This is the classical half of Shor's factoring algorithm: it reduces factoring an odd nnn to finding the order of a random element. The bound is informative exactly when nnn is not a prime power (k≥2k \ge 2k≥2), where it is at least 1/21/21/2; for a prime power (k=1k = 1k=1) it reads 000 and holds trivially. The paper's remark that "the scheme will work as long as nnn is odd and not a prime power" explains when the bound is useful and is not an extra hypothesis. The constant is sharp: for n=21n = 21n=21 exactly 666 of the 121212 units succeed.

Formalization Note The sample space is the unit group (ZMod n)ˣ, of cardinality φ(n)\varphi(n)φ(n); a non-unit xxx has no multiplicative order and is not sampled (Shor's "choose a random x(modn)x \pmod nx(modn)" in "the multiplicative group (modn)\pmod n(modn)"). The probability is stated in cleared-denominator form over R\mathbb{R}R, with the count taken as Nat.card of the subtype of successful units. n.primeFactors.card is kkk, which is at least 111 for n>1n > 1n>1, so k - 1 does not truncate. The success event is the definition successEvent.

Preamble
import Mathlib
import Definitions.Def_ShorAlgorithms_Reduction_successEvent
Formal statement
namespace ShorAlgorithms.Reduction

/-- Shor (1997), §5, p. 1498: for odd `n > 1` with `k` distinct (odd) prime factors, the
procedure applied to a uniformly random unit `x (mod n)` yields a nontrivial factor of `n`
with probability at least `1 - 1/2^{k-1}`; stated as
`(1 - 1/2^{k-1}) · φ(n) ≤ #{x ∈ (ℤ/n)ˣ : success}`. -/
theorem factor_prob_ge (n : ℕ) (hodd : Odd n) (hn : 1 < n) :
    (1 - 1 / 2 ^ (n.primeFactors.card - 1) : ℝ) * (Nat.totient n : ℝ)
      ≤ (Nat.card {u : (ZMod n)ˣ // successEvent n u} : ℝ) := by sorry

end ShorAlgorithms.Reduction
Source
Shor, Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer, SIAM J. Comput. 26(5) (1997), p. 1498, §5, "Using this criterion, it can be shown that this procedure, when applied to a random x (mod n), yields a nontrivial factor of n with probability at least 1 − 1/2^{k−1}, where k is the number of distinct odd prime factors of n."
Human review
  • Endorsed by Shuze Chen · Oct 1, 2026

    Confirmed by the moderator at approval.

  • Endorsed by mikedeng1 · Oct 1, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me