mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
refactor: remove arity parameter from mt functions
Former-commit-id: be4dc8a721b2859276a8f820b4261fa0e9817714 [formerly 9e29c730f8333e1d7f905706b3646b4986f5ac17] [formerly ba28450065e767a3ba0a9126009d63444460af70 [formerly 604ae430f3c97a3a74fae613a43c4ce20ee68f2c]] [formerly d7f8ec4f7f8d252aa1d3d4de71942575fcaeaf99 [formerly b777595daf418d6bfe886f90c3ba8623a6ed89df] [formerly 8e3f37319640acda556c80f7603c120eb98ac60e [formerly 8bc23d43ac]]]
Former-commit-id: d3672ba986685174d7b56bd0691e26f31ee90068 [formerly 505f9d6c7bb5f61c35d1a6951a1dec75591633a7] [formerly 846ff76039920c91c9cf5924468058b73843e422 [formerly 45b05069d210b18e5ed6d5940074781725e14bdc]]
Former-commit-id: c07ca2f8095f41993ca4726d58e4d1ab55402143 [formerly 37d28b5ba3597d38b6deb64b054f801e4922ea37]
Former-commit-id: 24400d0825b6086db394e8a3b6fe5242c5b8b805
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user