-
Notifications
You must be signed in to change notification settings - Fork 154
all: Unify keccaking #580
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
all: Unify keccaking #580
Conversation
9d27074 to
c2a9d36
Compare
bragaigor
left a comment
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, wondering if we should run a complete set of tests on nitro side just to be double sure everything still works?
@bragaigor good point! I have opened a companion PR in nitro: OffchainLabs/nitro#4108 - CI is green ✅ |
eljobe
left a comment
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.
I'm nervous about this change.
I could be completely wrong. But, I seem to remember that the resetting the incremental version of the KeccakState instance is less computationally expensive than allocating and destroying it over and over again (which is what happens inside Keccak256.
Did you run any benchmarks to test for possible regressions?
I could be wrong. But, I don't want to cause a big performance slow-down in our current validators as we get ready for our future ZK-Validators.
|
@eljobe please notice that I'm using |
This does make me more comfortable. In fact, I'm going approve and merge the change. But, I'd still love to have seen the output of a benchmarking run that showed that the replacement doesn't introduce any performance overhead. (For example, I could imagine that a heavily contended for pool of hashers might introduce some locking or something.) But, at least, the code is trying to be as efficient as the previous implementation. |
pulled in by OffchainLabs/nitro#4122
upstream PR was rejected as non-essential
closes NIT-4123
Context
Keccak hashing algorithm can be used in two ways: (1) as a one-shot hash function (we are just interested in hash of the data that we already have in full) or (2) as a cryptographic sponge (for cases where we don't know all the data to be hashed yet, we need some intermediate hashes, etc.)
From the caller perspective, (1) is a completely stateless process, but (2) requires keeping some state.
The
cryptopackage exposesKeccak256Hash()andKeccak256()functions realizing (1) andNewKeccakState()for (2).Problem
There are ~20 places where we use stateful approach (2) for (1). In other words, we:
NewKeccakState()to create the sponge stateWrite(),Read(),Sum())whereas we could (and should) just use
Keccak256()utility.Proposed changes
spongeDbin some testing.Keccak256Hash()andKeccak256()KeccakStateinterface just extendio.Readerinstead of inliningio.Reader's methodMotivation
When running geth within ZK VM (like SP1) it is highly beneficial to outsource heavy computation (like Keccak) to the dedicated VM precompiles. By removing all unnecessarily stateful hashing and reusing the
Keccak256Hash()andKeccak256()utilities, it is way easier to replace all keccak invocations than to worry that some of them will persist in this strange form.