-
Notifications
You must be signed in to change notification settings - Fork 332
crypto: Use Almost Montgomery Multiplication in modexp #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
dcf71b3 to
4a4320a
Compare
There was a problem hiding this 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.
1de9128 to
37db421
Compare
There was a problem hiding this 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.
lib/evmone_precompiles/modexp.cpp
Outdated
| /// 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. |
Copilot
AI
Jan 29, 2026
There was a problem hiding this comment.
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).
| /// 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. |
fb715b5 to
1408de1
Compare
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% ```
1408de1 to
d87c854
Compare
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).