crypto: Use Almost Montgomery Multiplication in modexp#1427
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1427 +/- ##
==========================================
+ Coverage 81.70% 83.03% +1.32%
==========================================
Files 152 152
Lines 13602 13802 +200
Branches 3222 3223 +1
==========================================
+ Hits 11114 11460 +346
+ Misses 343 197 -146
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.
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.
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.
be90520 to
2cfa624
Compare
|
Newer benchmark set: |
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% ```
2cfa624 to
4f0666d
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).