feat(circuits): add variable-length BLAKE3 gadget and wire it into the example#1743
Open
gopikannappan wants to merge 3 commits into
Open
feat(circuits): add variable-length BLAKE3 gadget and wire it into the example#1743gopikannappan wants to merge 3 commits into
gopikannappan wants to merge 3 commits into
Conversation
…e example Adds binius_circuits::blake3::blake3_variable, a BLAKE3 hash gadget whose message length is a runtime witness bounded by a compile-time capacity — the BLAKE3 analog of Sha256::new. Wires it into the blake3 example so --max-message-len is accepted (previously rejected), removing the single-chunk restriction. For a length-len message the digest is fixed by the final-block index bd, its byte length fbl, and the content; all blocks before bd are full, so the chunk/tree shape follows from bd. Following the SHA-256 variable circuit's approach: mask the message tail to zero against len_bytes; build one candidate digest per possible final-block index (reusing the exact blake3_fixed chunk/tree machinery, sharing full interior chunks, with only the final block's block_len as a runtime wire); then multi_wire_multiplex the candidate at bd. len_bytes <= max_len_bytes is enforced so the multiplex selector stays in range. New unit tests compare the circuit digest against the blake3 crate across empty, single-block, single-chunk, multi-chunk, and non-aligned capacities, plus an overlong-length rejection test. Example proves+verifies end-to-end for variable messages. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Collaborator
|
Awesome, thanks! Will review soon |
… + add negative tests blake3_variable hashes the prover-claimed length: it enforces digest == blake3(message[..len_bytes]) and len_bytes <= max_len_bytes, but does not constrain the len_bytes value itself. With a free len_bytes and an internally-consumed digest, a prover can substitute the digest of any prefix. This mirrors the keccak len_bytes contract documented in binius-zk#1758. - document the len_bytes caller obligation on blake3_variable (safe patterns: pin to a public input / derived wire, or use blake3_fixed for constant length) - add variable_unconstrained_len_admits_prefix_hash: honest-length control, the prefix-hash characterization, and a positive-binding control showing message[..len_bytes] is bound (full-message digest rejected under a short len) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What. Adds
binius_circuits::blake3::blake3_variable, a BLAKE3 hash gadget whose message length is a runtime witness bounded by a compile-time capacity — the BLAKE3 analog ofSha256::new. Wires it into theblake3example so--max-message-lenis accepted (previously rejected), and removes the example's single-chunk restriction.Note one thing I found while scoping this: the
blake3_fixedTODO about multi-chunk support is stale —blake3_fixed/blake3_tree_rootalready handle multi-chunk messages. The genuine remaining gap is a runtime-variable-length gadget (theSha256::newanalog), which is what this adds.Why. BLAKE3 only had
blake3_fixed(compile-time length). Applications that verify variable-length inputs bounded by a max had no BLAKE3 option, unlike SHA-256/SHA-512/Keccak.How. For a length-
lenmessage the digest is fixed by the final-block indexbd, its byte lengthfbl, and the content; all blocks beforebdare full, so the chunk/tree shape follows frombd. Following the SHA-256 variable circuit's approach: mask the message tail to zero againstlen_bytes; build one candidate digest per possible final-block index (reusing the exactblake3_fixedchunk/tree machinery, sharing full interior chunks, with only the final block'sblock_lenas a runtime wire); thenmulti_wire_multiplexthe candidate atbd.len_bytes <= max_len_bytesis enforced so the multiplex selector stays in range.Testing. New unit tests compare the circuit digest against the
blake3crate across empty, single-block, single-chunk, multi-chunk, and non-aligned capacities, plus an overlong-length rejection test.cargo test -p binius-circuits -p binius-examplesgreen; fmt/clippy clean; example proves+verifies end-to-end for variable messages.Known limitation (flagging honestly). This builds
max_blockscandidate digests, each rebuilding its chunk + parent tree, so circuit size is roughly quadratic inmax_blocks. I chose the clearly-correct construction over the optimal one for a first pass; a production version likely wants an incremental CV-stack (à la BLAKE3 streaming) or shared sub-tree nodes across candidates to reach ~O(max_blocks). Happy to follow up with that if you'd prefer it before merge.Also flagging unrelated pre-existing drift I noticed:
blake3 check-snapshotfails on the default fixed circuit on pristineHEADtoo (verified viagit stash) — the committed snapshot looks stale relative to currentmain. This PR leaves the default fixed-length circuit shape untouched, so I did not re-bless it.Authored with Claude Code.