Skip to content
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 5 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ module.exports = {

// Prevents accidentally pushing a commit with .only in Mocha tests
"no-only-tests/no-only-tests": "error",

// TEMP Disabled while eslint-plugin-import support ESM (Typescript does support it) https://github.com/import-js/eslint-plugin-import/issues/2170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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

"import/no-unresolved": "off",
},
overrides: [
{
Expand Down
13 changes: 13 additions & 0 deletions packages/as-sha256/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
"url": "git+https://github.com/chainsafe/as-sha256.git"
},
"main": "lib/index.js",
"exports": {
".": "./lib/index.js",
"./hashObject": "./lib/hashObject.js"
},
"typesVersions": {
"*": {
"*": [
"*",
"lib/*",
"lib/*/index"
]
}
},
"types": "lib/index.d.ts",
"files": [
"lib",
Expand Down
18 changes: 17 additions & 1 deletion packages/persistent-merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
"version": "0.5.0",
"description": "Merkle tree implemented as a persistent datastructure",
"main": "lib/index.js",
"exports": {
".": "./lib/index.js",
"./hasher": "./lib/hasher/index.js",
"./hasher/as-sha256": "./lib/hasher/as-sha256.js",
"./hasher/noble": "./lib/hasher/noble.js"
},
"typesVersions": {
"*": {
"*": [
"*",
"lib/*",
"lib/*/index"
]
}
},
"files": [
"lib"
],
Expand Down Expand Up @@ -35,6 +50,7 @@
},
"homepage": "https://github.com/ChainSafe/persistent-merkle-tree#readme",
"dependencies": {
"@chainsafe/as-sha256": "^0.3.1"
"@chainsafe/as-sha256": "^0.3.1",
"@noble/hashes": "^1.3.0"
}
}
40 changes: 0 additions & 40 deletions packages/persistent-merkle-tree/src/hash.ts

This file was deleted.

7 changes: 7 additions & 0 deletions packages/persistent-merkle-tree/src/hasher/as-sha256.ts
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,
};
12 changes: 12 additions & 0 deletions packages/persistent-merkle-tree/src/hasher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Hasher} from "./types";
import {hasher as nobleHasher} from "./noble";

export {HashObject} from "@chainsafe/as-sha256/hashObject";
export * from "./types";
export * from "./util";

export let hasher: Hasher = nobleHasher;

export function setHasher(newHasher: Hasher): void {
hasher = newHasher;
}
10 changes: 10 additions & 0 deletions packages/persistent-merkle-tree/src/hasher/noble.ts
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))),
};
6 changes: 6 additions & 0 deletions packages/persistent-merkle-tree/src/hasher/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {HashObject} from "@chainsafe/as-sha256/hashObject";

export type Hasher = {
digest64(a32Bytes: Uint8Array, b32Bytes: Uint8Array): Uint8Array;
digest64HashObjects(a: HashObject, b: HashObject): HashObject;
};
11 changes: 11 additions & 0 deletions packages/persistent-merkle-tree/src/hasher/util.ts
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);
}
2 changes: 1 addition & 1 deletion packages/persistent-merkle-tree/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./gindex";
export * from "./hash";
export * from "./hasher";
export * from "./node";
export * from "./packedNode";
export * from "./proof";
Expand Down
6 changes: 3 additions & 3 deletions packages/persistent-merkle-tree/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HashObject} from "@chainsafe/as-sha256";
import {hashObjectToUint8Array, hashTwoObjects, uint8ArrayToHashObject} from "./hash";
import {HashObject} from "@chainsafe/as-sha256/hashObject";
import {hashObjectToUint8Array, hasher, uint8ArrayToHashObject} from "./hasher";

const TWO_POWER_32 = 2 ** 32;

Expand Down Expand Up @@ -72,7 +72,7 @@ export class BranchNode extends Node {

get rootHashObject(): HashObject {
if (this.h0 === null) {
super.applyHash(hashTwoObjects(this.left.rootHashObject, this.right.rootHashObject));
super.applyHash(hasher.digest64HashObjects(this.left.rootHashObject, this.right.rootHashObject));
}
return this;
}
Expand Down
26 changes: 0 additions & 26 deletions packages/persistent-merkle-tree/test/perf/hash.test.ts

This file was deleted.

33 changes: 33 additions & 0 deletions packages/persistent-merkle-tree/test/perf/hasher.test.ts
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);
});
}
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from "chai";
import {uint8ArrayToHashObject, hash, hashTwoObjects, hashObjectToUint8Array} from "../src/hash";
import {uint8ArrayToHashObject, hasher, hashObjectToUint8Array} from "../../src/hasher";

describe("hash", function () {
it("hash and hashTwoObjects should be the same", () => {
describe("hasher", function () {
it("hasher methods should be the same", () => {
const root1 = Buffer.alloc(32, 1);
const root2 = Buffer.alloc(32, 2);
const root = hash(root1, root2);
const root = hasher.digest64(root1, root2);

const obj1 = uint8ArrayToHashObject(root1);
const obj2 = uint8ArrayToHashObject(root2);
const obj = hashTwoObjects(obj1, obj2);
const obj = hasher.digest64HashObjects(obj1, obj2);
const newRoot = hashObjectToUint8Array(obj);
expect(newRoot).to.be.deep.equal(root, "hash and hash2 is not equal");
});
Expand Down
2 changes: 1 addition & 1 deletion packages/ssz/src/branchNodeStruct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashObject} from "@chainsafe/as-sha256";
import {HashObject} from "@chainsafe/as-sha256/hashObject";
import {hashObjectToUint8Array, Node} from "@chainsafe/persistent-merkle-tree";

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/src/util/merkleize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {digest2Bytes32} from "@chainsafe/as-sha256";
import {hasher} from "@chainsafe/persistent-merkle-tree/hasher";
import {zeroHash} from "./zeros";

export function hash64(bytes32A: Uint8Array, bytes32B: Uint8Array): Uint8Array {
return digest2Bytes32(bytes32A, bytes32B);
return hasher.digest64(bytes32A, bytes32B);
}

export function merkleize(chunks: Uint8Array[], padFor: number): Uint8Array {
Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/src/util/zeros.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {digest2Bytes32} from "@chainsafe/as-sha256";
import {hasher} from "@chainsafe/persistent-merkle-tree/hasher";

// create array of "zero hashes", successively hashed zero chunks
const zeroHashes = [new Uint8Array(32)];

export function zeroHash(depth: number): Uint8Array {
if (depth >= zeroHashes.length) {
for (let i = zeroHashes.length; i <= depth; i++) {
zeroHashes[i] = digest2Bytes32(zeroHashes[i - 1], zeroHashes[i - 1]);
zeroHashes[i] = hasher.digest64(zeroHashes[i - 1], zeroHashes[i - 1]);
}
}
return zeroHashes[depth];
Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/test/perf/eth2/hashTreeRoot.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {itBench} from "@dapplion/benchmark";
import {hashTwoObjects, uint8ArrayToHashObject} from "@chainsafe/persistent-merkle-tree";
import {hasher, uint8ArrayToHashObject} from "@chainsafe/persistent-merkle-tree";
import * as sszPhase0 from "../../lodestarTypes/phase0/sszTypes";
import * as sszAltair from "../../lodestarTypes/altair/sszTypes";
import {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe("HashTreeRoot individual components", () => {
});

itBench(`hashTwoObjects x${count}`, () => {
for (let i = 0; i < count; i++) hashTwoObjects(ho, ho);
for (let i = 0; i < count; i++) hasher.digest64HashObjects(ho, ho);
});
}

Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,7 @@ __metadata:
resolution: "@chainsafe/persistent-merkle-tree@workspace:packages/persistent-merkle-tree"
dependencies:
"@chainsafe/as-sha256": ^0.3.1
"@noble/hashes": ^1.3.0
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1920,6 +1921,13 @@ __metadata:
languageName: node
linkType: hard

"@noble/hashes@npm:^1.3.0":
version: 1.3.0
resolution: "@noble/hashes@npm:1.3.0"
checksum: 06d27f9e7dfbe379dbfb02073358aa2c25a6363acc3a2b09e074f0f47f630da1d5a81c731696faa9407594371b61a27c496076fdecc2f60fb1c6a24247a5dbef
languageName: node
linkType: hard

"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"
Expand Down