Skip to content

crypto: Specialize the Fq2 multiplication by ksi - #1648

Open
chfast wants to merge 1 commit into
masterfrom
crypto/mul-by-ksi
Open

crypto: Specialize the Fq2 multiplication by ksi#1648
chfast wants to merge 1 commit into
masterfrom
crypto/mul-by-ksi

Conversation

@chfast

@chfast chfast commented Aug 12, 2026

Copy link
Copy Markdown
Member

Multiplication by ksi = 9 + u, the non-residue defining the Fq6 extension, went through the generic Fq2 multiplication. That costs 4 field multiplications where 2 suffice: the u coefficient of ksi is 1, so a1 * 1 and a0 * 1 were computed for nothing. mul_by_ksi() computes (a0 + a1*u)*(9 + u) = (9a0 - a1) + (a0 + 9a1)*u directly and is used at the 13 sites, spread over the Fq6 and Fq12 multiplication, squaring and inversion, the cyclotomic squaring and the Miller loop's line multiplication.

The dependency depth is unchanged (multiplication then addition, exactly as before), so this is a strict removal of work rather than a trade-off.

ecpairing benchmark, interleaved A/B with two-point subtraction: instructions -6.6% (0.02% spread), cycles -2.6% by minimum and -4.6% by median over 7 repetitions, faster in 7/7. Only the cycles range is soft, the box was at load 50-64 throughout.

The obvious stronger version, computing 9a with additions only so that no multiplication is left at all, is a regression and the TODO in the code records it: -9.3% instructions but +5.2% cycles, IPC 1.78 to 1.54. The chain v, 2v, 4v, 8v, 9v is 4 dependent modular additions, each with its own carry chain and conditional subtraction, replacing multiplications that were independent and pipelined well. Fewer instructions, longer critical path.

Correctness: mul_by_ksi was differentially tested against the generic multiply(a, ksi) over 200,100 inputs, the field edge cases (0, 1, 2, 8, 9, p-1, p-2, p/2, p/2+1, (p-1)/9 in both coordinates) and 200k random Fq2 values, with no mismatch. The static_assert pins the coefficient that licenses dropping the two multiplications.

🤖 Generated with Claude Code

Multiplication by ksi = 9 + u, the non-residue defining the Fq6
extension, went through the generic Fq2 multiplication, which needs 4
field multiplications where 2 suffice: the u coefficient of ksi is 1,
so a1 * 1 and a0 * 1 were computed for nothing. Add mul_by_ksi()
computing (a0 + a1*u)*(9 + u) = (9a0 - a1) + (a0 + 9a1)*u directly and
use it at the 13 sites, spread over the Fq6 and Fq12 multiplication,
squaring and inversion, the cyclotomic squaring and the Miller loop's
line multiplication. Cuts up to 7% off the ECPAIRING instruction count
and a few percent off its cycles.
@chfast
chfast force-pushed the crypto/mul-by-ksi branch from 9ba9a9c to 8503d0a Compare August 12, 2026 11:43
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.72%. Comparing base (6742265) to head (8503d0a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1648      +/-   ##
==========================================
- Coverage   97.72%   97.72%   -0.01%     
==========================================
  Files         171      171              
  Lines       15631    15630       -1     
  Branches     3617     3617              
==========================================
- Hits        15275    15274       -1     
  Misses        269      269              
  Partials       87       87              
Flag Coverage Δ
eest-develop 88.58% <100.00%> (-0.01%) ⬇️
eest-develop-gmp 26.51% <100.00%> (-0.01%) ⬇️
eest-legacy 17.14% <0.00%> (+<0.01%) ⬆️
eest-libsecp256k1 28.81% <100.00%> (-0.01%) ⬇️
eest-stable 88.58% <100.00%> (-0.01%) ⬇️
evmone-unittests 93.44% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
core 96.10% <100.00%> (-0.01%) ⬇️
tooling 91.92% <ø> (ø)
tests 99.80% <ø> (ø)
Files with missing lines Coverage Δ
lib/evmone_precompiles/pairing/bn254/fields.hpp 100.00% <100.00%> (ø)
lib/evmone_precompiles/pairing/bn254/pairing.cpp 100.00% <100.00%> (ø)
lib/evmone_precompiles/pairing/bn254/utils.hpp 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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