-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat: add swappable hasher, default to noble-hashes #314
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3461dc8
Add swappable hasher, default to noble-hashes
wemeetagain 5fa16ce
Use as-sha256 hasher in benchmarks
wemeetagain 21c8fda
Add some comments
wemeetagain 455f76d
Use as-sha256 hasher in tests
wemeetagain e0bd57f
Increase test timeout
wemeetagain 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
colors: true | ||
require: | ||
- ts-node/register | ||
- setHasher.mjs | ||
|
||
# benchmark opts | ||
threshold: 3 | ||
|
This file contains 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 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 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
colors: true | ||
require: ts-node/register | ||
require: | ||
- ts-node/register | ||
- ../../setHasher.mjs |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,7 @@ | ||
import {digest2Bytes32, digest64HashObjects} from "@chainsafe/as-sha256"; | ||
import type {Hasher} from "./types"; | ||
|
||
export const hasher: Hasher = { | ||
digest64: digest2Bytes32, | ||
digest64HashObjects, | ||
}; |
This file contains 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,20 @@ | ||
import {Hasher} from "./types"; | ||
import {hasher as nobleHasher} from "./noble"; | ||
|
||
export {HashObject} from "@chainsafe/as-sha256/hashObject"; | ||
export * from "./types"; | ||
export * from "./util"; | ||
|
||
/** | ||
* Hasher used across the SSZ codebase | ||
*/ | ||
export let hasher: Hasher = nobleHasher; | ||
|
||
/** | ||
* Set the hasher to be used across the SSZ codebase | ||
* | ||
* WARNING: This function is intended for power users and must be executed before any other SSZ code is imported | ||
*/ | ||
export function setHasher(newHasher: Hasher): void { | ||
hasher = newHasher; | ||
} |
This file contains 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,10 @@ | ||
import {sha256} from "@noble/hashes/sha256"; | ||
import type {Hasher} from "./types"; | ||
import {hashObjectToUint8Array, uint8ArrayToHashObject} from "./util"; | ||
|
||
const digest64 = (a: Uint8Array, b: Uint8Array): Uint8Array => sha256.create().update(a).update(b).digest(); | ||
twoeths marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export const hasher: Hasher = { | ||
digest64, | ||
digest64HashObjects: (a, b) => uint8ArrayToHashObject(digest64(hashObjectToUint8Array(a), hashObjectToUint8Array(b))), | ||
}; |
This file contains 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,12 @@ | ||
import type {HashObject} from "@chainsafe/as-sha256/hashObject"; | ||
|
||
export type Hasher = { | ||
/** | ||
* Hash two 32-byte Uint8Arrays | ||
*/ | ||
digest64(a32Bytes: Uint8Array, b32Bytes: Uint8Array): Uint8Array; | ||
/** | ||
* Hash two 32-byte HashObjects | ||
*/ | ||
digest64HashObjects(a: HashObject, b: HashObject): HashObject; | ||
}; |
This file contains 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,11 @@ | ||
import {byteArrayToHashObject, HashObject, hashObjectToByteArray} from "@chainsafe/as-sha256/hashObject"; | ||
|
||
export function hashObjectToUint8Array(obj: HashObject): Uint8Array { | ||
const byteArr = new Uint8Array(32); | ||
hashObjectToByteArray(obj, byteArr, 0); | ||
return byteArr; | ||
} | ||
|
||
export function uint8ArrayToHashObject(byteArr: Uint8Array): HashObject { | ||
return byteArrayToHashObject(byteArr); | ||
} |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,33 @@ | ||
import {itBench} from "@dapplion/benchmark"; | ||
import {uint8ArrayToHashObject} from "../../src/hasher"; | ||
import {hasher as asShaHasher} from "../../src/hasher/as-sha256"; | ||
import {hasher as nobleHasher} from "../../src/hasher/noble"; | ||
|
||
describe("hasher", () => { | ||
const root1 = new Uint8Array(32); | ||
const root2 = new Uint8Array(32); | ||
for (let i = 0; i < root1.length; i++) { | ||
root1[i] = 1; | ||
} | ||
for (let i = 0; i < root2.length; i++) { | ||
root2[i] = 2; | ||
} | ||
|
||
// total number of time running hash for 250_000 validators | ||
const iterations = 2250026; | ||
|
||
for (const {hasher, name} of [ | ||
{hasher: asShaHasher, name: "as-sha256"}, | ||
{hasher: nobleHasher, name: "noble"}, | ||
]) { | ||
itBench(`hash 2 Uint8Array ${iterations} times - ${name}`, () => { | ||
for (let j = 0; j < iterations; j++) hasher.digest64(root1, root2); | ||
}); | ||
|
||
const obj1 = uint8ArrayToHashObject(root1); | ||
const obj2 = uint8ArrayToHashObject(root2); | ||
itBench(`hashTwoObjects ${iterations} times - ${name}`, () => { | ||
for (let j = 0; j < iterations; j++) hasher.digest64HashObjects(obj1, obj2); | ||
}); | ||
} | ||
}); |
10 changes: 5 additions & 5 deletions
10
.../persistent-merkle-tree/test/hash.test.ts → ...tent-merkle-tree/test/unit/hasher.test.ts
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
colors: true | ||
require: ts-node/register | ||
require: | ||
- ts-node/register | ||
- ../../setHasher.mjs | ||
# benchmark | ||
maxMs: 30_000 | ||
threshold: 3 |
This file contains 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 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 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 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 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,5 @@ | ||
// Set the hasher to as-sha256 | ||
// Used to run benchmarks with with visibility into as-sha256 performance, useful for Lodestar | ||
import {setHasher} from "@chainsafe/persistent-merkle-tree/hasher"; | ||
import {hasher} from "@chainsafe/persistent-merkle-tree/hasher/as-sha256"; | ||
setHasher(hasher); |
This file contains 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
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.
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.
Have you tried https://github.com/import-js/eslint-import-resolver-typescript ?
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.
No these lines were just copy pastad from lodestar's eslintrc