Skip to content

Commit

Permalink
fix: rough out of hashtreeHasher
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Jun 7, 2024
1 parent 042c23b commit 2c4fa59
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
3 changes: 3 additions & 0 deletions packages/persistent-merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"@chainsafe/hashtree": "1.0.0",
"@noble/hashes": "^1.3.0"
},
"devDependencies": {
"@chainsafe/hashtree-darwin-arm64": "1.0.0"
},
"peerDependencies": {
"@chainsafe/hashtree-linux-x64-gnu": "1.0.0",
"@chainsafe/hashtree-linux-arm64-gnu": "1.0.0",
Expand Down
12 changes: 8 additions & 4 deletions packages/persistent-merkle-tree/src/hasher/hashtree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {HashComputation, Node} from "../node";

export const hasher: Hasher = {
digest64(obj1: Uint8Array, obj2: Uint8Array): Uint8Array {
return hash(Uint8Array.of(obj1, obj2));
return hash(Uint8Array.from([...obj1, ...obj2]));
},
digest64HashObjects(obj1: HashObject, obj2: HashObject): HashObject {
return byteArrayToHashObject(hasher.digest64(hashObjectToByteArray(obj1), hashObjectToByteArray(obj2)));
const input1 = Uint8Array.from(new Array<number>(32));
const input2 = Uint8Array.from(new Array<number>(32));
hashObjectToByteArray(obj1, input1, 0);
hashObjectToByteArray(obj2, input2, 0);
return byteArrayToHashObject(hasher.digest64(input1, input2));
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
batchHashObjects(inputs: HashObject[]): HashObject[] {
Expand All @@ -23,7 +27,7 @@ export const hasher: Hasher = {
}

// size input array to 2 HashObject per computation * 32 bytes per object
const input: Uint8Array = Uint8Array.of(new Array(hcArr.length * 2 * 32));
const input: Uint8Array = Uint8Array.from(new Array(hcArr.length * 2 * 32));
const output: Node[] = [];
for (const [i, hc] of hcArr.entries()) {
const offset = (i - 1) * 64; // zero index * 2 leafs * 32 bytes
Expand All @@ -36,7 +40,7 @@ export const hasher: Hasher = {

for (const [i, out] of output.entries()) {
const offset = (i - 1) * 32;
out.applyHash(result.slice(offset, offset + 32));
out.applyHash(byteArrayToHashObject(result.slice(offset, offset + 32)));
}
}
},
Expand Down
48 changes: 20 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,23 @@
core-js "2.6.10"
require-resolve "0.0.2"

"@chainsafe/hashtree-darwin-arm64@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@chainsafe/hashtree-darwin-arm64/-/hashtree-darwin-arm64-1.0.0.tgz#a9fb6b70eaf1f715c14caff22a64152a1903258e"
integrity sha512-duJfn57lUXkSedvEisEhXNJcUZAZLKY3D3t5Jx2EUfNS1PpVLM9k5oBG2cjolyNso2n94LJGlyYKFMrPoPig1w==

"@chainsafe/hashtree-linux-arm64-gnu@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@chainsafe/hashtree-linux-arm64-gnu/-/hashtree-linux-arm64-gnu-1.0.0.tgz#168db259636261d9f3612354cad9f730a4be7110"
integrity sha512-XdYEV6z503Oxa7+mPtUEq9KoKfBAs0BcxGaRiDttCbZK2/J7CcTlobBGd7KMxJ/dQ4IUonaXsob0BnXBcrlwuw==

"@chainsafe/hashtree@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@chainsafe/hashtree/-/hashtree-1.0.0.tgz#529439fb07299758ca5bbe69a00d1dc4ad83a949"
integrity sha512-qft0MZiLl5jbe8omZaSp1vQ2YCO9qCb262+5qD1vsgN6l1ga3ZFKLyNI6xvwbhC7ZnzZd46vr+p+KvdUIgruOw==
optionalDependencies:
"@chainsafe/hashtree-linux-arm64-gnu" "1.0.0"

"@chainsafe/ssz@^0.15.1":
version "0.15.1"
resolved "https://registry.yarnpkg.com/@chainsafe/ssz/-/ssz-0.15.1.tgz#008a711c3bcdc0d207cd4be15108870b0b1c60c0"
Expand Down Expand Up @@ -11283,7 +11300,7 @@ streamroller@^3.1.5:
debug "^4.3.4"
fs-extra "^8.1.0"

"string-width-cjs@npm:string-width@^4.2.0":
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand All @@ -11300,15 +11317,6 @@ streamroller@^3.1.5:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^3.0.0, string-width@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-3.1.0.tgz#22767be21b62af1081574306f69ac51b62203961"
Expand Down Expand Up @@ -11387,7 +11395,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -11415,13 +11423,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -12616,7 +12617,7 @@ workerpool@6.1.0:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.1.0.tgz#a8e038b4c94569596852de7a8ea4228eefdeb37b"
integrity sha512-toV7q9rWNYha963Pl/qyeZ6wG+3nnsyvolaNUS8+R5Wtw6qJPTxIlOP1ZSvcGhEJw+l3HMMmtiNo9Gl61G4GVg==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -12643,15 +12644,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit 2c4fa59

Please sign in to comment.