Skip to content

crypto: Multiply by 9 with a single reduction in the ksi product - #1804

Open
chfast wants to merge 1 commit into
masterfrom
crypto/ksi-mul9-fused
Open

chfast wants to merge 1 commit into
masterfrom
crypto/ksi-mul9-fused

Conversation

@chfast

@chfast chfast commented Nov 3, 2026

Copy link
Copy Markdown
Member

The multiplication by ksi computed 9a0 − a1 and 9a1 + a0 with
a Montgomery multiplication by 9 followed by a modular subtraction or
addition.

Add modmuladd9(x, y, mod) computing (9x + y) % mod directly: the sum
9x + y is below 10·mod, so a quotient estimated from its top word is
exact or one less, and one conditional subtraction completes the
reduction. Multiplying by an integer keeps the Montgomery form, so it
works on the internal representation as is.

Field elements expose it through the a * 9 + b and a * 9 - b
notation: a * 9 returns a proxy reduced together with the following
addition or subtraction.

This reduces the ecpairing cycles by about 4.5% with GCC 15 and 5% with
Clang 23.

The multiplication by ksi computed 9a0 − a1 and 9a1 + a0 with
a Montgomery multiplication by 9 followed by a modular subtraction or
addition.

Add modmuladd9(x, y, mod) computing (9x + y) % mod directly: the sum
9x + y is below 10·mod, so a quotient estimated from its top word is
exact or one less, and one conditional subtraction completes the
reduction. Multiplying by an integer keeps the Montgomery form, so it
works on the internal representation as is.

Field elements expose it through the `a * 9 + b` and `a * 9 - b`
notation: `a * 9` returns a proxy reduced together with the following
addition or subtraction.

This reduces the ecpairing cycles by about 4.5% with GCC 15 and 5% with
Clang 23.
@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.24%. Comparing base (d018cee) to head (d04f0cf).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1804   +/-   ##
=======================================
  Coverage   98.24%   98.24%           
=======================================
  Files         186      186           
  Lines       17169    17191   +22     
  Branches     3950     3953    +3     
=======================================
+ Hits        16867    16889   +22     
  Misses        233      233           
  Partials       69       69           
Flag Coverage Δ
eest-develop-gmp 27.04% <100.00%> (+0.09%) ⬆️
eest-legacy 17.21% <0.00%> (-0.03%) ⬇️
eest-libsecp256k1 29.01% <100.00%> (+0.09%) ⬆️
eest-stable 83.86% <100.00%> (+0.05%) ⬆️
evmone-unittests 93.95% <100.00%> (+<0.01%) ⬆️
riscv64 94.83% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
core 96.63% <100.00%> (+0.01%) ⬆️
tooling 94.87% <ø> (ø)
tests 99.82% <ø> (ø)
Files with missing lines Coverage Δ
lib/evmone_precompiles/ecc.hpp 98.19% <100.00%> (+0.02%) ⬆️
lib/evmone_precompiles/modarith.hpp 100.00% <100.00%> (ø)
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.

@codspeed

codspeed Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 3.1%

1 warning

⚠️ 11 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 11 improved benchmarks
✅ 888 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_alt_bn128[fork_Osaka-blockchain_test-bn128_one_pairing-benchmark-gas-value_1M] 96.1 ms 92 ms +4.45%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_1-benchmark-gas-value_1M] 96.1 ms 92 ms +4.45%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-bn128_two_pairings-benchmark-gas-value_1M] 89.1 ms 86 ms +3.59%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_2_pair-benchmark-gas-value_1M] 89.1 ms 86 ms +3.59%
⚡ precompile<ecpairing, ecpairing_execute> 259.2 ms 250.8 ms +3.33%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_3-benchmark-gas-value_1M] 85.6 ms 83 ms +3.12%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_4_pair-benchmark-gas-value_1M] 86.9 ms 84.5 ms +2.82%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_5_pair-benchmark-gas-value_1M] 82.1 ms 80.1 ms +2.61%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_6-benchmark-gas-value_1M] 71.3 ms 69.6 ms +2.44%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_12-benchmark-gas-value_1M] 84.8 ms 83.1 ms +2.01%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_24-benchmark-gas-value_1M] 80.2 ms 78.8 ms +1.75%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing crypto/ksi-mul9-fused (d04f0cf) with master (d018cee)

Open in CodSpeed

This branch has not been deployed

No deployments
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