sage-check: test every lemma on small cases first
Why
Proving a false lemma wastes days, and formalising one in Lean wastes weeks. Most
wrong statements fail on a toy instance: a small prime, a small ring dimension, a
2×2 case. Run a finite check before proving and before formalising.
Protocol
- Transcribe the statement exactly as in THEORY.md. Include every quantifier and
side condition. Each checked statement gets an ID matching THEORY.md / CLAIMS.md.
- Choose a parameter grid.
- Include every smallest case (p ∈ {2, 3, 5, 7, 11, 13}, n ∈ {1, 2, 4, 8}).
- Cover both residue classes the statement distinguishes (for example p ≡ 1 and
p ≡ 3 mod 4).
- Include the boundary of any inequality hypothesis, on both sides.
- Include at least one parameter as large as the experiments use, if it is feasible.
- Test the hypotheses too. Check that the hypotheses are satisfiable on the grid,
because a vacuous theorem "passes" every check. Then check the conclusion.
- Search exhaustively wherever the space is finite and small (≤ 10⁷ cases).
Otherwise use random sampling with a fixed seed, and report the seed and the sample
size.
- Also test the converse. If a condition is claimed to be necessary and
sufficient, test both directions separately.
- Use exact arithmetic:
ZMod/GF, Integer, QQ, sympy.Rational. Use no
floats unless the statement is about floats. For asymptotic claims, check the exact
finite formula on a range and fit the trend; do not trust a plotted curve.
- Output one JSON line per check:
{"check": ID, "params": {...}, "cases": N, "passed": bool, "counterexample": {...} | null}.
Save the script under checks/ and the output under results/checks/, then add an
EVIDENCE row if any number from it reaches the paper.
- If a check fails, give the minimal counterexample (smallest parameters) to
falsifier and theorist. Do not patch the statement silently.
Script template
scripts/sanity_check_template.py is a runnable skeleton with a guarded Sage import,
a check(...) helper that stops at the first counterexample, and a toy statement
about roots of unity mod p. Copy it, then replace the STATEMENTS list.
python3 skills/sage-check/scripts/sanity_check_template.py # pure Python
sage -python skills/sage-check/scripts/sanity_check_template.py # uses Sage if present
Typical checks by area
| area |
what to check on toy parameters |
| algebra |
existence and order of roots of unity mod p; factorisation of Φ_m mod p (number and degree of factors = ord_m(p)); injectivity of encodings; polynomial identities coefficient by coefficient |
| FHE |
noise bound vs measured noise on a toy scheme (the bound must hold on every sample); correctness of the claimed decryption condition at its boundary; operation counts by instrumented toy evaluation |
| lattice |
toy LWE instances solved by LLL/BKZ in small dimensions, compared with the estimator's predicted β |
| symmetric |
S-box properties (DDT/LAT/differential uniformity) by exhaustive computation; small-round trail probabilities by exhaustive search on scaled-down versions |
| protocols |
secret-sharing reconstruction thresholds; OT/PSI correctness on small sets; communication formulas vs counted messages |
Tooling notes
- In Sage,
GF(p), Zmod(n), CyclotomicField(m) and cyclotomic_polynomial(m)
cover most checks. Run with sage -python script.py so the same file stays
importable as plain Python.
- In pure Python,
sympy (cyclotomic_poly, factor_list(..., modulus=p),
n_order, primitive_root) and pow(a, -1, p) are enough for most toy checks.
- Mathematica, if connected, is useful for closed forms and series. Treat its output
as a hint, then recheck with exact finite computation.
1---2name: sage-check3description: Numeric sanity check of every lemma, identity and parameter claim on small parameters, with exhaustive search where feasible. Use it before writing a proof, before Lean formalisation, and whenever a falsifier needs a counterexample search. Works with SageMath if installed and falls back to pure Python/sympy otherwise.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/sage-check6---78# sage-check: test every lemma on small cases first910## Why1112Proving a false lemma wastes days, and formalising one in Lean wastes weeks. Most13wrong statements fail on a toy instance: a small prime, a small ring dimension, a142×2 case. Run a finite check **before** proving and **before** formalising.1516## Protocol17181. **Transcribe the statement exactly** as in THEORY.md. Include every quantifier and19 side condition. Each checked statement gets an ID matching THEORY.md / CLAIMS.md.202. **Choose a parameter grid.**21 - Include every smallest case (p ∈ {2, 3, 5, 7, 11, 13}, n ∈ {1, 2, 4, 8}).22 - Cover both residue classes the statement distinguishes (for example p ≡ 1 and23 p ≡ 3 mod 4).24 - Include the boundary of any inequality hypothesis, on both sides.25 - Include at least one parameter as large as the experiments use, if it is feasible.263. **Test the hypotheses too.** Check that the hypotheses are satisfiable on the grid,27 because a vacuous theorem "passes" every check. Then check the conclusion.284. **Search exhaustively** wherever the space is finite and small (≤ 10⁷ cases).29 Otherwise use random sampling with a fixed seed, and report the seed and the sample30 size.315. **Also test the converse.** If a condition is claimed to be necessary *and*32 sufficient, test both directions separately.336. **Use exact arithmetic**: `ZMod`/`GF`, `Integer`, `QQ`, `sympy.Rational`. Use no34 floats unless the statement is about floats. For asymptotic claims, check the exact35 finite formula on a range and fit the trend; do not trust a plotted curve.367. **Output one JSON line per check**:37 `{"check": ID, "params": {...}, "cases": N, "passed": bool, "counterexample": {...} | null}`.38 Save the script under `checks/` and the output under `results/checks/`, then add an39 EVIDENCE row if any number from it reaches the paper.408. If a check fails, give the **minimal** counterexample (smallest parameters) to41 `falsifier` and `theorist`. Do not patch the statement silently.4243## Script template4445`scripts/sanity_check_template.py` is a runnable skeleton with a guarded Sage import,46a `check(...)` helper that stops at the first counterexample, and a toy statement47about roots of unity mod p. Copy it, then replace the `STATEMENTS` list.4849```bash50python3 skills/sage-check/scripts/sanity_check_template.py # pure Python51sage -python skills/sage-check/scripts/sanity_check_template.py # uses Sage if present52```5354## Typical checks by area5556| area | what to check on toy parameters |57|---|---|58| algebra | existence and order of roots of unity mod p; factorisation of Φ_m mod p (number and degree of factors = ord_m(p)); injectivity of encodings; polynomial identities coefficient by coefficient |59| FHE | noise bound vs measured noise on a toy scheme (the bound must hold on every sample); correctness of the claimed decryption condition at its boundary; operation counts by instrumented toy evaluation |60| lattice | toy LWE instances solved by LLL/BKZ in small dimensions, compared with the estimator's predicted β |61| symmetric | S-box properties (DDT/LAT/differential uniformity) by exhaustive computation; small-round trail probabilities by exhaustive search on scaled-down versions |62| protocols | secret-sharing reconstruction thresholds; OT/PSI correctness on small sets; communication formulas vs counted messages |6364## Tooling notes6566- In Sage, `GF(p)`, `Zmod(n)`, `CyclotomicField(m)` and `cyclotomic_polynomial(m)`67 cover most checks. Run with `sage -python script.py` so the same file stays68 importable as plain Python.69- In pure Python, `sympy` (`cyclotomic_poly`, `factor_list(..., modulus=p)`,70 `n_order`, `primitive_root`) and `pow(a, -1, p)` are enough for most toy checks.71- Mathematica, if connected, is useful for closed forms and series. Treat its output72 as a hint, then recheck with exact finite computation.