agentleFS
r-irber-irbe/proof-skills/skills/lean-unboxed-limbs-swar/SKILL.md

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.

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
  1. Very specific:

    Never emit x &&& (0 - x) on Nat (0 - x = 0).

    line 53

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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^63 limbs (5 x 52-bit with 11-bit carry headroom), packing parallel bounded counters into 62-bit SWAR lanes (mask/guard cross-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

  1. Audit Allocation & Limb Width -- Check whether intermediate Nat states cross $2^{63}$ (mpz_t) and pick unsaturated limb widths w < 63 giving enough carry headroom ($2^{63 - w}$ unnormalized additions before carry propagation).
  2. 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).
  3. Compile Branches to ANF -- Replace branching Bool.rec/if inside hot recursor steps with branch-free Zhegalkin polynomials (Maj(a,b,c) = (a &&& (b ^^^ c)) ^^^ (b &&& c)), avoiding 0 - x on truncating Nat (KERNEL-LAW-18B).
  4. Prove Equivalence & Lowering Guards -- Bridge packed/limb execution to the mathematical spec in Section 3 (rfl or omega) and pair multi-limb C++20/Rust targets with checked-small -> NeedsWide guards (N01..N12).

Recovery & STOP

  • Never emit x &&& (0 - x) on Nat (0 - x = 0); use Nat.land x (Nat.sub x 1) or BitVec 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..31 kernel cost model
  • ../lean-asymptotic-algorithms/SKILL.md -- Asymptotic algorithm selection

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.