⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Conversation

@chfast
Copy link
Member

@chfast chfast commented Jan 27, 2026

Use the relaxed version of the Montgomery multiplication called Almost Montgomery Multiplication from
"Efficient Software Implementations of Modular Exponentiation" (https://eprint.iacr.org/2011/239.pdf).

                                                     │ old              │          new                       │
                                                     │      gas/s       │    gas/s     vs base               │
modexp<expmod_execute>/mod_len:8/exp_bits:33-14             902.5M ± 0%   941.3M ± 0%  +4.29% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:16/exp_bits:33-14            896.6M ± 1%   931.5M ± 0%  +3.88% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:24/exp_bits:33-14            233.0M ± 1%   241.2M ± 0%  +3.53% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:33-14            233.9M ± 1%   242.0M ± 0%  +3.43% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:256-14           245.3M ± 1%   254.7M ± 0%  +3.83% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:8192-14          488.2M ± 0%   505.2M ± 0%  +3.48% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:40/exp_bits:11-14            204.1M ± 0%   212.3M ± 1%  +3.99% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:48/exp_bits:8-14             277.2M ± 0%   288.6M ± 0%  +4.14% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:48/exp_bits:256-14           338.4M ± 0%   351.9M ± 0%  +4.01% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:56/exp_bits:6-14             359.1M ± 0%   371.5M ± 0%  +3.46% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:64/exp_bits:5-14             434.1M ± 0%   446.5M ± 0%  +2.87% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:72/exp_bits:4-14             160.5M ± 1%   161.3M ± 0%  +0.48% (p=0.001 n=11)
modexp<expmod_execute>/mod_len:112/exp_bits:4-14            368.1M ± 0%   369.0M ± 0%  +0.24% (p=0.002 n=11)
modexp<expmod_execute>/mod_len:136/exp_bits:3-14            132.1M ± 0%   133.2M ± 1%  +0.79% (p=0.001 n=11)
modexp<expmod_execute>/mod_len:192/exp_bits:2-14            186.7M ± 1%   186.7M ± 2%       ~ (p=0.199 n=11)
modexp<expmod_execute>/mod_len:504/exp_bits:2-14            82.04M ± 0%   81.86M ± 1%  -0.21% (p=0.010 n=11)
modexp<expmod_execute>/mod_len:512/exp_bits:2-14            84.51M ± 1%   84.42M ± 0%       ~ (p=0.270 n=11)
modexp<expmod_execute>/mod_len:512/exp_bits:8192-14         348.0M ± 0%   348.4M ± 0%  +0.12% (p=0.034 n=11)
modexp<expmod_execute>/mod_len:520/exp_bits:2-14            87.13M ± 0%   87.00M ± 0%       ~ (p=0.300 n=11)
modexp<expmod_execute>/mod_len:1016/exp_bits:2-14           310.5M ± 1%   309.7M ± 0%       ~ (p=0.171 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:2-14           315.2M ± 0%   314.7M ± 0%       ~ (p=0.151 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:256-14         704.2M ± 0%   705.1M ± 0%       ~ (p=0.217 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:2048-14        1.327G ± 0%   1.325G ± 0%       ~ (p=0.519 n=11)
geomean                                                     290.3M        295.5M       +1.79%

@chfast chfast requested review from Copilot and rodiazet and removed request for Copilot January 27, 2026 11:37
@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.97%. Comparing base (10680a8) to head (d87c854).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1427      +/-   ##
==========================================
+ Coverage   81.71%   81.97%   +0.26%     
==========================================
  Files         152      152              
  Lines       13606    13806     +200     
  Branches     3222     3223       +1     
==========================================
+ Hits        11118    11318     +200     
  Misses        343      343              
  Partials     2145     2145              
Flag Coverage Δ
eest-develop 91.09% <77.77%> (-0.04%) ⬇️
eest-develop-gmp 28.69% <3.43%> (-0.41%) ⬇️
eest-legacy 15.08% <0.00%> (-0.23%) ⬇️
eest-legacy-silkpre 24.10% <3.43%> (-0.34%) ⬇️
evmone-unittests 76.66% <100.00%> (+0.38%) ⬆️

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

Components Coverage Δ
core 94.40% <100.00%> (+<0.01%) ⬆️
tooling 83.63% <ø> (ø)
tests 74.31% <100.00%> (+0.66%) ⬆️
Files with missing lines Coverage Δ
lib/evmone_precompiles/modexp.cpp 99.07% <100.00%> (+0.04%) ⬆️
test/unittests/precompiles_expmod_test.cpp 98.17% <100.00%> (+1.88%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chfast chfast force-pushed the crypto/modexp_amm branch from dcf71b3 to 4a4320a Compare January 28, 2026 14:57
Copilot AI review requested due to automatic review settings January 28, 2026 14:57
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the expmod precompile’s modular exponentiation to use Almost Montgomery Multiplication (AMM) for improved performance, and extends unit coverage with additional vectors targeting AMM edge cases.

Changes:

  • Replace classic Montgomery multiplication with AMM in the odd-modulus exponentiation path.
  • Add a final conditional reduction step after converting out of Montgomery form to ensure canonical output.
  • Add extensive unit test vectors intended to exercise AMM-related corner cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
lib/evmone_precompiles/modexp.cpp Switches modexp’s multiplication primitive to AMM and adjusts the final reduction step.
test/unittests/precompiles_expmod_test.cpp Adds new expmod test vectors covering AMM-specific scenarios and boundary conditions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chfast chfast force-pushed the crypto/modexp_amm branch 2 times, most recently from 1de9128 to 37db421 Compare January 29, 2026 10:20
@chfast chfast requested a review from Copilot January 29, 2026 10:20
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// The AMM is relaxed version of the Montgomery multiplication which produces
/// results in the range result < 2*mod, i.e., it may be larger than the modulus.
/// This allows to skip the final conditional subtraction in most cases, improving performance.
/// The inputs are expected to be in the Montgomery form and also in the range < 2*mod.
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The mul_amm doc says inputs are expected to be in Montgomery form, but modexp_odd() calls it with y = 1 to convert from Montgomery form. Please clarify the contract/docstring to mention this non-Montgomery operand use (or adjust wording so it matches actual usage).

Suggested change
/// The inputs are expected to be in the Montgomery form and also in the range < 2*mod.
/// In the typical case, both inputs x and y are expected to be in Montgomery form and also in
/// the range < 2*mod. A notable supported special case is y == 1, which is used to convert
/// a value from Montgomery form back to the standard representation.

Copilot uses AI. Check for mistakes.
@chfast chfast force-pushed the crypto/modexp_amm branch 3 times, most recently from fb715b5 to 1408de1 Compare January 29, 2026 12:48
Use the relaxed version of the Montgomery multiplication called
Almost Montgomery Multiplication from
"Efficient Software Implementations of Modular Exponentiation"
(https://eprint.iacr.org/2011/239.pdf).

```
                                                     │ old              │          new                       │
                                                     │      gas/s       │    gas/s     vs base               │
modexp<expmod_execute>/mod_len:8/exp_bits:33-14             902.5M ± 0%   941.3M ± 0%  +4.29% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:16/exp_bits:33-14            896.6M ± 1%   931.5M ± 0%  +3.88% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:24/exp_bits:33-14            233.0M ± 1%   241.2M ± 0%  +3.53% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:33-14            233.9M ± 1%   242.0M ± 0%  +3.43% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:256-14           245.3M ± 1%   254.7M ± 0%  +3.83% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:32/exp_bits:8192-14          488.2M ± 0%   505.2M ± 0%  +3.48% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:40/exp_bits:11-14            204.1M ± 0%   212.3M ± 1%  +3.99% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:48/exp_bits:8-14             277.2M ± 0%   288.6M ± 0%  +4.14% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:48/exp_bits:256-14           338.4M ± 0%   351.9M ± 0%  +4.01% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:56/exp_bits:6-14             359.1M ± 0%   371.5M ± 0%  +3.46% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:64/exp_bits:5-14             434.1M ± 0%   446.5M ± 0%  +2.87% (p=0.000 n=11)
modexp<expmod_execute>/mod_len:72/exp_bits:4-14             160.5M ± 1%   161.3M ± 0%  +0.48% (p=0.001 n=11)
modexp<expmod_execute>/mod_len:112/exp_bits:4-14            368.1M ± 0%   369.0M ± 0%  +0.24% (p=0.002 n=11)
modexp<expmod_execute>/mod_len:136/exp_bits:3-14            132.1M ± 0%   133.2M ± 1%  +0.79% (p=0.001 n=11)
modexp<expmod_execute>/mod_len:192/exp_bits:2-14            186.7M ± 1%   186.7M ± 2%       ~ (p=0.199 n=11)
modexp<expmod_execute>/mod_len:504/exp_bits:2-14            82.04M ± 0%   81.86M ± 1%  -0.21% (p=0.010 n=11)
modexp<expmod_execute>/mod_len:512/exp_bits:2-14            84.51M ± 1%   84.42M ± 0%       ~ (p=0.270 n=11)
modexp<expmod_execute>/mod_len:512/exp_bits:8192-14         348.0M ± 0%   348.4M ± 0%  +0.12% (p=0.034 n=11)
modexp<expmod_execute>/mod_len:520/exp_bits:2-14            87.13M ± 0%   87.00M ± 0%       ~ (p=0.300 n=11)
modexp<expmod_execute>/mod_len:1016/exp_bits:2-14           310.5M ± 1%   309.7M ± 0%       ~ (p=0.171 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:2-14           315.2M ± 0%   314.7M ± 0%       ~ (p=0.151 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:256-14         704.2M ± 0%   705.1M ± 0%       ~ (p=0.217 n=11)
modexp<expmod_execute>/mod_len:1024/exp_bits:2048-14        1.327G ± 0%   1.325G ± 0%       ~ (p=0.519 n=11)
geomean                                                     290.3M        295.5M       +1.79%
```
@chfast chfast force-pushed the crypto/modexp_amm branch from 1408de1 to d87c854 Compare January 29, 2026 12:59
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