SHA-256: Initial constraint optimizations#247
Merged
Conversation
6fd8fd0 to
5b78f63
Compare
Bisht13
requested changes
Jan 16, 2026
Collaborator
|
Nit: Remove unused import. |
Bisht13
approved these changes
Jan 16, 2026
…edule expansion in SHA256 functions
…256 round functions
…ion, skip zero-carry range checks, remove unused variants
…e-level ops and optimizing witness decomposition
a834994 to
e331ec2
Compare
Use range_ops_total from R1CS breakdown instead of ACIR RANGE opcode count when calculating non-SHA256 range checks. The previous code incorrectly used ACIR-level counts which don't match R1CS-level range operations.
The combined AND/XOR lookup table operates on 8-bit operands. When handling BlackBoxFuncCall::AND/XOR with constant operands, the code was pushing full 32-bit constants directly to the ops vectors instead of decomposing them into 4 bytes first. This caused index out of bounds panics in the witness builder when computing multiplicities: the index was computed as (lhs << 8) + rhs, expecting 8-bit values (max index 65535), but receiving 32-bit values (producing indices like 3389742323). Fix: Decompose constant operands into [u8; 4] bytes and push byte-level constant operations to the lookup table, matching the expected byte-level semantics.
When handling AND/XOR operations where lhs is a constant and rhs is a witness, the code was using the raw 32-bit rhs witness directly instead of decomposing it into bytes. This caused index out of bounds panics in the witness builder when computing multiplicities for the 8-bit lookup table (max index 65535, but receiving indices like 2282366754). Fix: Add explicit handling for the (lhs=constant, rhs=witness) case that decomposes the rhs witness into bytes via add_digital_decomposition before pushing to the binop ops vectors.
dcbuild3r
pushed a commit
that referenced
this pull request
May 16, 2026
SHA-256: Initial constraint optimizations
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
Optimizes SHA256 compression constraint generation through byte-level operations and fused constraints.
Results :-
SHA256 Compression – R1CS Cost Breakdown
Marginal SHA256 Call
Key Optimizations:-
1. New Type System for Byte-Level Operations
range_checkedflag2. ROTR with Fused Constraints
result * 2^k + lo = byte + lo_next * 256This achieves 4 constraints per ROTR (one per byte) instead of multiple decomposition constraints.
3. SHR Implementation with Fused Constraints
result * 2^k + lo = byte(no next byte term)4. Range Check Optimization
5.. New BytePartition WitnessBuilder
x = lo + hi * 2^k6.. Chained U32 Addition with Fused Constraints
a + b + c + d + e = result + carry * 2^327. Byte-Level Binary Operations (AND/XOR)
and_ops_byteandxor_ops_bytetracking and anis_byte_levelflag inadd_binop_constraints, avoiding unnecessary 32-bit decomposition while preserving the full decomposition path for general Noir blackbox ops.8. Range Check & LogUp Optimizations
(X - c·v)and verifydenominator * inverse = 1, 2 constraints + 2 witnesses.(X - c·v) * inverse = 1, 1 constraint + 1 witness.WitnessBuilder::LogUpInversevariant that computes the inverse directly during batch inversion, eliminating the intermediate denominator witness.(Σ table_terms - Σ witness_terms) = 0. Removes 2 constraints and 2 intermediate sum witnesses per range-check lookup.LogUpInverse.LogUpInverseis deferred and batched alongside existing inverse operations.(X - c·v)are computed inline during batch inversion.8. BinOp LogUp Constraint Fusion (
binops.rs)(Σ binop_terms − Σ table_terms) = 0.9. Combined AND/XOR Lookup Table
sz − (lhs + rs·rhs + rs²·and + rs³·xor). Eliminates one full lookup table.10. Byte-Level Binary Operations (SHA256)
11. Inlined T1 / T2 Computation in SHA256 Rounds
12. Inline Table Entry Inverse (CombinedTableEntryInverse)
(sz − lhs − rs·rhs − rs²·and − rs³·xor) × inv = 1. Eliminates the intermediate denominator witness.13. Pack Cache for U32 Operations
pack_cached()caches pack results keyed by byte indices and reuses them.