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

fix(lean-imt): After a sequence of updates a proof is invalid. #355

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion packages/lean-imt/src/lean-imt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ export default class LeanIMT<N = bigint> {
// (like the 'insert' function).
const sibling = this._nodes[level][index + 1]

if (sibling) {
// If the sibling node does not exist, it means that the node at
// this level has the same value as its child. Therefore, there
// no hash to calculate.
if (sibling !== undefined) {
node = this._hash(node, sibling)
}
}
Expand Down
24 changes: 24 additions & 0 deletions packages/lean-imt/tests/lean-imt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ describe("Lean IMT", () => {
}
expect(LeanIMT.verifyProof(proof, badHash)).toBe(false)
})

it(`Should insert members,remove member,update member and verifyProof`, () => {
const tree = new LeanIMT(poseidon)

tree.insert(BigInt(1))

tree.insert(BigInt(2))

tree.insert(BigInt(3))

// Remove the third member.
tree.update(2, BigInt(0))

let proof = tree.generateProof(1)

expect(tree.verifyProof(proof)).toBe(true)

// Update the second member.
tree.update(1, BigInt(3))

// Validating a proof generated by the first member
proof = tree.generateProof(1)
expect(tree.verifyProof(proof)).toBe(true)
})
})

describe("# import/export", () => {
Expand Down
Loading