-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize FixedLengthSum of hash with minLen #1445
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request optimizes the FixedLengthSum methods for SHA2 and SHA3 by adding a new minLen parameter to reduce unnecessary constraints during the hashing process. Key changes include:
- Updating the FixedLengthSum function signatures and internal logic in both SHA2 and SHA3 implementations to incorporate minLen.
- Adjusting and adding new test cases (including TestSHA3FixedLengthSumWithMinLen and updates in SHA2 tests) to validate the new functionality.
- Updating the hash interface documentation to reflect the new parameter.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
std/hash/sha3/sha3_test.go | Updated test cases to call FixedLengthSum with appropriate minLen values. |
std/hash/sha2/sha2_test.go | Updated tests to loop over variable lengths using defined minLen and maxLen. |
std/hash/sha2/sha2.go | Revised padding and summing logic in FixedLengthSum to use minLen. |
std/hash/sha3/sha3.go | Modified FixedLengthSum, paddingFixedWidth, and absorbingFixedWidth to incorporate minLen. |
std/hash/hash.go | Updated the BinaryFixedLengthHasher interface documentation. |
Comments suppressed due to low confidence (3)
std/hash/sha3/sha3_test.go:115
- [nitpick] Using a literal '0' for the minLen parameter may be unclear; consider defining a descriptive constant or adding a comment to clarify that this case is intended as a baseline comparison.
res := h.FixedLengthSum(0, c.Length)
std/hash/sha2/sha2.go:115
- Review the loop boundary here: using 'i <= maxLen' assumes that the appended padding data aligns correctly with the original input length. Verify that this condition correctly accounts for the extended slice length and does not lead to off-by-one errors.
for i := minLen; i <= maxLen; i++ {
std/hash/sha3/sha3.go:82
- Ensure that the modified boundary condition in the paddingFixedWidth function correctly handles the range of indices after appending additional padding. A review of the off-by-one behavior here is advised.
for i := minLen; i <= maxLen; i++ {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the FixedLengthSum implementations for SHA2 and SHA3 hashes by introducing a minimalLength parameter that allows skipping unnecessary constraints when the input is guaranteed to meet a lower bound.
- Introduces a new option WithMinimalLength in the hasher configuration for both SHA2 and SHA3.
- Updates padding, block composition, and absorbing loops in both hash implementations to take advantage of the minimal length.
- Adjusts test circuits and hash constructor helpers to pass and validate the minimalLength parameter.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
std/hash/sha2/sha2_test.go | Updated tests to incorporate minimalLength variations. |
std/hash/sha2/sha2.go | Modified hash construction and FixedLengthSum to use minimalLength. |
std/hash/hash.go | Added option WithMinimalLength and updated documentation. |
std/hash/sha3/sha3.go | Integrated minimalLength in padding and absorbing functions. |
std/hash/sha3/sha3_test.go | Adjusted tests to validate new minimalLength parameter. |
std/hash/sha3/hashes.go | Updated SHA3 hash constructors to support minimalLength option. |
Comments suppressed due to low confidence (1)
std/hash/sha3/sha3.go:133
- Consider verifying if integer division for minNbOfBlocks correctly handles cases where minimalLength is not a multiple of rate. If partial blocks should also trigger initialization, adjust the calculation accordingly.
minNbOfBlocks := d.minimalLength / d.rate
Co-authored-by: Copilot <[email protected]>
Thanks for the contribution! I really like it and it indeed makes the implementation more efficient. I only change the interfaces to keep the current Congrats btw for this change, it is super good catch! @ggq89, @weijiguo have a look how it seems now. It is good to merge from my side but I'll accept review+merge when you confirm that my changes are good. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's good, it would be even better to move the MinimalLength option to the hash constructor
Looks good to me. And thank you for stepping in @ivokub |
Description
Use minLen to reduce the times of selects and optimize the FixedLengthSum of hash.
In practice, the length of a piece of data to be hashed is variable, but its minimum value is rarely 0. In this case, if minLen is passed to FixedLengthSum, a lot of constraints can be saved. For example, in the project we are currently working on, one of the data lengths to be hashed ranges from 1680 to 1710. Using the new FixedLengthSum can reduce about 200,000 constraints, as shown in the results of Test_SHA3FixedLengthSum_WithMinLen_VS_Zero:
Type of change
How has this been tested?
How has this been benchmarked?
Checklist:
golangci-lint
does not output errors locally