-
Notifications
You must be signed in to change notification settings - Fork 130
zkevm: keccak worst-case #1497
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
Merged
Merged
zkevm: keccak worst-case #1497
Conversation
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
jsign
commented
Apr 25, 2025
kevaundray
reviewed
Apr 25, 2025
55853d2
to
a672724
Compare
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
jsign
commented
May 1, 2025
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
marioevz
reviewed
May 1, 2025
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.
Overall looks good and just have a few comments, thanks!
Co-authored-by: Mario Vega <marioevz@gmail.com>
kevaundray
approved these changes
May 2, 2025
marioevz
approved these changes
May 2, 2025
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.
LGTM, thanks!
felix314159
pushed a commit
to felix314159/execution-spec-tests
that referenced
this pull request
May 5, 2025
* zkEVM: add keccak attack Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * lints Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * only leave 36M gas limit Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * lints Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * feedback Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * Update tests/zkevm/test_worst_compute.py Co-authored-by: Mario Vega <marioevz@gmail.com> * cleanup Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * lints Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Mario Vega <marioevz@gmail.com>
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.
This PR adds a test that builds a block to maximize the number of keccak permutations for a given gas limit (considering the max contract size).
There are two main aspects of the test design.
Find the optimal input size
The metric we care about is gas per permutation, not simply max opcode calls. Although the dynamic cost of
KECCAK256
is linear in input length, memory expansion is quadratic, so there is a sweet spot for minimal cost per permutation.Instead of baking magic numbers, the test does a scan for the optimal cost and then does the attack with it. This is much more transparent on how this optimal length is found, plus it sounds also more future-proof.
Another way of understanding the above is by looking at this chart I created from that same script, with a more detailed output:

The “attack” loop
Once the optimal length is known, I create a loop that drives as many permutations as the block gas limit allows. The loop shouldn't be a tight loop, but quite the opposite -- run as many calls as possible within the max contract size, so the "JUMP"-like gas overhead is amortized as much as possible.
The general structure is:
Note that for 36M you can fit the
(....)
without a loop since the max you can add fits within 24KiB contract limit. But for bigger gas limits, you run short with 24KiB and need the loop!zkVM cycles: 36m gas -> 632 million cycles