@semaphore-protocol/proof 3.11.1 → 3.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 3.11.1
3
+ * @version 3.12.1
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -120,7 +120,7 @@ function poseidon2(inputs) {
120
120
  function hash(message) {
121
121
  message = BigNumber.from(message).toTwos(256).toHexString();
122
122
  message = zeroPad(message, 32);
123
- return BigInt(keccak256(message)) >> BigInt(8);
123
+ return (BigInt(keccak256(message)) >> BigInt(8)).toString();
124
124
  }
125
125
 
126
126
  /**
@@ -199,19 +199,19 @@ function __generator(thisArg, body) {
199
199
 
200
200
  /**
201
201
  * Packs a proof into a format compatible with Semaphore.
202
- * @param originalProof The proof generated with SnarkJS.
202
+ * @param proof The Groth16 proof generated with SnarkJS.
203
203
  * @returns The proof compatible with Semaphore.
204
204
  */
205
- function packProof(originalProof) {
205
+ function packProof(proof) {
206
206
  return [
207
- originalProof.pi_a[0],
208
- originalProof.pi_a[1],
209
- originalProof.pi_b[0][1],
210
- originalProof.pi_b[0][0],
211
- originalProof.pi_b[1][1],
212
- originalProof.pi_b[1][0],
213
- originalProof.pi_c[0],
214
- originalProof.pi_c[1]
207
+ proof.pi_a[0],
208
+ proof.pi_a[1],
209
+ proof.pi_b[0][1],
210
+ proof.pi_b[0][0],
211
+ proof.pi_b[1][1],
212
+ proof.pi_b[1][0],
213
+ proof.pi_c[0],
214
+ proof.pi_c[1]
215
215
  ];
216
216
  }
217
217
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 3.11.1
3
+ * @version 3.12.1
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -124,7 +124,7 @@ function poseidon2(inputs) {
124
124
  function hash(message) {
125
125
  message = bignumber.BigNumber.from(message).toTwos(256).toHexString();
126
126
  message = bytes.zeroPad(message, 32);
127
- return BigInt(keccak256.keccak256(message)) >> BigInt(8);
127
+ return (BigInt(keccak256.keccak256(message)) >> BigInt(8)).toString();
128
128
  }
129
129
 
130
130
  /**
@@ -203,19 +203,19 @@ function __generator(thisArg, body) {
203
203
 
204
204
  /**
205
205
  * Packs a proof into a format compatible with Semaphore.
206
- * @param originalProof The proof generated with SnarkJS.
206
+ * @param proof The Groth16 proof generated with SnarkJS.
207
207
  * @returns The proof compatible with Semaphore.
208
208
  */
209
- function packProof(originalProof) {
209
+ function packProof(proof) {
210
210
  return [
211
- originalProof.pi_a[0],
212
- originalProof.pi_a[1],
213
- originalProof.pi_b[0][1],
214
- originalProof.pi_b[0][0],
215
- originalProof.pi_b[1][1],
216
- originalProof.pi_b[1][0],
217
- originalProof.pi_c[0],
218
- originalProof.pi_c[1]
211
+ proof.pi_a[0],
212
+ proof.pi_a[1],
213
+ proof.pi_b[0][1],
214
+ proof.pi_b[0][0],
215
+ proof.pi_b[1][1],
216
+ proof.pi_b[1][0],
217
+ proof.pi_c[0],
218
+ proof.pi_c[1]
219
219
  ];
220
220
  }
221
221
 
@@ -2,7 +2,7 @@ import { BytesLike, Hexable } from "@ethersproject/bytes";
2
2
  import { Group } from "@semaphore-protocol/group";
3
3
  import type { Identity } from "@semaphore-protocol/identity";
4
4
  import { MerkleProof } from "@zk-kit/incremental-merkle-tree";
5
- import { FullProof, SnarkArtifacts } from "./types";
5
+ import { SemaphoreProof, SnarkArtifacts } from "./types";
6
6
  /**
7
7
  * Generates a Semaphore proof.
8
8
  * @param identity The Semaphore identity.
@@ -12,4 +12,4 @@ import { FullProof, SnarkArtifacts } from "./types";
12
12
  * @param snarkArtifacts The SNARK artifacts.
13
13
  * @returns The Semaphore proof ready to be verified.
14
14
  */
15
- export default function generateProof({ trapdoor, nullifier, commitment }: Identity, groupOrMerkleProof: Group | MerkleProof, externalNullifier: BytesLike | Hexable | number | bigint, signal: BytesLike | Hexable | number | bigint, snarkArtifacts?: SnarkArtifacts): Promise<FullProof>;
15
+ export default function generateProof({ trapdoor, nullifier, commitment }: Identity, groupOrMerkleProof: Group | MerkleProof, externalNullifier: BytesLike | Hexable | number | bigint, signal: BytesLike | Hexable | number | bigint, snarkArtifacts?: SnarkArtifacts): Promise<SemaphoreProof>;
@@ -1,7 +1,8 @@
1
1
  import { BytesLike, Hexable } from "@ethersproject/bytes";
2
+ import { NumericString } from "snarkjs";
2
3
  /**
3
4
  * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
4
5
  * @param message The message to be hashed.
5
6
  * @returns The message digest.
6
7
  */
7
- export default function hash(message: BytesLike | Hexable | number | bigint): bigint;
8
+ export default function hash(message: BytesLike | Hexable | number | bigint): NumericString;
@@ -1,7 +1,8 @@
1
- import { SnarkJSProof, Proof } from "./types";
1
+ import { Groth16Proof } from "snarkjs";
2
+ import { PackedProof } from "./types";
2
3
  /**
3
4
  * Packs a proof into a format compatible with Semaphore.
4
- * @param originalProof The proof generated with SnarkJS.
5
+ * @param proof The Groth16 proof generated with SnarkJS.
5
6
  * @returns The proof compatible with Semaphore.
6
7
  */
7
- export default function packProof(originalProof: SnarkJSProof): Proof;
8
+ export default function packProof(proof: Groth16Proof): PackedProof;
@@ -1,29 +1,22 @@
1
- export type BigNumberish = string | bigint;
1
+ import type { NumericString } from "snarkjs";
2
2
  export type SnarkArtifacts = {
3
3
  wasmFilePath: string;
4
4
  zkeyFilePath: string;
5
5
  };
6
- export type SnarkJSProof = {
7
- pi_a: BigNumberish[];
8
- pi_b: BigNumberish[][];
9
- pi_c: BigNumberish[];
10
- protocol: string;
11
- curve: string;
6
+ export type SemaphoreProof = {
7
+ merkleTreeRoot: NumericString;
8
+ signal: NumericString;
9
+ nullifierHash: NumericString;
10
+ externalNullifier: NumericString;
11
+ proof: PackedProof;
12
12
  };
13
- export type FullProof = {
14
- merkleTreeRoot: BigNumberish;
15
- signal: BigNumberish;
16
- nullifierHash: BigNumberish;
17
- externalNullifier: BigNumberish;
18
- proof: Proof;
19
- };
20
- export type Proof = [
21
- BigNumberish,
22
- BigNumberish,
23
- BigNumberish,
24
- BigNumberish,
25
- BigNumberish,
26
- BigNumberish,
27
- BigNumberish,
28
- BigNumberish
13
+ export type PackedProof = [
14
+ NumericString,
15
+ NumericString,
16
+ NumericString,
17
+ NumericString,
18
+ NumericString,
19
+ NumericString,
20
+ NumericString,
21
+ NumericString
29
22
  ];
@@ -1,7 +1,8 @@
1
- import { SnarkJSProof, Proof } from "./types";
1
+ import { Groth16Proof } from "snarkjs";
2
+ import { PackedProof } from "./types";
2
3
  /**
3
4
  * Unpacks a proof into its original form.
4
5
  * @param proof The proof compatible with Semaphore.
5
6
  * @returns The proof compatible with SnarkJS.
6
7
  */
7
- export default function unpackProof(proof: Proof): SnarkJSProof;
8
+ export default function unpackProof(proof: PackedProof): Groth16Proof;
@@ -1,8 +1,8 @@
1
- import { FullProof } from "./types";
1
+ import { SemaphoreProof } from "./types";
2
2
  /**
3
3
  * Verifies a Semaphore proof.
4
4
  * @param fullProof The SnarkJS Semaphore proof.
5
5
  * @param treeDepth The Merkle tree depth.
6
6
  * @returns True if the proof is valid, false otherwise.
7
7
  */
8
- export default function verifyProof({ merkleTreeRoot, nullifierHash, externalNullifier, signal, proof }: FullProof, treeDepth: number): Promise<boolean>;
8
+ export default function verifyProof({ merkleTreeRoot, nullifierHash, externalNullifier, signal, proof }: SemaphoreProof, treeDepth: number): Promise<boolean>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/proof",
3
- "version": "3.11.1",
3
+ "version": "3.12.1",
4
4
  "description": "A library to generate and verify Semaphore proofs.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -23,8 +23,7 @@
23
23
  "scripts": {
24
24
  "build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
25
25
  "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
26
- "prepublishOnly": "yarn build",
27
- "docs": "typedoc src/index.ts --out ../../docs/proof"
26
+ "prepublishOnly": "yarn build"
28
27
  },
29
28
  "publishConfig": {
30
29
  "access": "public"
@@ -36,12 +35,11 @@
36
35
  "ffjavascript": "^0.2.54",
37
36
  "poseidon-lite": "^0.2.0",
38
37
  "rollup-plugin-cleanup": "^3.2.1",
39
- "rollup-plugin-typescript2": "^0.31.2",
40
- "typedoc": "^0.22.11"
38
+ "rollup-plugin-typescript2": "^0.31.2"
41
39
  },
42
40
  "peerDependencies": {
43
- "@semaphore-protocol/group": "3.11.1",
44
- "@semaphore-protocol/identity": "3.11.1"
41
+ "@semaphore-protocol/group": "3.12.1",
42
+ "@semaphore-protocol/identity": "3.12.1"
45
43
  },
46
44
  "dependencies": {
47
45
  "@ethersproject/bignumber": "^5.5.0",
@@ -49,6 +47,6 @@
49
47
  "@ethersproject/keccak256": "^5.7.0",
50
48
  "@ethersproject/strings": "^5.5.0",
51
49
  "@zk-kit/incremental-merkle-tree": "0.4.3",
52
- "snarkjs": "0.4.16"
50
+ "snarkjs": "0.7.1"
53
51
  }
54
52
  }
@@ -3,10 +3,10 @@ import { BytesLike, Hexable } from "@ethersproject/bytes"
3
3
  import { Group } from "@semaphore-protocol/group"
4
4
  import type { Identity } from "@semaphore-protocol/identity"
5
5
  import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
6
- import { groth16 } from "snarkjs"
6
+ import { groth16, NumericString } from "snarkjs"
7
7
  import hash from "./hash"
8
8
  import packProof from "./packProof"
9
- import { FullProof, SnarkArtifacts } from "./types"
9
+ import { SemaphoreProof, SnarkArtifacts } from "./types"
10
10
 
11
11
  /**
12
12
  * Generates a Semaphore proof.
@@ -23,7 +23,7 @@ export default async function generateProof(
23
23
  externalNullifier: BytesLike | Hexable | number | bigint,
24
24
  signal: BytesLike | Hexable | number | bigint,
25
25
  snarkArtifacts?: SnarkArtifacts
26
- ): Promise<FullProof> {
26
+ ): Promise<SemaphoreProof> {
27
27
  let merkleProof: MerkleProof
28
28
 
29
29
  if ("depth" in groupOrMerkleProof) {
@@ -61,8 +61,8 @@ export default async function generateProof(
61
61
  return {
62
62
  merkleTreeRoot: publicSignals[0],
63
63
  nullifierHash: publicSignals[1],
64
- signal: BigNumber.from(signal).toString(),
65
- externalNullifier: BigNumber.from(externalNullifier).toString(),
64
+ signal: BigNumber.from(signal).toString() as NumericString,
65
+ externalNullifier: BigNumber.from(externalNullifier).toString() as NumericString,
66
66
  proof: packProof(proof)
67
67
  }
68
68
  }
package/src/hash.ts CHANGED
@@ -1,15 +1,16 @@
1
1
  import { BigNumber } from "@ethersproject/bignumber"
2
2
  import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes"
3
3
  import { keccak256 } from "@ethersproject/keccak256"
4
+ import { NumericString } from "snarkjs"
4
5
 
5
6
  /**
6
7
  * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
7
8
  * @param message The message to be hashed.
8
9
  * @returns The message digest.
9
10
  */
10
- export default function hash(message: BytesLike | Hexable | number | bigint): bigint {
11
+ export default function hash(message: BytesLike | Hexable | number | bigint): NumericString {
11
12
  message = BigNumber.from(message).toTwos(256).toHexString()
12
13
  message = zeroPad(message, 32)
13
14
 
14
- return BigInt(keccak256(message)) >> BigInt(8)
15
+ return (BigInt(keccak256(message)) >> BigInt(8)).toString() as NumericString
15
16
  }
package/src/index.test.ts CHANGED
@@ -6,7 +6,7 @@ import calculateNullifierHash from "./calculateNullifierHash"
6
6
  import generateProof from "./generateProof"
7
7
  import hash from "./hash"
8
8
  import packProof from "./packProof"
9
- import { FullProof } from "./types"
9
+ import { SemaphoreProof } from "./types"
10
10
  import unpackProof from "./unpackProof"
11
11
  import verifyProof from "./verifyProof"
12
12
 
@@ -21,7 +21,7 @@ describe("Proof", () => {
21
21
 
22
22
  const identity = new Identity()
23
23
 
24
- let fullProof: FullProof
24
+ let fullProof: SemaphoreProof
25
25
  let curve: any
26
26
 
27
27
  beforeAll(async () => {
@@ -34,9 +34,7 @@ describe("Proof", () => {
34
34
 
35
35
  describe("# generateProof", () => {
36
36
  it("Should not generate Semaphore proofs if the identity is not part of the group", async () => {
37
- const group = new Group(treeDepth)
38
-
39
- group.addMembers([BigInt(1), BigInt(2)])
37
+ const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2)])
40
38
 
41
39
  const fun = () =>
42
40
  generateProof(identity, group, externalNullifier, signal, {
@@ -48,9 +46,7 @@ describe("Proof", () => {
48
46
  })
49
47
 
50
48
  it("Should not generate a Semaphore proof with default snark artifacts with Node.js", async () => {
51
- const group = new Group(treeDepth)
52
-
53
- group.addMembers([BigInt(1), BigInt(2), identity.commitment])
49
+ const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
54
50
 
55
51
  const fun = () => generateProof(identity, group, externalNullifier, signal)
56
52
 
@@ -58,9 +54,7 @@ describe("Proof", () => {
58
54
  })
59
55
 
60
56
  it("Should generate a Semaphore proof passing a group as parameter", async () => {
61
- const group = new Group(treeDepth)
62
-
63
- group.addMembers([BigInt(1), BigInt(2), identity.commitment])
57
+ const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
64
58
 
65
59
  fullProof = await generateProof(identity, group, externalNullifier, signal, {
66
60
  wasmFilePath,
@@ -72,9 +66,7 @@ describe("Proof", () => {
72
66
  }, 20000)
73
67
 
74
68
  it("Should generate a Semaphore proof passing a Merkle proof as parameter", async () => {
75
- const group = new Group(treeDepth)
76
-
77
- group.addMembers([BigInt(1), BigInt(2), identity.commitment])
69
+ const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
78
70
 
79
71
  fullProof = await generateProof(identity, group.generateMerkleProof(2), externalNullifier, signal, {
80
72
  wasmFilePath,
@@ -104,47 +96,35 @@ describe("Proof", () => {
104
96
  it("Should hash the signal value correctly", async () => {
105
97
  const signalHash = hash(signal)
106
98
 
107
- expect(signalHash.toString()).toBe(
108
- "8665846418922331996225934941481656421248110469944536651334918563951783029"
109
- )
99
+ expect(signalHash).toBe("8665846418922331996225934941481656421248110469944536651334918563951783029")
110
100
  })
111
101
 
112
102
  it("Should hash the external nullifier value correctly", async () => {
113
103
  const externalNullifierHash = hash(externalNullifier)
114
104
 
115
- expect(externalNullifierHash.toString()).toBe(
105
+ expect(externalNullifierHash).toBe(
116
106
  "244178201824278269437519042830883072613014992408751798420801126401127326826"
117
107
  )
118
108
  })
119
109
 
120
110
  it("Should hash a number", async () => {
121
- expect(hash(2).toString()).toBe(
122
- "113682330006535319932160121224458771213356533826860247409332700812532759386"
123
- )
111
+ expect(hash(2)).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
124
112
  })
125
113
 
126
114
  it("Should hash a big number", async () => {
127
- expect(hash(BigInt(2)).toString()).toBe(
128
- "113682330006535319932160121224458771213356533826860247409332700812532759386"
129
- )
115
+ expect(hash(BigInt(2))).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
130
116
  })
131
117
 
132
118
  it("Should hash an hex number", async () => {
133
- expect(hash("0x2").toString()).toBe(
134
- "113682330006535319932160121224458771213356533826860247409332700812532759386"
135
- )
119
+ expect(hash("0x2")).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
136
120
  })
137
121
 
138
122
  it("Should hash an string number", async () => {
139
- expect(hash("2").toString()).toBe(
140
- "113682330006535319932160121224458771213356533826860247409332700812532759386"
141
- )
123
+ expect(hash("2")).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
142
124
  })
143
125
 
144
126
  it("Should hash an array", async () => {
145
- expect(hash([2]).toString()).toBe(
146
- "113682330006535319932160121224458771213356533826860247409332700812532759386"
147
- )
127
+ expect(hash([2])).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
148
128
  })
149
129
  })
150
130
 
package/src/packProof.ts CHANGED
@@ -1,19 +1,20 @@
1
- import { SnarkJSProof, Proof } from "./types"
1
+ import { Groth16Proof } from "snarkjs"
2
+ import { PackedProof } from "./types"
2
3
 
3
4
  /**
4
5
  * Packs a proof into a format compatible with Semaphore.
5
- * @param originalProof The proof generated with SnarkJS.
6
+ * @param proof The Groth16 proof generated with SnarkJS.
6
7
  * @returns The proof compatible with Semaphore.
7
8
  */
8
- export default function packProof(originalProof: SnarkJSProof): Proof {
9
+ export default function packProof(proof: Groth16Proof): PackedProof {
9
10
  return [
10
- originalProof.pi_a[0],
11
- originalProof.pi_a[1],
12
- originalProof.pi_b[0][1],
13
- originalProof.pi_b[0][0],
14
- originalProof.pi_b[1][1],
15
- originalProof.pi_b[1][0],
16
- originalProof.pi_c[0],
17
- originalProof.pi_c[1]
11
+ proof.pi_a[0],
12
+ proof.pi_a[1],
13
+ proof.pi_b[0][1],
14
+ proof.pi_b[0][0],
15
+ proof.pi_b[1][1],
16
+ proof.pi_b[1][0],
17
+ proof.pi_c[0],
18
+ proof.pi_c[1]
18
19
  ]
19
20
  }
@@ -1,33 +1,25 @@
1
- export type BigNumberish = string | bigint
1
+ import type { NumericString } from "snarkjs"
2
2
 
3
3
  export type SnarkArtifacts = {
4
4
  wasmFilePath: string
5
5
  zkeyFilePath: string
6
6
  }
7
7
 
8
- export type SnarkJSProof = {
9
- pi_a: BigNumberish[]
10
- pi_b: BigNumberish[][]
11
- pi_c: BigNumberish[]
12
- protocol: string
13
- curve: string
8
+ export type SemaphoreProof = {
9
+ merkleTreeRoot: NumericString
10
+ signal: NumericString
11
+ nullifierHash: NumericString
12
+ externalNullifier: NumericString
13
+ proof: PackedProof
14
14
  }
15
15
 
16
- export type FullProof = {
17
- merkleTreeRoot: BigNumberish
18
- signal: BigNumberish
19
- nullifierHash: BigNumberish
20
- externalNullifier: BigNumberish
21
- proof: Proof
22
- }
23
-
24
- export type Proof = [
25
- BigNumberish,
26
- BigNumberish,
27
- BigNumberish,
28
- BigNumberish,
29
- BigNumberish,
30
- BigNumberish,
31
- BigNumberish,
32
- BigNumberish
16
+ export type PackedProof = [
17
+ NumericString,
18
+ NumericString,
19
+ NumericString,
20
+ NumericString,
21
+ NumericString,
22
+ NumericString,
23
+ NumericString,
24
+ NumericString
33
25
  ]
@@ -1,11 +1,12 @@
1
- import { SnarkJSProof, Proof } from "./types"
1
+ import { Groth16Proof } from "snarkjs"
2
+ import { PackedProof } from "./types"
2
3
 
3
4
  /**
4
5
  * Unpacks a proof into its original form.
5
6
  * @param proof The proof compatible with Semaphore.
6
7
  * @returns The proof compatible with SnarkJS.
7
8
  */
8
- export default function unpackProof(proof: Proof): SnarkJSProof {
9
+ export default function unpackProof(proof: PackedProof): Groth16Proof {
9
10
  return {
10
11
  pi_a: [proof[0], proof[1]],
11
12
  pi_b: [
@@ -1,6 +1,6 @@
1
1
  import { groth16 } from "snarkjs"
2
2
  import hash from "./hash"
3
- import { FullProof } from "./types"
3
+ import { SemaphoreProof } from "./types"
4
4
  import unpackProof from "./unpackProof"
5
5
  import verificationKeys from "./verificationKeys.json"
6
6
 
@@ -11,7 +11,7 @@ import verificationKeys from "./verificationKeys.json"
11
11
  * @returns True if the proof is valid, false otherwise.
12
12
  */
13
13
  export default function verifyProof(
14
- { merkleTreeRoot, nullifierHash, externalNullifier, signal, proof }: FullProof,
14
+ { merkleTreeRoot, nullifierHash, externalNullifier, signal, proof }: SemaphoreProof,
15
15
  treeDepth: number
16
16
  ): Promise<boolean> {
17
17
  if (treeDepth < 16 || treeDepth > 32) {