@semaphore-protocol/proof 2.1.0 → 2.2.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 +4 -1
- package/dist/index.mjs +18 -11
- package/dist/index.node.js +18 -11
- package/dist/types/generateProof.d.ts +3 -2
- package/package.json +2 -1
- package/src/generateProof.ts +22 -10
- package/src/index.test.ts +13 -3
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
|
|
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.
|
|
3
|
+
* @version 2.2.0
|
|
4
4
|
* @file A library to generate and verify Semaphore proofs.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -83,22 +83,29 @@ function genSignalHash(signal) {
|
|
|
83
83
|
return BigInt(keccak256(["bytes32"], [formatBytes32String(signal)])) >> BigInt(8);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
function generateProof(identity,
|
|
87
|
-
if (snarkArtifacts === void 0) { snarkArtifacts = {
|
|
88
|
-
wasmFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(group.depth, "/semaphore.wasm"),
|
|
89
|
-
zkeyFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(group.depth, "/semaphore.zkey")
|
|
90
|
-
}; }
|
|
86
|
+
function generateProof(identity, groupOrMerkleProof, externalNullifier, signal, snarkArtifacts) {
|
|
91
87
|
return __awaiter(this, void 0, void 0, function () {
|
|
92
|
-
var commitment,
|
|
88
|
+
var commitment, merkleProof, index, _a, proof, publicSignals;
|
|
93
89
|
return __generator(this, function (_b) {
|
|
94
90
|
switch (_b.label) {
|
|
95
91
|
case 0:
|
|
96
92
|
commitment = identity.generateCommitment();
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
if ("depth" in groupOrMerkleProof) {
|
|
94
|
+
index = groupOrMerkleProof.indexOf(commitment);
|
|
95
|
+
if (index === -1) {
|
|
96
|
+
throw new Error("The identity is not part of the group");
|
|
97
|
+
}
|
|
98
|
+
merkleProof = groupOrMerkleProof.generateProofOfMembership(index);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
merkleProof = groupOrMerkleProof;
|
|
102
|
+
}
|
|
103
|
+
if (!snarkArtifacts) {
|
|
104
|
+
snarkArtifacts = {
|
|
105
|
+
wasmFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.wasm"),
|
|
106
|
+
zkeyFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(merkleProof.siblings.length, "/semaphore.zkey")
|
|
107
|
+
};
|
|
100
108
|
}
|
|
101
|
-
merkleProof = group.generateProofOfMembership(index);
|
|
102
109
|
return [4 /*yield*/, groth16.fullProve({
|
|
103
110
|
identityTrapdoor: identity.getTrapdoor(),
|
|
104
111
|
identityNullifier: identity.getNullifier(),
|
package/dist/index.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/proof
|
|
3
|
-
* @version 2.
|
|
3
|
+
* @version 2.2.0
|
|
4
4
|
* @file A library to generate and verify Semaphore proofs.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -87,22 +87,29 @@ function genSignalHash(signal) {
|
|
|
87
87
|
return BigInt(solidity.keccak256(["bytes32"], [strings.formatBytes32String(signal)])) >> BigInt(8);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
function generateProof(identity,
|
|
91
|
-
if (snarkArtifacts === void 0) { snarkArtifacts = {
|
|
92
|
-
wasmFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(group.depth, "/semaphore.wasm"),
|
|
93
|
-
zkeyFilePath: "https://www.trusted-setup-pse.org/semaphore/".concat(group.depth, "/semaphore.zkey")
|
|
94
|
-
}; }
|
|
90
|
+
function generateProof(identity, groupOrMerkleProof, externalNullifier, signal, snarkArtifacts) {
|
|
95
91
|
return __awaiter(this, void 0, void 0, function () {
|
|
96
|
-
var commitment,
|
|
92
|
+
var commitment, merkleProof, index, _a, proof, publicSignals;
|
|
97
93
|
return __generator(this, function (_b) {
|
|
98
94
|
switch (_b.label) {
|
|
99
95
|
case 0:
|
|
100
96
|
commitment = identity.generateCommitment();
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
+
};
|
|
104
112
|
}
|
|
105
|
-
merkleProof = group.generateProofOfMembership(index);
|
|
106
113
|
return [4 /*yield*/, snarkjs.groth16.fullProve({
|
|
107
114
|
identityTrapdoor: identity.getTrapdoor(),
|
|
108
115
|
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,
|
|
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.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A library to generate and verify Semaphore proofs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.node.js",
|
|
@@ -39,6 +39,7 @@
|
|
|
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
|
}
|
package/src/generateProof.ts
CHANGED
|
@@ -1,27 +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
|
-
|
|
10
|
+
groupOrMerkleProof: Group | MerkleProof,
|
|
10
11
|
externalNullifier: BigNumberish,
|
|
11
12
|
signal: string,
|
|
12
|
-
snarkArtifacts
|
|
13
|
-
wasmFilePath: `https://www.trusted-setup-pse.org/semaphore/${group.depth}/semaphore.wasm`,
|
|
14
|
-
zkeyFilePath: `https://www.trusted-setup-pse.org/semaphore/${group.depth}/semaphore.zkey`
|
|
15
|
-
}
|
|
13
|
+
snarkArtifacts?: SnarkArtifacts
|
|
16
14
|
): Promise<FullProof> {
|
|
17
15
|
const commitment = identity.generateCommitment()
|
|
18
|
-
const index = group.indexOf(commitment)
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
|
|
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
|
+
}
|
|
25
37
|
|
|
26
38
|
const { proof, publicSignals } = await groth16.fullProve(
|
|
27
39
|
{
|
package/src/index.test.ts
CHANGED
|
@@ -58,7 +58,17 @@ describe("Proof", () => {
|
|
|
58
58
|
await expect(fun).rejects.toThrow("The identity is not part of the group")
|
|
59
59
|
})
|
|
60
60
|
|
|
61
|
-
it("Should generate a Semaphore proof", async () => {
|
|
61
|
+
it("Should not generate a Semaphore proof with default snark artifacts with Node.js", async () => {
|
|
62
|
+
const group = new Group(treeDepth)
|
|
63
|
+
|
|
64
|
+
group.addMembers([BigInt(1), BigInt(2), identityCommitment])
|
|
65
|
+
|
|
66
|
+
const fun = () => generateProof(identity, group, externalNullifier, signal)
|
|
67
|
+
|
|
68
|
+
await expect(fun).rejects.toThrow("ENOENT: no such file or directory")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("Should generate a Semaphore proof passing a group as parameter", async () => {
|
|
62
72
|
const group = new Group(treeDepth)
|
|
63
73
|
|
|
64
74
|
group.addMembers([BigInt(1), BigInt(2), identityCommitment])
|
|
@@ -73,12 +83,12 @@ describe("Proof", () => {
|
|
|
73
83
|
expect(fullProof.publicSignals.merkleRoot).toBe(group.root.toString())
|
|
74
84
|
}, 20000)
|
|
75
85
|
|
|
76
|
-
it("Should generate a Semaphore proof
|
|
86
|
+
it("Should generate a Semaphore proof passing a Merkle proof as parametr", async () => {
|
|
77
87
|
const group = new Group(treeDepth)
|
|
78
88
|
|
|
79
89
|
group.addMembers([BigInt(1), BigInt(2), identityCommitment])
|
|
80
90
|
|
|
81
|
-
fullProof = await generateProof(identity, group, externalNullifier, signal, {
|
|
91
|
+
fullProof = await generateProof(identity, group.generateProofOfMembership(2), externalNullifier, signal, {
|
|
82
92
|
wasmFilePath: `${snarkArtifactsPath}/semaphore.wasm`,
|
|
83
93
|
zkeyFilePath: `${snarkArtifactsPath}/semaphore.zkey`
|
|
84
94
|
})
|