feat(evm_interpreter): add MULMOD fast paths for modulus = 1 and 2^256-1#647
Open
0xVolosnikov wants to merge 1 commit into
Open
feat(evm_interpreter): add MULMOD fast paths for modulus = 1 and 2^256-1#6470xVolosnikov wants to merge 1 commit into
0xVolosnikov wants to merge 1 commit into
Conversation
e06a0bf to
4684546
Compare
There was a problem hiding this comment.
Pull request overview
This PR optimizes the EVM interpreter’s MULMOD handling by adding fast paths for trivial moduli while preserving protocol behavior and gas/native-cost charging.
Changes:
- Adds a
modulus = 1short-circuit returning zero. - Adds a
modulus = U256::MAXreduction helper using the2^256 ≡ 1identity. - Adds unit coverage comparing the new reduction helper against ruint’s
mul_mod.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ly0va
approved these changes
May 21, 2026
MULMOD currently has only one fast path (modulus = 0). Two more cases are
worth special-casing:
- modulus = 1: result is always 0 by EVM spec. Skip the widening_mul and
oracle-backed wide div_rem.
- modulus = 2^256 - 1 (U256::MAX): use the identity
(a*b) mod (2^256 - 1) ≡ (lo + hi) mod (2^256 - 1)
where (lo, hi) is the widening product, since 2^256 ≡ 1 (mod 2^256 - 1).
One carry-aware add reduces, with a final normalization mapping 2^256 - 1
to 0. This skips the oracle-backed wide div_rem path entirely on hit.
The reduction math is extracted into a free `reduce_mod_max` helper and unit
tested against ruint's `mul_mod` reference across edge cases (zero, one,
half_max, U256::MAX, mixed limbs).
Benchmark (bench_scripts/bench.sh compare against custom-u256 baseline):
- block_19299001 process_block: -0.04% effective (-86K cycles)
- block_22244135 process_block: ~0% (flat)
- MULMOD median: 403 -> 373 cycles (-7.4%)
- MULMOD total: -7.1% (-96K raw cycles on block_19299001)
- Bigint delegations: +0.03% (is_one() check adds one delegation per call,
partly offset by MAX hits skipping wide div_rem)
The benchmark blocks don't heavily exercise modulus=2^256-1 (~1% hit rate
on block_19299001). Workloads that use math libraries with mulmod(a, b, ~0)
patterns should see larger gains.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4684546 to
e018ece
Compare
Contributor
Block-level effective cyclesAverage across all block fixtures (
Per-block effective cycles
Block-level sub-phases
Precompiles test-crate bench (synthetic workload, all labels)
FRI precompile bench (FriProofTx + sidecar + contract call)
Per-opcodePer-opcode cycle diff
Per-precompilePer-precompile per-execution ratios (head) |
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.
What ❔
Add two new fast paths to the EVM
MULMODhandler in addition to the existingmodulus = 0short-circuit:modulus = 1— result is always 0 per EVM spec. Skip both the widening mul and the oracle-backed wide div_rem.modulus = 2^256 - 1(U256::MAX) — use the identity(lo, hi)is the widening product, since2^256 ≡ 1 (mod 2^256 - 1). One carry-aware add reduces, with a final normalization step mapping2^256 - 1to 0. Skips the oracle-backed wide div_rem on hit.The reduction math is extracted into a free
reduce_mod_maxhelper and unit-tested against ruint'smul_modreference across edge-case inputs (zero, one, half_max,U256::MAX, mixed-limb patterns).Why ❔
MULMODis one of the more expensive opcodes in the interpreter (~11% of opcode-sampled cycles before this change). The oracle-backedwide_div_remis fast in the average case but disproportionately expensive when the modulus has a trivial structure.modulus = 2^256 - 1is a common pattern in math libraries that implement custom field arithmetic, hashing-style constructions, or RNGs, and there is no reason to pay for a full quotient-remainder verification when a single carry-aware add suffices.Benchmark
bench_scripts/bench.sh compare, baseline =custom-u256at HEAD:block_19299001block_22244135Per-opcode (block_19299001, 3372 MULMOD executions):
is_one()check adds one delegation per non-zero MULMOD call; mostly absorbed by the savings from MAX hits skippingwide_div_rem.The benchmark blocks happen not to exercise
modulus = 2^256 - 1heavily (estimated ~1% hit rate on block_19299001). Workloads that lean onmulmod(a, b, ~0)patterns should see proportionally larger gains.Is this a breaking change?
No protocol-visible behavior change. Gas and native cost charging are unchanged. The fast-path result is provably congruent to the existing
wide_div_remresult, verified by:2^256 ≡ 1 (mod 2^256 - 1)).reduce_mod_max_matches_ruint_referencethat compares against ruint's referencemul_modover 49 (a, b) combinations spanning edge cases.Note:
op3 = 1andop3 = U256::MAXwould also be handled correctly by the existingwide_div_rempath; these are pure optimizations, not correctness fixes.Checklist
evm_interpreter::instructions::arithmetic::testsverifying thereduce_mod_maxhelper against ruint'smul_mod. Verified locally:cargo test -p evm_interpreter --features testing(14/14, including the new test),tests/instances/evmrig (14/14).🤖 Generated with Claude Code