From 820778e368497832a0be1146b7477ff33cd4295d Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 09:59:29 -0300 Subject: [PATCH 1/6] fix/lean-imt-update-member --- packages/lean-imt/src/lean-imt.ts | 2 +- packages/lean-imt/tests/lean-imt.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/lean-imt/src/lean-imt.ts b/packages/lean-imt/src/lean-imt.ts index f453ff1dc..44144a3cf 100644 --- a/packages/lean-imt/src/lean-imt.ts +++ b/packages/lean-imt/src/lean-imt.ts @@ -222,7 +222,7 @@ export default class LeanIMT { // (like the 'insert' function). const sibling = this._nodes[level][index + 1] - if (sibling) { + if (sibling !== undefined) { node = this._hash(node, sibling) } } diff --git a/packages/lean-imt/tests/lean-imt.test.ts b/packages/lean-imt/tests/lean-imt.test.ts index 38212c1c5..4fb21b0b8 100644 --- a/packages/lean-imt/tests/lean-imt.test.ts +++ b/packages/lean-imt/tests/lean-imt.test.ts @@ -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(2)) + + // Validating a proof generated by the first member + proof = tree.generateProof(1) + expect(tree.verifyProof(proof)).toBe(true) + }) }) describe("# import/export", () => { From e988b08a7530453b67375d773a04b4ef635184fa Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 11:32:29 -0300 Subject: [PATCH 2/6] fix/lean-imt-update-member --- packages/lean-imt/src/lean-imt.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/lean-imt/src/lean-imt.ts b/packages/lean-imt/src/lean-imt.ts index 44144a3cf..f153ea198 100644 --- a/packages/lean-imt/src/lean-imt.ts +++ b/packages/lean-imt/src/lean-imt.ts @@ -222,6 +222,9 @@ export default class LeanIMT { // (like the 'insert' function). const sibling = this._nodes[level][index + 1] + // 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) } From 554e821447b69cb076cc3340bfe231069411a2ca Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 11:35:42 -0300 Subject: [PATCH 3/6] fix/lean-imt-update-member --- packages/lean-imt/tests/lean-imt.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lean-imt/tests/lean-imt.test.ts b/packages/lean-imt/tests/lean-imt.test.ts index 4fb21b0b8..7505d691c 100644 --- a/packages/lean-imt/tests/lean-imt.test.ts +++ b/packages/lean-imt/tests/lean-imt.test.ts @@ -461,7 +461,7 @@ describe("Lean IMT", () => { expect(tree.verifyProof(proof)).toBe(true) // Update the second member. - tree.update(1, BigInt(2)) + tree.update(1, BigInt(3)) // Validating a proof generated by the first member proof = tree.generateProof(1) From f559a28fa335b20b656c72af2b839063dbedf76c Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 09:59:29 -0300 Subject: [PATCH 4/6] 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. --- packages/lean-imt/src/lean-imt.ts | 2 +- packages/lean-imt/tests/lean-imt.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/lean-imt/src/lean-imt.ts b/packages/lean-imt/src/lean-imt.ts index f453ff1dc..44144a3cf 100644 --- a/packages/lean-imt/src/lean-imt.ts +++ b/packages/lean-imt/src/lean-imt.ts @@ -222,7 +222,7 @@ export default class LeanIMT { // (like the 'insert' function). const sibling = this._nodes[level][index + 1] - if (sibling) { + if (sibling !== undefined) { node = this._hash(node, sibling) } } diff --git a/packages/lean-imt/tests/lean-imt.test.ts b/packages/lean-imt/tests/lean-imt.test.ts index 38212c1c5..4fb21b0b8 100644 --- a/packages/lean-imt/tests/lean-imt.test.ts +++ b/packages/lean-imt/tests/lean-imt.test.ts @@ -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(2)) + + // Validating a proof generated by the first member + proof = tree.generateProof(1) + expect(tree.verifyProof(proof)).toBe(true) + }) }) describe("# import/export", () => { From c484b6058397243d07f4686e5f605b24df23453b Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 11:32:29 -0300 Subject: [PATCH 5/6] 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. --- packages/lean-imt/src/lean-imt.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/lean-imt/src/lean-imt.ts b/packages/lean-imt/src/lean-imt.ts index 44144a3cf..f153ea198 100644 --- a/packages/lean-imt/src/lean-imt.ts +++ b/packages/lean-imt/src/lean-imt.ts @@ -222,6 +222,9 @@ export default class LeanIMT { // (like the 'insert' function). const sibling = this._nodes[level][index + 1] + // 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) } From fc90f137cd4f83041b5c5bd948ed782610e8bc0a Mon Sep 17 00:00:00 2001 From: Lautaro Di Sanza Date: Mon, 2 Dec 2024 11:35:42 -0300 Subject: [PATCH 6/6] 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. --- packages/lean-imt/tests/lean-imt.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lean-imt/tests/lean-imt.test.ts b/packages/lean-imt/tests/lean-imt.test.ts index 4fb21b0b8..7505d691c 100644 --- a/packages/lean-imt/tests/lean-imt.test.ts +++ b/packages/lean-imt/tests/lean-imt.test.ts @@ -461,7 +461,7 @@ describe("Lean IMT", () => { expect(tree.verifyProof(proof)).toBe(true) // Update the second member. - tree.update(1, BigInt(2)) + tree.update(1, BigInt(3)) // Validating a proof generated by the first member proof = tree.generateProof(1)