@semaphore-protocol/proof 4.0.0-alpha.3 → 4.0.0-alpha.5
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 -4
- package/dist/{index.browser.mjs → index.browser.js} +143 -139
- package/dist/{index.node.mjs → index.node.cjs} +171 -161
- package/dist/index.node.js +165 -167
- package/dist/types/generate-proof.d.ts +15 -15
- package/dist/types/get-snark-artifacts.browser.d.ts +2 -2
- package/dist/types/get-snark-artifacts.node.d.ts +2 -2
- package/dist/types/hash.d.ts +8 -8
- package/dist/types/index.d.ts +7 -7
- package/dist/types/pack-points.d.ts +8 -8
- package/dist/types/types/index.d.ts +24 -24
- package/dist/types/unpack-points.d.ts +8 -8
- package/dist/types/verify-proof.d.ts +7 -8
- package/package.json +13 -13
- package/src/verify-proof.ts +1 -2
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/proof
|
|
3
|
-
* @version 4.0.0-alpha.
|
|
3
|
+
* @version 4.0.0-alpha.5
|
|
4
4
|
* @file A library to generate and verify Semaphore proofs.
|
|
5
5
|
* @copyright Ethereum Foundation 2024
|
|
6
6
|
* @license MIT
|
|
7
7
|
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/proof}
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
var bignumber = require('@ethersproject/bignumber');
|
|
12
|
+
var snarkjs = require('snarkjs');
|
|
13
|
+
var download = require('download');
|
|
14
|
+
var fs = require('fs');
|
|
15
|
+
var tmp = require('tmp');
|
|
16
|
+
var bytes = require('@ethersproject/bytes');
|
|
17
|
+
var keccak256 = require('@ethersproject/keccak256');
|
|
16
18
|
|
|
17
19
|
/******************************************************************************
|
|
18
20
|
Copyright (c) Microsoft Corporation.
|
|
@@ -28,7 +30,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
28
30
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
29
31
|
PERFORMANCE OF THIS SOFTWARE.
|
|
30
32
|
***************************************************************************** */
|
|
31
|
-
/* global Reflect, Promise */
|
|
33
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
32
34
|
|
|
33
35
|
|
|
34
36
|
var __assign = function() {
|
|
@@ -78,147 +80,152 @@ function __generator(thisArg, body) {
|
|
|
78
80
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
79
81
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
80
82
|
}
|
|
81
|
-
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
86
|
+
var e = new Error(message);
|
|
87
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
88
|
+
};
|
|
82
89
|
|
|
83
|
-
function getSnarkArtifacts(treeDepth) {
|
|
84
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
-
var tmpDir, tmpPath;
|
|
86
|
-
return __generator(this, function (_a) {
|
|
87
|
-
switch (_a.label) {
|
|
88
|
-
case 0:
|
|
89
|
-
tmpDir = "semaphore-proof";
|
|
90
|
-
tmpPath = "".concat(tmp.tmpdir, "/").concat(tmpDir, "-").concat(treeDepth);
|
|
91
|
-
if (!(!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
|
|
92
|
-
return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), tmpPath)];
|
|
93
|
-
case 1:
|
|
94
|
-
_a.sent();
|
|
95
|
-
return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), tmpPath)];
|
|
96
|
-
case 2:
|
|
97
|
-
_a.sent();
|
|
98
|
-
_a.label = 3;
|
|
99
|
-
case 3: return [2 /*return*/, {
|
|
100
|
-
wasmFilePath: "".concat(tmpPath, "/semaphore.wasm"),
|
|
101
|
-
zkeyFilePath: "".concat(tmpPath, "/semaphore.zkey")
|
|
102
|
-
}];
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
});
|
|
90
|
+
function getSnarkArtifacts(treeDepth) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
92
|
+
var tmpDir, tmpPath;
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
switch (_a.label) {
|
|
95
|
+
case 0:
|
|
96
|
+
tmpDir = "semaphore-proof";
|
|
97
|
+
tmpPath = "".concat(tmp.tmpdir, "/").concat(tmpDir, "-").concat(treeDepth);
|
|
98
|
+
if (!(!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
|
|
99
|
+
return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), tmpPath)];
|
|
100
|
+
case 1:
|
|
101
|
+
_a.sent();
|
|
102
|
+
return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), tmpPath)];
|
|
103
|
+
case 2:
|
|
104
|
+
_a.sent();
|
|
105
|
+
_a.label = 3;
|
|
106
|
+
case 3: return [2 /*return*/, {
|
|
107
|
+
wasmFilePath: "".concat(tmpPath, "/semaphore.wasm"),
|
|
108
|
+
zkeyFilePath: "".concat(tmpPath, "/semaphore.zkey")
|
|
109
|
+
}];
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
/**
|
|
109
|
-
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
|
110
|
-
* @param message The message to be hashed.
|
|
111
|
-
* @returns The message digest.
|
|
112
|
-
*/
|
|
113
|
-
function hash(message) {
|
|
114
|
-
message = BigNumber.from(message).toTwos(256).toHexString();
|
|
115
|
-
message = zeroPad(message, 32);
|
|
116
|
-
return (BigInt(keccak256(message)) >> BigInt(8)).toString();
|
|
115
|
+
/**
|
|
116
|
+
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
|
117
|
+
* @param message The message to be hashed.
|
|
118
|
+
* @returns The message digest.
|
|
119
|
+
*/
|
|
120
|
+
function hash(message) {
|
|
121
|
+
message = bignumber.BigNumber.from(message).toTwos(256).toHexString();
|
|
122
|
+
message = bytes.zeroPad(message, 32);
|
|
123
|
+
return (BigInt(keccak256.keccak256(message)) >> BigInt(8)).toString();
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
/**
|
|
120
|
-
* Packs the Groth16 proof points into a format compatible with Semaphore.
|
|
121
|
-
* @param proof The proof points generated with SnarkJS.
|
|
122
|
-
* @returns The proof points compatible with Semaphore.
|
|
123
|
-
*/
|
|
124
|
-
function packPoints(proof) {
|
|
125
|
-
return [
|
|
126
|
-
proof.pi_a[0],
|
|
127
|
-
proof.pi_a[1],
|
|
128
|
-
proof.pi_b[0][1],
|
|
129
|
-
proof.pi_b[0][0],
|
|
130
|
-
proof.pi_b[1][1],
|
|
131
|
-
proof.pi_b[1][0],
|
|
132
|
-
proof.pi_c[0],
|
|
133
|
-
proof.pi_c[1]
|
|
134
|
-
];
|
|
126
|
+
/**
|
|
127
|
+
* Packs the Groth16 proof points into a format compatible with Semaphore.
|
|
128
|
+
* @param proof The proof points generated with SnarkJS.
|
|
129
|
+
* @returns The proof points compatible with Semaphore.
|
|
130
|
+
*/
|
|
131
|
+
function packPoints(proof) {
|
|
132
|
+
return [
|
|
133
|
+
proof.pi_a[0],
|
|
134
|
+
proof.pi_a[1],
|
|
135
|
+
proof.pi_b[0][1],
|
|
136
|
+
proof.pi_b[0][0],
|
|
137
|
+
proof.pi_b[1][1],
|
|
138
|
+
proof.pi_b[1][0],
|
|
139
|
+
proof.pi_c[0],
|
|
140
|
+
proof.pi_c[1]
|
|
141
|
+
];
|
|
135
142
|
}
|
|
136
143
|
|
|
137
|
-
/**
|
|
138
|
-
* Generates a Semaphore proof.
|
|
139
|
-
* @param identity The Semaphore identity.
|
|
140
|
-
* @param groupOrMerkleProof The Semaphore group or its Merkle proof.
|
|
141
|
-
* @param scope The external nullifier.
|
|
142
|
-
* @param message The Semaphore signal.
|
|
143
|
-
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
144
|
-
* @param snarkArtifacts The SNARK artifacts.
|
|
145
|
-
* @returns The Semaphore proof ready to be verified.
|
|
146
|
-
*/
|
|
147
|
-
function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeDepth, snarkArtifacts) {
|
|
148
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
149
|
-
var merkleProof, leafIndex, merkleProofLength, merkleProofIndices, merkleProofSiblings, i, _a, proof, publicSignals;
|
|
150
|
-
return __generator(this, function (_b) {
|
|
151
|
-
switch (_b.label) {
|
|
152
|
-
case 0:
|
|
153
|
-
if ("siblings" in groupOrMerkleProof) {
|
|
154
|
-
merkleProof = groupOrMerkleProof;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
leafIndex = groupOrMerkleProof.indexOf(identity.commitment);
|
|
158
|
-
merkleProof = groupOrMerkleProof.generateMerkleProof(leafIndex);
|
|
159
|
-
}
|
|
160
|
-
merkleProofLength = merkleProof.siblings.length;
|
|
161
|
-
if (merkleTreeDepth !== undefined) {
|
|
162
|
-
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
163
|
-
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
merkleTreeDepth = merkleProofLength;
|
|
168
|
-
}
|
|
169
|
-
if (!!snarkArtifacts) return [3 /*break*/, 2];
|
|
170
|
-
return [4 /*yield*/, getSnarkArtifacts(merkleTreeDepth)];
|
|
171
|
-
case 1:
|
|
172
|
-
snarkArtifacts = _b.sent();
|
|
173
|
-
_b.label = 2;
|
|
174
|
-
case 2:
|
|
175
|
-
merkleProofIndices = [];
|
|
176
|
-
merkleProofSiblings = merkleProof.siblings;
|
|
177
|
-
for (i = 0; i < merkleTreeDepth; i += 1) {
|
|
178
|
-
merkleProofIndices.push((merkleProof.index >> i) & 1);
|
|
179
|
-
if (merkleProofSiblings[i] === undefined) {
|
|
180
|
-
merkleProofSiblings[i] = "0";
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return [4 /*yield*/, groth16.fullProve({
|
|
184
|
-
secret: identity.secretScalar,
|
|
185
|
-
merkleProofLength: merkleProofLength,
|
|
186
|
-
merkleProofIndices: merkleProofIndices,
|
|
187
|
-
merkleProofSiblings: merkleProofSiblings,
|
|
188
|
-
scope: hash(scope),
|
|
189
|
-
message: hash(message)
|
|
190
|
-
}, snarkArtifacts.wasmFilePath, snarkArtifacts.zkeyFilePath)];
|
|
191
|
-
case 3:
|
|
192
|
-
_a = _b.sent(), proof = _a.proof, publicSignals = _a.publicSignals;
|
|
193
|
-
return [2 /*return*/, {
|
|
194
|
-
merkleTreeDepth: merkleTreeDepth,
|
|
195
|
-
merkleTreeRoot: publicSignals[0],
|
|
196
|
-
nullifier: publicSignals[1],
|
|
197
|
-
message: BigNumber.from(message).toString(),
|
|
198
|
-
scope: BigNumber.from(scope).toString(),
|
|
199
|
-
points: packPoints(proof)
|
|
200
|
-
}];
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
});
|
|
144
|
+
/**
|
|
145
|
+
* Generates a Semaphore proof.
|
|
146
|
+
* @param identity The Semaphore identity.
|
|
147
|
+
* @param groupOrMerkleProof The Semaphore group or its Merkle proof.
|
|
148
|
+
* @param scope The external nullifier.
|
|
149
|
+
* @param message The Semaphore signal.
|
|
150
|
+
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
151
|
+
* @param snarkArtifacts The SNARK artifacts.
|
|
152
|
+
* @returns The Semaphore proof ready to be verified.
|
|
153
|
+
*/
|
|
154
|
+
function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeDepth, snarkArtifacts) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
156
|
+
var merkleProof, leafIndex, merkleProofLength, merkleProofIndices, merkleProofSiblings, i, _a, proof, publicSignals;
|
|
157
|
+
return __generator(this, function (_b) {
|
|
158
|
+
switch (_b.label) {
|
|
159
|
+
case 0:
|
|
160
|
+
if ("siblings" in groupOrMerkleProof) {
|
|
161
|
+
merkleProof = groupOrMerkleProof;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
leafIndex = groupOrMerkleProof.indexOf(identity.commitment);
|
|
165
|
+
merkleProof = groupOrMerkleProof.generateMerkleProof(leafIndex);
|
|
166
|
+
}
|
|
167
|
+
merkleProofLength = merkleProof.siblings.length;
|
|
168
|
+
if (merkleTreeDepth !== undefined) {
|
|
169
|
+
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
170
|
+
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
merkleTreeDepth = merkleProofLength;
|
|
175
|
+
}
|
|
176
|
+
if (!!snarkArtifacts) return [3 /*break*/, 2];
|
|
177
|
+
return [4 /*yield*/, getSnarkArtifacts(merkleTreeDepth)];
|
|
178
|
+
case 1:
|
|
179
|
+
snarkArtifacts = _b.sent();
|
|
180
|
+
_b.label = 2;
|
|
181
|
+
case 2:
|
|
182
|
+
merkleProofIndices = [];
|
|
183
|
+
merkleProofSiblings = merkleProof.siblings;
|
|
184
|
+
for (i = 0; i < merkleTreeDepth; i += 1) {
|
|
185
|
+
merkleProofIndices.push((merkleProof.index >> i) & 1);
|
|
186
|
+
if (merkleProofSiblings[i] === undefined) {
|
|
187
|
+
merkleProofSiblings[i] = "0";
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return [4 /*yield*/, snarkjs.groth16.fullProve({
|
|
191
|
+
secret: identity.secretScalar,
|
|
192
|
+
merkleProofLength: merkleProofLength,
|
|
193
|
+
merkleProofIndices: merkleProofIndices,
|
|
194
|
+
merkleProofSiblings: merkleProofSiblings,
|
|
195
|
+
scope: hash(scope),
|
|
196
|
+
message: hash(message)
|
|
197
|
+
}, snarkArtifacts.wasmFilePath, snarkArtifacts.zkeyFilePath)];
|
|
198
|
+
case 3:
|
|
199
|
+
_a = _b.sent(), proof = _a.proof, publicSignals = _a.publicSignals;
|
|
200
|
+
return [2 /*return*/, {
|
|
201
|
+
merkleTreeDepth: merkleTreeDepth,
|
|
202
|
+
merkleTreeRoot: publicSignals[0],
|
|
203
|
+
nullifier: publicSignals[1],
|
|
204
|
+
message: bignumber.BigNumber.from(message).toString(),
|
|
205
|
+
scope: bignumber.BigNumber.from(scope).toString(),
|
|
206
|
+
points: packPoints(proof)
|
|
207
|
+
}];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
204
211
|
}
|
|
205
212
|
|
|
206
|
-
/**
|
|
207
|
-
* Unpacks the Groth16 proof points into their original form.
|
|
208
|
-
* @param proof The proof points compatible with Semaphore.
|
|
209
|
-
* @returns The proof points compatible with SnarkJS.
|
|
210
|
-
*/
|
|
211
|
-
function unpackPoints(proof) {
|
|
212
|
-
return {
|
|
213
|
-
pi_a: [proof[0], proof[1]],
|
|
214
|
-
pi_b: [
|
|
215
|
-
[proof[3], proof[2]],
|
|
216
|
-
[proof[5], proof[4]]
|
|
217
|
-
],
|
|
218
|
-
pi_c: [proof[6], proof[7]],
|
|
219
|
-
protocol: "groth16",
|
|
220
|
-
curve: "bn128"
|
|
221
|
-
};
|
|
213
|
+
/**
|
|
214
|
+
* Unpacks the Groth16 proof points into their original form.
|
|
215
|
+
* @param proof The proof points compatible with Semaphore.
|
|
216
|
+
* @returns The proof points compatible with SnarkJS.
|
|
217
|
+
*/
|
|
218
|
+
function unpackPoints(proof) {
|
|
219
|
+
return {
|
|
220
|
+
pi_a: [proof[0], proof[1]],
|
|
221
|
+
pi_b: [
|
|
222
|
+
[proof[3], proof[2]],
|
|
223
|
+
[proof[5], proof[4]]
|
|
224
|
+
],
|
|
225
|
+
pi_c: [proof[6], proof[7]],
|
|
226
|
+
protocol: "groth16",
|
|
227
|
+
curve: "bn128"
|
|
228
|
+
};
|
|
222
229
|
}
|
|
223
230
|
|
|
224
231
|
var protocol = "groth16";
|
|
@@ -795,25 +802,28 @@ var verificationKeys = {
|
|
|
795
802
|
IC: IC
|
|
796
803
|
};
|
|
797
804
|
|
|
798
|
-
/**
|
|
799
|
-
* Verifies a Semaphore proof.
|
|
800
|
-
* @param
|
|
801
|
-
* @
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
});
|
|
805
|
+
/**
|
|
806
|
+
* Verifies a Semaphore proof.
|
|
807
|
+
* @param proof The Semaphore proof.
|
|
808
|
+
* @returns True if the proof is valid, false otherwise.
|
|
809
|
+
*/
|
|
810
|
+
function verifyProof(_a) {
|
|
811
|
+
var merkleTreeDepth = _a.merkleTreeDepth, merkleTreeRoot = _a.merkleTreeRoot, nullifier = _a.nullifier, message = _a.message, scope = _a.scope, points = _a.points;
|
|
812
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
813
|
+
var verificationKey;
|
|
814
|
+
return __generator(this, function (_b) {
|
|
815
|
+
// TODO: support all tree depths after trusted-setup.
|
|
816
|
+
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
817
|
+
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
818
|
+
}
|
|
819
|
+
verificationKey = __assign(__assign({}, verificationKeys), { vk_delta_2: verificationKeys.vk_delta_2[merkleTreeDepth - 1], IC: verificationKeys.IC[merkleTreeDepth - 1] });
|
|
820
|
+
return [2 /*return*/, snarkjs.groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)], unpackPoints(points))];
|
|
821
|
+
});
|
|
822
|
+
});
|
|
817
823
|
}
|
|
818
824
|
|
|
819
|
-
|
|
825
|
+
exports.generateProof = generateProof;
|
|
826
|
+
exports.getSnarkArtifacts = getSnarkArtifacts;
|
|
827
|
+
exports.packPoints = packPoints;
|
|
828
|
+
exports.unpackPoints = unpackPoints;
|
|
829
|
+
exports.verifyProof = verifyProof;
|