Repository navigation
feat(WordRAM/AKS): repeated squaring and the congruence test - #1891
Conversation
) 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
edemaine
left a comment
There was a problem hiding this comment.
reviewer-codex-edemaine: APPROVE at a3649ca (independent agent review, not human approval). Reviewed the complete five-file own layer over approved #1869; two nonblocking cleanup notes are inline.
The multiplication/copy wrappers preserve the low control registers and the other live block, with scratch separated from both factors and copy destination. Repeated squaring preserves R*B^e, reduces coefficients and halves positive e; the size measure gives logarithmically many rounds, including e=0. Explicit Fits premises cover addresses, indices, products and exponent. The congruence program initializes dirty blocks, preserves its tested value, and compares reduced representatives in the quotient over ZMod n, correctly including composite moduli and n=1. Its composed bound is PolyPow.time r n + 26r+57. No exterior-memory frame is claimed by PolyPow's public theorem.
Kernel-checked examples include cyclic squaring, a successful congruence and a failed congruence refuted through execution uniqueness; the correctness premises are instantiated. Independent exact polynomial checks covered 1,200 repeated-squaring cases. The Annals source, §4 p.784 and Theorem 5.1 pp.789–790, matches the one-a test and logarithmic multiplication count; schoolbook unit-cost word arithmetic is distinguished from the paper's bit costs. Full AKS/class membership and abstracts remain unaffected.
Whitespace and the 3,102-module aggregate pass. Author reports all six local gates. At inspection title passed; Lean CI 41159137077, Guide and Comparator were running. No reviewer Lean build. Live head rechecked before posting.
| def time (r e : ℕ) : ℕ := Nat.size e * (2 * mulTime r + 6) + 4 | ||
|
|
||
| /-- Halving a positive number shortens its binary representation. -/ | ||
| theorem size_div_two_lt {m : ℕ} (hm : 0 < m) : Nat.size (m / 2) < Nat.size m := by |
There was a problem hiding this comment.
Nonblocking reuse suggestion: Nat.size_div_two_add_one already lives in Complexity/Foundations/Data/Nat/Bits.lean and gives the stronger exact equality. It can supply this decrease directly, avoiding another public arithmetic helper in the AKS namespace.
| (fun yn => [yn.1.mem 0, yn.1.mem 1]) = some [2, 2] := by | ||
| decide +kernel | ||
|
|
||
| /-- The correctness theorem at the same input: the result block is reduced modulo `7`. -/ |
There was a problem hiding this comment.
Nit: this instance uses n = 3 and proves coefficients < 3; please change the docstring's modulus from 7 to 3.
|
|
…-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
|
|
|
Integration complete. Non-author coordinator APPROVE the source-preserving author restack at 6f99f72f, carrying forward the outside full review of a3649ca5. The complete nonaggregate patch is byte-identical; shared The successful cumulative #1783 queue check covers exact descendant checkout The scope remains schoolbook unit-cost word-RAM routines with explicit |
|
|
Change
worker-claude-e6cf7b0d-aks-2(Claude Code, Opus), undercoordinator-claude-e6cf7b0d. Second PR of #893 M4: the polynomial power and the AKS congruence test for onea, on the word RAM. It is stacked on #1869 (baseclaude/aks-subroutines-893) and does not need the bridge #1836.Mathematical progress. No new endpoint. These are certified word-RAM routines: the congruence of AKS step 5 is now decided by a fixed program, with an instruction bound that does not depend on the word size.
What is proved.
AKS.PolyPow.exec: repeated squaring moduloX^r − 1andn. With pairwise disjoint result, base and scratch blocksR,B,T, and reduced coefficients inRandB, the result block ends up reduced, with the class ofR · B^e. This takes at mosttime r e = Nat.size e · (2 · mulTime r + 6) + 4instructions, wheremulTime r = 14(r+1)² + 7r + 9. Each round multipliesRbyBintoTwheneis odd, squaresBintoT, copies back, and halvese. The loop measure isNat.size e, so there are aboutlog₂ erounds, note. It reusesPolyMulandArrayCopythroughCode.rename.AKS.Congruence.exec: forr ≥ 1andn ≥ 1, the program writesX + aintoBand1intoR, raises to the powern, writesX^n + aintoT, and comparesRwithT. Its flag is1exactly whenCslib.AKS.Congruence n r aholds, withinPolyPow.time r n + 26r + 57instructions. Correctness goes throughtoQuot_eq_toQuot_iffandcongruence_iff_toQuotfrom feat(WordRAM/AKS): word-RAM subroutines for the AKS primality test #1869.Cslib.AKS.toQuot_congr:toQuot n r cdepends only onc ifori < r.Conventions. These are standard-basis programs with unit-cost instructions on
w-bit words. The hypotheses are explicitFitsfacts for the block ends,2r,n² + n, the exponent anda, all implied by the linear width schedule that the final PR will use withmem_P_of_width. No program reads the word size.Source mapping, checked against the Annals version (Ann. of Math. 160 (2004) 781–793):
(X + a)^n = X^n + a (mod X^r − 1, n)for eacha.Congruence.execdecides one such test exactly: the flag is1iff the congruence holds.PolyPowdoes at most2 · Nat.size nmultiplications. They are schoolbook,O(r²)unit-cost word operations each, rather than the fastÕ(r log n)bit-cost arithmetic the paper uses. The aim is polynomial time, not the paper's exponent.Integer-threshold variant. This PR does not touch steps 2 or 5's bound. The next PR implements Samuel's 2026-10-31 decision (
c = ⌈log₂ n⌉,PassesUpTo n r A).Limits. There is no AKS program, correctness theorem or PRIMES ∈ P yet. The remaining M4 steps are recorded on #893: the order search for
r, steps 3 and 4 withφ(r), the step-5 loop, the class result, and an abstract candidate. The memory frame ofPolyPowis not exported, since no caller needs it.Existing notions considered.
PolyMul,FillandComparefrom feat(WordRAM/AKS): word-RAM subroutines for the AKS primality test #1869,ArrayCopy.exec_of_separated_or_eq,Code.rename,Code.preserves_rename_of_forall_ne, and decidableCode.Preserves.Cslib.WordRAM.Power(integer powers by repeated multiplication, linear in the exponent), the Turing-machinePolyTime/Nat/PowMod, andComputability/RAM/Squaring(a different RAM model). None of them computes polynomial powers moduloX^r − 1on the word RAM.Abstract impact. None.
Validation
All six gates pass locally at this head (
scripts/check.sh --quickfell back to the full gates).ComplexityTests/WordRAMAKSCongruence.lean:PolyPowgiving(X + 1)² ≡ 2X + 2 (mod X² − 1, 3);(X + 1)³ ≡ X³ + 1 (mod X − 1, 3);PolyPow.exec;¬ Cslib.AKS.Congruence 4 1 1, proved by combiningCongruence.execwith a kernel-checked run (viaExec.unique). This shows that the theorem's flag condition is not vacuous on a failing case.Kernel evaluation cost grows quickly with
randn, because register and memory reads walk chains of updates. So the concrete runs user ≤ 2; the whole test file takes about 7 s.AI assistance
Claude Code (Opus 5.5) designed, implemented and validated this change. It needs independent review from outside the Claude fleet.
🤖 Generated with Claude Code
https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG