Skip to content

Consolidate BN254 field math under the Lampe layer#305

Merged
Eduardogbg merged 4 commits into
mainfrom
edu/bn254-refactor
Jun 12, 2026
Merged

Consolidate BN254 field math under the Lampe layer#305
Eduardogbg merged 4 commits into
mainfrom
edu/bn254-refactor

Conversation

@Eduardogbg

@Eduardogbg Eduardogbg commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Stacked on edu/blake3 only because edu/pedersen will rebase on
top of this.

I created this PR to organize BN254 logic to match the pattern we introduced
on the curve ops + ECDSA work. It was a bit ad-hoc and scattered, with a lot
of pure logic living on Stdlib, and about to get worse due to the Pedersen PR.

Now it has a Pratt certificate and the necessary instances, and only Hoare
triples live on Stdlib.

Update: I also added a few things around detecting orphan modules to this PR, which is a bit of misc but fit this PR better than the other one and it would require an annoying juggling of rebases to make this in a third branch

Base automatically changed from edu/blake3 to main June 8, 2026 06:41
@Eduardogbg Eduardogbg force-pushed the edu/bn254-refactor branch 2 times, most recently from fa79a81 to 5add67c Compare June 10, 2026 23:28
…e Lampe layer

Move r_scalar, plo, phi, pow128, the Bn254.Prime typeclass, and the
pure-math limb-arithmetic lemmas out of Stdlib/Field/Bn254.lean into
the new Lampe/Crypto/Bn254.lean, and move the Pratt primality
certificate with the concrete prime (Stdlib/Field/Bn254/Prime.lean ->
Lampe/Crypto/Bn254/Prime.lean), matching the Secp256k1/Secp256r1
layering. The stdlib files now only house Noir-extracted bindings and
STHoare specs.

The Bn254.Prime contract is unified on natVal_eq_r_scalar (the limb
form stays available as the natVal_eq_limbs lemma). The concrete prime
is namespaced as Lampe.Crypto.Bn254.prime, mirroring
Lampe.Crypto.Secp256k1.prime, with primeNat defined as r_scalar so the
Bn254.Prime Bn254.prime instance holds by rfl, shrinking the trust
path.

Also fixes Mathlib bitrot in EmbeddedCurveOps/Bn254:
WeierstrassCurve.Affine.Point.some now takes x y explicitly.
Drop the duplicated scalarModulus definition in favour of re-exporting
Lampe.Crypto.Bn254.r_scalar via `export`. Tests/Poseidon2 picks up the
exported name through the existing `open Lampe.Crypto.Poseidon2.BN254T4`.
…k_all

Stdlib/EcdsaSecp256K1.lean, Stdlib/EcdsaSecp256R1.lean and
Stdlib/EmbeddedCurveOps/Bn254.lean were never imported from the Stdlib
library root, so lake build (and CI) never elaborated them and their
proofs could silently rot; this is how the ECDSA specs went unbuilt
since #296. All three compile cleanly once registered.

The Stdlib lib root is now the flat mk_all-managed import list
(Stdlib/Stdlib.lean shrinks to the env export it exists for), and CI
runs `lake exe mk_all --check --lib Stdlib` after the stdlib build -
the same guard the Lampe package has. The testing Spec roots import
the new root.
gen_pratt.py now serves all three Prime.lean files and the bespoke
gen_bn254_pratt.py is deleted. Rather than carrying per-namespace
header/footer templates, the checked-in Prime.lean files own their
hand-maintained headers/footers, and the generated certificate region
is delimited by BEGIN/END marker comments:

- Update mode: `gen_pratt.py --prime <P> --update <Prime.lean>`
  regenerates only the marked region in place, preserving everything
  outside the markers byte-for-byte. Errors if markers are missing or
  duplicated, or if the file never references prime_<P> outside the
  region (wrong --prime).
- Bootstrap mode for brand-new primes: `--prime <P> --namespace <Name>`
  emits a generic scaffold (with markers) to stdout for the author to
  hand-tune.

The Secp256k1 and Secp256r1 Prime.lean files gain the markers and
updated regeneration instructions (comment-only changes; defs,
theorems and instances untouched); the Bn254 file carries them since
its extraction. --update is verified idempotent on all three.
@Eduardogbg Eduardogbg force-pushed the edu/bn254-refactor branch from 5add67c to 9b32bca Compare June 12, 2026 03:57
@Eduardogbg Eduardogbg merged commit d7e0529 into main Jun 12, 2026
13 checks passed
@Eduardogbg Eduardogbg deleted the edu/bn254-refactor branch June 12, 2026 05:29
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.

2 participants