-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add swappable hasher, default to noble-hashes (#314)
* Add swappable hasher, default to noble-hashes * Use as-sha256 hasher in benchmarks * Add some comments * Use as-sha256 hasher in tests * Increase test timeout
- Loading branch information
1 parent
dbae75a
commit 4b44614
Showing
24 changed files
with
163 additions
and
86 deletions.
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(); | ||
|
||
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
4b44614
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.
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
Full benchmark results