Repository navigation
fix(fields): make the KoalaBear Rabin certificates replay-safe - #396
Conversation
🤖 PR SummaryProtocols / Soundness
Refactoring
Documentation
No Statistics
Lean Declarations ✏️ Affected: 2 declaration(s) (line number changed)
📋 **Additional Analysis**Review of PR DiffThe diff introduces substantive changes across multiple files. Below is a section-by-section analysis, keyed to the instructions in CONTRIBUTING.md. 1. Title Format
2. Description
3. Alignment with the Performance Contribution Rules
4. New Types Owe a Codec
5. Style and Naming Guidelines
6. Documentation Standards
7. Check for Overlap
8. Summary of Compliance
9. Specific FindingsFinding 1 – Docstring clarity and explanation (positive): Finding 2 – Potential naming inconsistency (minor): Finding 3 – Line length in Lean docstrings: 10. ConclusionThe diff is a well-motivated fix to a subtle kernel-replay reliability problem. The documentation updates are thorough. The primary remaining requirements are (a) providing a compliant PR title and (b) writing a description that states the problem, the fix, the validation commands run, and any related issues. No style or structural violations prevent merging, assuming the description is provided. 📄 **Per-File Summaries**
Last updated: 2026-11-19 21:21 UTC. |
|
Two corrections to the generated summary above, which ran before the title and body were fixed. The title is now |
alexanderlhicks
left a comment
There was a problem hiding this comment.
Thanks for the PR. I reproduced the bug and the fix independently, so the code change itself is right. Before we merge, I'd like to correct the stated cause, because #369 already built docs on a similar explanation, and to make sure the docs say what actually makes the fix work. Evidence is below, then four concrete changes.
1. Reproduction
I replayed each declaration's full dependency closure (about 20.4k declarations) into mkEmptyEnvironment with Lean.Kernel.Environment.replay, which is what Comparator does. The script is at the end.
| Declaration | main |
this PR |
|---|---|---|
KoalaBear.sexticPoly_irreducible |
❌ (kernel) deep recursion detected |
✅ |
KoalaBear.quinticPoly_irreducible |
❌ (kernel) deep recursion detected |
✅ |
The other criterion callers replay cleanly on main: {KoalaBear,BabyBear,Hachi}.ext4Params_poly_irreducible, AesField.modulus_irreducible, BF64.basePoly_irreducible, and the two fixtures in tests/CompPolyTests/Data/Polynomial/RabinCertificate.lean. The PR touches exactly the right two declarations.
2. Why lake build accepted the old proof
The description says the elaborator reconciles the two instance terms, while a replay from an empty environment "need not take the same path". The kernel is deterministic, though. What changes between the two runs is which definition bodies it can see:
- Inside a
modulefile, imported definitions that aren't@[expose]d are axioms to the kernel. CompilingSexticIrreducible.lean, about 20k imported definitions have no body, among them Mathlib'sPolynomial.instAdd. - A full-closure replay sees every body, and so does any non-
modulefile that imports this one.
Here is what shows it:
main's stored proof term is structurally identical in both settings (sameExpr.hash), and so is every constant the module declares. Yet sending it toaddDeclCorepasses in 17 ms inside the module, and fails with deep recursion in about 1 s after a plainimport. No replay is involved. The originalrefineproof doesn't even elaborate in a non-moduleimporter.- Replaying
main's term with onlyPolynomial.instAddmade opaque passes. I found it by bisecting the 17 bodilessPolynomial,MonoidAlgebra,FinsuppandZModdefinitions in the closure; with all ~20k bodiless definitions made opaque, the replay passes in about 7 s. When the body is visible, the kernel unfoldsX ^ (fieldSize ^ 6) - XthroughSubintoPolynomial.instAdd, and then has to normaliseX ^ (fieldSize ^ 6)itself throughnpowRec.
So the boundary isn't elaboration versus replay, or an empty versus a full environment. It's between the defining module's check, which runs against Mathlib's public interface, and any check that sees all the bodies. The "serialized and reconstructed expression graph" wording that #369 put in Rabin.lean and field-extensions.md should be corrected the same way.
3. What actually makes the fix work
The criterion has to be instantiated at exactly the carrier and instance the certificate lemmas use. Neither change alone is enough:
| Spelling | full replay |
|---|---|
@… (ZMod fieldSize) (ZMod.instField fieldSize) … (this PR) |
✅ |
@… KoalaBear.Field (ZMod.instField fieldSize) … |
❌ |
@… (ZMod fieldSize) KoalaBear.instFieldField … |
❌ |
irreducible_of_rabin_degree_six (F := ZMod fieldSize) (ZMod.card _) … (instance synthesized) |
❌ |
original refine, plus attribute [-instance] instFieldField |
❌ |
original refine, plus letI := ZMod.instField fieldSize |
❌ |
Every one of these builds cleanly. So the positional @ form can't be simplified: anyone who later rewrites it with named arguments brings the bug back, and nothing in CI notices.
4. Requested changes
- Docstrings (inline suggestions below): state the real cause, say that the carrier and the instance are both required, and say not to simplify the
@form. - Wiki and agent guide. These files still teach the pattern that broke:
- The recipe in
docs/wiki/field-extensions.md(step 3, around lines 353–358) tells authors to writehcard : Fintype.card Field = fieldSize := ZMod.card _, and points to these two files as the worked examples. Suggested replacement:Assemble with one term-mode application whose carrier and instances are exactly those of the certificate lemmas, e.g.
@irreducible_of_rabin_degree_six (ZMod fieldSize) (ZMod.instField fieldSize) (ZMod.fintype fieldSize) f fieldSize (ZMod.card _) …. Do notrefineagainst a goal stated over the field's abbrev (KoalaBear.Field): it builds, but a full-closure replay does not. - In the "Field Size Enters As A Numeral" section (around lines 263–270) and the matching section comment in
CompPoly/Data/Polynomial/Rabin.lean, replace the "serialized and reconstructed expression graph" explanation with the cause from §2. - Add to the TCB policy in
AGENTS.md:Apply a Rabin criterion at exactly the carrier and instances its certificate lemmas use (
ZMod p,ZMod.instField p,ZMod.fintype p), as one term.lake buildcannot detect a mismatch: inside amodule, unexposed Mathlib bodies such asPolynomial.instAddare opaque, so the slow path only appears in a full replay.
- The recipe in
- Regression guard (fine as a follow-up PR).
lake build,lake testandaxiomsweepall passed while this bug was live. The script below takes about 1–2 minutes per declaration and could become alake exerun over the certificate theorems. One caveat: it also failsBF128Ghash.ghashPoly_irreducibletoday, atgcd_start_reduction(Fields/Binary/BF128Ghash/XPowTwoPowGcdCertificate.lean:61, arwunderX^(2^64)). That bug predates this PR and needs its own issue before the check can gate CI.
Replay script (lake env lean --run Replay.lean <Module> <decl>…)
import Lean
open Lean
partial def closure (env : Environment) (roots : List Name) : Std.HashMap Name ConstantInfo := Id.run do
let mut seen : Std.HashMap Name ConstantInfo := {}
let mut todo := roots
while !todo.isEmpty do
match todo with
| [] => break
| n :: rest =>
todo := rest
if seen.contains n then continue
let some ci := env.find? n | continue
seen := seen.insert n ci
todo := ci.getUsedConstantsAsSet.toList ++ todo
match ci with
| .inductInfo v => todo := v.ctors ++ (Name.mkStr n "rec") :: todo
| .ctorInfo v => todo := v.induct :: todo
| .recInfo v => todo := v.all ++ todo
| _ => pure ()
return seen
def main (args : List String) : IO UInt32 := do
initSearchPath (← findSysroot)
let env ← importModules #[{ module := args[0]!.toName }] {} (loadExts := false)
let cs := closure env ((args.drop 1).map String.toName)
IO.println s!"closure: {cs.size} constants"
let t0 ← IO.monoMsNow
try
let _ ← (← mkEmptyEnvironment).toKernelEnv.replay cs
IO.println s!"OK in {(← IO.monoMsNow) - t0} ms"; return 0
catch e =>
IO.println s!"FAILED after {(← IO.monoMsNow) - t0} ms: {(toString e).take 300}"; return 1For the Polynomial.instAdd experiment, I replaced that constant's ConstantInfo in the map with an axiomInfo of the same type. I also turned the closure's other theorems into axioms, so only the target declaration was checked against the opaque body.
| factors, with kernel-checked certificates for all three conditions. | ||
|
|
||
| Applied at `ZMod fieldSize` as one fully-applied term, rather than by `refine` against the | ||
| `Field`-shaped goal. `Field` is reducibly `ZMod fieldSize`, but the two sides carry different | ||
| instance *terms* for the same instance -- `Field.toCommRing instFieldField` against | ||
| `ZMod.commRing fieldSize` -- and the types carrying them contain `X ^ (fieldSize ^ 6)`. The | ||
| elaborator reconciles them, but a kernel replay from an empty environment need not take the same | ||
| path, and reducing that power through `Polynomial.pow → npowRec → Nat.rec` does not finish. This is | ||
| the hazard the section comment in `CompPoly/Data/Polynomial/Rabin.lean` describes, reached through | ||
| the instance path rather than through a cast. Fixing every argument keeps each instance path on one | ||
| side, so they meet only at `Irreducible sexticPoly`, where no exponent appears. -/ |
There was a problem hiding this comment.
This explanation is the part to correct (see §2–3 of the review). The two instance terms aren't "reconciled by the elaborator". The defining module's kernel check passes only because Mathlib's Polynomial.instAdd has no visible body inside a module. Also, the carrier is as load-bearing as the instance: @… KoalaBear.Field (ZMod.instField fieldSize) … still fails a full replay. Suggested wording:
| factors, with kernel-checked certificates for all three conditions. | |
| Applied at `ZMod fieldSize` as one fully-applied term, rather than by `refine` against the | |
| `Field`-shaped goal. `Field` is reducibly `ZMod fieldSize`, but the two sides carry different | |
| instance *terms* for the same instance -- `Field.toCommRing instFieldField` against | |
| `ZMod.commRing fieldSize` -- and the types carrying them contain `X ^ (fieldSize ^ 6)`. The | |
| elaborator reconciles them, but a kernel replay from an empty environment need not take the same | |
| path, and reducing that power through `Polynomial.pow → npowRec → Nat.rec` does not finish. This is | |
| the hazard the section comment in `CompPoly/Data/Polynomial/Rabin.lean` describes, reached through | |
| the instance path rather than through a cast. Fixing every argument keeps each instance path on one | |
| side, so they meet only at `Irreducible sexticPoly`, where no exponent appears. -/ | |
| factors, with kernel-checked certificates for all three conditions. | |
| Applied as one term at exactly the carrier and instances the certificate lemmas use: | |
| `ZMod fieldSize`, `ZMod.instField fieldSize` and `ZMod.fintype fieldSize`. The carrier and the | |
| instance are both required. With `KoalaBear.Field` in place of `ZMod fieldSize`, or with | |
| `instFieldField` (or any synthesized instance, as `(F := ZMod fieldSize)` gives) in place of | |
| `ZMod.instField fieldSize`, the kernel unfolds `X ^ (fieldSize ^ 6) - X` into | |
| `Polynomial.pow → npowRec`, and a full-closure replay hits deep recursion. `lake build` cannot | |
| catch this: inside a `module`, Mathlib's `Polynomial.instAdd` has no visible body, so the kernel | |
| never takes that path. Keep the explicit `@` application. -/ |
| Applied at `ZMod fieldSize` as one fully-applied term, for the reason given on | ||
| `sexticPoly_irreducible`. -/ |
There was a problem hiding this comment.
Same correction, kept short since the sextic docstring carries the explanation:
| Applied at `ZMod fieldSize` as one fully-applied term, for the reason given on | |
| `sexticPoly_irreducible`. -/ | |
| Applied as one term at `ZMod fieldSize` with `ZMod.instField fieldSize`; see | |
| `sexticPoly_irreducible` for why both are required and why the explicit `@` form must stay. -/ |
|
Thanks — this is a better explanation than the one I shipped, and I've taken it. Pushed as Your mechanism is right and mine was hand-waving. "A replay need not take the same path" describes the symptom; the kernel is deterministic and the asymmetry is exposure. I've replaced it everywhere with the I verified the carrier claim independently rather than take it on faith, since it's what the docs now assert. It holds — One thing I did not do as asked. You asked to replace the "serialized and reconstructed expression graph" explanation in the On the regression guard — I'd rather land it than defer it. You're right that the docstring decays: nothing stops someone respelling the Minor: my timings in the description were on a ~20.0k closure against your ~20.4k, because my traversal didn't add constructors and recursors explicitly. Yours is the better script; the conclusion is unchanged. Disclosure: This comment was written by AI. |
`sexticPoly_irreducible` and `quinticPoly_irreducible` applied the Rabin criteria by `refine` against the `Field`-shaped goal. `Field` is reducibly `ZMod fieldSize`, but the two sides carry different instance terms for the same instance, and the types carrying them contain `X ^ (fieldSize ^ d)`. The elaborator reconciles them; a kernel replay from an empty environment reduces that power through `Polynomial.pow → npowRec → Nat.rec` and does not finish. Applying the criteria at `ZMod fieldSize` as one fully-applied term keeps each instance path on one side, so they meet only at `Irreducible f`, where no exponent appears. Same statements, same certificates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ing too Review feedback. The previous explanation said the elaborator reconciles the two instance terms while a replay "need not take the same path"; the kernel is deterministic, and the actual asymmetry is exposure. Inside a `module`, imported definitions that are not `@[expose]`d have no body for the kernel -- Mathlib's `Polynomial.instAdd` among them -- so the unfolding path does not exist while the defining module compiles, and a full-closure replay, which sees every body, takes it. The carrier matters as much as the instance: `KoalaBear.Field` with the right instance, or `ZMod fieldSize` with `instFieldField` or a synthesized one, all fail a full replay. The docstrings now say both are required and that the explicit `@` application must not be respelled. One correction to the reviewer's note: the mismatched spellings do not always build. At the default `maxRecDepth` they fail to elaborate; only with a raised limit do they compile and leave a replay as the sole check. The docs say that rather than "`lake build` cannot catch this". Also updates the recipe in `docs/wiki/field-extensions.md`, which still taught the `hcard : Fintype.card Field = fieldSize` pattern, and adds the rule to the TCB policy in `AGENTS.md`. The cast hazard the `Rabin.lean` section comment documents is a separate mechanism and is kept; only its build-versus-replay sentence changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1f87c6b to
b3e9e5b
Compare
Applies the KoalaBear Rabin criteria at
ZMod fieldSizeas one fully-applied term, sosexticPoly_irreducibleandquinticPoly_irreduciblereplay from an empty kernel environment in about 5s each, which is what Comparator-style checkers do on every run. Statements and certificates are unchanged; the docs andAGENTS.mdrecord the rule. Rebased onto currentmain(Lean 4.36).🤖 Generated with Claude Code