Skip to content

Feat/curve generic zolt arith#76

Merged
MatteoMer merged 2 commits into
mainfrom
feat/curve-generic-zolt-arith
Apr 9, 2026
Merged

Feat/curve generic zolt arith#76
MatteoMer merged 2 commits into
mainfrom
feat/curve-generic-zolt-arith

Conversation

@MatteoMer

Copy link
Copy Markdown
Owner

No description provided.

MatteoMer and others added 2 commits April 9, 2026 10:08
Add a curve-generic MontgomeryField(N, modulus, r2, inv) factory and
extension field factories (Fp2, Fp6, Fp12) to zolt-arith, enabling
multi-curve support alongside the existing BN254 stack.

Architecture:
- src/curves/montgomery_field.zig: comptime-N generic field with 40+
  methods, x86 BMI2+ADX asm mul, ARM64 asm dispatch, LLVM adc/sbb
  intrinsics for add/sub, ProductAccum for deferred reduction
- src/curves/extensions.zig: generic Fp2(BaseFp), Fp6(Fp2, mulByXi),
  Fp12(Fp6, Fp2) factories
- src/curves/bn254/: BN254 params + instantiation through generic factory
- src/curves/bls12_381/: full BLS12-381 implementation (G1/G2/pairing/
  hash-to-curve/BLS sign+verify) for zyli's validator verification
- src/bigint.zig: generic N-limb arithmetic

BN254 migration:
- BN254Scalar = curves.bn254.Fr (generic factory replaces 1000+ line
  bespoke struct)
- BN254BaseField uses in-file MontgomeryField for optimal codegen
- Fp2 = curves.extensions.Fp2(Fp) (generic factory)
- Fp6 = curves.extensions.Fp6(Fp2, bn254MulByXi) (generic factory)
- UnreducedProductAccum = BN254Scalar.ProductAccum (generic)

Performance: zero regression — generated assembly is byte-for-byte
identical to the original (verified via objdump diff). All x86-64
BMI2+ADX and AArch64 inline asm paths preserved.

Also fixes pre-existing build breakage from llvm.x86.addcarry.u64
intrinsics (mode-guarded: Release uses intrinsics, Debug uses u128).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ity with OG

- inverse: replace `inline for (0..N*64)` with runtime inner loop to
  avoid icache blowout from 256+ inlined square/mul blocks (80% Fr
  inverse regression)
- Fp2.mul: use sumOfProducts fusion when BaseFp supports it (2
  reductions instead of 3 via Karatsuba)
- Fp2.square: use addNoReduce for (a+b) when available (saves 1
  reduction)
- Move @setEvalBranchQuota before arm64 early-return in add/sub/
  subtractModulus to fix comptime branch limit errors on N=6

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@MatteoMer MatteoMer merged commit f34e684 into main Apr 9, 2026
16 checks passed
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.

1 participant