Skip to content

Dynamic spread table width + Ch algebraic optimization#295

Merged
Bisht13 merged 4 commits into
mainfrom
rs/dynamic_spread
Feb 18, 2026
Merged

Dynamic spread table width + Ch algebraic optimization#295
Bisht13 merged 4 commits into
mainfrom
rs/dynamic_spread

Conversation

@rose2221
Copy link
Copy Markdown
Collaborator

@rose2221 rose2221 commented Feb 18, 2026

Summary

  • Replaces the hardcoded 8-bit spread table with a cost-model-driven dynamic width selection. A new calculate_spread_witness_cost function estimates total witness cost across table overhead, inline witnesses, and LogUp queries for each candidate width (2–20 bits), and get_optimal_spread_width picks the minimum. All spread internals are generalized from fixed 4-byte chunks to variable-width chunks (subchunks, spread_decompose, pack_chunks, spread_from_chunk_spreads). The chosen width is displayed in circuit_stats output.

  • Reduces Ch(e,f,g) from 3 spread_decompose calls to 2 + one algebraic constraint, using the identity Ch = (e&f) + g - (e&g). Saves ~39 witnesses/round (~2,496/compression) from eliminated spread decomposition and lookups. Dead code removed: spread_not_terms, spread_from_chunk_spreads, SPREAD_ALL_ONES.

Benchmarks

35 SHA256 compressions (spread width: 8-bit → 16-bit)

Metric Before After Reduction
Constraints 445,739 328,739 -26.3%
Witnesses 1,033,576 705,076 -31.8%

1 SHA256 compression (spread width: 8-bit → 11-bit)

Metric Before After Reduction
Constraints 14,389 11,561 -19.7%
Witnesses 31,680 25,475 -19.6%

@rose2221 rose2221 requested a review from Bisht13 February 18, 2026 06:50
@rose2221 rose2221 changed the title Dynamic spread table width for SHA256 Dynamic spread table width + Ch algebraic optimization Feb 18, 2026
Comment thread provekit/r1cs-compiler/src/spread.rs Outdated
Comment thread provekit/r1cs-compiler/src/spread.rs
@Bisht13 Bisht13 merged commit 8e4fc38 into main Feb 18, 2026
3 of 9 checks passed
@Bisht13 Bisht13 deleted the rs/dynamic_spread branch February 18, 2026 18:39
dcbuild3r pushed a commit that referenced this pull request May 16, 2026
Dynamic spread table width + Ch algebraic optimization
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