Repository navigation
perf(zkevm): reduce MOD by the BN254 top limb in the guest - #183
Conversation
LukaszRozmej
left a comment
There was a problem hiding this comment.
Reviewed commit 5fffeb1ac353169b08027fd492a78b5dd813182b.
Reviewed all three files. The specialized kernel moves unchanged into shared code; the guest dispatch tests the same top-limb condition as the standard build and retains general division otherwise. No correctness findings.
Optimization pass: sharing the existing kernel avoids duplicate implementations and preserves the standard build. The documented tradeoff is an extra compare on nonmatching guest divisors. No further actionable opportunity found; guest-step measurements are from the PR, not independently rerun here.
Validation: ModTopLimbTests passed 16/16 with EnableZkEvm=true, then 16/16 with EnableZkEvm=true and Int256ZkEvmCompact=true, on Windows x64. This exercises boundary/carry/alias cases against BigInteger, not RISC-V execution.
Reviewed files (3); none skipped
src/Nethermind.Int256/UInt256.divmod.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 kernel and the dispatch are exact copies of the standard build, and the existing ModTopLimbTests now run them in both zkEVM test configurations.
I ran ModTopLimbTests and a 400,000-case random differential test against BigInteger in the zkEVM and zkEVM compact builds. The cases include moduli with the BN254 top limb and random lower limbs, x = 2^256 - 1, x near multiples of m and x < m. All pass.
The guest-step gain is measured: -30% for BN254 p and r, +1 to +2 steps on some SMOD shapes.
🤖 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): SP1 -0.025%, OpenVM -0.032% (cost -0.018%), Airbender -0.057%, ZisK unchanged (its MOD uses the ZisK accelerator). No block is slower on any guest. |
Changes
y.u3 == 0x30644E72E131A029) withRemainder256ByTopLimb254, the same as the standard build.Remainder256ByTopLimb254moves fromUInt256.std.cstoUInt256.divmod.cs, with no change, so that both builds compile it.ModFullinUInt256.divmod.cs, once for both builds. TheRemainder256ByFullModuluspartial and its two bodies are removed.y3local thatModFullalready holds. The guest does not load the top limb again.ModFullhas the same instructions as main; only the register names change.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.Cost: a divisor from 2^192 to 2^255 that does not have the BN254 top limb pays one compare: 3 RISC-V instructions (two load the constant, one compares). The SMOD rows show this cost. The first version of this PR also loaded the top limb again (+1.3 to +2.7 steps).
Runs: https://github.com/NethermindEth/int256/actions/runs/43653022372 (variant
f183= this PR,pr183= the first version,base= b2ec5bb), and https://github.com/NethermindEth/int256/actions/runs/43623819309 for the compare variants.Native
The standard build only changes where the compare is written. The arm64 JIT code for
ModFull(Release, full opts) has the same 240 lines as main. The only differences are register names and call addresses. The local arm64 timing runs had too much noise from other jobs on the machine to show a difference in either direction, so they are not shown.Tests
ModTopLimbTestsnow also checks the kernel in the zkEVM and zkEVM compact test configurations.