Skip to content
Monpediaby Unity Nodes
OpenPull Request

zkVM: optimizing the guest for Zisk — Arithmetic (replaces #2747, #2873, #2874)

by goodlyrottenappleCreated Dec 3, 2026category-labs/monad

Replaces #2747, #2873 and #2874. It carries the same ZisK arithmetic optimizations in one PR, restructured so that category/ has no MONAD_ZKVM_ZISK conditionals and no overrides.hpp.

What changes

Host (category/)

  • The uint256_t operations whose best implementation depends on the platform move from uint256.hpp into uint256/intrinsics.hpp, as functions over the four words: is_nonzero, equal, less, slt, shift_left, shift_right, multiply, udivrem, addmod, mulmod. uint256.hpp calls them. On x86 they are the code that was in uint256.hpp before.
  • The multi-word long division (long_div, knuth_div, generic udivrem) moves to a new uint256/long_division.hpp, parameterised on the word primitives (div, shld, shrd) so each platform's intrinsics.hpp instantiates it with its own. The host uses the x86 ones, as before.
  • The interpreter's ADD and SUB call runtime::add / runtime::sub, two inline functions in runtime/math.hpp that are b = a + b / b = a - b on the host.
  • From #2747, on every target: exp shifts for any one-word power-of-two base, and checked_mul has a fast path for one-word operands.

Guest (zkvm/)

  • zkvm/category/core/runtime/uint256/intrinsics.hpp defines the same ten operations for ZisK: early-exit comparisons, simpler shifts, a one-word multiply (#2747); arith256_mod for addmod/mulmod (#2873); 256-bit division by shift, native divide, or an executor hint verified with arith256 (#2874). The precompile markers live in the same header.
  • A shadow of runtime/math.hpp maps ADD/SUB to add256 and writes ADDMOD/MULMOD results straight into the stack slot. MUL uses arith256 in the existing runtime/math/intrinsics.hpp shadow.
  • The uint256 test from the three PRs, as zkvm/guest/test/uint256_zisk_test.cpp: a host executable for the software paths, and a guest ELF (MONAD_ZKVM_UINT256_SELFTEST=ON, now passed through build-support) for the precompile and hint paths. zkvm/README.md has the commands.

Differences from the three PRs

  • The add256 parameter blocks are function-local statics instead of Context fields, so Context and execute.cpp are unchanged.
  • addmod/mulmod use the inline 0x802 marker rather than syscall_arith256_mod.
  • On the guest, a four-word udivrem on raw word arrays also takes the hint path, not only the uint256_t overload.

Testing

  • Host: full build and ctest pass (6070 tests, GCC 15, Debug).
  • Guest selftest under ziskemu 1.4.0-alpha: all six cases pass, including the three that inject a wrong division hint and expect a halt.
  • 211 mainnet inputs, against a guest built from the parent commit: the 143 the baseline accepts give identical outputs with fewer steps on every block (median −7.8%, aggregate −15.8%). The other 68 exit with code 1 on both builds. These runs were done before the final rebase, which only picked up one unrelated RPC commit.
  • Not done: clang-tidy, and a host benchmark of the moved operations.

🤖 Generated with Claude Code