Skip to content

Feat: Add BlackBox::Sha256Compression#203

Merged
Bisht13 merged 20 commits into
mainfrom
sl/sha256-blackbox
Oct 10, 2025
Merged

Feat: Add BlackBox::Sha256Compression#203
Bisht13 merged 20 commits into
mainfrom
sl/sha256-blackbox

Conversation

@shreyas-londhe
Copy link
Copy Markdown
Collaborator

@shreyas-londhe shreyas-londhe commented Sep 24, 2025

Description

Adds complete SHA256 compression blackbox function support to NoirToR1CSCompiler, enabling efficient SHA256 hashing in R1CS circuits with significant performance improvements through optimized binary operation batching.

Key Innovation: Global Binary Operation Optimization

Implements novel approach to decouple binary operation value computation from constraint generation:

  • Collection Phase: add_binop() allocates witnesses, adds WitnessBuilders for immediate value computation, and collects operations
  • Constraint Phase: add_binop_constraints() processes all collected AND/XOR operations in a single batch with shared lookup tables
  • Benefits: Eliminates redundant digital decompositions and lookup table setups, dramatically reducing memory usage and constraint count

Performance Results

Benchmarked on complete_age_check example:

Metric Main Branch SHA256 Blackbox Improvement
Opcodes 590,297 84,204 86% reduction
Proving Time 22s ~9s 59% faster
Peak Memory 10.4GB 5.25GB 50% reduction
Constraints 3,604,748 2,503,439 31% reduction
Witnesses 5,074,284 3,578,996 29% reduction

Changes Made

Core Implementation

  • sha256_compression.rs: New module implementing SHA256 compression algorithm
    • add_sha256_compression(): Main entry point processing 16 input words + 8 hash values → 8 output words
    • 64-round compression loop using SHA256 constants and message schedule

Binary Operation Batching

  • binops.rs: Refactored for collection-based processing
    • add_binop(): Collects operations while computing values immediately
    • add_binop_constraints(): Processes all collected operations with shared digital decomposition and lookup tables

WitnessBuilder Extensions

  • WitnessBuilder::U32Addition: Computes result = (a + b) % 2^32, carry = (a + b) / 2^32
  • WitnessBuilder::And / WitnessBuilder::Xor: Bitwise operations for 32-bit values
  • Added corresponding solver logic in prover/src/witness/witness_builder.rs

Integration

  • noir_to_r1cs.rs: Added SHA256 compression blackbox function handling with batched binary operation processing

@shreyas-londhe shreyas-londhe marked this pull request as draft September 24, 2025 14:19
@shreyas-londhe shreyas-londhe force-pushed the sl/sha256-blackbox branch 2 times, most recently from b1584d5 to 2d9c9b4 Compare September 25, 2025 04:00
@shreyas-londhe shreyas-londhe marked this pull request as ready for review September 27, 2025 13:21
/// Allocate a witness for a binary operation result, add the appropriate
/// WitnessBuilder for value computation, and collect the operation for later
/// constraint generation. Returns the witness index of the result.
pub(crate) fn add_binop(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just say here that you need to call add_binop_constrains on every collected_ops Vec that you pass into this method, if we end up needing to add more Binary ops than we did so far

Copy link
Copy Markdown
Collaborator

@veljkovranic veljkovranic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a minor comment, otherwise great work!
Really awesome PR :)

@Bisht13 Bisht13 force-pushed the sl/sha256-blackbox branch from 489ae19 to 0926670 Compare October 10, 2025 00:56
@Bisht13 Bisht13 merged commit 483a660 into main Oct 10, 2025
5 of 7 checks passed
@Bisht13 Bisht13 deleted the sl/sha256-blackbox branch October 10, 2025 04:45
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.

3 participants