Skip to content

Commit

Permalink
fix(lean-imt): After a sequence of updates a proof is invalid. (#355)
Browse files Browse the repository at this point in the history
* fix/lean-imt-update-member

* fix/lean-imt-update-member

* fix/lean-imt-update-member

* fix: lean-imt-update-member
When a member is removed from the tree (update a leaf with 0n) and then another member is updated, the root that is saved is not correct, so the proof that is generated is not valid.This was caused by a type validation failure.
This fix the behavior described above.
I detected this when I was testing in Semphore groups.

* fix(lean-imt): After a sequence of updates a proof is invalid.
due to a bug in type checking, the root is not generated correctly.

* fix(lean-imt): After a sequence of updates a proof is invalid.
due to a bug in type checking, the root is not generated correctly.
  • Loading branch information
Lauman authored Dec 4, 2024
1 parent 1b86231 commit 659d728
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit 659d728

Please sign in to comment.