Skip to content

crypto: Multiply Fq2 with the Karatsuba method - #1644

Merged
chfast merged 1 commit into
masterfrom
crypto/fq2-karatsuba
Aug 10, 2026
Merged

crypto: Multiply Fq2 with the Karatsuba method#1644
chfast merged 1 commit into
masterfrom
crypto/fq2-karatsuba

Conversation

@chfast

@chfast chfast commented Aug 10, 2026

Copy link
Copy Markdown
Member

multiply(Fq2) was schoolbook, 4 base field multiplications, while Fq6 and
Fq12 already used Karatsuba. At Fq2 the trade is one multiplication for
three additions, which pays because a Montgomery multiplication costs
several times an addition.

About 6% off the ECPAIRING instruction count, measured with paired
interleaved A/B runs of both binaries.

A TODO records the step left on the table: holding the three products at
double width and reducing once per output coefficient instead of after
every multiplication. That needs a wide multiplication and a standalone
Montgomery reduction in ModArith.

🤖 Generated with Claude Code

multiply(Fq2) was schoolbook, 4 base field multiplications, while every
other level of the tower already used Karatsuba. Trading one
multiplication for three additions pays here because a Montgomery
multiplication costs several times an addition: about 6% off the
ECPAIRING instruction count.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Optimizes BN254 Fq2 multiplication using Karatsuba’s method.

Changes:

  • Reduces base-field multiplications from four to three.
  • Documents the algorithm and potential lazy-reduction optimization.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@chfast
chfast merged commit d3c2a75 into master Aug 10, 2026
21 of 22 checks passed
@chfast
chfast deleted the crypto/fq2-karatsuba branch August 10, 2026 23:02
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.71%. Comparing base (7693eee) to head (ef99ce6).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1644   +/-   ##
=======================================
  Coverage   97.71%   97.71%           
=======================================
  Files         171      171           
  Lines       15607    15609    +2     
  Branches     3610     3610           
=======================================
+ Hits        15251    15253    +2     
  Misses        269      269           
  Partials       87       87           
Flag Coverage Δ
eest-develop 88.63% <100.00%> (+<0.01%) ⬆️
eest-develop-gmp 26.54% <100.00%> (+<0.01%) ⬆️
eest-legacy 17.15% <0.00%> (-0.01%) ⬇️
eest-libsecp256k1 28.84% <100.00%> (+<0.01%) ⬆️
eest-stable 88.63% <100.00%> (+<0.01%) ⬆️
evmone-unittests 93.42% <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%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chfast

chfast commented Aug 11, 2026

Copy link
Copy Markdown
Member Author

Reverted because causes performance regressions.

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.

2 participants