Merge pull request #17 from appliedzkp/fix/update-zero-handling

fix(incremental-merkle-tree): merkle tree supports zero leaves. Merkle proof fails for zero value

Former-commit-id: a961f107c8492c42eda3490e5f7aa46b4b0740c2 [formerly 9a10c338213a5c9825a49828a6a245386644aa8b] [formerly 60f9f95acc12b3d68e99c79f2860399c36e6a9ad [formerly 20bb55989daea35b64f06e95849118f12eca2320]] [formerly 7d21551201bfefa64ba3f53210c92fb7554ee0b4 [formerly 2df22ec07e8794443367938b5ef0f5984d9cd18c] [formerly 7d009ae578934827c5fef0ed8389961570f21bf8 [formerly 82ee0269a8]]]
Former-commit-id: 019791c05ecb53501b3784ff83ba2a710de90abc [formerly f0f7180505cea32fdb5b6f6420351570ce6e1561] [formerly 753d122f771435017881938840cd7d12b156470c [formerly 6723a76e5c9adedf9a007635cdebade8818b38c3]]
Former-commit-id: 457be8dbcd29c48ec70dfd0eb7565fe94a4f748a [formerly a35642b4ee9fb9915cd3fd8312072c337613ca2f]
Former-commit-id: a4fffb7a5501b879068886926f91741c6f368f72
This commit is contained in:
Nasi
2022-02-21 12:59:02 +01:00
committed by GitHub
6 changed files with 15 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@zk-kit/incremental-merkle-tree",
"version": "0.4.2",
"version": "0.4.3",
"description": "Incremental Merkle tree implementation in TypeScript.",
"license": "MIT",
"iife": "dist/index.js",

View File

@@ -11,10 +11,6 @@ export default function insert(
): Node {
checkParameter(leaf, "leaf", "number", "string", "bigint")
if (leaf === zeroes[0]) {
throw new Error("The leaf cannot be a zero value")
}
if (nodes[0].length >= arity ** depth) {
throw new Error("The tree is full")
}

View File

@@ -37,18 +37,12 @@ describe("Incremental Merkle Tree", () => {
expect(tree.arity).toEqual(arity)
})
it("Should not insert a zero leaf", () => {
const fun = () => tree.insert(BigInt(0))
expect(fun).toThrow("The leaf cannot be a zero value")
})
it("Should not insert a leaf in a full tree", () => {
const fullTree = new IncrementalMerkleTree(poseidon, 1, BigInt(0), 3)
fullTree.insert(BigInt(0))
fullTree.insert(BigInt(1))
fullTree.insert(BigInt(2))
fullTree.insert(BigInt(3))
const fun = () => fullTree.insert(BigInt(4))

View File

@@ -38,7 +38,7 @@
"@ethersproject/bytes": "^5.5.0",
"@ethersproject/solidity": "^5.5.0",
"@ethersproject/strings": "^5.5.0",
"@zk-kit/incremental-merkle-tree": "^0.4.2",
"@zk-kit/incremental-merkle-tree": "^0.4.3",
"circomlibjs": "0.0.8",
"ffjavascript": "0.2.38",
"snarkjs": "^0.4.13"

View File

@@ -64,6 +64,8 @@ export function generateMerkleProof(
leaves: StrBigInt[],
leaf: StrBigInt
): MerkleProof {
if (leaf === zeroValue) throw new Error("Can't generate a proof for a zero leaf")
const tree = generateMerkleTree(depth, zeroValue, arity, leaves)
const leafIndex = tree.leaves.indexOf(BigInt(leaf))

View File

@@ -48,6 +48,16 @@ describe("RLN", () => {
expect(typeof witness).toBe("object")
})
it("Should throw an exception for a zero leaf", () => {
const zeroIdCommitment = BigInt(0)
const leaves = Object.assign([], identityCommitments)
leaves.push(zeroIdCommitment)
const fun = () => generateMerkleProof(15, zeroIdCommitment, 2, leaves, zeroIdCommitment)
expect(fun).toThrow("Can't generate a proof for a zero leaf")
})
// eslint-disable-next-line jest/no-disabled-tests
it.skip("Should generate rln proof and verify it", async () => {
const identity = new ZkIdentity()