Skip to content

Commit

Permalink
Push Digest (#26)
Browse files Browse the repository at this point in the history
* push digest

* part end

* minor renaming

* mask partial end result
  • Loading branch information
Trinidadec authored Oct 27, 2023
1 parent 367b01f commit dd3dc69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 20 additions & 5 deletions tree.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,15 +34,28 @@ const push = state => nu8 => {
}

/** @type {(state: State) => bigint} */
const end = state => {
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 internalEnd = state => {
let last0 = 0n
for (let subTree of state) {
last0 = endSubTree(subTree)(last0)
}
return compress(last0) & mask244
return last0
}

export default {
push,
end
end,
partialEnd
}

0 comments on commit dd3dc69

Please sign in to comment.