Skip to content

feat(avm): AVM gas schedule from ZisK proving cost (proposal) - #25924

Closed
zac-williamson wants to merge 3 commits into
zkvm/avm-blake3-h4from
zkvm/avm-gas-zisk
Closed

zac-williamson wants to merge 3 commits into
zkvm/avm-blake3-h4from
zkvm/avm-gas-zisk

Conversation

@zac-williamson

Copy link
Copy Markdown
Contributor

Requested by Zac · project thread

Stacked on #25923 (zkvm/avm-blake3-h4). This is a draft proposal and is not for auto-merge. It changes the AVM's L2 gas prices protocol-wide.

Why

Turbo proves public execution inside its ZisK epoch program, so L2 gas should track ZisK proving steps. If an opcode is underpriced, a tx can use up more of a checkpoint's ~72 s proving budget per unit of gas than intended. That is a denial-of-service risk for the prover.

Method (all counts measured with ziskemu, ZisK 1.4.0-alpha)

  • Each opcode's cost in steps is the slope over loop counts, run through Turbo's Rust avm-simulator in a ZisK guest with precompiles on, at its worst variant.
  • Tree opcodes add the cost of the checked pq1-fr tree per access: 64 accesses into 2^16 leaves.
  • New gas = round(steps × 0.052504), which keeps the old schedule's median price per step.
  • Unchanged: DA gas, addressing gas, RETURN and REVERT, the storage premium (1,024) and the L2→L1 message charge (5,200).

Biggest changes

Opcode Old gas New gas Note
POSEIDON2 360 593 11.3k steps, no precompile
TORADIXBE (per limb) 3 11
EMITPUBLICLOG 15 31
KECCAKF1600 58176 78
BLAKE3COMPRESSION 14123 66
SHA256COMPRESSION 12288 59
SSTORE 33140 3147
EMITNULLIFIER 30800 3059
EMITNOTEHASH 19275 1405
CALL 9936 9936 its worst case, a cold call to the largest bytecode, is 189k steps

Epoch-72's public txs: a transfer goes from 79,279 to 19,189 L2 gas, and the largest claim from 403,730 to 300,280.

Open questions for review

  • L2 gas on state writes (SSTORE and the EMIT* opcodes) may be meant to also price node storage and state growth, not only proving. If so, the 10× cuts there should not land as they are. Only the raises (POSEIDON2, TORADIXBE, EMITPUBLICLOG and the basic ALU ops) are clearly safe.
  • Decoding an instruction the first time it runs costs about 270–450 steps and isn't charged. One option is about 7 gas per instruction for decode; another is making decode cheaper.
  • Moving pq1-fr's tree node hash onto ZisK's BLAKE3 precompile costs 57 steps instead of about 1,035. The tree opcodes would need repricing after that.

Checks

  • avm-simulator: 66/66, with and without pq1fr.
  • aztec-epoch, avm-guest, aztec-core and aztec-crypto: 148 tests pass.
  • gen_constants.sh --check, fmt and clippy -D warnings are clean.

Not done:

  • The C++ test numbers were updated by arithmetic only; bb was not rebuilt.
  • The TS/labs constants were not regenerated.
  • Epoch-72's recorded input no longer replays: the gas change alters the block 32 header.

Generated by Claude Code

Turbo proves public execution inside its ZisK epoch program, so AVM gas should track ZisK steps.
Each opcode's L2 gas is now round(steps x 0.05254), where steps is the ziskemu (ZisK 1.4.0-alpha)
step count of one instance of the opcode run by aztec-rs's avm-simulator in a ZisK guest (zisk
feature, precompiles on, pq1fr), measured as the slope over loop counts at the opcode's worst
measured input, and 0.05254 is the old schedule's median gas per step over the 45 measured opcodes.

- Tree opcodes add the checked trees' cost per access (aztec-core trees, pq1-fr nodes; 64 random
  accesses into 2^16-leaf trees, ~1,300 steps per pq1-fr node hash as shipped).
- CALL/STATICCALL are priced at a cold call to a new contract with the largest bytecode: 189 k
  steps, which gives 9936 again.
- SSTORE, EMITNOTEHASH and EMITNULLIFIER keep L2_GAS_DISTRIBUTED_STORAGE_PREMIUM on top, and
  SENDL2TOL1MSG keeps L2_GAS_PER_L2_TO_L1_MSG. DA gas, addressing gas, RETURN and REVERT are
  unchanged.
- Underpriced before (steps/gas over 2x the median of 19): TORADIXBE's per-limb gas (71 steps/gas;
  3 -> 11) and EMITPUBLICLOG's base (39; 15 -> 31). Largest cuts: KECCAKF1600 58176 -> 78,
  BLAKE3COMPRESSION 14123 -> 66, SHA256COMPRESSION 12288 -> 59, SSTORE 33140 -> 3148,
  EMITNULLIFIER 30800 -> 3061, EMITNOTEHASH 19275 -> 1405. POSEIDON2 360 -> 593.

Epoch-72's public txs (recomputed from per-opcode charges, which reproduce the recorded gas):
transfer_in_public 79,279 -> 19,200 public L2 gas, the 12,940-instruction claim 403,730 ->
300,290. The recorded epoch-72 no longer replays under the new schedule (fees and roots in its
headers follow the old gas); its README says so. H4 fixture: 25,433 -> 9,710 with the opcode,
422,628 -> 394,001 in pure Noir (Rust test measured; the C++ test's numbers by arithmetic from
the per-opcode counts, bb not built).
…orks' byte-wise reduction

Fr::from_be_bytes_reduce goes through arkworks' from_be_bytes_mod_order, about 2,000 ZisK steps
per SETFF decoded. Straight-line SETFF code cost 2,313 steps per instruction (first decode), 210
steps per gas at the new SET gas of 11, against a median of 19. Fr::from_hash gives the same
value (a 32-byte big-endian value reduced mod p, at most five subtractions) and brings it to 666
steps. Decode is cached per batch, so loops are unaffected (measured equal within 23 steps).

The decode test now compares both reductions on p - 1, p, p + 1, 5p, 2^256 - 1, 0 and 64
pseudo-random values.
Same method; the build with the SETFF decode change moves a few step counts by up to 150, so
the median is 0.05250 gas per step (was 0.05254) and five constants move by 1 or 2 gas:
GETENVVAR 12 -> 11, SLOAD 941 -> 940, SSTORE 3148 -> 3147, EMITNULLIFIER 3061 -> 3059,
GETCONTRACTINSTANCE 3136 -> 3137. The fixture gas (9,710 / 394,001) is unchanged.
@zac-williamson zac-williamson self-assigned this Dec 5, 2026
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