crypto: Specialize the Fq2 multiplication by ksi - #1648
Open
chfast wants to merge 1 commit into
Open
Conversation
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
force-pushed
the
crypto/mul-by-ksi
branch
from
August 12, 2026 11:43
9ba9a9c to
8503d0a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 * 1anda0 * 1were computed for nothing.mul_by_ksi()computes(a0 + a1*u)*(9 + u) = (9a0 - a1) + (a0 + 9a1)*udirectly 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_ksiwas differentially tested against the genericmultiply(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. Thestatic_assertpins the coefficient that licenses dropping the two multiplications.🤖 Generated with Claude Code