Prove how fast a factoring algorithm is, in Lean
Factoring is the standard example of a problem where a quantum computer is believed to have an exponential advantage: Shor's algorithm runs in polynomial time. On the classical side the best proven deterministic bound is about N^(1/5) (Harvey 2021, up to logarithmic factors), and the best proven randomized one is an expected L_N[1/2, 1 + o(1)] (Lenstra and Pomerance 1992). These bounds are proved on paper, for Turing machines. QArena found no machine-checked running-time proof for any factoring algorithm in any proof assistant (a web search, not exhaustive). The board asks for such proofs: a bound proven here holds for every input, and two independent kernels have replayed the proof.
Leaderboard: Cost model v1 (Lean v4.36.0, Mathlib v4.36.0) Hot
QArena's record 0.5 (e, the exponent of N in the proven bound) by QArena's own run Ties it
| # | Participant | Score ((e, the exponent of N in the proven bound)) | vs Moura et al. (published) | Submission |
|---|---|---|---|---|
| 1 | lit-mathlib-minfac (published method, run by QArena)(literature) Baseline | 0.5 | #1 | |
| 2 | qarena-baseline (QArena's run)(QArena) | 0.5 | #2 |
Baseline: Mathlib's Nat.minFac (trial division by 2 and the odd numbers up to the square root), ported to QArena's cost model with a cost proof (published method, run by QArena): 0.5 (e, the exponent of N in the proven bound) · paper
Instance: Cost model version
Overview
Task
Submit an integer factoring algorithm and a proof of its cost. The algorithm is a program for a small register machine,
written as a value of the Lean 4 type QArena.Factor.Stmt. The proof is a Lean theorem that two independent kernels
check. It must show that on every input N ≥ 2 the program stops, that its output d satisfies
1 < d, d divides N, and d = N only when N is prime,
and that the run's cost is at most C · Ne · (len N)k, where len N is the bit length of N
(N.log2 + 1, so len 2 = 2 and len 106 = 20). The cost counts frozen log-cost RAM operations:
the charges of the machine below, which every instruction pays as it runs. A composite N must get a proper factor. A prime N
gets d = N, which signals primality only through the proven theorem; d = N alone is not a certificate anyone could check.
Why this problem
Factoring is the standard example of a problem on which a quantum computer is believed to have an exponential advantage: Shor's
algorithm runs in time polynomial in len N. The best proven classical bounds are far higher. Deterministically, Harvey (2021)
factors in time O(N1/5 log16/5 N) on a multitape Turing machine, and Harvey and Hittmeir (2022) save a
further factor (log log N)3/5. With randomness, Lenstra and Pomerance (1992) prove an expected time
LN[1/2, 1 + o(1)]. These bounds are proved on paper. QArena found no machine-checked proof of the running time of any
factoring algorithm, in any proof assistant and any cost model (a web search, not an exhaustive one). Proofs of correctness do
exist: Mathlib proves that Nat.minFac returns the least prime factor, and Mathematical Components verifies prime
decomposition. Peng et al. (PNAS 2023) verified Shor's algorithm in Coq with a bound on its quantum gate count. This board asks
for the classical side in checkable form: a bound proven here holds for every input N ≥ 2.
The cost model
A program is a syntax tree built from these statements: skip, const r k, copy r s,
bin op r a b, load r a, store a v, coin r, seq,
ite (if) and loop (while). The ten operations are add, sub (truncated at 0), mul, div, mod
(a / 0 = 0, a % 0 = a), shl, shr, and, or and xor. Registers and memory cells hold natural numbers of
any size. Register numbers are fixed in the program text; memory addresses are computed values. The input N sits in register 0,
every other register and memory cell starts at 0, and the answer is read from register 1. Tests compare two registers with <
or =. The program is data, so it cannot call Nat.minFac or any other Lean function: every step of the computation
goes through these instructions, and each one is charged.
| Step | Operations charged | Plain meaning |
|---|---|---|
skip | 1 | one step |
const r k | 1 + len k | writing the constant |
copy r s | 1 + 2 len v | reading and writing the value v |
bin op r a b with add, sub, shl, shr, and, or, xor | 1 + len x + len y + len z | reading both operands x, y and writing the result z |
bin op r a b with mul, div, mod | the above + len x · len y | schoolbook multiplication and long division |
load r a, store a v | 1 + len(address) + len(value) | a memory access pays for its address and its value |
coin r | 1 | one random bit |
test of ite or loop | 1 + len x + len y | comparing two numbers |
Every instruction pays at least the bit length of every number it reads or writes. shl x y writes a number of
len x + y bits when x > 0 and pays for them, so a huge number costs as much as writing it down; shifting 0 gives 0, which
keeps its one-bit length. This is the logarithmic-cost random access machine of Cook and Reckhow (1973), with schoolbook charges
for the three operations whose cost is not linear. Unit-cost arithmetic on huge numbers gains nothing here: Shamir's 1977 MIT
memo (Information Processing Letters, 1979) factors n in O(log n) arithmetic steps on numbers of up to about c·n²
bits, and here each such step costs at least that many operations. QArena's design files prove the general fact against the
frozen model (len_le_cost in discussion/lean_factor/lean/QArenaFactor/Facts.lean, not part of the frozen
file): in every run, every number in a register or memory cell has at most len N + (cost so far) bits.
The score counts this model's operations, not steps of another machine. A word addition that a word RAM does in one step costs Θ(log N) here. A published bound for multitape Turing machines transfers here at the price of a factor log T: a machine that runs for T steps can be simulated in the model at cost O(T log T). The design does not show the converse, a simulation of the model's random access on a multitape machine within a constant factor, so a bound proven here is not claimed as a Turing machine bound. What is not charged: the program's size (a constant), placing N in register 0, and the time Lean spends checking the proof.
The frozen statement
The model is the file QArenaFactor/Model.lean (217 lines, model version 1, on Lean v4.36.0 and Mathlib v4.36.0).
Its definitions are frozen: the verifier compares every one of them, and every constant the kernel treats as built in (the 30
names on the Lean toolchain's own list, which includes the natively evaluated Nat operations, and Bool),
with QArena's trusted build (whose reference export has SHA-256 0c1f3873764fb310…). A change would be a new model
version with its own leaderboard. A submission proves, for one closed program algo : Stmt and natural-number
literals C, a, b, D:
theorem QArenaSubmission.result : QArena.Factor.PolyBound QArenaSubmission.algo C a b D
-- where PolyBound algo C a b D unfolds to
0 < D ∧ ∀ N : Nat, 2 ≤ N → ∀ tape : Nat → Bool,
(∃ σ, Exec algo (init N tape) σ) ∧
∀ σ, Exec algo (init N tape) σ →
(1 < σ.reg 1 ∧ σ.reg 1 ∣ N ∧ (σ.reg 1 = N → N.Prime)) ∧
σ.cost ^ D ≤ C ^ D * N ^ a * len N ^ b
with len N = N.log2 + 1. So for every N ≥ 2 and every random tape the run stops, and every run is correct with
cost ≤ C · Na/D · (len N)b/D. Raising both sides to the power D keeps real numbers out of
the statement. Exec is the model's big-step semantics; a run that never stops has no Exec derivation,
so it cannot meet the statement. A program may read coins, and it is then ranked by its worst tape.
Submission file
A JSON object {"lean": "…"} whose only key holds the text of one Lean 4 file: UTF-8, at most 4 MiB
(4,194,304 bytes), in a JSON file of at most 8 MiB. The file is compiled as module Submission. It may import
anything in the verifier's store (Lean core, Mathlib v4.36.0 with its dependencies, and QArenaFactor.Model) and must
define
def QArenaSubmission.algo : QArena.Factor.Stmt := ... theorem QArenaSubmission.result : QArena.Factor.PolyBound QArenaSubmission.algo C a b D := ...
with C, a, b and D written as natural-number literals of at most 300 digits each. algo must be a concrete
program: the kernel must reduce it all the way to the constructors of Stmt, Cond and the ten
BinOp constructors, with numeric literals, in at most 100,000 nodes. The proof may use the axioms
propext, Classical.choice and Quot.sound and no other, so sorry and
native_decide are refused. Nothing else constrains how the program or the proof is written. The 4 MiB cap limits the
source, not what it generates: a macro or tactic can build much larger terms, and the time and memory limits bound those.
The instance file lf-model-v1.json carries the
frozen model, the Lake project (toolchain, lakefile and the manifest that pins every Mathlib dependency), the second Lean checker
and the store's install script. A Lake project made from it (lake exe cache get, then lake build) is the
place to develop a submission. The two baselines below are complete examples of the format.
Scoring
An accepted file scores e = a/D and k = b/D as exact fractions, and C. Lower e ranks first, then lower k, then lower C; equal (e, k, C) rank by submission number, earlier first. Equivalent fractions are equal: a = 2, D = 4 gives the same e as a = 1, D = 2.
This order compares how bounds grow as N goes to infinity. Two bounds can cross at some finite N, so a better rank does not mean a cheaper run on every input. The family C · Ne · (len N)k cannot see factors smaller than a power of len N: Harvey and Hittmeir's (log log N)3/5 saving over Harvey 2021 ranks the same as Harvey 2021 by (e, k), and a saving such as exp(−c log N / log log N) enters only through C. C is the last tie-breaker and measures the proof as much as the algorithm: the two baselines share (e, k) = (1/2, 2), and the Mathlib port ranks first because its proof gives C = 96 against 117. A finite table of answers for small N can lower C without changing e or k. Only this deterministic, every-tape statement is ranked. A track for randomized algorithms with an expected-cost bound has draft definitions and is not open.
Methods proven and run by QArena
| Row | Program | Proven statement | e, k, C | Verifier |
|---|---|---|---|---|
lit-mathlib-minfac | Mathlib's Nat.minFac: test 2, then 3, 5, 7, … while
k² ≤ N | PolyBound algo 96 1 4 2 | 1/2, 2, 96 | accepted in about 43 s on one Derecho core |
| QArena's baseline | trial division by 2, 3, 4, … while d² ≤ N | PolyBound algo 117 1 4
2 | 1/2, 2, 117 | accepted in about 40 s on one Derecho core |
Both proofs are complete: they use exactly the three allowed axioms and no sorry, and both kernels replay their
exports (4,685 and 4,913 declarations). The Mathlib row is a port. Mathlib proves that Nat.minFac returns
the least prime factor and says nothing about cost. The port writes Mathlib's recursion minFacAux as a program of the
model, proves that its output equals Nat.minFac N through Mathlib's own equation for minFacAux, takes
correctness from Mathlib's minFac_dvd and minFac_prime, and adds QArena's cost proof. The file header
carries the attribution.
The proven constants are loose. The design's reference interpreter ran both programs on 14 inputs from 2 to 9,999,999,967; every output was correct, and the largest ratio of cost to proven bound was 0.088 for trial division and 0.117 for the port, so on those inputs the bounds sit at least 11.4 and 8.5 times above the cost. Fourteen samples show neither the largest ratio over all N nor that the Python interpreter matches the Lean semantics.
Published bounds, not yet proven here
| Method | Published bound | Machine | What it would give here |
|---|---|---|---|
| Lehman 1974 | O(N1/3) arithmetic operations | unit-cost arithmetic | e = 1/3; QArena proved the integer square root, Euclid's gcd and Lehman's theorem in integer form as parts of a draft, but no proof of the whole program yet |
| Pollard 1974, Strassen 1976/77; Bostan, Gaudry, Schost 2007; Costa, Harvey 2014 | N1/4+o(1), the later ones O(Mint(N1/4 log N)) and better | multitape Turing machine | e = 1/4 with a fast multiplication program in the model |
| Hittmeir 2021 | O(Mint(N2/9 log5/3 N) log N) | multitape Turing machine | e = 2/9 |
| Harvey 2021 | O(N1/5 log16/5 N) | deterministic multitape Turing machine | a = 1, b = 21, D = 5 by the simulation above (k = 21/5); k = 16/5 would need a sharper implementation |
| Harvey, Hittmeir 2022 | Harvey's bound divided by (log log N)3/5 | same | the same (e, k) as Harvey 2021; the saving is not visible in the ranking |
| Gao, Feng, Hu, Pan, arXiv:2512.19076 | O(N1/5 log13/5 N / (log log N)3/5) | same, for semiprimes N = pq with p, q = Θ(N1/2) only | nothing as it stands: the board asks for every N ≥ 2 |
| Lenstra, Pomerance 1992 | expected time LN[1/2, 1 + o(1)] | randomized | a worst-tape bound only; an expected-cost track is not open |
For Pollard's rho method, Bach (1991) proves a bound on the probability that it finds a factor within a fixed number of steps; its familiar running time of about the square root of the factor remains heuristic. A Las Vegas or Monte Carlo algorithm can be analysed in Lean as well, since Lean can certify an error probability; this board ranks the deterministic, every-tape statement only.
Verification
Compiling a Lean file runs arbitrary code (tactics, macros and #eval are programs). So one stage compiles the file
inside a sandbox, and the checking stages trust nothing that stage produced.
| Stage | Runs | Decides |
|---|---|---|
| U (untrusted) | Lean compiles Submission.lean, then leanexport writes the theorem, the
program and the kernel's built-in constants, with everything they depend on, as text (lean4export format 3.1.0) |
nothing; only the export leaves the sandbox, and the driver copies it to a fresh read-only file that every later stage reads |
| K1 | leanchecker --from-export: Lean's kernel replays every declaration into an empty environment |
every proof checks |
| K2 | nanoda_bin, an independent kernel written in Rust, replays the same export; any axiom but the three
allowed ones is a hard error | every proof checks, no other axiom |
| S1 | leanfactor_s1.py: a strict reading of the export (exact record fields, one tag per record,
indices defined before use, canonical numerals, unique names) compared with QArena's trusted reference export | the statement and its literals, the frozen model's whole closure and the kernel's built-in constants identical to the trusted build, the axioms by kind and type |
| S2 | qafactor_check (Lean): its own replay of the export, the statement and axioms read again, and the
program reduced by the kernel to a concrete syntax tree whose operations are exactly the ten BinOp
constructors | the program is concrete; it is printed and shown on the board |
A file is accepted only when every stage exits normally with an accepted verdict and S1, S2 and K2 report the same numerals and
axioms. A stage stopped by a time, CPU or memory limit, a stage during which the memory cap killed any process, a kernel that
stops at one of its own resource limits (deep recursion, a numeral too large for it to evaluate), or a checker that exits
abnormally gives status error, never a verdict.
The sandbox has no network and sees only the host's /usr, the read-only store and fresh capped temporary folders;
every process is killed with it, and each check runs in a private folder of its own. The deadline is kept by the driver,
outside the untrusted stage.
The export is not trusted. In the design's tests a forged export that redefined Nat.gcd passed both kernels; S1
now compares every built-in constant of the kernel with the trusted build, which catches it. Five of the eleven forged exports in the
test set pass both kernels, and S1 refuses them. A second checker, written by another author from the specification
without reading the verifier, reads the exports in its own way, decodes the program with its own reducer and runs both kernels;
it agreed with the verifier on all 32 exports of the test set, including the forged exports and the decoding attacks (an opaque or
noncomputable operation hidden in a branch that never runs). The design's earlier mutation test changed accepted exports in 72
ways and none was accepted. S1 and S2 differed in pass or fail on 31 of those mutants, since each stops at its own first
finding; the design's agreement figure covered only the statement checks on mutants that both kernels passed.
Verifications run on NCAR's Derecho supercomputer, one core and 16,000 MB per check, never on QArena's own server. The Lean store (toolchain, Mathlib and the trusted reference export, about 10 GB) is built once on the lane by the install script in the instance file. Trial division takes about 40 s there: about 6 s to read the store's 8.8 GB of module files (Lean's own page faults on the cluster's shared file system would take minutes), 18 s to compile and export, and 13 s for the four checks.
Sources and licences
- The cost model, the statement, the checkers S1 and S2, the install script and the trial-division proof are QArena's own work (Apache 2.0).
- Lean 4 v4.36.0, the official release (github.com/leanprover/lean4,
commit 611c117bfaf7, Apache 2.0). It bundles
leanchecker,leanexport,nanoda_bin(Chris Bailey) andlean4lean(Mario Carneiro), all Apache 2.0 per its LICENSES file. QArena runs them unchanged. - Mathlib v4.36.0 (github.com/leanprover-community/mathlib4, commit fc4f59e577feb1a4956671bc04ac18aaaf8cd842, Apache 2.0) and its dependencies at the commits of the manifest: batteries, aesop, Qq, ProofWidgets, plausible, LeanSearchClient and import-graph (Apache 2.0) and lean4-cli (MIT). The store holds their compiled files; nothing of them is copied into QArena's files.
- The
lit-mathlib-minfacrow portsNat.minFacandNat.minFacAuxfromMathlib/Data/Nat/Prime/Defs.lean(Copyright 2015 Microsoft Corporation, Apache 2.0; Leonardo de Moura, Jeremy Avigad, Mario Carneiro) and uses its theorems by name. - S. A. Cook, R. A. Reckhow, Time bounded random access machines, J. Comput. System Sci. 7 (1973) 354–375.
- A. Shamir, Factoring numbers in O(log n) arithmetic steps, MIT/LCS/TM-91 (1977); Inform. Process. Lett. 8 (1979) 28–31.
- P. W. Shor, Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer, SIAM J. Comput. 26 (1997) 1484–1509.
- R. S. Lehman, Factoring large integers, Math. Comp. 28 (1974) 637–646.
- D. Harvey, An exponent one-fifth algorithm for deterministic integer factorisation, Math. Comp. 90 (2021) 2937–2950, arXiv:2010.05450 (Theorem 1.1).
- D. Harvey, M. Hittmeir, A log-log speedup for exponent one-fifth deterministic integer factorisation, Math. Comp. 91 (2022) 1367–1379.
- M. Hittmeir, A time-space tradeoff for Lehman's deterministic integer factorization method, Math. Comp. 90 (2021) 1999–2010.
- E. Costa, D. Harvey, Faster deterministic integer factorization, Math. Comp. 83 (2014) 339–345.
- Gao, Feng, Hu, Pan, arXiv:2512.19076 (2025), Theorem 1.1.
- H. W. Lenstra Jr., C. Pomerance, A rigorous time bound for factoring integers, J. Amer. Math. Soc. 5 (1992) 483–516.
- E. Bach, Toward a theory of Pollard's rho method, Inform. and Comput. 90 (1991) 139–155.
- Y. Peng, K. Hietala, R. Tao, L. Li, R. Rand, M. Hicks, X. Wu, A formally certified end-to-end implementation of Shor's factorization algorithm, PNAS 120 (2023) e2218775120.
- Mathematical Components, mathcomp.boot.prime: prime decomposition.
Where verification runs
Verifications of this board run on NCAR's Derecho supercomputer (project UCUB0188) with this board's verifier, the same version as QArena's server (CPU budget 3600 s, which is 2 times the board's 1800 s; 16000 MB of memory): quick ones in a standing job that starts them within about a minute, ones estimated over 30 minutes in a job of their own. The result goes to QArena only.
Current records
| Instance | Baseline | Record ((e, the exponent of N in the proven bound)) | Reference score | Record holder | Status |
|---|---|---|---|---|---|
| Cost model v1 (Lean v4.36.0, Mathlib v4.36.0) Hot
The ranked statement QArena.Factor.PolyBound of the frozen model (leanfactor/QArenaFactor/Model.lean, 217 lines; its trusted export has SHA-256 0c1f3873764fb310…): every input N ≥ 2, every random tape |
0.5 Moura et al., published | 0.5 | 0.5 Proven and run by QArena: Mathlib's Nat.minFac ported to the model (lit-mathlib-minfac), e = 1/2, k = 2, C = 96; QArena's own trial division gives e = 1/2, k = 2, C = 117, Mathlib v4.36.0, Mathlib/Data/Nat/Prime/Defs.lean (Apache 2.0; Leonardo de Moura, Jeremy Avigad, Mario Carneiro), with QArena's cost proof |
lit-mathlib-minfac (published method, run by QArena)(literature) | Matches reference |
The baseline of an instance is its published result where there is one (the hot instance's famous result, a cited published reference, or a published result or method on its leaderboard), else QArena's own run. Reference scores come from cited papers or derived constructions.
Record changes
The first strict improvement on a ranked instance will appear here as a card, and a chart will follow the headline record over time.
Headline record now:
- Lean factoring cost, Cost model v1 (Lean v4.36.0, Mathlib v4.36.0): 0.5 (e, the exponent of N in the proven bound) (exact) by lit-mathlib-minfac