crypto: Use fixed-window exponentiation in modexp - #1618
Open
AskAlexSharov wants to merge 1 commit into
Open
Conversation
AskAlexSharov
force-pushed
the
alex/modexp-windowing
branch
2 times, most recently
from
August 1, 2026 13:00
7959fbf to
ef07630
Compare
modexp_odd used binary square-and-multiply: one Montgomery multiply per set exponent bit. For large exponents that is roughly twice the multiplies a windowed method needs. Precompute a small table of base powers (b^1 .. b^(2^w - 1) in Montgomery form) and consume w exponent bits per multiply. The window width scales with the exponent size (w = 1..4) so the table cost stays amortized even for a sparse exponent, and small exponents keep the plain binary path (w = 1). With w = 1 the loop is identical to the previous binary square-and-multiply. Measured ~1.5-1.6x on large-exponent modexp (256-bit modulus, 256-bit exponent: 19.6us -> 12.9us; 4096-bit modulus, 8192-bit exponent: 96.5ms -> 60.6ms on an AMD EPYC 4344P); smaller exponents also improve and none regress. The power table adds MODEXP_TABLE_MAX*n words to the stack scratch buffer. Add expmod.windowing_vs_gmp: a differential test against GMP over many exponent bit-lengths and patterns, for odd and even moduli.
AskAlexSharov
force-pushed
the
alex/modexp-windowing
branch
from
August 1, 2026 13:13
ef07630 to
006ba7d
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.
Summary
modexp_odduses binary square-and-multiply — one Montgomery multiply per set exponent bit. For large exponents that is roughly twice the multiplies a windowed method needs.This precomputes a small table of base powers (
b^1 .. b^(2^w - 1)in Montgomery form) and consumeswexponent bits per multiply. The window width scales with the exponent size so the table cost stays amortized even for a sparse exponent:w = 1(plain binary, no table) for exponents ≤ 16 bits;w = 2up to 48 bits,w = 3up to 144 bits,w = 4above.With
w = 1the loop is byte-for-byte the previous binary square-and-multiply.Benchmarks
evmone-precompiles-bench --benchmark_filter='modexp<expmod_execute_evmone>', AMD EPYC 4344P, gcc 15.2.0, Release:Small exponents (≤ 16 bits) are unchanged; none regress.
Cost
The power table adds
MODEXP_TABLE_MAX * nwords to the stack scratch buffer (STACK_CAPACITYand themodexp_oddscratch requirement are updated). At the EIP-7823 limit (n = 128words) that is ~15 KB of additional stack in the single modexp frame.The window widths and thresholds are simple, conservative choices; happy to tune them or switch to a sliding window (odd-power table, ~half the entries) if preferred.
Correctness
Adds
expmod.windowing_vs_gmp: a differential test comparing evmone against GMP across many exponent bit-lengths (crossing the window-width thresholds) and bit patterns (all window values), for odd and even moduli. Existingexpmodvectors andlarge_inputscontinue to pass.