Repository navigation
perf(zkevm): reduce MULMOD by well-known moduli in the guest - #184
svlachakis wants to merge 1 commit into
Conversation
LukaszRozmej
left a comment
There was a problem hiding this comment.
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.cssrc/Nethermind.Int256/UInt256.std.cssrc/Nethermind.Int256/UInt256.zkevm.cs
🤖 AI agent (Codex / GPT-6) on behalf of @LukaszRozmej.
LukaszRozmej
left a comment
There was a problem hiding this comment.
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
|
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). |
|
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. |
Changes
Remainder512ByKnownModulusmoves fromUInt256.std.cstoUInt256.barrett.cs, with no change, so that both builds compile it.UInt256.zkevm.cs:Remainder512ByFullModulussends products with a non-zero upper half toRemainder512ByKnownModuluswhenm.u3is the top limb of a known modulus, and divides in all other cases. This uses aswitchonm.u3; the standard build is unchanged.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.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 (variantsmulk,mulk2,mulk3againstbase= b2ec5bb; this PR ismulk3): https://github.com/NethermindEth/int256/actions/runs/43607662724Nethermind 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'simpl/zkevmassembly: 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.This PR gives no gain on these blocks. The EVM
MULMODof 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 toMULMODby a listed modulus on the OpenVM guest and to other callers ofMultiplyMod.Tests
MultiplyModBarrettTests,MultiplyModPseudoMersenneTestsandMultiplyModShortBarrettTestsnow also check these kernels in the zkEVM and zkEVM compact test configurations.