lean-unboxed-limbs-swar
Skill3 starsChanged 13 days agoApache-2.0View on GitHub
USE FOR: Fiat-Crypto unsaturated `< 2^63` `lean_box` limbs (`5 x 52-bit`), zero GMP allocation, guarded SWAR arithmetic, and branch-free Zhegalkin ANF circuits (`Maj`, `Ch`) in Lean 4. DO NOT USE FOR: general real analysis (`@lean-math-analysis`) or high-level `Prop` refactoring (`@lean-proof-refactor`). TRIGGERS: lean_box, 2^63, unsaturated limbs, Fiat-Crypto, SWAR, broadword, Zhegalkin, ANF, Maj, Ch, mpz_t, carry headroom.
In this repository43 other files it gives its agents.
- AGENTS.mdAGENTS.md
- counterexample-first-provingSkill
- hol-metamath-adapterSkill
- isabelle-isar-adapterSkill
- itp-cross-portingSkill
- lean-ai-formalizationSkill
- lean-asymptotic-algorithmsSkill
- lean-bisectSkill
- lean-blueprintSkill
- lean-buildSkill
- lean-computational-reflectionSkill
- lean-core-onlySkill
- lean-corpus-auditSkill
- lean-dependency-managementSkill
- lean-doc-syncSkill
- lean-egraph-cpsat-superoptSkill
- lean-gatewaySkill
- lean-kernel-optimizationSkill
- lean-knowledge-formalizationSkill
- lean-math-analysisSkill
- lean-math-discreteSkill
- lean-math-dynamicalSkill
- lean-math-foundationsSkill
- lean-math-optimizationSkill
- lean-math-stochasticSkill
- lean-mweSkill
- lean-pedagogical-expositionSkill
- lean-proof-refactorSkill
- lean-proof-repairSkill
- lean-proof-reviewSkill
- lean-proofSkill
- lean-researchSkill
- lean-retro-methodologySkill
- lean-review-councilSkill
- lean-security-formalizationSkill
- lean-setupSkill
- lean-specificationSkill
- lean-tautology-triageSkill
- lean-upstream-prSkill
- lean-verification-gateSkill
- lean-worker-wrapper-cpsSkill
- research-councilSkill
- rocq-proof-adapterSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 53
Never emit x &&& (0 - x) on Nat (0 - x = 0).
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Unboxed < 2^63 Limb Tuples & Guarded SWAR in Lean 4 Kernel WHNF
Eliminate mpz_t heap allocations in Lean 4's C++ reduction kernel by keeping all hot scalars strictly below $2^{63}$
(lean_box immediate tagged pointers) via unsaturated multi-limb tuples, guarded SWAR lanes, and branch-free ANF logic.
Routing
- USE FOR: splitting 128-to-512-bit integers into unsaturated
< 2^63limbs (5 x 52-bitwith 11-bit carry headroom), packing parallel bounded counters into 62-bit SWAR lanes (mask/guardcross-lane isolation), and compiling conditionals into 3-gate Zhegalkin ANF circuits (Maj,Ch). - DO NOT USE FOR: general algebraic proofs (
@lean-proof) or single-file refactorings (@lean-proof-refactor).
Workflow
- Audit Allocation & Limb Width -- Check whether intermediate
Natstates cross $2^{63}$ (mpz_t) and pick unsaturated limb widthsw < 63giving enough carry headroom ($2^{63 - w}$ unnormalized additions before carry propagation). - Enforce Guarded SWAR & Width-2 Pareto -- For parallel integer lanes inside one scalar
< 2^63, separate sums with(mask, guard)bits or prefer width-2 register pairs(loWord, hiWord)with 1-bit steps (KERNEL-LAW-24). - Compile Branches to ANF -- Replace branching
Bool.rec/ifinside hot recursor steps with branch-free Zhegalkin polynomials (Maj(a,b,c) = (a &&& (b ^^^ c)) ^^^ (b &&& c)), avoiding0 - xon truncatingNat(KERNEL-LAW-18B). - Prove Equivalence & Lowering Guards -- Bridge packed/limb execution to the mathematical spec in Section 3 (
rfloromega) and pair multi-limb C++20/Rust targets withchecked-small -> NeedsWideguards (N01..N12).
Recovery & STOP
- Never emit
x &&& (0 - x)onNat(0 - x = 0); useNat.land x (Nat.sub x 1)orBitVec w(KERNEL-LAW-18B). - Re-check carry overflow invariants (
sum < 2^63) whenever unrolling more additions between carry passes.
Handoffs
- Predecessors:
lean-specification,lean-math-discrete. - Successors:
lean-kernel-optimization,lean-proof-review,lean-verification-gate.
Detailed reference
- Fast lookup card:
../../references/cards/lean-unboxed-limbs-swar-tables.md - Full handbook:
../../references/handbooks/lean-unboxed-limbs-swar-handbook.md - Template:
../../templates/core/Template_UnboxedLimbTuples_SWAR.md
See also
../lean-kernel-optimization/SKILL.md--KERNEL-LAW-01..31kernel cost model../lean-asymptotic-algorithms/SKILL.md-- Asymptotic algorithm selection
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

