Convert BLAKE2s circuit to BLAKE2b#3
Merged
Conversation
BLAKE2B-MOD changes: - 64-bit words (was 32-bit) - 12 rounds (was 10) - Rotations: (32, 24, 16, 63) (was (16, 12, 8, 7)) - 64-bit IV constants - 128-byte blocks (16 x 64-bit words) - 128-bit counter - 16-byte personalization (was 8 bytes) - 64-byte output (was 32 bytes) Circuit changes: - Blake2bWord now contains 64 bits - Added word_decompose_32 for field decomposition (canonicality check) - word_decompose now handles 8 bytes for 64-bit words - Updated s_word_decompose gate for 8 bytes - Updated s_word_add gate for 64-bit arithmetic - Updated s_result_encode for 64-bit word pairs - field_decompose returns 4 x 64-bit words per field - process() chunks inputs by 4 fields per 128-byte block - compress() uses 128-bit counter Note: Circuit requires k=17 due to increased size from 64-bit operations.
SOUNDNESS FIX: - Add s_word_combine gate to constrain 64-bit words - The gate enforces: word_64 = word_32_lo + word_32_hi * 2^32 - Previously, 64-bit words were assigned via assign_free_advice without any constraint tying them to the 32-bit words from field decomposition - A malicious prover could set arbitrary 64-bit values while the bits remained correctly constrained, breaking hash integrity TEST FIX: - Reference function now uses 128-byte blocks (was incorrectly using 64) - Counter calculation matches circuit: (block_idx + 1) * 128 for intermediate blocks, total_bytes.max(128) for final block All 4 tests now pass: - test_blake2b_circuit - test_blake2b_empty_input - test_blake2b_against_reference - test_blake2b_zeros_against_reference
czarcas7ic
pushed a commit
that referenced
this pull request
Apr 20, 2026
Address review feedback on zcash#480
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.
Summary
This PR converts the BLAKE2s Halo2 circuit to BLAKE2b for use in Orchard nullifier proofs.
BLAKE2s → BLAKE2b Changes
Implementation Details
All changes are marked with
// BLAKE2B-MOD:comments for easy review.Constants:
i % 10)Structs:
Blake2bWordwith 64 bits (was 32)Blake2bByteunchanged (8 bits)Gates:
s_word_decompose: Updated for 8 bytes (was 4)s_word_add: UsesF::from_u128(1u128 << 64)for carrys_result_encode: Encodes 2×64-bit words per field (was 4×32-bit)s_word_combine: NEW - Constrains 64-bit word = two 32-bit words combinedArchitecture Decision - 32-bit Words for Field Decomposition:
The field decomposition and canonicality check retain 32-bit word granularity because:
s_word_combinegate soundly constrains:word_64 = word_32_lo + word_32_hi * 2^32Soundness Fix (Critical):
The
field_decomposefunction now properly constrains the 64-bit words vias_word_combine.Previously, the 64-bit words were assigned via
assign_free_advicewithout constraint,allowing a malicious prover to manipulate hash inputs while passing verification.
Chip Methods:
add_mod_u64: Checks byte 8 for overflowword_combine: Constrains 64-bit words to 32-bit pairsword_decompose_32: Kept for field/canonicality (4 bytes)word_decompose: Updated for BLAKE2b operations (8 bytes)word_xor: Updated for 64 bitsword_rotate: Updated for 64 bitsProcess/Compress:
inputs.chunks(4))t: u128)Test plan
test_blake2b_circuit: Basic constraint satisfactiontest_blake2b_empty_input: Empty input handlingtest_blake2b_against_reference: Output matches reference implementationtest_blake2b_zeros_against_reference: Zero input testSecurity Considerations
copy_advices_word_combinegate ensures 64-bit words match 32-bit decompositionBase
Built on top of
blake2b-orchardbranch (includes Phase 1 BLAKE2s soundness fixes).