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

The incremental-merkle-tree can be constructed with any value, including zeros. The generation of
merkle proof will fail for zero value.


Former-commit-id: 9786bd018d1a1d118b25211b81bc130ce73dd72d [formerly 6bcb273b46528f9598d63e741b623e431f550279] [formerly fc6b6316f431a40fd5d3227984ee10ed9ae91512 [formerly 76fa7474e1bf580b65ef1ebf5cc2f3bc9a298fcf]] [formerly 1239a3573218f05bd7874fc93f307e912e256782 [formerly 7e2d839ace7e3b5ad9b8955bc8342e302cb17c21] [formerly e884bd5e471842ead2c6769e9c67b97f2dc7f9da [formerly d001f11730]]]
Former-commit-id: 4ca0db7c48be9f11a18219052fc18942669a31ba [formerly add55bb929f06f818db1b65382900c7005c3215f] [formerly 5576da9d8efcb2c5c1b8c198beeaf11e389c7d09 [formerly 8d92106155c937cf77d542f362cca882fb2560c7]]
Former-commit-id: 58e8a5c36bc1667d071b9e35e39a118bf2c9bec2 [formerly d822513d2e092cbc1ee293cfaae283abc15b94b6]
Former-commit-id: 76048b82db6f9d220622cf7b388ff5039247c817
This commit is contained in:
Nasi Jofce
2022-02-21 12:27:01 +01:00
parent 47a82a3983
commit d694534801
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()