Speed up SHA-256 constraint and witness synthesis - #512
Open
Leandro Rometsch (leandro-ro) wants to merge 1 commit into
Open
Speed up SHA-256 constraint and witness synthesis#512Leandro Rometsch (leandro-ro) wants to merge 1 commit into
Leandro Rometsch (leandro-ro) wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
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
This PR removes three sources of overhead in the frontend gadgets used by SHA-256. In the benchmark below, the fitted per-block proving cost shows a 4.2× reduction (without changing the generated constraints or witness).
1. Avoid cloning
MultiEqaccumulatorsMultiEq::enforce_equalcloned its growing accumulators whenever an equality was packed:Using
mem::takepreserves the result while eliminating repeated copying. The same unnecessary clones were removed fromMultiEq::accumulate.2. Skip unused work during witness generation
WitnessCS::enforcediscards constraints, so constructing linear combinations during witness generation is unnecessary.MultiEq::enforce_equalnow returns early in witness-only mode, andUInt32::addmanyskips constructinglcandresult_lc. Variable allocation, ordering, and assignment computation are unchanged.3. Avoid an intermediate
LinearCombinationper bitUInt32::addmanypreviously constructed a temporary one-termLinearCombinationthroughBoolean::lcfor every bit. The newpush_boolhelper adds the equivalent term directly to the destination.Correctness
For constraint-generating systems, the same linear combinations are produced in the same order. For witness generators, the same variables are allocated in the same order with the same assignments; only linear combinations that would be discarded are omitted.
Validation:
Benchmarks
Configuration:
ProveCriterion reported a statistically significant improvement at every size (
p < 0.05). The speedup grows with the number of SHA-256 blocks.A linear fit gives:
This is consistent with unchanged fixed overhead and an approximately 4.2× reduction in per-block cost.