refactor: remove arity parameter from mt functions

This commit is contained in:
cedoor
2022-02-25 18:50:36 +01:00
parent 6885a38603
commit 8bc23d43ac
4 changed files with 10 additions and 22 deletions

View File

@@ -66,12 +66,11 @@ import { generateMerkleProof } from "@zk-kit/protocols"
const depth = 20
const zeroValue = BigInt(0)
const arity = 2
const identity = new ZkIdentity()
const identityCommitment = identity.genIdentityCommitment()
const identityCommitments = [BigInt(1), identityCommitment, BigInt(2)]
const merkleProof = generateMerkleProof(depth, zeroValue, arity, identityCommitments, 1)
const merkleProof = generateMerkleProof(depth, zeroValue, identityCommitments, 1)
```
### Creating Semaphore proofs

View File

@@ -29,17 +29,11 @@ export function genExternalNullifier(plaintext: string): string {
* Creates a Merkle tree.
* @param depth The depth of the tree.
* @param zeroValue The zero value of the tree.
* @param arity The number of leaves per node.
* @param leaves The list of the leaves of the tree.
* @returns The Merkle tree.
*/
export function generateMerkleTree(
depth: number,
zeroValue: StrBigInt,
arity: number,
leaves: StrBigInt[]
): IncrementalMerkleTree {
const tree = new IncrementalMerkleTree(poseidon, depth, zeroValue, arity)
export function generateMerkleTree(depth: number, zeroValue: StrBigInt, leaves: StrBigInt[]): IncrementalMerkleTree {
const tree = new IncrementalMerkleTree(poseidon, depth, zeroValue, 2)
for (const leaf of leaves) {
tree.insert(BigInt(leaf))
@@ -52,7 +46,6 @@ export function generateMerkleTree(
* Creates a Merkle proof.
* @param depth The depth of the tree.
* @param zeroValue The zero value of the tree.
* @param arity The number of leaves per node.
* @param leaves The list of the leaves of the tree.
* @param leaf The leaf for which Merkle proof should be created.
* @returns The Merkle proof.
@@ -60,13 +53,12 @@ export function generateMerkleTree(
export function generateMerkleProof(
depth: number,
zeroValue: StrBigInt,
arity: number,
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 tree = generateMerkleTree(depth, zeroValue, leaves)
const leafIndex = tree.leaves.indexOf(BigInt(leaf))
@@ -76,10 +68,7 @@ export function generateMerkleProof(
const merkleProof = tree.createProof(leafIndex)
if (arity === 2) {
// It makes it compatible with the parameter in the contracts.
merkleProof.siblings = merkleProof.siblings.map((s) => s[0])
}
merkleProof.siblings = merkleProof.siblings.map((s) => s[0])
return merkleProof
}

View File

@@ -41,7 +41,7 @@ describe("RLN", () => {
const epoch: string = genExternalNullifier("test-epoch")
const rlnIdentifier = RLN.genIdentifier()
const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment)
const merkleProof = generateMerkleProof(15, BigInt(0), leaves, identityCommitment)
const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier)
expect(typeof witness).toBe("object")
@@ -52,7 +52,7 @@ describe("RLN", () => {
const leaves = Object.assign([], identityCommitments)
leaves.push(zeroIdCommitment)
const fun = () => generateMerkleProof(15, zeroIdCommitment, 2, leaves, zeroIdCommitment)
const fun = () => generateMerkleProof(15, zeroIdCommitment, leaves, zeroIdCommitment)
expect(fun).toThrow("Can't generate a proof for a zero leaf")
})
@@ -90,7 +90,7 @@ describe("RLN", () => {
const epoch = genExternalNullifier("test-epoch")
const rlnIdentifier = RLN.genIdentifier()
const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment)
const merkleProof = generateMerkleProof(15, BigInt(0), leaves, identityCommitment)
const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier)
const vkeyPath = path.join(zkeyFiles, "rln", "verification_key.json")

View File

@@ -26,7 +26,7 @@ describe("Semaphore", () => {
const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)]
const signal = "0x111"
const merkleProof = generateMerkleProof(20, BigInt(0), 2, leaves, identityCommitment)
const merkleProof = generateMerkleProof(20, BigInt(0), leaves, identityCommitment)
const witness = Semaphore.genWitness(
identity.getTrapdoor(),
@@ -47,7 +47,7 @@ describe("Semaphore", () => {
const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)]
const signal = "0x111"
const merkleProof = generateMerkleProof(20, BigInt(0), 2, leaves, identityCommitment)
const merkleProof = generateMerkleProof(20, BigInt(0), leaves, identityCommitment)
const witness = Semaphore.genWitness(
identity.getTrapdoor(),