Skip to content

Feat: add purego ext 6 over koalabear and babybear fields#772

Merged
yelhousni merged 22 commits into
masterfrom
feat/smallfield-E6
May 13, 2026
Merged

Feat: add purego ext 6 over koalabear and babybear fields#772
yelhousni merged 22 commits into
masterfrom
feat/smallfield-E6

Conversation

@yelhousni
Copy link
Copy Markdown
Collaborator

@yelhousni yelhousni commented Dec 2, 2025

Description

We might need extension 6 over koalabear instead of 4 for higher security. This PR implements purego ext 6 over koala/baby bears.

Type of change

  • New feature (non-breaking change which adds functionality)

How has this been tested?

Corresponding tests implemented in e6_test.go

How has this been benchmarked?

Benchmarks in e6_test.go

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Note

Medium Risk
Adds new extension-field arithmetic (E6/E6D) and changes non-residue multiplication APIs used by higher-degree operations, so correctness/performance regressions are possible despite property tests and benches.

Overview
Adds degree-6 extension field support for babybear and koalabear by generating new E6 (tower over E2) and E6D (direct Fr[w]/(...)) implementations, including batch inversion, exponentiation, division, and conversion between tower/direct representations.

Renames/clarifies non-residue helpers across extensions: MulByNonResidueMulByQuadraticNonResidue, removes the old inverse helper, and (for babybear/koalabear) introduces MulByCubicNonResidue used by the new E6 formulas; E4 is updated accordingly. Generator templates and docs are updated to emit the new files and document extension-6 constructions, and new property tests/benchmarks are added for the E6/E6D APIs.

Reviewed by Cursor Bugbot for commit da0499e. Bugbot is set up for automated code reviews on this repo. Configure here.

@yelhousni yelhousni self-assigned this Dec 2, 2025
@yelhousni yelhousni added dep: linea Issues affecting downstream Linea proposition labels Dec 2, 2025
Comment thread field/generator/internal/templates/extensions/e6.go.tmpl Outdated
Comment thread field/babybear/extensions/e6.go Outdated
Comment thread field/generator/internal/templates/extensions/e6.go.tmpl Outdated
Comment thread field/koalabear/extensions/e6_direct.go
gbotrel
gbotrel previously approved these changes Dec 15, 2025
Copy link
Copy Markdown
Collaborator

@gbotrel gbotrel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just add goimports to your flow half of the files are modified / staticcheck doesn't pass because of this :)

Comment thread field/koalabear/extensions/e6_direct.go
Comment thread field/babybear/extensions/e6_direct.go
Comment thread field/babybear/extensions/e6.go Outdated
@yelhousni yelhousni requested a review from gbotrel December 15, 2025 22:26
@TomWambsgans
Copy link
Copy Markdown

Hello! I am curious, what is the current state of performance of the degree 6 extension for koala-bear (in particular vs degree 5 if you have any number), and which algorithm is the fastest on gnark? (is it Toom3?) Thanks in advance ;)

@gbotrel
Copy link
Copy Markdown
Collaborator

gbotrel commented May 7, 2026

Review pass from 2026-05-07.

I did not find blocking issues in this pass. The targeted extension-field packages compile and pass locally, and GitHub checks are green at the PR head I reviewed.

Verification run locally:

  • go test ./field/koalabear/extensions ./field/babybear/extensions

- Fix Exp/ExpInt64 doc strings: (mod q⁴) → (mod q⁶) (copy-paste from E4).
- E6.MulByElement: copy y locally to match MulByE2's aliasing-safe pattern.
- E6.Exp: skip leading zero bits of the most-significant byte. ~4% faster
  on a 200-bit exponent (5318 vs 5523 ns/op on Apple M5 Max), no allocations
  beyond the existing one for big.Int negation.
- E6D.Inverse: convert in-place via toTowerInto/setFromTower helpers instead
  of allocating two heap structs via ToTower/FromTower.
- FromTower/ToTower: document the v=w, u=w³-1 isomorphism.
- Fix typo: "We then we re-arrange" → "We then re-arrange".
- Add Halve docstring.
- Add BenchmarkE6DMulNaive and Exp tests for negative big.Int and k > int64.

Benchmarks also revealed that mulNaive is currently ~45% faster than
mulMontgomery6 on koalabear (27.5 vs 49.6 ns/op) and ~62% faster on
babybear (29.2 vs 76.1 ns/op) — Montgomery6's 17M-vs-36M tradeoff loses
to schoolbook when Fr.Mul is a few cycles, and `interpolate` returning
&v forces a heap allocation per call. Worth revisiting the Mul default
in a follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@yelhousni yelhousni merged commit d00b4bf into master May 13, 2026
13 checks passed
@yelhousni yelhousni deleted the feat/smallfield-E6 branch May 13, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dep: linea Issues affecting downstream Linea proposition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants