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

Push Digest #26

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
}