zkVM: optimizing the guest for Zisk — Arithmetic (replaces #2747, #2873, #2874)
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_toperations whose best implementation depends on the platform move fromuint256.hppintouint256/intrinsics.hpp, as functions over the four words:is_nonzero,equal,less,slt,shift_left,shift_right,multiply,udivrem,addmod,mulmod.uint256.hppcalls them. On x86 they are the code that was inuint256.hppbefore. - The multi-word long division (
long_div,knuth_div, genericudivrem) moves to a newuint256/long_division.hpp, parameterised on the word primitives (div,shld,shrd) so each platform'sintrinsics.hppinstantiates 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 inruntime/math.hppthat areb = a + b/b = a - bon the host. - From #2747, on every target:
expshifts for any one-word power-of-two base, andchecked_mulhas a fast path for one-word operands.
Guest (zkvm/)
zkvm/category/core/runtime/uint256/intrinsics.hppdefines the same ten operations for ZisK: early-exit comparisons, simpler shifts, a one-word multiply (#2747);arith256_modforaddmod/mulmod(#2873); 256-bit division by shift, native divide, or an executor hint verified witharith256(#2874). The precompile markers live in the same header.- A shadow of
runtime/math.hppmaps ADD/SUB toadd256and writes ADDMOD/MULMOD results straight into the stack slot. MUL usesarith256in the existingruntime/math/intrinsics.hppshadow. - 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 throughbuild-support) for the precompile and hint paths.zkvm/README.mdhas the commands.
Differences from the three PRs
- The
add256parameter blocks are function-local statics instead ofContextfields, soContextandexecute.cppare unchanged. addmod/mulmoduse the inline0x802marker rather thansyscall_arith256_mod.- On the guest, a four-word
udivremon raw word arrays also takes the hint path, not only theuint256_toverload.
Testing
- Host: full build and
ctestpass (6070 tests, GCC 15, Debug). - Guest selftest under
ziskemu1.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