Skip to content

optimize paddingFixedWidth of sha3 using divmod hint #1450

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

ggq89
Copy link
Contributor

@ggq89 ggq89 commented Mar 24, 2025

Description

In this PR, a divmod function is implemented with hint, and it is used to optimize the paddingFixedWidth of sha3 to reduce the number of selects in the process of padding data and calculating numberOfBlocks.

In the project we are currently working on, one of the data lengths to be keccak256 hashed ranges from 1130 to 1710. Using the new FixedLengthSum can reduce about 50,000 constraints, as shown below:

Old version paddingFixedWidth: hash=keccak256/bound=1130/maxLen=1710/nbConstraints=3871960
New version paddingFixedWidth: hash=keccak256/bound=1130/maxLen=1710/nbConstraints=3820827

Type of change

  • New feature (non-breaking change which adds functionality)

How has this been tested?

  • TestSHA2FixedLengthSum
  • TestSHA3FixedLengthSum

How has this been benchmarked?

  • Benchmark A, on Macbook pro M1, 32GB RAM
  • Benchmark B, on x86 Intel xxx, 16GB RAM

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@ivokub
Copy link
Collaborator

ivokub commented Mar 24, 2025

Thanks for the PR. I'll review it soon.

@ivokub ivokub self-requested a review March 24, 2025 09:48
if d.minimalLength > 0 {
comparator.AssertIsLessEq(d.minimalLength, length)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the constraint comparator.AssertIsLessEq(length, maxLen) should be added

Copy link
Contributor Author

@ggq89 ggq89 left a comment

Choose a reason for hiding this comment

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

The constraint comparator.AssertIsLessEq(length, maxLen) should be added to prevent the wrong value from being returned when length>maxLen.

@@ -109,7 +115,7 @@ func (d *digest) FixedLengthSum(length frontend.Variable) []uints.U8 {

Copy link
Contributor Author

@ggq89 ggq89 Mar 29, 2025

Choose a reason for hiding this comment

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

the constraint comparator.AssertIsLessEq(length, maxLen) should be added

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