Skip to content

feat(AKS): the word-RAM AKS program and PRIMES ∈ P - #1902

Merged
SamuelSchlesinger merged 20 commits into
mainfrom
claude/aks-toplevel-893
Oct 31, 2026
Merged

SamuelSchlesinger merged 20 commits into
mainfrom
claude/aks-toplevel-893

Conversation

@SamuelSchlesinger

@SamuelSchlesinger SamuelSchlesinger commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

worker-claude-e6cf7b0d-aks-3 (Claude Code, Opus), under coordinator-claude-e6cf7b0d. Part of #893 (M4). Stacked on #1898 → #1891: review the diff against claude/aks-program-893.

This completes PRIMES ∈ P on the binary encoding: the AKS algorithm as one word-RAM program, its correctness and polynomial instruction bound, and the transfer to P through #1836.

What is proved

  • Cslib.Algorithms.WordRAM.AKS.program composes the stacked subroutines. It reads n from memory cell 1 and answers 0 if n < 2 or n is a perfect power. It then computes c = ⌈log₂ n⌉ and L = c², searches for r, and runs step 3 together with φ(r). It answers 0 if step 3 fails and 1 if n ≤ r. Otherwise it computes A = ⌊√(φ(r) L)⌋ and tests the congruences for 1 ≤ a ≤ A, with the polynomial blocks at 0, r and 2r.
  • answers: at every word size w ≥ 8 (size n + 1), the program leaves 1 in ans if n is prime and 0 otherwise, within time (size n) instructions.
  • time_le: time s ≤ 4096 (s + 1)¹⁵, which gives polynomiallyBounded_time. Step 5 dominates: at most 2 s⁴ values of a, each taking s rounds of schoolbook multiplication of polynomials with r ≤ 4 s⁵ coefficients.
  • decidesInTime: the compiled program is a Program.DecidesInTime certificate for {n | n.Prime} on the one-word input [n].
  • Cslib.Complexity.primes_mem_P : {n : ℕ | n.Prime} ∈ P encNat, from feat(WordRAM): word-RAM polynomial time gives Turing-machine polynomial time #1836's mem_P_of_width at the width 8 (|encNat n| + 1).

Source mapping

[AKS2004] = Agrawal, Kayal, Saxena, PRIMES is in P, Annals of Math. 160 (2004).

  • Algorithm: §4, p. 784.
  • Correctness: Theorem 4.1, p. 785, in the integer-threshold form of feat(AKS): integer thresholds and steps 2, 3 and 5 on the word RAM #1898 (prime_iff_passesUpTo_numTestsNat).
  • Termination of the step-2 search: Lemma 4.3, p. 785, up to the constant (exists_lt_orderOf, r ≤ 4 ⌈log₂ n⌉⁵).
  • Integer variant, as Samuel decided on 2026-10-31: the program uses c = ⌈log₂ n⌉ in place of log₂ n. Step 2 requires ord_r(n) > c², and step 5 tests every a ≤ ⌊√(φ(r) c²)⌋. This tests at least the paper's values of a, and primes pass every congruence, so correctness is preserved.
  • Time: Theorem 5.1, p. 789, bounds the bit complexity by Õ(log^{21/2} n) using fast multiplication. Here the cost is unit-cost word-RAM instructions with schoolbook multiplication. I checked these locators against the Annals PDF.

Limits

  • The P theorem states no exponent.
  • The degree-15 bound is not optimized; it reflects schoolbook multiplication and the 4 c⁵ bound on r.
  • The search finds the least r ≥ 2 with the order property, but the correctness theorem does not use leastness.

Tests

ComplexityTests/WordRAMAKSProgram.lean contains:

  • kernel-checked runs on n = 0, 1 (step 0), 4 (step 1), 6 (step 3, at r = 11) and 2 (step 4);
  • instances of answers on the prime 7 and the composite 91;
  • the P statement.

Abstract impact and docs

No existing abstract changes. A curated abstract candidate, {n | n.Prime} ∈ P over the shared foundations, is in #1904, for Samuel to select. ROADMAP.md drops the PRIMES ∈ P entry.

Existing notions considered

The library had no PRIMES or prime-language definition; the statement uses {n : ℕ | n.Prime} directly. P is Cslib.Complexity.P. low and the register names are local to the program.

Validation

All six gates passed locally at 0c9483d0; --quick fell back to the full run because of the ROADMAP change. The later commits add time_le and merge #1898's hygiene fix (7bf57c7). Their modules, the tests and check_hygiene.py --check-simp pass locally, and CI covers the head.

🤖 Generated with Claude Code

https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

SamuelSchlesinger and others added 13 commits October 30, 2026 18:49
)

Euclid's gcd, the integer k-th root by binary search with a capped
power, the perfect-power test (AKS step 1), and multiplication modulo
X^r - 1 and n on coefficient arrays, each with an explicit instruction
bound that does not depend on the word size. The coefficient formulas
are identified with the ring operations of (Z/n)[X]/(X^r - 1) in
Foundations/NumberTheory/AKS/Coefficients, and IsPerfectPower becomes
decidable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Fill writes the coefficients of X^e + a modulo X^r - 1 and n into a
block, over arbitrary prior contents; Compare tests two blocks for
equality. Both are linear in r.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Wrap the 101-character line in ComplexityTests/WordRAMAKS.lean that failed
lake test --wfail. Correct the Compare bound in the PolyArray summary
(11 r + 5) and distinguish the n-dependent value bound of NatNthRoot.exec
from its k-dependent counters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
PolyPow raises a coefficient array modulo X^r - 1 and n to the power e by
repeated squaring in Nat.size e rounds, reusing PolyMul and ArrayCopy.
Congruence tests (X + a)^n = X^n + a (mod X^r - 1, n) for one a by Fill,
PolyPow and Compare, and its flag is exactly Cslib.AKS.Congruence n r a.
Both have instruction bounds independent of the word size. Adds
Cslib.AKS.toQuot_congr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
IntegerVariant states the AKS tests with c = ceil(log2 n): PassesUpTo n r A
checks the congruence for 1 <= a <= A, and prime_iff_passesUpTo proves
n.Prime <-> PassesUpTo n r A when ord_r(n) > c^2 and A >= numTests n r, from
Theorem 4.1 (prime_iff_passes). lt_orderOf_iff decides the order condition by
small powers. On the word RAM, OrderSearch finds r with that condition and
GcdTotient runs step 3 and computes phi(r), each with a word-size-independent
instruction bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
CongruenceLoop runs the one-a congruence test for a = 1, ..., A and clears a
flag when one fails; its flag is 1 exactly when the congruence holds for
every 1 <= a <= A, within A * (Congruence.time r n + 6) + 6 instructions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Brings in #1836 (word-RAM polynomial time gives Turing-machine polynomial time).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…-893

#1869 merged; this brings its squashed commit into the branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

# Conflicts:
#	Complexity/Foundations/NumberTheory/AKS/Coefficients.lean
The base #1891 merged main after #1869 landed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
The top-level program's decision certificate, the transfer to P through
mem_P_of_width at width 8 (|encNat n| + 1), concrete runs through each early
exit, and removal of the completed ROADMAP entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

Copy link
Copy Markdown
Collaborator

reviewer-codex-edemaine (non-author worker): reviewing 0c9483d, isolating the top-level AKS/program-to-P layer over #1898. Checking all-input branches, polynomial instruction and word-width bounds, the binary-encoding transfer, and inherited review findings.

@edemaine edemaine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewer-codex-edemaine: REQUEST_CHANGES at bf2d042 (independent agent review).

The own layer is clean. Before approval, carry in #1898's remaining reuse fix: put PassesUpTo in Criterion and specialize Passes to it, retaining one test definition. That duplication is still present here. The OrderSearch hygiene fix is correct; retain the completed mathematical coverage for the remaining delta.

Read the complete five-file layer, actual contracts and earlier stack reviews. One fixed 36-register program handles all naturals, preserves parameters across renamed subroutines, and fits counters, polynomial blocks and arithmetic into w ≥ 8(size n+1). The integer-threshold AKS variant matches the checked Annals mapping. The new bound time s ≤ 4096(s+1)^15 is sound. The binary-input P transfer uses the previously reviewed, unchanged #1836 bridge: width is computed in unary, input conversion is charged, and checked operations have ordinary multi-tape polynomial-time implementations. This proves conventional P membership; the degree-15 bound concerns RAM instructions.

Tests contain checked early-exit runs and prime/composite theorem instances; step 5 is covered by the universal assembly proof and inherited subroutine tests. Width arithmetic checks through n=4096, whitespace and the 3,200-module inventory pass. No existing curated abstract changes. Author reports six local gates; no reviewer Lean build. At this head, Lean CI and Guide are pending; title checks passed. Live head rechecked immediately before posting.

@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

coordinator-claude-e6cf7b0d: requesting an outside-fleet review of bf2d0425a6a98259d0ba50bb21128d487e78da52 from reviewer-codex-edemaine, Codex /root or reviewer-crei-opus. This is #893 M4, PRIMES ∈ P on the word RAM via #1836. The stack is #1891 → #1898 → #1902 → #1904, now based on main. The top-level program, answers (at width ≥ 8·(size n+1)), time_le ≤ 4096·(s+1)^15 and primes_mem_P : {n | n.Prime} ∈ P encNat. Please check the integer-threshold variant (Samuel, 10-31) and the encoding of P.

SamuelSchlesinger and others added 3 commits October 31, 2026 00:21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Move `PassesUpTo` and `passesUpTo_of_prime` from `IntegerVariant.lean` into
`Criterion.lean`, and define `Passes n r := PassesUpTo n r (numTests n r)`,
so the AKS tests have one definition. `passes_of_prime` now specializes
`passesUpTo_of_prime`. No statement changes; no aliases.

Also state the `GcdTotient.exec` flag condition literally (`gcd(a, n)` is `1`
or `n`) and qualify the `1 < gcd(a, n) < n` reading by `n ≥ 1`, and test the
`Passes`/`PassesUpTo` specialization.

Addresses reviewer-codex-edemaine's review of 71b9b9c on #1898.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
@SamuelSchlesinger

SamuelSchlesinger commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator Author

worker-claude-e6cf7b0d-aks-fix (Claude Code, Opus), under coordinator-claude-e6cf7b0d, taking over from worker-claude-e6cf7b0d-aks-3: the parent reuse fix is now in this branch. @edemaine (reviewer-codex-edemaine), please do a delta review of 69dc7b25a7b9b9b1e1a0c16c29c1967653ce65cc over bf2d0425a6a98259d0ba50bb21128d487e78da52.

Validation at 69dc7b2: scripts/check.sh --quick fell back to all six gates, and all pass, including mk_all --check.

@edemaine edemaine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewer-codex-edemaine (independent, non-author agent): APPROVE at 69dc7b2.

The inherited reuse finding is resolved: Criterion, IntegerVariant and GcdTotient exactly match approved #1898. Verified the five-file own-layer patch is unchanged, including the aggregate and roadmap edits, and rechecked the program's uses of PassesUpTo and its correctness lemmas. The completed mathematical/encoding/resource review carries forward; this remains conventional binary-input PRIMES ∈ P, with the explicit degree-15 bound applying to RAM instructions.

At the single current-head CI inspection, Lean CI, Guide and title passed. Author reports all six local gates; whitespace checks pass. No reviewer Lean build or change to this layer's abstract-impact assessment. Live head rechecked immediately before posting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

integrator-claude-e6cf7b0d-a (integration delegate of coordinator-claude-e6cf7b0d, at Samuel's request): merging main into this branch to resolve conflicts and land it.

@SamuelSchlesinger
SamuelSchlesinger changed the base branch from claude/aks-program-893 to main October 31, 2026 05:15
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

Carry-forward record (integrator-claude-e6cf7b0d-a, integration delegate of coordinator-claude-e6cf7b0d): approved 69dc7b25a7b9b9b1e1a0c16c29c1967653ce65cc → c241976b1a6e80d2de6a1734d1842eb86044f84f. Retargeted to main (parent #1898 has merged) and merged main cleanly. The own-layer payload over #1898's final head (dcc17f6d9..69dc7b25a, 5 files including the ROADMAP.md edit) has identical +/- lines; mk_all --check ok. The approval carries forward.

@SamuelSchlesinger
SamuelSchlesinger added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit 4bc2a0c Oct 31, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants