-
Notifications
You must be signed in to change notification settings - Fork 11
Replace stdlib keccak with XKCP for ~1.4x speedup #28
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const std = @import("std"); | ||
| const eth = @import("eth"); | ||
|
|
||
| pub fn main() !void { | ||
| var args = std.process.args(); | ||
| _ = args.next(); // skip program name | ||
|
|
||
| const size_str = args.next() orelse "32"; | ||
| const iters_str = args.next() orelse "1000000"; | ||
| const backend_str = args.next() orelse "xkcp"; | ||
|
|
||
| const size = try std.fmt.parseInt(usize, size_str, 10); | ||
| const iters = try std.fmt.parseInt(usize, iters_str, 10); | ||
|
|
||
| // Allocate input data | ||
| const allocator = std.heap.page_allocator; | ||
| const data = try allocator.alloc(u8, size); | ||
| defer allocator.free(data); | ||
| @memset(data, 0xAB); | ||
|
|
||
| if (std.mem.eql(u8, backend_str, "xkcp")) { | ||
| // XKCP backend (our new implementation) | ||
| for (0..iters) |_| { | ||
| const result = eth.keccak.hash(data); | ||
| std.mem.doNotOptimizeAway(&result); | ||
| } | ||
| } else if (std.mem.eql(u8, backend_str, "stdlib")) { | ||
| // Zig stdlib backend | ||
| for (0..iters) |_| { | ||
| var result: [32]u8 = undefined; | ||
| std.crypto.hash.sha3.Keccak256.hash(data, &result, .{}); | ||
| std.mem.doNotOptimizeAway(&result); | ||
| } | ||
| } | ||
| } | ||
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,50 @@ | ||
| /* | ||
| The eXtended Keccak Code Package (XKCP) | ||
| https://github.com/XKCP/XKCP | ||
|
|
||
| Implementation by Andre Moraes | ||
|
|
||
| --- | ||
|
|
||
| Please refer to SnP-documentation.h for more details. | ||
| */ | ||
|
|
||
| #ifndef _KeccakP_1600_SnP_h_ | ||
| #define _KeccakP_1600_SnP_h_ | ||
|
|
||
| #include <stdint.h> | ||
| #include "align.h" | ||
| #include "SnP-common.h" | ||
|
|
||
| typedef struct { | ||
| ALIGN(64) uint64_t A[25]; | ||
| } KeccakP1600_align512plain64_state; | ||
|
|
||
| typedef KeccakP1600_align512plain64_state KeccakP1600_state; | ||
|
|
||
| #define KeccakP1600_GetImplementation() "64-bit optimized ARMv8a assembler implementation" | ||
| #define KeccakP1600_GetFeatures() (SnP_Feature_Main) | ||
| #define KeccakP1600_stateAlignment 64 | ||
|
|
||
| #define KeccakP1600_StaticInitialize() | ||
| void KeccakP1600_Initialize(KeccakP1600_align512plain64_state *state); | ||
| void KeccakP1600_AddByte(KeccakP1600_align512plain64_state *state, unsigned char data, unsigned int offset); | ||
| void KeccakP1600_AddBytes(KeccakP1600_align512plain64_state *state, const unsigned char *data, unsigned int offset, unsigned int length); | ||
| void KeccakP1600_OverwriteBytes(KeccakP1600_align512plain64_state *state, const unsigned char *data, unsigned int offset, unsigned int length); | ||
| void KeccakP1600_OverwriteWithZeroes(KeccakP1600_align512plain64_state *state, unsigned int byteCount); | ||
| void KeccakP1600_Permute_Nrounds(KeccakP1600_align512plain64_state *state, unsigned int nrounds); | ||
| void KeccakP1600_Permute_12rounds(KeccakP1600_align512plain64_state *state); | ||
| void KeccakP1600_Permute_24rounds(KeccakP1600_align512plain64_state *state); | ||
| void KeccakP1600_ExtractBytes(const KeccakP1600_align512plain64_state *state, unsigned char *data, unsigned int offset, unsigned int length); | ||
| void KeccakP1600_ExtractAndAddBytes(const KeccakP1600_align512plain64_state *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length); | ||
|
|
||
| #define KeccakF1600_FastLoop_Absorb(...) 0 | ||
| #define KeccakP1600_12rounds_FastLoop_Absorb(...) 0 | ||
| #define KeccakP1600_ODDuplexingFastInOut(...) 0 | ||
| #define KeccakP1600_12rounds_ODDuplexingFastInOut(...) 0 | ||
| #define KeccakP1600_ODDuplexingFastOut(...) 0 | ||
| #define KeccakP1600_12rounds_ODDuplexingFastOut(...) 0 | ||
| #define KeccakP1600_ODDuplexingFastIn(...) 0 | ||
| #define KeccakP1600_12rounds_ODDuplexingFastIn(...) 0 | ||
|
|
||
| #endif |
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.