Skip to content

feat(avm): BLAKE3COMPRESSION opcode for pq1-fr hashes in public code - #25923

Closed
zac-williamson wants to merge 7 commits into
zkvm/hash-pq1fr-rsfrom
zkvm/avm-blake3-h4
Closed

zac-williamson wants to merge 7 commits into
zkvm/hash-pq1fr-rsfrom
zkvm/avm-blake3-h4

Conversation

@zac-williamson

@zac-williamson zac-williamson commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Zac · project thread

Stacked on zkvm/hash-pq1fr-rs (pq1-fr BLAKE3 protocol hashes in aztec-rs).

What

A BLAKE3COMPRESSION AVM opcode, so public code computes aztec-nr's pq1-fr hashes (keyed BLAKE3 truncated to 253 bits) at one instruction per 64-byte block. Private code (leanVM) and public code must agree on map slots and note hashes (partial notes, nullifiers checked in public).

  • Wire byte 68, appended after TORADIXBE, so no other opcode moves. Layout [Indirect8, out, cv, block, params], all u32-tagged; params = [counter_lo, counter_hi, block_len, flags]; output = the 8-word chaining value. Failure order and halts are the same as SHA256COMPRESSION.
  • Gas AVM_BLAKE3COMPRESSION_BASE_L2_GAS = 5440 (SHA256COMPRESSION × 0.443, from callgrind: 1,800 vs 4,067 x86 instructions per compression). Added to constants.nr and regenerated.
  • avm-transpiler: the Noir foreign call aztec_blake3Compression(cv: [u32; 8], block: [u32; 16], params: [u32; 4]) -> [u32; 8] becomes the opcode.
  • Turbo's Rust simulator (aztec-rs/crates/avm-simulator): the handler; aztec_crypto::blake3_compression uses ZisK's BLAKE3F precompile in guests.
  • C++ vm2 simulation: opcode, wire format, spec, gas, execution (bb's BLAKE3 rounds). There is no circuit work, because Turbo removed the AVM circuit.

Why

Measured with nargo 1.0.0-rc.5 → avm-transpiler → Rust simulator, for one pq1-fr hash of two Fields:

AVM instructions L2 gas
pure-Noir BLAKE3 (no opcode) 18,341 422,635
opcode + u128 field_words in aztec-nr 430 16,757

Without the opcode a public transfer_in_public costs 55–65× more ZisK steps (about 20M vs 0.37M today), and a 64-input hash exceeds the 6M AVM gas limit.

Tests

  • cargo test -p avm-simulator: 66 passed, both with and without --features pq1fr. New tests:
    • the opcode against the official blake3 crate (random keyed chunks);
    • a hand-written bytecode pq1-fr hash against vectors.json;
    • the transpiled Noir fixture against vectors.json;
    • exact gas;
    • refusal of wrong tags and of out-of-range addresses.
  • avm-transpiler cargo test: 6 passed, 4 of them new.
  • barretenberg vm2_sim_tests (clang20-assert preset): 420 tests, 419 passed, 1 skipped (the opcode-spam benchmark, which runs only when an environment variable is set). The C++ needed no fixes. b1d104f adds:
    • 7 BLAKE3COMPRESSION execution tests (the vector, wrong tags, out-of-range addresses);
    • 2 tests that run the Rust fixture's exact bytecode through the fast and hint-collection paths;
    • a BLAKE3COMPRESSION entry in the opcode-spam benchmark.
  • C++/Rust parity on the fixture: both execute 430 instructions with the opcode and 18,341 without it, and the L2 gas is identical once the Rust test's starting gas is accounted for (it is 7 higher).
  • Not built: the WASM build of bb-avm-sim.

Companion changes (outside this repo)

  • aztec-nr pq1.nr: the oracle, compress calling it, and a u128 field_words.
  • PXE oracle handler in acir_callback.ts.
  • A noir2lean mock oracle.

Patches are kept with the project's integration notes.

https://claude.ai/code/session_01Q1AhUehv3mVEnJr82ZeNes


Generated by Claude Code

…(PLAN H4a)

New AVM opcode BLAKE3COMPRESSION, wire byte 68 (appended; no opcode moves),
operands [Indirect8, output U16, cv U16, block U16, params U16], all inputs
u32-tagged: cv [u32; 8], block [u32; 16], params [counter_lo, counter_hi,
block_len, flags]; writes the 8-word chaining value v[i] ^ v[i + 8]. Range and
tag failures halt exceptionally in the same order as SHA256COMPRESSION.
Gas AVM_BLAKE3COMPRESSION_BASE_L2_GAS = 5440 (SHA256COMPRESSION's 12288 times
the measured cost ratio 1800 / 4067 x86 instructions per compression).

- avm-transpiler: ForeignCall aztec_blake3Compression -> BLAKE3COMPRESSION,
  with input/output shape asserts and unit tests.
- aztec-rs: aztec_crypto::blake3_compression (ZisK BLAKE3F precompile in
  zisk guests), the simulator opcode, tests against the official blake3 crate,
  negatives (tags, out-of-range, gas), and an end-to-end test that runs
  transpiled Noir (aztec-nr's pq1-fr hint) on the simulator and matches
  vectors.json's NOTE_HASH_NODE.
- barretenberg vm2 (C++ fast simulator): opcode enum, wire format, spec, gas
  and Execution::blake3_compression over bb's BLAKE3 rounds; compression unit
  test. The full bb build was not run here.
- constants.nr: AVM_BLAKE3COMPRESSION_BASE_L2_GAS; generated.rs regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…f 3,130 AVM instructions per one-block pq1-fr hash)
@zac-williamson zac-williamson self-assigned this Dec 5, 2026
- Execution::blake3_compression on a real memory: the pq1-fr NOTE_HASH_NODE
  vector, output overlapping the chaining value, wrong-tag negatives for cv,
  block and params, out-of-range negatives for each operand range (memory is
  not touched), and the highest in-range output address.
- The transpiled pq1-fr NOTE_HASH_NODE contract (the same bytes as aztec-rs's
  testdata/h4_pq1fr_note_hash_node.avm) through PublicTxSimulationTester: both
  modes succeed with the expected hash, a wrong expectation reverts, hint
  collection agrees with fast simulation, and instruction counts and public
  L2 gas (430 / 16,750 with the opcode, 18,341 / 422,628 in pure Noir) equal
  the Rust simulator's.
- BLAKE3COMPRESSION entry in the opcode spam benchmark.

Copy link
Copy Markdown
Contributor Author

The ci/x-fast failure is not caused by this PR. Its base branch, zkvm/hash-pq1fr-rs, already fails:

  • cargo fmt --check in aztec-rs. It fails at the base commit 92fcd83 with 1 hunk in crates/zisk-pilfflonk-verifier/src/fixed/lines.rs and 26 hunks in crates/zisk-pilfflonk-verifier/src/generated.rs, under the pinned rustfmt 1.89. This PR does not touch either file. None of the base branch's last 40 commits has had a CI run, so this run is the first to reach the check.
  • The next step also fails on the base. cargo clippy --all-targets -Dwarnings gives 15 errors in crates/zisk-stark-verifier, which this PR also does not touch.

This PR's own checks pass locally:

  • clippy -Dwarnings on aztec-crypto, avm-simulator and aztec-core;
  • scripts/gen_constants.sh --check;
  • avm-transpiler fmt and clippy;
  • clang-format-20 --Werror on the 13 changed vm2 files;
  • vm2_sim_tests: 419 of 420 pass, 1 skipped by an environment variable.

I couldn't read the CI log itself (ci.aztec-labs.com needs credentials), so a later failure can't be ruled out. I did not port the fixes into this PR. They belong in the Turbo crates (zisk-pilfflonk-verifier, zisk-stark-verifier), and the verifier changes deserve their own review.


Generated by Claude Code

@zac-williamson

Copy link
Copy Markdown
Contributor Author

Merge desk: left open. The review is APPROVE with conditions.

An independent reviewer looked at head b1d104f against base zkvm/hash-pq1fr-rs and found no soundness or security bugs.

  • The Rust simulator, the C++ vm2 simulator and the ZisK path all perform the exact BLAKE3 compress, returning the first 8 words of v[i]^v[i+8].
  • Both simulators use the same memory and tag order. Output aliasing its inputs is safe.
  • The opcode is appended last on the wire (byte 68), so no other opcode shifts.
  • The transpiler refuses bad shapes.
  • The flat 5440 gas keeps a DoS bounded.

Tests run:

  • avm-simulator: 66/66, with and without pq1fr.
  • aztec-crypto blake3: 2/2.
  • avm-transpiler: 6/6.
  • A differential test against a Python compress written from the spec:
    • Rust matched 20,000/20,000 random cases, including block_len > 64 and high flags.
    • The standalone C++ function matched 20,000/20,000.
    • The opcode end to end matched 300 cases × 5 aliasing and indirect variants.

Not verified: the C++ vm2 test suite (no bb build), a ZisK guest run, and regenerating the .avm fixture.

Conditions before merge:

  1. Run the fixture once in the ZisK guest (syscall_blake3f path) and diff it against the software path.
  2. Recalibrate gas against ZisK step and precompile cost. The current figure comes from x86 instruction counts.

🤖 Generated with Claude Code

AVM_BLAKE3COMPRESSION_BASE_L2_GAS is now SHA256COMPRESSION's 12288 times
the ZisK step ratio of the two opcodes, 1268 / 1103.25 = 1.149, in place
of the x86 instruction ratio 0.443. Steps are ziskemu (ZisK 1.4.0-alpha)
marginal steps per opcode when aztec-rs's avm-simulator executes it in a
ZisK guest, each compression on its precompile (BLAKE3F, SHA256F); the
per-cell tagged memory traffic of the opcode, not the compression,
dominates both. ZisK's cost model (ziskemu -X) gives a similar ratio,
1.081.

Reference, in the same guest (steps per opcode in the simulator):
BLAKE3COMPRESSION 1268 on BLAKE3F / 2437 portable; SHA256COMPRESSION
1103 on SHA256F / 4469 portable (aztec_crypto::sha256_compression has
no precompile path today, so 4469 is what Turbo runs); KECCAKF1600 1499
/ 5702; POSEIDON2PERM 11284 (ARITH256_MOD). The other opcodes' gas is
not ZisK-calibrated (POSEIDON2PERM 360 gas for 10x SHA's steps); only
this opcode is set here.

The H4 fixture (pq1-fr NOTE_HASH_NODE, one compression) now costs 25,433
public L2 gas with the opcode (was 16,750); pure Noir is unchanged at
422,628. The Rust fixture test now asserts both, as the C++ test does.
The C++ test's expectation is updated by arithmetic; bb was not built.

Copy link
Copy Markdown
Contributor Author

Both conditions from the review above are addressed in a47f168.

1. The fixture in a ZisK guest (ziskemu, ZisK 1.4.0-alpha). These four builds give identical results:

  • the syscall_blake3f precompile path
  • a forced portable path
  • a guest built without the zisk feature
  • native x86

Results:

  • The fixture runs 430 instructions with the opcode and 18,341 in pure Noir, and a wrong expected value reverts in both modes.
  • Fixture steps with the opcode: 132,619 on the precompile, 133,788 portable. In pure Noir: 5,327,631.
  • 2,200 random compressions match bit for bit across precompile, portable and native, with 0 mismatches. Of these, 1,000 are checked against a spec reference, 1,000 against the blake3 crate and 200 through the AVM opcode. They include full-range u64 counters, block_len above 64 and high flag bits.

2. Gas recalibrated on ZisK steps per opcode through the simulator:

Opcode Precompile Portable
BLAKE3COMPRESSION 1,268 2,437
SHA256COMPRESSION 1,103 4,469
KECCAKF1600 1,499 5,702
POSEIDON2PERM 11,284 —
  • The new gas is AVM_BLAKE3COMPRESSION_BASE_L2_GAS = 12288 × 1268 / 1103 = 14123, up from 5440. ZisK's own cost model (ziskemu -X) gives a 1.08 ratio, which would be about 13,287.
  • The Rust tests now assert 14123, and the fixture test asserts 25,433 gas with the opcode and 422,628 in pure Noir. avm-simulator: 66 tests pass both with and without pq1fr. aztec-crypto passes, and gen_constants.sh --check, fmt and clippy are clean.
  • The C++ test expectation was moved from 16,750 to 25,433 by arithmetic only; barretenberg was not rebuilt for this commit.

Notes for follow-up (not in this PR):

  • Turbo's aztec_crypto::sha256_compression is software only. Inside ZisK it costs 4,469 steps instead of 1,103. Routing it through ZisK's SHA-256 precompile is a small change.
  • The rest of the AVM gas schedule doesn't track ZisK cost. POSEIDON2PERM costs about 10× SHA's steps for much less gas, and KECCAKF1600 costs 4.7× SHA's gas for 1.36× its steps.

Generated by Claude Code

… rustfmt

cargo fmt --check in aztec-rs/bootstrap.sh failed on the base branch (zkvm/hash-pq1fr-rs @ 92fcd83) on the two
generated files; ported here so this PR's CI can run past it.
…K guests

4,469 -> 1,103 ZisK steps per opcode (h4c measurement, output equal to native over 11 chained
compressions). The BLAKE3COMPRESSION gas (14123) is calibrated against this precompiled cost.

Copy link
Copy Markdown
Contributor Author

C++ build of the BLAKE3COMPRESSION opcode: green.

  • Built vm2_sim_tests from this branch merged onto zkvm/hash-pq1fr-wsdb (merge was clean), with AZTEC_PQ1FR off, which matches this branch. No compile changes were needed.
  • 419 of 420 tests pass. The one skip is OpcodeSpam, which is gated behind RUN_AVM_OPCODE_SPAM.
  • All 12 BLAKE3 tests pass: Blake3CompressionTest (3), ExecutionBlake3CompressionTest (7) and Blake3CompressionBytecodeTest (2). These include the pq1-fr NOTE_HASH_NODE vector, matching pure Noir, and the wrong-expectation revert.
  • RUN_AVM_OPCODE_SPAM=1 OpcodeSpam.SpamAllOpcodes passes, with BLAKE3COMPRESSION at mana 460000, fastSim 41.6 ms and hintSim 55.8 ms.

This covers simulation only: the opcode has no vm2 circuit (tracegen or constraints) yet.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

CI: ci / ci/x-fast failed twice in the same way: the original run, and one re-run of the failed job (run 47500894833, attempt 2). Both times the build instance's ./bootstrap.sh ci-fast exited with status 2 about 4 minutes in. The step-level log is only on ci.aztec-labs.com (http://ci.aztec-labs.com/1796461968410308), which I can't reach from this session, so I can't name the failing step.

I ran every PR-relevant step from the build scripts locally on this branch's head (04b503d), and all of them pass:

  • aztec-rs: cargo build --locked (lockfile consistent), cargo fmt --check, cargo clippy --locked --all-targets (exit 0) and scripts/gen_constants.sh --check (up to date).
  • avm-transpiler, with RUSTFLAGS=-Dwarnings: cargo build --release --locked --lib, cargo fmt --check and cargo clippy.
  • barretenberg: format.sh check across all of src with clang-format 20.1.8 (clean); vm2_sim_tests 419/420, plus opcode spam (see the comment above).

No commit on the Turbo base (fc/aztec-turbo) or on zkvm/hash-pq1fr-rs has ever been through ci-fast. That leaves this PR's first CI run as the first test of the base itself. So I can't yet tell whether the failure comes from this diff or is already on the base. The next step is reading the CI log above from a machine that can reach ci.aztec-labs.com.


Generated by Claude Code

… from fc/aztec-turbo

aztec-rs/bootstrap.sh builds with RUSTFLAGS=-Dwarnings, and on fc/aztec-turbo (f629ac5)
`cargo build --release --locked` stops at 9 errors in zisk-stark-verifier, so ci-fast fails
before any of this branch's code is checked:

- unexpected `cfg` condition value `std` (log.rs: v_debug!/v_error! sit behind a `std`
  feature the manifest does not define), and with it the loop variables they would log
  (stir.rs:940, verifier.rs:142) are unused;
- unused imports (verifier.rs: DotAcc, cubic_mul_acc; fields/advice.rs: PrimeField64);
- layout::Tree::leaf_bits is never read.

Once it builds, `cargo clippy --locked --all-targets` stops at needless_range_loop and
manual_memcpy in the same crate, then let_and_return in zisk-pilfflonk-verifier's pairing
test and needless_update / redundant_locals in aztec-epoch's state tests.

zisk-stark-verifier's code is left unchanged: its manifest declares the `std` cfg and
allows those lints. The two test files take clippy's suggested rewrites. All five
aztec-rs build steps pass afterwards, and so does `cargo test --locked`.

Copy link
Copy Markdown
Contributor Author

The CI fast-job failure is in code this PR doesn't touch, so I ported a lint-only fix into this PR (4dd6552).

The failure. aztec-rs/bootstrap.sh builds with RUSTFLAGS=-Dwarnings. At 04b503d, cargo build --release --locked stops after about 45 s with 9 errors in crates/zisk-stark-verifier:

  • unexpected cfg value std (in log.rs)
  • unused imports and unused variables
  • a field that is never read

Not this PR's. The same 9 errors reproduce at fc/aztec-turbo f629ac5 and at the base 92fcd83. zisk-stark-verifier is identical in all three commits.

The fix:

  • The crate's Cargo.toml now declares the std cfg value and allows these lints, plus needless_range_loop and manual_memcpy. It is a config change only, and no verifier code changed.
  • clippy's suggested rewrites are applied in two test files: zisk-pilfflonk-verifier/tests/pairing.rs and the aztec-epoch state.rs tests.

The build failed before the fix. After it, these all pass on zac-box:

  • the five aztec-rs CI steps: gen_ipa_srs, build, fmt --check, clippy --all-targets, gen_constants --check;
  • scripts/run_tests.sh;
  • constants-codegen.

The full ci-fast could not be run locally, so a later step may still fail.


Generated by Claude Code

@zac-williamson
zac-williamson marked this pull request as draft December 5, 2026 10:57
@zac-williamson

Copy link
Copy Markdown
Contributor Author

This is an experimental zkVM branch, validated by local checks on a dev box rather than org CI. It isn't meant to merge into the master track for now, so I've converted it to a draft. The work stays on zkvm/avm-blake3-h4 (head 4dd6552).

These local checks pass at 4dd6552:

  • noir: nargo fmt --check and nargo check --deny-warnings on noir-protocol-circuits/crates/types, the crate holding constants.nr (pinned nargo 1.0.0-rc.5, 0f759ffe).
  • aztec-rs: cargo fmt --check, and cargo clippy --locked --all-targets with RUSTFLAGS=-Dwarnings on the changed crates (avm-simulator, aztec-crypto, aztec-core, aztec-epoch, zisk-pilfflonk-verifier, zisk-stark-verifier, pilfflonk-input-builder). scripts/gen_constants.sh --check reports generated.rs up to date.
  • avm-transpiler: cargo fmt --check, cargo clippy, and cargo build --release --locked --bin avm-transpiler, all under -Dwarnings.
  • barretenberg: clang-format-20 --dry-run --Werror on all 1,583 sources (format.sh check). All 9 changed vm2 .cpp files compile with clang-20 (RelWithAssert) against a regenerated aztec_constants.hpp, which includes AVM_BLAKE3COMPRESSION_BASE_L2_GAS 14123.
  • protocol/constants-codegen: node --test src/*.test.ts passes 19/19.
  • labs-patches: all 28 patches apply in order onto the labs gitlink 677343f0ea.

These were not run locally: a full bb build and link, the full aztec-rs workspace build and clippy, the noir-protocol-circuits compile, and the test suites.

🤖 Generated with Claude Code

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