From a1029db6e0bf7d056473664ef5944c7a5245bab6 Mon Sep 17 00:00:00 2001 From: Shandar Denys Date: Sat, 28 Oct 2023 00:01:56 +0300 Subject: [PATCH 1/4] push digest --- tree.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tree.mjs b/tree.mjs index 9c47f4b..157209c 100644 --- a/tree.mjs +++ b/tree.mjs @@ -13,9 +13,11 @@ const { compress } = sha224 const mask244 = ((1n << 224n) - 1n) /** @type {(state: State) => (nu8: number) => void} */ -const push = state => nu8 => { +const push = state => nu8 => pushDigest(state)(byteToDigest(nu8)) + +/** @type {(state: State) => (bu256: bigint) => void} */ +const pushDigest = state => last0 => { let i = 0 - let last0 = byteToDigest(nu8) while (true) { let subTree = state[i] if (subTree === undefined) { From 29bab4e182b7a0cf20aee5db7500c22e1cfc5b2e Mon Sep 17 00:00:00 2001 From: Shandar Denys Date: Sat, 28 Oct 2023 00:04:44 +0300 Subject: [PATCH 2/4] part end --- tree.mjs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tree.mjs b/tree.mjs index 157209c..2aec057 100644 --- a/tree.mjs +++ b/tree.mjs @@ -34,12 +34,15 @@ const pushDigest = state => last0 => { } /** @type {(state: State) => bigint} */ -const end = state => { +const end = state => compress(partEnd(state)) & mask244 + +/** @type {(state: State) => bigint} */ +const partEnd = state => { let last0 = 0n for (let subTree of state) { last0 = endSubTree(subTree)(last0) } - return compress(last0) & mask244 + return last0 } export default { From 9f361a04390a301d53f0f70936394bf3f1ff7a5b Mon Sep 17 00:00:00 2001 From: Shandar Denys Date: Sat, 28 Oct 2023 00:13:00 +0300 Subject: [PATCH 3/4] minor renaming --- index.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.mjs b/index.mjs index e8ffcc4..58c2f6d 100644 --- a/index.mjs +++ b/index.mjs @@ -5,9 +5,9 @@ const { toAddress } = base32 /** @type {(hash: string) => string} */ const getPath = hash => `${hash.substring(0, 2)}/${hash.substring(2, 4)}/${hash.substring(4)}` -/** @type {(root: string) => Buffer} */ -const getBuffer = root => { - const data = fs.readFileSync(`cdt0/${root}`) +/** @type {(path: string) => Buffer} */ +const getBuffer = path => { + const data = fs.readFileSync(`cdt0/${path}`) const tailLength = data[0] console.log(`tail length = ${tailLength}`) if (tailLength === 32) { From 3144caaed15d81cdc443cdfcaccd93f5ba87fb52 Mon Sep 17 00:00:00 2001 From: Shandar Denys Date: Sat, 28 Oct 2023 00:17:28 +0300 Subject: [PATCH 4/4] mask partial end result --- tree.mjs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tree.mjs b/tree.mjs index 2aec057..2478e71 100644 --- a/tree.mjs +++ b/tree.mjs @@ -34,10 +34,19 @@ const pushDigest = state => last0 => { } /** @type {(state: State) => bigint} */ -const end = state => compress(partEnd(state)) & mask244 +const end = state => compress(internalEnd(state)) & mask244 + +/** @type {(state: State) => bigint | null} */ +const partialEnd = state => { + let digest256 = internalEnd(state) + if (digest256 >> 224n === 0xffff_ffffn) { + return null + } + return digest256 & mask244 +} /** @type {(state: State) => bigint} */ -const partEnd = state => { +const internalEnd = state => { let last0 = 0n for (let subTree of state) { last0 = endSubTree(subTree)(last0) @@ -47,5 +56,6 @@ const partEnd = state => { export default { push, - end + end, + partialEnd } \ No newline at end of file