Skip to content

perf(zkevm): reduce MOD by the BN254 top limb in the guest - #183

Merged
svlachakis merged 2 commits into
mainfrom
perf/zkvm-mod-bn254-top-limb
Nov 15, 2026
Merged

svlachakis merged 2 commits into
mainfrom
perf/zkvm-mod-bn254-top-limb

Conversation

@svlachakis

@svlachakis svlachakis commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • The zkEVM build now reduces MOD by divisors with the BN254 top limb (y.u3 == 0x30644E72E131A029) with Remainder256ByTopLimb254, the same as the standard build.
  • fix: keep the MOD division in zkEVM builds and close test gaps from review #177 kept the division in the zkEVM build because the guest-step cost of this kernel was not measured. This PR measures it: the kernel uses about 31% fewer guest steps than the division.
  • Remainder256ByTopLimb254 moves from UInt256.std.cs to UInt256.divmod.cs, with no change, so that both builds compile it.
  • The top-limb test is now in ModFull in UInt256.divmod.cs, once for both builds. The Remainder256ByFullModulus partial and its two bodies are removed.
  • The test compares the y3 local that ModFull already holds. The guest does not load the top limb again.
  • The standard build has the same kernel and the same dispatch. On arm64 the JIT code for ModFull has 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.

Operation main this PR Change
MOD 256-bit by BN254 p 265 184 -30.5%
MOD 256-bit by BN254 r 266 184 -30.8%
SMOD +255-bit by +255-bit 174 175 +1 step (+0.6%)
SMOD +255-bit by -255-bit 205 206 +1 step (+0.5%)
SMOD -255-bit by +255-bit 285 287 +2 steps (+0.7%)
SMOD -255-bit by -255-bit 300 302 +2 steps (+0.7%)
Other MOD, SMOD, DIV, SDIV, MUL, MULMOD, ADDMOD, EXP shapes (102 shapes) no change

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

  • ModTopLimbTests now also checks the kernel in the zkEVM and zkEVM compact test configurations.
  • Local run, Release: std 602,344 passed, zkEVM 602,318 passed, zkEVM compact 602,318 passed, 0 failed.
  • 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 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.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 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

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): 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.

@svlachakis
svlachakis marked this pull request as ready for review November 14, 2026 23:49
@svlachakis
svlachakis merged commit ca0ce63 into main Nov 15, 2026
17 checks passed
@svlachakis
svlachakis deleted the perf/zkvm-mod-bn254-top-limb branch November 15, 2026 10:51
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