Skip to content

Performance: field-op counts + batched group law + smalljac comparison - #2

Merged
salindne merged 2 commits into
mainfrom
perf-smalljac-comparison
Jun 18, 2026
Merged

Performance: field-op counts + batched group law + smalljac comparison#2
salindne merged 2 commits into
mainfrom
perf-smalljac-comparison

Conversation

@salindne

@salindne salindne commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Follow-up to #1 (genus-2 split model). Performance characterization + a new batched group law. No changes to the existing scalar formulas.

1. Field-operation counts (e7d6d4a)

src/g2/split/op_counts.rs: a CountingField wrapper that runs the real add/double formulas and tallies M/S/I/A (the field-size-independent metric from Lange / Erickson–Jacobson–Stein / Costello–Lauter). Asserts exactly one inversion per op. Table in README.

2. Batched group law — Montgomery simultaneous inversion (846c258)

  • field::batch_invert — one field inversion + ~3(n−1) mults for a whole slice.
  • g2::ramified::not_char2: two-phase split of the generic deg-2 add/double (deg2_add_pre/deg2_add_post, …) + add_batch/double_batch drivers that amortize the single inversion across a batch. Mirrors smalljac's ctx + ff_parallel_invert path.
  • Test: batched results are identical, element-for-element, to scalar on 1000 random inputs.

3. smalljac wall-clock comparison — scalar + batched (752c87b, a545959)

Built smalljac v4.1.3 + ff_poly v1.2.7 on arm64 (benches/smalljac-compare/, harness only — smalljac/ff_poly not vendored, GPL).

⚠️ Correction to an earlier revision of this PR. The first harness left f[4] ≠ 0, which makes hecurve_g2_compose silently revert to smalljac's slow generic Cantor path — so those scalar numbers (and the "this crate is ~2× faster" claim) measured Cantor, not smalljac's fast formula. Fixed (f4 = 0, the depressed quintic this crate's not_char2 also uses). The corrected conclusion is the opposite: smalljac is faster.

Corrected, same-machine results (ns per op):

field op crate scalar smalljac scalar crate batched smalljac batched
16-bit add 222 100 99 47
16-bit double 245 105 115 54
56-bit add 673 190 348 48
56-bit double 691 210 384 54

Batching helps both (removes the inversion): crate 56-bit add 673→348 (1.9×), smalljac 190→48 (4×). smalljac is faster in every cell, and the reason is the field layer, not the genus-2 formulas (op counts match): at 56-bit, multiply 8.2 ns (crate) vs 4.5 ns (smalljac, Montgomery), inversion 529 ns vs 157 ns. Closing the gap is a Montgomery-PrimeField task, not a formula one. Full table + analysis in the README.

Checks

cargo fmt --check, cargo +stable clippy --all-targets -- -D warnings, cargo test --release (228 + doctest), cargo bench --no-run, cargo doc all green.

salindne added 2 commits June 18, 2026 14:24
Add an instrumented `CountingField` and a reproducible test that reports the
generic-branch degree-2 add/double field-operation counts for each variant
(run: cargo test --release g2::split::op_counts -- --nocapture). The test
asserts each group operation uses exactly one field inversion (affine formula
invariant). README documents the table; these counts are the field-size-
independent metric for comparing against Lange / Erickson-Jacobson-Stein /
Costello-Lauter.
Add a ~56-bit prime instantiation to the ramified not_char2 benchmarks and a
reproducible C harness (benches/smalljac-compare/) that times smalljac's
genus-2 imaginary group law (hecurve_g2_compose/square) on the affine path.

At matched field width the crate's degree-2-specialized explicit formulas
(~741/682 ns add/double) run about 2x faster than smalljac's general
composition (~1470/1619 ns); at 16-bit the gap is wider but unfair, since
ff_poly always uses 57-bit Montgomery arithmetic. Document the numbers and
the (field-width, batched-inversion, specialized-vs-general) caveats in the
README, alongside the field-operation-count table.
@salindne
salindne merged commit f2be61d into main Jun 18, 2026
4 checks passed
@salindne salindne changed the title Performance characterization: field-op counts + smalljac wall-clock comparison Performance: field-op counts + batched group law + smalljac comparison Jun 18, 2026
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