@semaphore-protocol/proof 2.0.1 → 2.3.0

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/README.md CHANGED
@@ -59,7 +59,7 @@ yarn add @semaphore-protocol/proof
59
59
 
60
60
  ## 📜 Usage
61
61
 
62
- \# **generateProof**(identity: _Identity_, group: _Group_, externalNullifier: _BigNumberish_, signal: _string_, snarkArtifacts: _SnarkArtifacts_): Promise\<_SemaphoreFullProof_>
62
+ \# **generateProof**(identity: _Identity_, group: _Group_, externalNullifier: _BigNumberish_, signal: _string_, snarkArtifacts?: _SnarkArtifacts_): Promise\<_SemaphoreFullProof_>
63
63
 
64
64
  ```typescript
65
65
  import { Identity } from "@semaphore-protocol/identity"
@@ -77,6 +77,9 @@ const fullProof = await generateProof(identity, merkleProof, externalNullifier,
77
77
  zkeyFilePath: "./semaphore.zkey",
78
78
  wasmFilePath: "./semaphore.wasm"
79
79
  })
80
+
81
+ // You can also use the default zkey/wasm files (only for browsers!).
82
+ // const fullProof = await generateProof(identity, merkleProof, externalNullifier, signal)
80
83
  ```
81
84
 
82
85
  \# **verifyProof**(verificationKey: _any_, fullProof: _FullProof_): Promise\<_boolean_>
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 2.0.1
3
+ * @version 2.3.0
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { poseidon } from 'circomlibjs';
10
10
  import { groth16 } from 'snarkjs';
11
+ import { isHexString } from '@ethersproject/bytes';
11
12
  import { keccak256 } from '@ethersproject/solidity';
12
13
  import { formatBytes32String } from '@ethersproject/strings';
13
14
 
@@ -80,21 +81,35 @@ function __generator(thisArg, body) {
80
81
  * @returns The signal hash.
81
82
  */
82
83
  function genSignalHash(signal) {
83
- return BigInt(keccak256(["bytes32"], [formatBytes32String(signal)])) >> BigInt(8);
84
+ if (!isHexString(signal, 32)) {
85
+ signal = formatBytes32String(signal);
86
+ }
87
+ return BigInt(keccak256(["bytes32"], [signal])) >> BigInt(8);
84
88
  }
85
89
 
86
- function generateProof(identity, group, externalNullifier, signal, snarkArtifacts) {
90
+ function generateProof(identity, groupOrMerkleProof, externalNullifier, signal, snarkArtifacts) {
87
91
  return __awaiter(this, void 0, void 0, function () {
88
- var commitment, index, merkleProof, _a, proof, publicSignals;
92
+ var commitment, merkleProof, index, _a, proof, publicSignals;
89
93
  return __generator(this, function (_b) {
90
94
  switch (_b.label) {
91
95
  case 0:
92
96
  commitment = identity.generateCommitment();
93
- index = group.indexOf(commitment);
94
- if (index === -1) {
95
- throw new Error("The identity is not part of the group");
97
+ if ("depth" in groupOrMerkleProof) {
98
+ index = groupOrMerkleProof.indexOf(commitment);
99
+ if (index === -1) {
100
+ throw new Error("The identity is not part of the group");
101
+ }
102
+ merkleProof = groupOrMerkleProof.generateProofOfMembership(index);
103
+ }
104
+ else {
105
+ merkleProof = groupOrMerkleProof;
106
+ }
107
+ if (!snarkArtifacts) {
108
+ snarkArtifacts = {
109
+ wasmFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.wasm"),
110
+ zkeyFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.zkey")
111
+ };
96
112
  }
97
- merkleProof = group.generateProofOfMembership(index);
98
113
  return [4 /*yield*/, groth16.fullProve({
99
114
  identityTrapdoor: identity.getTrapdoor(),
100
115
  identityNullifier: identity.getNullifier(),
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 2.0.1
3
+ * @version 2.3.0
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -12,6 +12,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
12
12
 
13
13
  var circomlibjs = require('circomlibjs');
14
14
  var snarkjs = require('snarkjs');
15
+ var bytes = require('@ethersproject/bytes');
15
16
  var solidity = require('@ethersproject/solidity');
16
17
  var strings = require('@ethersproject/strings');
17
18
 
@@ -84,21 +85,35 @@ function __generator(thisArg, body) {
84
85
  * @returns The signal hash.
85
86
  */
86
87
  function genSignalHash(signal) {
87
- return BigInt(solidity.keccak256(["bytes32"], [strings.formatBytes32String(signal)])) >> BigInt(8);
88
+ if (!bytes.isHexString(signal, 32)) {
89
+ signal = strings.formatBytes32String(signal);
90
+ }
91
+ return BigInt(solidity.keccak256(["bytes32"], [signal])) >> BigInt(8);
88
92
  }
89
93
 
90
- function generateProof(identity, group, externalNullifier, signal, snarkArtifacts) {
94
+ function generateProof(identity, groupOrMerkleProof, externalNullifier, signal, snarkArtifacts) {
91
95
  return __awaiter(this, void 0, void 0, function () {
92
- var commitment, index, merkleProof, _a, proof, publicSignals;
96
+ var commitment, merkleProof, index, _a, proof, publicSignals;
93
97
  return __generator(this, function (_b) {
94
98
  switch (_b.label) {
95
99
  case 0:
96
100
  commitment = identity.generateCommitment();
97
- index = group.indexOf(commitment);
98
- if (index === -1) {
99
- throw new Error("The identity is not part of the group");
101
+ if ("depth" in groupOrMerkleProof) {
102
+ index = groupOrMerkleProof.indexOf(commitment);
103
+ if (index === -1) {
104
+ throw new Error("The identity is not part of the group");
105
+ }
106
+ merkleProof = groupOrMerkleProof.generateProofOfMembership(index);
107
+ }
108
+ else {
109
+ merkleProof = groupOrMerkleProof;
110
+ }
111
+ if (!snarkArtifacts) {
112
+ snarkArtifacts = {
113
+ wasmFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.wasm"),
114
+ zkeyFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.zkey")
115
+ };
100
116
  }
101
- merkleProof = group.generateProofOfMembership(index);
102
117
  return [4 /*yield*/, snarkjs.groth16.fullProve({
103
118
  identityTrapdoor: identity.getTrapdoor(),
104
119
  identityNullifier: identity.getNullifier(),
@@ -1,4 +1,5 @@
1
- import type { Identity } from "@semaphore-protocol/identity";
2
1
  import { Group } from "@semaphore-protocol/group";
2
+ import type { Identity } from "@semaphore-protocol/identity";
3
+ import { MerkleProof } from "@zk-kit/incremental-merkle-tree";
3
4
  import { BigNumberish, FullProof, SnarkArtifacts } from "./types";
4
- export default function generateProof(identity: Identity, group: Group, externalNullifier: BigNumberish, signal: string, snarkArtifacts: SnarkArtifacts): Promise<FullProof>;
5
+ export default function generateProof(identity: Identity, groupOrMerkleProof: Group | MerkleProof, externalNullifier: BigNumberish, signal: string, snarkArtifacts?: SnarkArtifacts): Promise<FullProof>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/proof",
3
- "version": "2.0.1",
3
+ "version": "2.3.0",
4
4
  "description": "A library to generate and verify Semaphore proofs.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -34,11 +34,12 @@
34
34
  "typedoc": "^0.22.11"
35
35
  },
36
36
  "dependencies": {
37
- "@ethersproject/bytes": "^5.5.0",
37
+ "@ethersproject/bytes": "^5.7.0",
38
38
  "@ethersproject/solidity": "^5.5.0",
39
39
  "@ethersproject/strings": "^5.5.0",
40
40
  "@semaphore-protocol/group": "2.0.0",
41
41
  "@semaphore-protocol/identity": "2.0.0",
42
+ "@zk-kit/incremental-merkle-tree": "0.4.3",
42
43
  "circomlibjs": "0.0.8",
43
44
  "snarkjs": "^0.4.13"
44
45
  }
@@ -1,24 +1,39 @@
1
- import type { Identity } from "@semaphore-protocol/identity"
2
1
  import { Group } from "@semaphore-protocol/group"
2
+ import type { Identity } from "@semaphore-protocol/identity"
3
+ import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
3
4
  import { groth16 } from "snarkjs"
4
5
  import generateSignalHash from "./generateSignalHash"
5
6
  import { BigNumberish, FullProof, SnarkArtifacts } from "./types"
6
7
 
7
8
  export default async function generateProof(
8
9
  identity: Identity,
9
- group: Group,
10
+ groupOrMerkleProof: Group | MerkleProof,
10
11
  externalNullifier: BigNumberish,
11
12
  signal: string,
12
- snarkArtifacts: SnarkArtifacts
13
+ snarkArtifacts?: SnarkArtifacts
13
14
  ): Promise<FullProof> {
14
15
  const commitment = identity.generateCommitment()
15
- const index = group.indexOf(commitment)
16
16
 
17
- if (index === -1) {
18
- throw new Error("The identity is not part of the group")
17
+ let merkleProof: MerkleProof
18
+
19
+ if ("depth" in groupOrMerkleProof) {
20
+ const index = groupOrMerkleProof.indexOf(commitment)
21
+
22
+ if (index === -1) {
23
+ throw new Error("The identity is not part of the group")
24
+ }
25
+
26
+ merkleProof = groupOrMerkleProof.generateProofOfMembership(index)
27
+ } else {
28
+ merkleProof = groupOrMerkleProof
19
29
  }
20
30
 
21
- const merkleProof = group.generateProofOfMembership(index)
31
+ if (!snarkArtifacts) {
32
+ snarkArtifacts = {
33
+ wasmFilePath: `https://www.trusted-setup-pse.org/semaphore/${merkleProof.siblings.length}/semaphore.wasm`,
34
+ zkeyFilePath: `https://www.trusted-setup-pse.org/semaphore/${merkleProof.siblings.length}/semaphore.zkey`
35
+ }
36
+ }
22
37
 
23
38
  const { proof, publicSignals } = await groth16.fullProve(
24
39
  {
@@ -1,3 +1,4 @@
1
+ import { isHexString } from "@ethersproject/bytes"
1
2
  import { keccak256 } from "@ethersproject/solidity"
2
3
  import { formatBytes32String } from "@ethersproject/strings"
3
4
 
@@ -7,5 +8,9 @@ import { formatBytes32String } from "@ethersproject/strings"
7
8
  * @returns The signal hash.
8
9
  */
9
10
  export default function genSignalHash(signal: string): bigint {
10
- return BigInt(keccak256(["bytes32"], [formatBytes32String(signal)])) >> BigInt(8)
11
+ if (!isHexString(signal, 32)) {
12
+ signal = formatBytes32String(signal)
13
+ }
14
+
15
+ return BigInt(keccak256(["bytes32"], [signal])) >> BigInt(8)
11
16
  }
package/src/index.test.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { formatBytes32String } from "@ethersproject/strings"
1
2
  import { Group } from "@semaphore-protocol/group"
2
3
  import { Identity } from "@semaphore-protocol/identity"
3
4
  import download from "download"
@@ -58,7 +59,17 @@ describe("Proof", () => {
58
59
  await expect(fun).rejects.toThrow("The identity is not part of the group")
59
60
  })
60
61
 
61
- it("Should generate a Semaphore proof", async () => {
62
+ it("Should not generate a Semaphore proof with default snark artifacts with Node.js", async () => {
63
+ const group = new Group(treeDepth)
64
+
65
+ group.addMembers([BigInt(1), BigInt(2), identityCommitment])
66
+
67
+ const fun = () => generateProof(identity, group, externalNullifier, signal)
68
+
69
+ await expect(fun).rejects.toThrow("ENOENT: no such file or directory")
70
+ })
71
+
72
+ it("Should generate a Semaphore proof passing a group as parameter", async () => {
62
73
  const group = new Group(treeDepth)
63
74
 
64
75
  group.addMembers([BigInt(1), BigInt(2), identityCommitment])
@@ -72,6 +83,21 @@ describe("Proof", () => {
72
83
  expect(fullProof.publicSignals.externalNullifier).toBe(externalNullifier)
73
84
  expect(fullProof.publicSignals.merkleRoot).toBe(group.root.toString())
74
85
  }, 20000)
86
+
87
+ it("Should generate a Semaphore proof passing a Merkle proof as parametr", async () => {
88
+ const group = new Group(treeDepth)
89
+
90
+ group.addMembers([BigInt(1), BigInt(2), identityCommitment])
91
+
92
+ fullProof = await generateProof(identity, group.generateProofOfMembership(2), externalNullifier, signal, {
93
+ wasmFilePath: `${snarkArtifactsPath}/semaphore.wasm`,
94
+ zkeyFilePath: `${snarkArtifactsPath}/semaphore.zkey`
95
+ })
96
+
97
+ expect(typeof fullProof).toBe("object")
98
+ expect(fullProof.publicSignals.externalNullifier).toBe(externalNullifier)
99
+ expect(fullProof.publicSignals.merkleRoot).toBe(group.root.toString())
100
+ }, 20000)
75
101
  })
76
102
 
77
103
  describe("# generateSignalHash", () => {
@@ -80,6 +106,12 @@ describe("Proof", () => {
80
106
 
81
107
  expect(signalHash.toString()).toBe(fullProof.publicSignals.signalHash)
82
108
  })
109
+
110
+ it("Should generate a valid signal hash by passing a valid hex string", async () => {
111
+ const signalHash = generateSignalHash(formatBytes32String(signal))
112
+
113
+ expect(signalHash.toString()).toBe(fullProof.publicSignals.signalHash)
114
+ })
83
115
  })
84
116
 
85
117
  describe("# generateNullifierHash", () => {