Skip to content

feat(primality): add the HexPrimalityMathlib correspondence layer - #9412

Merged
kim-em merged 64 commits into
mainfrom
primality-mathlib
Aug 24, 2026
Merged

feat(primality): add the HexPrimalityMathlib correspondence layer#9412
kim-em merged 64 commits into
mainfrom
primality-mathlib

Conversation

@kim-em

@kim-em kim-em commented Aug 22, 2026

Copy link
Copy Markdown
Owner

This PR scaffolds HexPrimalityMathlib (milestone 5a of HexPrimality/SPEC/hex-primality.md) and lands the correspondence: prime_iff is the one lemma the whole layer turns on (prime_iff_forall_lt against Nat.prime_def_lt), and everything else transports along it: natPrime_of_checkPrime, natPrime_of_checkPrimeAt (the statement the coming Nat.Prime tactic handler emits), isPrime_iff_natPrime, not_natPrime_of_millerRabin_false, and nextPrime?_natPrime. Per the SPEC, no DecidablePred Nat.Prime instance is declared — Mathlib's Nat.decidablePrime already exists and a second global instance would risk selection churn. Segment.lean carries the table statements in Mathlib's vocabulary: primeTable_spec, primesIn_spec, filter_prime_range over Finset.range, and forall_prime_lt, the scaffold that turns "every prime below x satisfies P" into a check over the committed table entries. The SPEC's libraries.yml block is updated to the landed statuses.

Stacked on #9411.

🤖 Prepared with Claude Code

Kim Morrison and others added 13 commits August 22, 2026 03:43
…visor lemmas

powModNat gains the p = 0 guard so it agrees with powMod at every
input, its recursion (powModNatGo, bitLength) is @[expose]d, its
correctness theorem is exported, and an unconditional @[csimp] names
powModNat the kernel-facing specification with powMod the runtime
twin. instDecidablePrime re-routes decide through isPrimeTrial,
exists_trial_divisor is exported, and exists_prime_dvd plus
exists_prime_le_sqrt are the composite-witness lemmas the hex-primality
Pocklington argument finishes with. SPEC amendments record the landed
state (hex-arith SPEC modular-exponentiation and primality sections;
hex-primality SPEC stale claims corrected).

Progress: progress/20260822T034253Z_primality-m0.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
… at 10^4

Adds the HexPrimality library (deps HexArith, HexBasic) with its
umbrella, libraries.yml entry, CI lib target, and the SPEC co-located
at HexPrimality/SPEC/hex-primality.md. Table.lean commits the
1229-entry primeTable below primeTableBound = 10000 with sortedness,
binary-search membership (isTablePrime_iff), soundness via one
kernel-replayed isPrimeTrial sweep, completeness via five balanced
kernel-replayed coverOk chunks, and primesIn with its membership
theorem. Statements are sieve-agnostic so the planned kernel-reducible
sieve swaps proof internals only.

Progress: progress/20260822T040440Z_primality-table.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Order.lean defines orderOf as a bounded least-search with junk value 0
off the coprime domain and proves the SPEC's order stack Mathlib-free:
coprimality from a unit power, coprime cancellation, multiplicative
Fermat, order positivity via pigeonhole through the ListShim, order
divisibility both ways, orderOf_dvd_pred, prime-power extraction into
the order, and the two square roots of 1 modulo a prime.

Progress: progress/20260822T041921Z_primality-order.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
…tion

MillerRabin.lean adds oddSplit with its spec, the squaring loop, and
millerRabin with the SPEC's exact branch table, plus defaultBases and
isProbablePrime. Soundness is the contrapositive
millerRabin_eq_true_of_prime by forward induction along the loop with
sq_roots_of_one; not_prime_of_millerRabin_false is the public theorem,
and no completeness is claimed. Guards cover Carmichael numbers,
strong pseudoprimes, and agreement with isPrimeTrial below 512.

Progress: progress/20260822T042306Z_primality-millerrabin.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Implements the hex-finite-field SPEC's Randomness section at its
specified site in hex-basic: Rand with next, RandError carrying the
advanced state on exhaustion, Rand.nat by rejection sampling that
rejects the incomplete top interval, Rand.ofSeed, and the range
theorem Rand.nat_lt. Known-answer guards pin the canonical splitmix64
seed-0 vector and deterministic replay.

Progress: progress/20260822T042502Z_hexbasic-rand.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
…e checker

Cert.lean defines PrimeCert (with the pock3 constructor carrying the
integer-square-root witness the SPEC amendment records, its arithmetic
stubbed false until milestone 4), the structurally recursive
checkPrime/checkChildren pair with all workers @[expose]d against the
kernel-facing powModNat, and checker soundness prime_of_checkPrime by
strong induction on the subject value, through the pocklington core
theorem, the fused coprime-prime-power combination, and the
gcd-to-noncongruence transport. CheckedPrimeCert and the
single-Bool-slot prime_of_checkPrimeAt serve the coming search and
tactic layers. Guards cover accepted multi-level certificates and one
rejected certificate of each kind.

Progress: progress/20260822T043327Z_primality-cert.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Search.lean adds RhoStop/RhoFailure with the resumable-failure
convention, rhoFactor? as Brent cycle search with restart draws from
Hex.Rand and dynamic validation at every success exit (making
rhoFactor?_spec a fuel induction over guards), and the internal
partialFactor: table trial division, then a rho worklist whose every
path preserves the product exactly, giving the unconditional
partialFactor_prod invariant.

Progress: progress/20260822T044003Z_primality-rho.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Search.lean gains the resumable failure types, the mutual
fuel-and-worklist certificate search primeCertGo/assembleGo (runtime
only, so well-founded recursion is fine), the indexed primeCert? whose
runtime dependent checks build CheckedPrimeCert with zero proof
plumbing, the verdict theorem primeCert?_composite by path analysis,
isPrime? across the table/trial/certificate tiers with its exactness
spec, the total isPrime with unconditional isPrime_iff, and nextPrime?
with its leastness spec.

Progress: progress/20260822T044458Z_primality-decide.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Elab.lean adds primality n and the three tactic forms per the SPEC:
compiled certificate search at elaboration time from the reproducible
seed, defeq transparency and bit-budget checks, verdict and exhaustion
diagnostics, an untrusted-search self-check with the same compiled
checkPrime the kernel replays, and an emitted term applying
prime_of_checkPrimeAt to the reified certificate with one Eq.refl true
slot. In-module tests cover every syntax form across all three
decision tiers and the failure messages.

Progress: progress/20260822T044810Z_primality-tactic.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
…rion

Cert3.lean proves pocklington3, the BLS m = 1 theorem, Mathlib-free:
divisor_mod_one lifts the per-prime-divisor congruence to all
divisors; the certificate equation abF + (a+b) = 2sF + r pins the
composite split; parity (R odd, F even) forces the mod-F offset even,
which r < 2F kills on one side and the size-bound cap 2s <= 2F + r
(two_s_le) kills on the other; the discriminant identity
r^2 = (a-b)^2 + 8s then contradicts each disjunct of the condition.
not_square_of_sqrt_witness is the two-multiplication witness form of
the integer-square test for the checker.

Progress: progress/20260822T045935Z_primality-bls.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
checkPock3Arith gains the real Brillhart-Lehmer-Selfridge conditions
with the stored square-root witness; pock_divisor_step is factored out
and shared by both soundness cases; the pock3 case of checker
soundness closes through pocklington3; certificate search routes to
mkPock3 when the factored part misses the square root, with the
wrapper's checkPrime validation deciding acceptance. Guards cover the
199/F=6 cube-root acceptance and one rejection per condition. The SPEC
records the except-step-4 correction, the witness-based discriminant
test, and Nat.sqrt's removal from the replay closure.

Progress: progress/20260822T050441Z_primality-pock3.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Scaffolds HexPrimalityMathlib and adds Prime.lean (prime_iff via
prime_iff_forall_lt against Nat.prime_def_lt, plus Nat.Prime-flavoured
transports of the checker, decision, Miller-Rabin, and next-prime
theorems; no DecidablePred Nat.Prime instance, per SPEC) and
Segment.lean (primeTable_spec, primesIn_spec, filter_prime_range, and
the forall_prime_lt scaffold for segment statements).

Progress: progress/20260822T050700Z_primality-companion.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Kim Morrison and others added 16 commits August 24, 2026 02:27
The stack changes factor-sweep-relevant sources with no effect on the
factorization service: the powModNat kernel-facing amendments and the
Decidable (Prime p) reroute in hex-arith, the primality conformance
emitters, the hotPathCandidates proof-carrying view with pinned values,
and per-milestone lakefile registrations. Each transition gets an exact
blob-pair exemption with its reason, and the file is unioned with
main's current entries so the stack also inherits the HexBasic/Rand
attestation that merged separately.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
# Conflicts:
#	scripts/bench/proof_only_runtime_exemptions.json
# Conflicts:
#	scripts/bench/proof_only_runtime_exemptions.json
# Conflicts:
#	.github/workflows/ci.yml
#	SPEC/future-work.md
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
Kim Morrison and others added 26 commits August 24, 2026 12:19
Codex review noted n.log2 alone admits an input one bit past the
advertised ceiling; a positive numeral has n.log2 + 1 bits.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133rJ7x9mYiiXpJceCnc1A9
# Conflicts:
#	.github/workflows/ci.yml
# Conflicts:
#	HexPrimality.lean
#	scripts/bench/proof_only_runtime_exemptions.json
# Conflicts:
#	scripts/bench/proof_only_runtime_exemptions.json
# Conflicts:
#	.github/workflows/ci.yml
#	scripts/bench/proof_only_runtime_exemptions.json
@kim-em
kim-em changed the base branch from primality-pock3 to main August 24, 2026 18:15
@kim-em
kim-em merged commit 037f9d6 into main Aug 24, 2026
1 check passed
@kim-em
kim-em deleted the primality-mathlib branch August 24, 2026 18:39
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.

1 participant