crypto: Reduce the exponent for the power-of-two modulus part - #1662
Merged
Merged
Conversation
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1662 +/- ##
=======================================
Coverage 98.02% 98.02%
=======================================
Files 179 179
Lines 16317 16347 +30
Branches 3760 3765 +5
=======================================
+ Hits 15994 16024 +30
Misses 243 243
Partials 80 80
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
chfast
force-pushed
the
crypto/modexp-pow2-exp
branch
4 times, most recently
from
August 19, 2026 14:19
9a67d2d to
6a870d6
Compare
chfast
force-pushed
the
crypto/modexp-pow2-exp
branch
from
September 7, 2026 13:51
6a870d6 to
e5fcfa3
Compare
There was a problem hiding this comment.
🟢 Approval recommended
The reduction logic is mathematically sound and covered by focused boundary tests.
Pull request overview
Optimizes modular exponentiation for power-of-two modulus factors by safely reducing exponent processing.
Changes:
- Bounds exponentiation work using modulus trailing-zero bits.
- Adds edge-case regression tests.
- Documents missing even-base benchmark coverage.
File summaries
| File | Description |
|---|---|
lib/evmone_precompiles/modexp.cpp |
Implements exponent reduction and early exits. |
test/unittests/precompiles_expmod_test.cpp |
Adds reduction and boundary tests. |
test/precompiles_bench/precompiles_bench.cpp |
Notes benchmark coverage limitation. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
chfast
force-pushed
the
crypto/modexp-pow2-exp
branch
from
September 11, 2026 13:55
e5fcfa3 to
aee0362
Compare
modexp_pow2 iterated over every exponent bit although base^exp % 2^k is periodic in exp. For an odd base the multiplicative order divides λ(2ᵏ) = 2ᵏ⁻², so only that many low bits of the exponent matter. For an even base the result is 0 once exp >= k, which the bit widths of exp and k are enough to decide. Either way the loop is now bounded by k instead of the exponent width: for a modulus with 8 trailing zero bits and an 8192-bit exponent it processes 6 exponent bits instead of 8192. Gives up to ~30% fewer cycles for even moduli with wide exponents.
chfast
force-pushed
the
crypto/modexp-pow2-exp
branch
from
September 11, 2026 14:02
aee0362 to
29d92b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
modexp_pow2 iterated over every exponent bit although base^exp % 2ᵏ is periodic in exp. For an odd base the multiplicative order divides λ(2ᵏ) = 2ᵏ⁻², so only that many low bits of the exponent matter. For an even base the result is 0 once exp >= k, which the bit widths of exp and k are enough to decide. Either way the loop is now bounded by k instead of the exponent width: for a modulus with 8 trailing zero bits and an 8192-bit exponent it processes 6 exponent bits instead of 8192.
Gives up to ~30% fewer cycles for even moduli with wide exponents.