Skip to content

perf(zkevm): reduce MULMOD by well-known moduli in the guest - #184

Closed
svlachakis wants to merge 1 commit into
mainfrom
perf/zkvm-mulmod-known-moduli
Closed

svlachakis wants to merge 1 commit into
mainfrom
perf/zkvm-mulmod-known-moduli

Conversation

@svlachakis

@svlachakis svlachakis commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • The zkEVM build now reduces MULMOD by the well-known moduli (BN254 r and p, secp256k1 n and p, P-256 n and p, and the prime 2^251 + 17 * 2^192 + 1) with the Barrett and fold kernels from perf: reduce full-width MULMOD by well-known field moduli with Barrett reduction #152 and perf: reduce MULMOD by BN254 and special-form moduli with fewer products #175. Before this PR, only the standard build used them.
  • Remainder512ByKnownModulus moves from UInt256.std.cs to UInt256.barrett.cs, with no change, so that both builds compile it.
  • UInt256.zkevm.cs: Remainder512ByFullModulus sends products with a non-zero upper half to Remainder512ByKnownModulus when m.u3 is the top limb of a known modulus, and divides in all other cases. This uses a switch on m.u3; the standard build is unchanged.
  • Two comments that said the zkEVM build divides are corrected or removed.

Guest steps

Zisk guest steps (ziskemu -X), for one call, from a small guest that calls the operation once per run. The emulator is deterministic, so one run is the measurement. Three random operand sets per shape; the values are means. Every output is checked against Python. "full" means two random 256-bit operands; "reduced" means operands below m.

MULMOD modulus Operands main this PR Change
secp256k1 p full 638 390 -38.9%
secp256k1 p reduced 624 390 -37.5%
2^251 + 17 * 2^192 + 1 full 693 565 -18.5%
2^251 + 17 * 2^192 + 1 reduced 637 501 -21.4%
BN254 r full 727 636 -12.5%
BN254 r reduced 651 567 -12.9%
BN254 p full 712 648 -9.0%
BN254 p reduced 674 562 -16.6%
secp256k1 n full 628 540 -14.0%
secp256k1 n reduced 614 567 -7.7%
P-256 p full 588 567 -3.6%
P-256 p reduced 587 567 -3.4%
P-256 n full 616 593 -3.7%
P-256 n reduced 598 603 +0.8%
random 256-bit full 615 629 +2.3%
random 256-bit reduced 598 612 +2.3%
2^255 - 19 full 717 731 +2.0%
2^255 - 19 reduced 656 670 +2.1%
64-, 128-, 192-bit moduli no change

Cost: a full-width modulus that is not in the list pays 14 more steps per call. Two other dispatch forms cost the same or more: a call for every product with a non-zero upper half (+21 steps) and the || chain of the standard build (+19 steps). Run (variants mulk, mulk2, mulk3 against base = b2ec5bb; this PR is mulk3): https://github.com/NethermindEth/int256/actions/runs/43607662724

Nethermind guests (all zkVMs)

The Nethermind stateless guest on all four zkVMs, the 11 blocks in Nethermind.Stateless.ZiskGuest/inputs.json, GitHub Actions run https://github.com/NethermindEth/nethermind/actions/runs/43649459039. Nethermind master 0d776d3b096. Both arms build the zkEVM variant of this repository (Nethermind.Int256.ZkEvm.csproj) and put it in place of the package's impl/zkevm assembly: base = b2ec5bb (the base of this PR), and this PR. The b2ec5bb build gives the same counts as the 1.14.0 package on every guest. One run per arm (all four zkVMs are deterministic). Every arm gives the expected output on all 11 blocks.

Guest Metric base this PR Change Blocks lower / higher
ZisK steps 1,484,199,803 1,484,199,803 0 (cost 0) 0 / 0
SP1 cycles 2,686,047,615 2,686,047,615 0 0 / 0
OpenVM instret 2,053,193,299 2,053,198,004 +0.0002% (cost +0.0001%) 1 / 4
Airbender cycles 4,376,931,994 4,376,931,994 0 0 / 0

This PR gives no gain on these blocks. The EVM MULMOD of ZisK, SP1 and Airbender uses each zkVM's own accelerator, not this kernel. Only OpenVM reaches it, and on these blocks its moduli are not in the list: four pre-Cancun blocks pay about 1,250 more instructions each (the extra dispatch, +0.0007% per block), and one block is 271 lower. The gain in the table above applies to MULMOD by a listed modulus on the OpenVM guest and to other callers of MultiplyMod.

Tests

  • The existing MultiplyModBarrettTests, MultiplyModPseudoMersenneTests and MultiplyModShortBarrettTests now also check these kernels in the zkEVM and zkEVM compact test configurations.
  • CI runs all test configurations.

@LukaszRozmej LukaszRozmej left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 63d261d6a160034a6d5034fc6d8e244ae0527aa6.

Reviewed all three files, including the unchanged relocated known-modulus dispatcher, exact-modulus checks and generic fallback. No correctness findings.

Optimization pass: the switch trades extra work on unmatched full-width moduli for cheaper common-modulus reductions, as quantified in the PR. The move avoids duplicate kernels. No additional optimization justified without guest workload measurements; I did not rerun the guest-step benchmark.

Validation: MultiplyModBarrettTests, MultiplyModPseudoMersenneTests and MultiplyModShortBarrettTests passed 214/214 with EnableZkEvm=true, and another 214/214 with Int256ZkEvmCompact=true. Windows x64 host execution; no RISC-V guest execution.

Reviewed files (3); none skipped
  • src/Nethermind.Int256/UInt256.barrett.cs
  • src/Nethermind.Int256/UInt256.std.cs
  • src/Nethermind.Int256/UInt256.zkevm.cs

🤖 AI agent (Codex / GPT-6) on behalf of @LukaszRozmej.

@LukaszRozmej LukaszRozmej left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issue. The kernels are moved without change, and the zkEVM dispatch selects the same moduli as the standard build. The existing Barrett, pseudo-Mersenne and short-Barrett tests now run them in both zkEVM test configurations.
I ran the MultiplyMod* tests and a 400,000-case random differential test against BigInteger in the zkEVM and zkEVM compact builds. The cases include the known moduli, moduli with a known top limb but other lower limbs, products with a zero and a non-zero upper half and operands of 2^256 - 1. All pass.
The gain is measured, and so is the cost: +14 steps (about +2%) for a full-width modulus that is not in the list.
This PR and #183 change adjacent lines in UInt256.zkevm.cs and UInt256.std.cs. Expect a rebase after the first one merges.

🤖 AI agent (Claude Code / Opus 5.5) on behalf of @LukaszRozmej

Comment thread src/Nethermind.Int256/UInt256.zkevm.cs Resolved
@svlachakis

Copy link
Copy Markdown
Contributor Author

All-guest measurement added to the description (Nethermind guest, run 43649459039, 11 blocks, identical outputs): no gain on these blocks. ZisK, SP1 and Airbender unchanged (their MULMOD uses their accelerators). OpenVM +0.0002% instret (+4,705 total; the dispatch cost on unlisted moduli in four pre-Cancun blocks).

@svlachakis

Copy link
Copy Markdown
Contributor Author

Closing. A guest measurement on the 11 stateless blocks (run https://github.com/NethermindEth/nethermind/actions/runs/43649459039) shows no gain on real blocks: ZisK 0, SP1 0 and Airbender 0 (their MULMOD uses accelerators), and OpenVM +0.0002% instructions (+4,705 in total; four pre-Cancun blocks pay about 1,250 more for the dispatch on other moduli). #183 stays: it is faster on SP1, OpenVM and Airbender, and no block is slower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants