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
as mul9_add(a, b) and mul9_sub(a, b).

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 (7c237c3) to head (8b0cc64).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1804   +/-   ##
=======================================
  Coverage   98.23%   98.24%           
=======================================
  Files         186      186           
  Lines       17141    17162   +21     
  Branches     3949     3952    +3     
=======================================
+ Hits        16839    16860   +21     
  Misses        233      233           
  Partials       69       69           
Flag Coverage Δ
eest-develop-gmp 26.92% <100.00%> (+0.08%) ⬆️
eest-legacy 17.24% <0.00%> (-0.03%) ⬇️
eest-libsecp256k1 28.89% <100.00%> (+0.08%) ⬆️
eest-stable 83.79% <100.00%> (+0.05%) ⬆️
evmone-unittests 93.94% <100.00%> (+<0.01%) ⬆️
riscv64 94.82% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
core 96.61% <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.01%) ⬆️
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 regress 1 benchmark

1 warning

⚠️ 16 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

⚡ 35 improved benchmarks
❌ 1 regressed benchmark
✅ 863 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP1-benchmark-gas-value_1M] 9.4 ms 9.5 ms -1.6%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-bn128_one_pairing-benchmark-gas-value_1M] 96.2 ms 92.4 ms +4.17%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_1-benchmark-gas-value_1M] 96.2 ms 92.4 ms +4.17%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-bn128_two_pairings-benchmark-gas-value_1M] 89.2 ms 86.3 ms +3.39%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_2_pair-benchmark-gas-value_1M] 89.2 ms 86.3 ms +3.38%
⚡ precompile<ecpairing, ecpairing_execute> 259.5 ms 251.6 ms +3.15%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_3-benchmark-gas-value_1M] 85.7 ms 83.3 ms +2.95%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_4_pair-benchmark-gas-value_1M] 87 ms 84.8 ms +2.68%
⚡ test_gas_op[fork_Osaka-blockchain_test-benchmark-gas-value_1M] 12.3 ms 12 ms +2.53%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP16-benchmark-gas-value_1M] 9.6 ms 9.3 ms +2.52%
⚡ test_alt_bn128[fork_Osaka-blockchain_test-ec_pairing_5_pair-benchmark-gas-value_1M] 82.2 ms 80.3 ms +2.48%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP10-benchmark-gas-value_1M] 9.6 ms 9.3 ms +2.34%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_6-benchmark-gas-value_1M] 71.4 ms 69.8 ms +2.33%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP6-benchmark-gas-value_1M] 9.5 ms 9.3 ms +2.23%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP15-benchmark-gas-value_1M] 9.6 ms 9.4 ms +2.22%
⚡ test_msize[fork_Osaka-blockchain_test-mem_size_1000-benchmark-gas-value_1M] 13.1 ms 12.9 ms +2.21%
⚡ baseline/analyse/main/structarray_alloc 5.3 µs 5.2 µs +2.08%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP14-benchmark-gas-value_1M] 9.5 ms 9.4 ms +1.95%
⚡ test_alt_bn128_benchmark[fork_Osaka-blockchain_test-num_pairs_12-benchmark-gas-value_1M] 84.9 ms 83.3 ms +1.94%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP11-benchmark-gas-value_1M] 9.5 ms 9.4 ms +1.78%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing crypto/ksi-mul9-fused (8b0cc64) with master (7c237c3)

Open in CodSpeed

@chfast
chfast force-pushed the crypto/ksi-mul9-fused branch from d04f0cf to a15f046 Compare November 4, 2026 19:04
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
as mul9_add(a, b) and mul9_sub(a, b).

This reduces the ecpairing cycles by about 4.5% with GCC 15 and 5% with
Clang 23.
@chfast
chfast requested a balanced review from Copilot November 5, 2026 06:03
@chfast
chfast force-pushed the crypto/ksi-mul9-fused branch from a15f046 to 8b0cc64 Compare November 5, 2026 06:03

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.

🟡 Changes recommended

The new reduction lacks direct tests validating its generic modulus contract, including boundary moduli, extra-word carry, and quotient-estimate edge cases.

1 open issue
What changed in this PR

Optimizes BN254 pairing’s multiplication by ksi by replacing Montgomery multiplications by 9 and separate additions or subtractions with combined modular reductions.

Changes:

  • Adds modmuladd9 to compute (9x + y) % mod directly while preserving Montgomery form.
  • Exposes mul9_add and mul9_sub on field elements and uses them in the ksi product.
File Description
lib/​evmone_precompiles/​pairing/​bn254/​fields.hpp Uses the combined operations for multiplication by ksi.
lib/​evmone_precompiles/​modarith.hpp Adds quotient-estimation reduction for 9x + y.
lib/​evmone_precompiles/​ecc.hpp Adds field-element wrappers for 9a + b and 9a - b.

Review effort: Balanced

Comment on lines +93 to +95
const auto s = UintT::num_bits - intx::clz(mod) - 64 + 4;
const auto d = static_cast<uint64_t>(mod >> s) + 1;
const auto q = static_cast<uint64_t>(t >> s) / d;

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.

2 participants