-
Notifications
You must be signed in to change notification settings - Fork 132
perf(l1): use asm ffi keccak #5247
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
Changes from 45 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
75408b2
wip: integrate cryptogam assembly versions of keccak
Oppen a73024f
Rewrite `x86_64` implementation using Intel syntax. Refactor keccak
azteca1998 5ee9f13
Fix keccak bug on almost full block.
azteca1998 fddc651
Remove unnecessary files.
azteca1998 5b379ed
fixes to armv8 asm
Oppen 993ff5a
Fix keccak asm for aarch64 (without SHA3).
azteca1998 ea3e78c
Avoid modifying the original keccak asm for `x86_64`.
azteca1998 20e78e5
replace usage of sha3 wherever possible and simplify some of the code
Oppen 7134c90
fixes
Oppen 7ed1d90
use option raw to avoid having to modify braces
Oppen 87c8eef
updated modified header
Oppen a9445a0
Merge remote-tracking branch 'origin/main' into perf/asm_ffi_keccak
Oppen b5ff6c8
cargo.lock
Oppen ba715ab
add update and finalize via Keccak256Asm
edg-l 36d9004
inline
edg-l 267469b
test
edg-l d3b7544
fixes and use keccak asm
edg-l b7828ea
remove sha3 from p2p
edg-l 94022b2
fix duplicate keys
edg-l 3358cb6
rename
edg-l abb40c9
lint
edg-l 779211e
add fallback
edg-l bfe736d
Merge remote-tracking branch 'origin/main' into perf/asm_ffi_keccak
edg-l 323cf5e
fix error
edg-l d229c53
fix
edg-l 7d48b57
changelog
edg-l 7527510
fmt
edg-l 93be439
lint
edg-l 9114c5c
use update finalize
edg-l 51b7f97
lint
edg-l cddf4de
Merge branch 'main' into perf/asm_ffi_keccak
jrchatruc c3bcdd8
fmt
jrchatruc ea8c2ad
Merge branch 'main' into perf/asm_ffi_keccak
jrchatruc a54408c
Merge branch 'main' into perf/asm_ffi_keccak
edg-l e33eb2c
clarify x86 choice
Oppen 2c6cd32
document choices better
Oppen e0f944c
remove outdated instruction
Oppen 965501c
fix instructions
Oppen 6746c0e
update docs
Oppen 411d5ce
remove armv8+sha3
Oppen 49f1a02
rewrite keccak_hash in terms of update and finalize
Oppen 6a0f3ee
Merge branch 'main' into perf/asm_ffi_keccak
jrchatruc 374502d
remove unused function
Oppen e82e809
remove stale comment
Oppen 6bac6e3
refactor new
Oppen c1e5e75
Merge branch 'main' into perf/asm_ffi_keccak
jrchatruc cdf6afd
Update crates/common/crypto/keccak/README.md
jrchatruc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Keccak Module | ||
|
|
||
| A thin layer over assembly implementations of (intentionally few) optimized Keccak for ARMv8 and x86_64. | ||
| The code is adapted from the output of the scripts written by the [cryptogams](https://github.com/dot-asm/cryptogams) project. See [#copyright-notice] for a copy of the licence. You can find the original text at [their repository](https://github.com/dot-asm/cryptogams/blob/680f98c1765a7cb89c193db169ed048599f92186/LICENSE). | ||
|
|
||
| > [!NOTE] | ||
| > This library is not endorsed nor supported by the original _Cryptogams_ team. | ||
| > The code has been modified to integrate to Rust in the simplest possible way and to avoid the need of extra toolchains to build the project. | ||
|
|
||
| ## Goals | ||
|
|
||
| The goal of this module is to have an efficient implementation of Keccak256 for Ethrex, reusing audited code as much as possible, while keeping complexity as low as possible. | ||
| To achieve low complexity, we leave explicitly out of scope implementing `Digest`, having implementations for all variants of CPUs (we keep a selected subset of those provided by _Cryptogams_) and compile-time translation of source files. | ||
| The module exposes only the following: | ||
| ```rust | ||
| pub fn keccak_hash(data: impl AsRef<[u8]>) -> [u8; 32]; | ||
| struct Keccak256; | ||
| impl Keccak256 { | ||
| fn new() -> Self; | ||
| fn update(&self, impl AsRef<[u8]>) -> Self; | ||
| fn finalize(self) -> [u8; 32]; | ||
| } | ||
| impl Default for Keccak256; | ||
| ``` | ||
| There are no feature flags. If building for `x86_64`, it will link an optimized assembly implementation. Because it uses generic `x86_64` code, no fallback is needed. | ||
| If building for `ARMv8`, it will link an optimized implementation using generic `ARMv8` instructions. | ||
| In both cases we chose the baseline instruction sets. This was not due to compatibility, which can be handled with dynamic dispatch, but because in the case of `ARMv8` using specialized `SHA3` instructions showed no improvement, and in `x86_64` using `AVX2` actually showed a regression of 30% in throughput. | ||
| For other architectures, it falls back to `tiny_keccak`. This is specially necessary for proving, as the ZKVMs are RISC-V based, but they are not guaranteed to support all of its extensions. We may revisit adding assembly versions for them at a later time. | ||
|
|
||
| ## Code Generation | ||
|
|
||
| The implementation is currently rather manual: | ||
| - Code is generated by running the scripts in the _Cryptogams_ project (currently at commit `680f98c1765a7cb89c193db169ed048599f92186`), as follows: | ||
| ```shell | ||
| $ cd cryptogams/arm | ||
| $ ./keccak-1600-armv8.pl linux64 keccak1600-armv8.s | ||
| $ cd ../x86_64 | ||
| $ ./keccak1600-x86_64.pl linux64 keccak1600-x86_64.s | ||
| ``` | ||
| - The x86 can be directly imported by the Rust compiler with the current options, but the ARM code requires a few changes, commented at the top of the `keccak1600-armv8.s` file. | ||
|
|
||
| ## Copyright Notice | ||
|
|
||
| Copyright (c) 2006, CRYPTOGAMS by <[email protected]> | ||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions | ||
| are met: | ||
|
|
||
| * Redistributions of source code must retain copyright notices, | ||
| this list of conditions and the following disclaimer. | ||
|
|
||
| * Redistributions in binary form must reproduce the above | ||
| copyright notice, this list of conditions and the following | ||
| disclaimer in the documentation and/or other materials | ||
| provided with the distribution. | ||
|
|
||
| * Neither the name of the CRYPTOGAMS nor the names of its | ||
| copyright holder and contributors may be used to endorse or | ||
| promote products derived from this software without specific | ||
| prior written permission. | ||
|
|
||
| ALTERNATIVELY, provided that this notice is retained in full, this | ||
| product may be distributed under the terms of the GNU General Public | ||
| License (GPL), in which case the provisions of the GPL apply INSTEAD OF | ||
| those given above. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS | ||
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.