@semaphore-protocol/proof 4.0.0-beta.1 → 4.0.0-beta.3
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.browser.js +61 -81
- package/dist/index.node.cjs +85 -105
- package/dist/index.node.js +61 -81
- package/dist/types/generate-proof.d.ts +2 -1
- package/dist/types/hash.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/to-bigint.d.ts +2 -2
- package/dist/types/types/index.d.ts +2 -12
- package/package.json +12 -11
- package/src/generate-proof.ts +9 -7
- package/src/get-snark-artifacts.browser.ts +1 -1
- package/src/get-snark-artifacts.node.ts +1 -1
- package/src/hash.ts +1 -1
- package/src/index.ts +1 -1
- package/src/to-bigint.ts +2 -2
- package/src/types/index.ts +2 -14
- package/src/verify-proof.ts +5 -11
package/dist/index.browser.js
CHANGED
|
@@ -1,88 +1,69 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/proof
|
|
3
|
-
* @version 4.0.0-beta.
|
|
3
|
+
* @version 4.0.0-beta.3
|
|
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
|
-
import { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils';
|
|
10
|
-
export { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils';
|
|
11
|
-
import {
|
|
9
|
+
import { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils/proof-packing';
|
|
10
|
+
export { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils/proof-packing';
|
|
11
|
+
import { MIN_DEPTH, MAX_DEPTH } from '@semaphore-protocol/utils/constants';
|
|
12
|
+
import { requireNumber, requireDefined, requireObject, requireTypes, requireString, requireArray } from '@zk-kit/utils/error-handlers';
|
|
12
13
|
import { groth16 } from 'snarkjs';
|
|
13
14
|
import { keccak256 } from 'ethers/crypto';
|
|
14
15
|
import { toBeHex, toBigInt as toBigInt$1 } from 'ethers/utils';
|
|
15
16
|
import { encodeBytes32String } from 'ethers/abi';
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
67
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
68
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
69
|
-
default:
|
|
70
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
71
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
72
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
73
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
74
|
-
if (t[2]) _.ops.pop();
|
|
75
|
-
_.trys.pop(); continue;
|
|
76
|
-
}
|
|
77
|
-
op = body.call(thisArg, _);
|
|
78
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
79
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
84
|
-
var e = new Error(message);
|
|
85
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
18
|
+
var __assign = function() {
|
|
19
|
+
__assign = Object.assign || function __assign(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function __generator(thisArg, body) {
|
|
38
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
39
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
+
function step(op) {
|
|
42
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
45
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
+
switch (op[0]) {
|
|
47
|
+
case 0: case 1: t = op; break;
|
|
48
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
+
default:
|
|
52
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
+
if (t[2]) _.ops.pop();
|
|
57
|
+
_.trys.pop(); continue;
|
|
58
|
+
}
|
|
59
|
+
op = body.call(thisArg, _);
|
|
60
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
65
|
+
var e = new Error(message);
|
|
66
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
86
67
|
};
|
|
87
68
|
|
|
88
69
|
/**
|
|
@@ -113,7 +94,7 @@ function hash(message) {
|
|
|
113
94
|
}
|
|
114
95
|
|
|
115
96
|
/**
|
|
116
|
-
* Converts a bignumberish to a bigint.
|
|
97
|
+
* Converts a bignumberish or a text to a bigint.
|
|
117
98
|
* @param value The value to be converted to bigint.
|
|
118
99
|
* @return The value converted to bigint.
|
|
119
100
|
*/
|
|
@@ -161,8 +142,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
161
142
|
requireDefined(scope, "scope");
|
|
162
143
|
requireObject(identity, "identity");
|
|
163
144
|
requireObject(groupOrMerkleProof, "groupOrMerkleProof");
|
|
164
|
-
requireTypes(message, "message", ["string", "bigint", "number", "
|
|
165
|
-
requireTypes(scope, "scope", ["string", "bigint", "number", "
|
|
145
|
+
requireTypes(message, "message", ["string", "bigint", "number", "Uint8Array"]);
|
|
146
|
+
requireTypes(scope, "scope", ["string", "bigint", "number", "Uint8Array"]);
|
|
166
147
|
if (merkleTreeDepth) {
|
|
167
148
|
requireNumber(merkleTreeDepth, "merkleTreeDepth");
|
|
168
149
|
}
|
|
@@ -184,8 +165,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
184
165
|
}
|
|
185
166
|
merkleProofLength = merkleProof.siblings.length;
|
|
186
167
|
if (merkleTreeDepth !== undefined) {
|
|
187
|
-
if (merkleTreeDepth <
|
|
188
|
-
throw new TypeError("The tree depth must be a number between
|
|
168
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
169
|
+
throw new TypeError("The tree depth must be a number between ".concat(MIN_DEPTH, " and ").concat(MAX_DEPTH));
|
|
189
170
|
}
|
|
190
171
|
}
|
|
191
172
|
else {
|
|
@@ -821,9 +802,8 @@ function verifyProof(proof) {
|
|
|
821
802
|
requireString(message, "proof.message");
|
|
822
803
|
requireString(scope, "proof.scope");
|
|
823
804
|
requireArray(points, "proof.points");
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
805
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
806
|
+
throw new TypeError("The tree depth must be a number between ".concat(MIN_DEPTH, " and ").concat(MAX_DEPTH));
|
|
827
807
|
}
|
|
828
808
|
verificationKey = __assign(__assign({}, verificationKeys), { vk_delta_2: verificationKeys.vk_delta_2[merkleTreeDepth - 1], IC: verificationKeys.IC[merkleTreeDepth - 1] });
|
|
829
809
|
return [2 /*return*/, groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)], unpackGroth16Proof(points))];
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/proof
|
|
3
|
-
* @version 4.0.0-beta.
|
|
3
|
+
* @version 4.0.0-beta.3
|
|
4
4
|
* @file A library to generate and verify Semaphore proofs.
|
|
5
5
|
* @copyright Ethereum Foundation 2024
|
|
6
6
|
* @license MIT
|
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
|
-
var
|
|
12
|
-
var
|
|
11
|
+
var proofPacking = require('@zk-kit/utils/proof-packing');
|
|
12
|
+
var constants = require('@semaphore-protocol/utils/constants');
|
|
13
|
+
var errorHandlers = require('@zk-kit/utils/error-handlers');
|
|
13
14
|
var snarkjs = require('snarkjs');
|
|
14
15
|
var node_fs = require('node:fs');
|
|
15
16
|
var promises$1 = require('node:fs/promises');
|
|
@@ -18,78 +19,58 @@ var node_path = require('node:path');
|
|
|
18
19
|
var node_stream = require('node:stream');
|
|
19
20
|
var promises = require('node:stream/promises');
|
|
20
21
|
var crypto = require('ethers/crypto');
|
|
21
|
-
var utils
|
|
22
|
+
var utils = require('ethers/utils');
|
|
22
23
|
var abi = require('ethers/abi');
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
74
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
75
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
76
|
-
default:
|
|
77
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
78
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
79
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
80
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
81
|
-
if (t[2]) _.ops.pop();
|
|
82
|
-
_.trys.pop(); continue;
|
|
83
|
-
}
|
|
84
|
-
op = body.call(thisArg, _);
|
|
85
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
86
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
91
|
-
var e = new Error(message);
|
|
92
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
25
|
+
var __assign = function() {
|
|
26
|
+
__assign = Object.assign || function __assign(t) {
|
|
27
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
28
|
+
s = arguments[i];
|
|
29
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
30
|
+
}
|
|
31
|
+
return t;
|
|
32
|
+
};
|
|
33
|
+
return __assign.apply(this, arguments);
|
|
34
|
+
};
|
|
35
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function __generator(thisArg, body) {
|
|
45
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
46
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
47
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
48
|
+
function step(op) {
|
|
49
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
50
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
51
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
52
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
53
|
+
switch (op[0]) {
|
|
54
|
+
case 0: case 1: t = op; break;
|
|
55
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
56
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
57
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
58
|
+
default:
|
|
59
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
60
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
61
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
62
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
63
|
+
if (t[2]) _.ops.pop();
|
|
64
|
+
_.trys.pop(); continue;
|
|
65
|
+
}
|
|
66
|
+
op = body.call(thisArg, _);
|
|
67
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
68
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
72
|
+
var e = new Error(message);
|
|
73
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
93
74
|
};
|
|
94
75
|
|
|
95
76
|
/**
|
|
@@ -134,7 +115,7 @@ function getSnarkArtifacts(treeDepth) {
|
|
|
134
115
|
return __generator(this, function (_a) {
|
|
135
116
|
switch (_a.label) {
|
|
136
117
|
case 0:
|
|
137
|
-
|
|
118
|
+
errorHandlers.requireNumber(treeDepth, "treeDepth");
|
|
138
119
|
tmpDir = "semaphore-proof";
|
|
139
120
|
tmpPath = "".concat(os.tmpdir(), "/").concat(tmpDir, "-").concat(treeDepth);
|
|
140
121
|
if (!(!node_fs.existsSync(tmpPath) || node_fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
|
|
@@ -160,21 +141,21 @@ function getSnarkArtifacts(treeDepth) {
|
|
|
160
141
|
* @returns The message digest.
|
|
161
142
|
*/
|
|
162
143
|
function hash(message) {
|
|
163
|
-
return (BigInt(crypto.keccak256(utils
|
|
144
|
+
return (BigInt(crypto.keccak256(utils.toBeHex(message, 32))) >> BigInt(8)).toString();
|
|
164
145
|
}
|
|
165
146
|
|
|
166
147
|
/**
|
|
167
|
-
* Converts a bignumberish to a bigint.
|
|
148
|
+
* Converts a bignumberish or a text to a bigint.
|
|
168
149
|
* @param value The value to be converted to bigint.
|
|
169
150
|
* @return The value converted to bigint.
|
|
170
151
|
*/
|
|
171
152
|
function toBigInt(value) {
|
|
172
153
|
try {
|
|
173
|
-
return utils
|
|
154
|
+
return utils.toBigInt(value);
|
|
174
155
|
}
|
|
175
156
|
catch (error) {
|
|
176
157
|
if (typeof value === "string") {
|
|
177
|
-
return utils
|
|
158
|
+
return utils.toBigInt(abi.encodeBytes32String(value));
|
|
178
159
|
}
|
|
179
160
|
throw TypeError(error.message);
|
|
180
161
|
}
|
|
@@ -206,19 +187,19 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
206
187
|
return __generator(this, function (_b) {
|
|
207
188
|
switch (_b.label) {
|
|
208
189
|
case 0:
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
190
|
+
errorHandlers.requireDefined(identity, "identity");
|
|
191
|
+
errorHandlers.requireDefined(groupOrMerkleProof, "groupOrMerkleProof");
|
|
192
|
+
errorHandlers.requireDefined(message, "message");
|
|
193
|
+
errorHandlers.requireDefined(scope, "scope");
|
|
194
|
+
errorHandlers.requireObject(identity, "identity");
|
|
195
|
+
errorHandlers.requireObject(groupOrMerkleProof, "groupOrMerkleProof");
|
|
196
|
+
errorHandlers.requireTypes(message, "message", ["string", "bigint", "number", "Uint8Array"]);
|
|
197
|
+
errorHandlers.requireTypes(scope, "scope", ["string", "bigint", "number", "Uint8Array"]);
|
|
217
198
|
if (merkleTreeDepth) {
|
|
218
|
-
|
|
199
|
+
errorHandlers.requireNumber(merkleTreeDepth, "merkleTreeDepth");
|
|
219
200
|
}
|
|
220
201
|
if (snarkArtifacts) {
|
|
221
|
-
|
|
202
|
+
errorHandlers.requireObject(snarkArtifacts, "snarkArtifacts");
|
|
222
203
|
}
|
|
223
204
|
// Message and scope can be strings, numbers or buffers (i.e. Uint8Array).
|
|
224
205
|
// They will be converted to bigints anyway.
|
|
@@ -235,8 +216,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
235
216
|
}
|
|
236
217
|
merkleProofLength = merkleProof.siblings.length;
|
|
237
218
|
if (merkleTreeDepth !== undefined) {
|
|
238
|
-
if (merkleTreeDepth <
|
|
239
|
-
throw new TypeError("The tree depth must be a number between
|
|
219
|
+
if (merkleTreeDepth < constants.MIN_DEPTH || merkleTreeDepth > constants.MAX_DEPTH) {
|
|
220
|
+
throw new TypeError("The tree depth must be a number between ".concat(constants.MIN_DEPTH, " and ").concat(constants.MAX_DEPTH));
|
|
240
221
|
}
|
|
241
222
|
}
|
|
242
223
|
else {
|
|
@@ -272,7 +253,7 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
272
253
|
nullifier: publicSignals[1],
|
|
273
254
|
message: message.toString(),
|
|
274
255
|
scope: scope.toString(),
|
|
275
|
-
points:
|
|
256
|
+
points: proofPacking.packGroth16Proof(proof)
|
|
276
257
|
}];
|
|
277
258
|
}
|
|
278
259
|
});
|
|
@@ -863,32 +844,31 @@ function verifyProof(proof) {
|
|
|
863
844
|
return __awaiter(this, void 0, void 0, function () {
|
|
864
845
|
var merkleTreeDepth, merkleTreeRoot, nullifier, message, scope, points, verificationKey;
|
|
865
846
|
return __generator(this, function (_a) {
|
|
866
|
-
|
|
867
|
-
|
|
847
|
+
errorHandlers.requireDefined(proof, "proof");
|
|
848
|
+
errorHandlers.requireObject(proof, "proof");
|
|
868
849
|
merkleTreeDepth = proof.merkleTreeDepth, merkleTreeRoot = proof.merkleTreeRoot, nullifier = proof.nullifier, message = proof.message, scope = proof.scope, points = proof.points;
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
850
|
+
errorHandlers.requireNumber(merkleTreeDepth, "proof.merkleTreeDepth");
|
|
851
|
+
errorHandlers.requireString(merkleTreeRoot, "proof.merkleTreeRoot");
|
|
852
|
+
errorHandlers.requireString(nullifier, "proof.nullifier");
|
|
853
|
+
errorHandlers.requireString(message, "proof.message");
|
|
854
|
+
errorHandlers.requireString(scope, "proof.scope");
|
|
855
|
+
errorHandlers.requireArray(points, "proof.points");
|
|
856
|
+
if (merkleTreeDepth < constants.MIN_DEPTH || merkleTreeDepth > constants.MAX_DEPTH) {
|
|
857
|
+
throw new TypeError("The tree depth must be a number between ".concat(constants.MIN_DEPTH, " and ").concat(constants.MAX_DEPTH));
|
|
878
858
|
}
|
|
879
859
|
verificationKey = __assign(__assign({}, verificationKeys), { vk_delta_2: verificationKeys.vk_delta_2[merkleTreeDepth - 1], IC: verificationKeys.IC[merkleTreeDepth - 1] });
|
|
880
|
-
return [2 /*return*/, snarkjs.groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)],
|
|
860
|
+
return [2 /*return*/, snarkjs.groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)], proofPacking.unpackGroth16Proof(points))];
|
|
881
861
|
});
|
|
882
862
|
});
|
|
883
863
|
}
|
|
884
864
|
|
|
885
865
|
Object.defineProperty(exports, "packGroth16Proof", {
|
|
886
866
|
enumerable: true,
|
|
887
|
-
get: function () { return
|
|
867
|
+
get: function () { return proofPacking.packGroth16Proof; }
|
|
888
868
|
});
|
|
889
869
|
Object.defineProperty(exports, "unpackGroth16Proof", {
|
|
890
870
|
enumerable: true,
|
|
891
|
-
get: function () { return
|
|
871
|
+
get: function () { return proofPacking.unpackGroth16Proof; }
|
|
892
872
|
});
|
|
893
873
|
exports.generateProof = generateProof;
|
|
894
874
|
exports.verifyProof = verifyProof;
|
package/dist/index.node.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/proof
|
|
3
|
-
* @version 4.0.0-beta.
|
|
3
|
+
* @version 4.0.0-beta.3
|
|
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
|
-
import { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils';
|
|
10
|
-
export { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils';
|
|
11
|
-
import {
|
|
9
|
+
import { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils/proof-packing';
|
|
10
|
+
export { packGroth16Proof, unpackGroth16Proof } from '@zk-kit/utils/proof-packing';
|
|
11
|
+
import { MIN_DEPTH, MAX_DEPTH } from '@semaphore-protocol/utils/constants';
|
|
12
|
+
import { requireNumber, requireDefined, requireObject, requireTypes, requireString, requireArray } from '@zk-kit/utils/error-handlers';
|
|
12
13
|
import { groth16 } from 'snarkjs';
|
|
13
14
|
import { existsSync, readdirSync, createWriteStream } from 'node:fs';
|
|
14
15
|
import { mkdir } from 'node:fs/promises';
|
|
@@ -20,75 +21,55 @@ import { keccak256 } from 'ethers/crypto';
|
|
|
20
21
|
import { toBeHex, toBigInt as toBigInt$1 } from 'ethers/utils';
|
|
21
22
|
import { encodeBytes32String } from 'ethers/abi';
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
73
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
74
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
75
|
-
default:
|
|
76
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
77
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
78
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
79
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
80
|
-
if (t[2]) _.ops.pop();
|
|
81
|
-
_.trys.pop(); continue;
|
|
82
|
-
}
|
|
83
|
-
op = body.call(thisArg, _);
|
|
84
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
85
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
90
|
-
var e = new Error(message);
|
|
91
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
24
|
+
var __assign = function() {
|
|
25
|
+
__assign = Object.assign || function __assign(t) {
|
|
26
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
27
|
+
s = arguments[i];
|
|
28
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
};
|
|
32
|
+
return __assign.apply(this, arguments);
|
|
33
|
+
};
|
|
34
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
35
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
36
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
37
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
38
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
39
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
40
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function __generator(thisArg, body) {
|
|
44
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
45
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
46
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
47
|
+
function step(op) {
|
|
48
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
49
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
50
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
51
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
52
|
+
switch (op[0]) {
|
|
53
|
+
case 0: case 1: t = op; break;
|
|
54
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
55
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
56
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
57
|
+
default:
|
|
58
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
59
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
60
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
61
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
62
|
+
if (t[2]) _.ops.pop();
|
|
63
|
+
_.trys.pop(); continue;
|
|
64
|
+
}
|
|
65
|
+
op = body.call(thisArg, _);
|
|
66
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
67
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
71
|
+
var e = new Error(message);
|
|
72
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
92
73
|
};
|
|
93
74
|
|
|
94
75
|
/**
|
|
@@ -163,7 +144,7 @@ function hash(message) {
|
|
|
163
144
|
}
|
|
164
145
|
|
|
165
146
|
/**
|
|
166
|
-
* Converts a bignumberish to a bigint.
|
|
147
|
+
* Converts a bignumberish or a text to a bigint.
|
|
167
148
|
* @param value The value to be converted to bigint.
|
|
168
149
|
* @return The value converted to bigint.
|
|
169
150
|
*/
|
|
@@ -211,8 +192,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
211
192
|
requireDefined(scope, "scope");
|
|
212
193
|
requireObject(identity, "identity");
|
|
213
194
|
requireObject(groupOrMerkleProof, "groupOrMerkleProof");
|
|
214
|
-
requireTypes(message, "message", ["string", "bigint", "number", "
|
|
215
|
-
requireTypes(scope, "scope", ["string", "bigint", "number", "
|
|
195
|
+
requireTypes(message, "message", ["string", "bigint", "number", "Uint8Array"]);
|
|
196
|
+
requireTypes(scope, "scope", ["string", "bigint", "number", "Uint8Array"]);
|
|
216
197
|
if (merkleTreeDepth) {
|
|
217
198
|
requireNumber(merkleTreeDepth, "merkleTreeDepth");
|
|
218
199
|
}
|
|
@@ -234,8 +215,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
|
|
|
234
215
|
}
|
|
235
216
|
merkleProofLength = merkleProof.siblings.length;
|
|
236
217
|
if (merkleTreeDepth !== undefined) {
|
|
237
|
-
if (merkleTreeDepth <
|
|
238
|
-
throw new TypeError("The tree depth must be a number between
|
|
218
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
219
|
+
throw new TypeError("The tree depth must be a number between ".concat(MIN_DEPTH, " and ").concat(MAX_DEPTH));
|
|
239
220
|
}
|
|
240
221
|
}
|
|
241
222
|
else {
|
|
@@ -871,9 +852,8 @@ function verifyProof(proof) {
|
|
|
871
852
|
requireString(message, "proof.message");
|
|
872
853
|
requireString(scope, "proof.scope");
|
|
873
854
|
requireArray(points, "proof.points");
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
855
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
856
|
+
throw new TypeError("The tree depth must be a number between ".concat(MIN_DEPTH, " and ").concat(MAX_DEPTH));
|
|
877
857
|
}
|
|
878
858
|
verificationKey = __assign(__assign({}, verificationKeys), { vk_delta_2: verificationKeys.vk_delta_2[merkleTreeDepth - 1], IC: verificationKeys.IC[merkleTreeDepth - 1] });
|
|
879
859
|
return [2 /*return*/, groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)], unpackGroth16Proof(points))];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Group, MerkleProof } from "@semaphore-protocol/group";
|
|
2
2
|
import type { Identity } from "@semaphore-protocol/identity";
|
|
3
|
-
import { BigNumberish
|
|
3
|
+
import type { BigNumberish } from "ethers";
|
|
4
|
+
import { SemaphoreProof, SnarkArtifacts } from "./types";
|
|
4
5
|
/**
|
|
5
6
|
* It generates a Semaphore proof, i.e. a zero-knowledge proof that an identity that
|
|
6
7
|
* is part of a group has shared an anonymous message.
|
package/dist/types/hash.d.ts
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { packGroth16Proof, unpackGroth16Proof } from "@zk-kit/utils";
|
|
1
|
+
import { packGroth16Proof, unpackGroth16Proof } from "@zk-kit/utils/proof-packing";
|
|
2
2
|
import generateProof from "./generate-proof";
|
|
3
3
|
import verifyProof from "./verify-proof";
|
|
4
4
|
export * from "./types";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BigNumberish } from "
|
|
1
|
+
import type { BigNumberish } from "ethers";
|
|
2
2
|
/**
|
|
3
|
-
* Converts a bignumberish to a bigint.
|
|
3
|
+
* Converts a bignumberish or a text to a bigint.
|
|
4
4
|
* @param value The value to be converted to bigint.
|
|
5
5
|
* @return The value converted to bigint.
|
|
6
6
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { NumericString } from "snarkjs";
|
|
2
|
-
|
|
2
|
+
import type { PackedGroth16Proof } from "@zk-kit/utils";
|
|
3
3
|
export type SnarkArtifacts = {
|
|
4
4
|
wasmFilePath: string;
|
|
5
5
|
zkeyFilePath: string;
|
|
@@ -10,15 +10,5 @@ export type SemaphoreProof = {
|
|
|
10
10
|
message: NumericString;
|
|
11
11
|
nullifier: NumericString;
|
|
12
12
|
scope: NumericString;
|
|
13
|
-
points:
|
|
13
|
+
points: PackedGroth16Proof;
|
|
14
14
|
};
|
|
15
|
-
export type PackedPoints = [
|
|
16
|
-
NumericString,
|
|
17
|
-
NumericString,
|
|
18
|
-
NumericString,
|
|
19
|
-
NumericString,
|
|
20
|
-
NumericString,
|
|
21
|
-
NumericString,
|
|
22
|
-
NumericString,
|
|
23
|
-
NumericString
|
|
24
|
-
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semaphore-protocol/proof",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.3",
|
|
4
4
|
"description": "A library to generate and verify Semaphore proofs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,23 +40,24 @@
|
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@rollup/plugin-alias": "^5.1.0",
|
|
42
42
|
"@rollup/plugin-json": "^6.1.0",
|
|
43
|
-
"@
|
|
44
|
-
"@types/snarkjs": "0
|
|
45
|
-
"@types/tmp": "^0.2.6",
|
|
43
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
44
|
+
"@types/snarkjs": "^0",
|
|
46
45
|
"poseidon-lite": "^0.2.0",
|
|
47
46
|
"rimraf": "^5.0.5",
|
|
48
|
-
"rollup": "^4.
|
|
49
|
-
"rollup-plugin-cleanup": "^3.2.1"
|
|
50
|
-
"rollup-plugin-typescript2": "^0.36.0"
|
|
47
|
+
"rollup": "^4.12.0",
|
|
48
|
+
"rollup-plugin-cleanup": "^3.2.1"
|
|
51
49
|
},
|
|
52
50
|
"peerDependencies": {
|
|
53
|
-
"@semaphore-protocol/group": "4.0.0-beta.
|
|
54
|
-
"@semaphore-protocol/identity": "4.0.0-beta.
|
|
51
|
+
"@semaphore-protocol/group": "4.0.0-beta.3",
|
|
52
|
+
"@semaphore-protocol/identity": "4.0.0-beta.3"
|
|
55
53
|
},
|
|
56
54
|
"dependencies": {
|
|
57
|
-
"@semaphore-protocol/utils": "4.0.0-beta.
|
|
58
|
-
"@zk-kit/utils": "0.
|
|
55
|
+
"@semaphore-protocol/utils": "4.0.0-beta.3",
|
|
56
|
+
"@zk-kit/utils": "0.8.1",
|
|
59
57
|
"ethers": "6.10.0",
|
|
60
58
|
"snarkjs": "0.7.3"
|
|
59
|
+
},
|
|
60
|
+
"resolutions": {
|
|
61
|
+
"web-worker": "1.2.0"
|
|
61
62
|
}
|
|
62
63
|
}
|
package/src/generate-proof.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { Group, MerkleProof } from "@semaphore-protocol/group"
|
|
2
2
|
import type { Identity } from "@semaphore-protocol/identity"
|
|
3
|
-
import {
|
|
3
|
+
import { MAX_DEPTH, MIN_DEPTH } from "@semaphore-protocol/utils/constants"
|
|
4
|
+
import { requireDefined, requireNumber, requireObject, requireTypes } from "@zk-kit/utils/error-handlers"
|
|
5
|
+
import { packGroth16Proof } from "@zk-kit/utils/proof-packing"
|
|
6
|
+
import type { BigNumberish } from "ethers"
|
|
4
7
|
import { NumericString, groth16 } from "snarkjs"
|
|
5
|
-
import { packGroth16Proof } from "@zk-kit/utils"
|
|
6
8
|
import getSnarkArtifacts from "./get-snark-artifacts.node"
|
|
7
9
|
import hash from "./hash"
|
|
8
10
|
import toBigInt from "./to-bigint"
|
|
9
|
-
import {
|
|
11
|
+
import { SemaphoreProof, SnarkArtifacts } from "./types"
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* It generates a Semaphore proof, i.e. a zero-knowledge proof that an identity that
|
|
@@ -43,8 +45,8 @@ export default async function generateProof(
|
|
|
43
45
|
|
|
44
46
|
requireObject(identity, "identity")
|
|
45
47
|
requireObject(groupOrMerkleProof, "groupOrMerkleProof")
|
|
46
|
-
requireTypes(message, "message", ["string", "bigint", "number", "
|
|
47
|
-
requireTypes(scope, "scope", ["string", "bigint", "number", "
|
|
48
|
+
requireTypes(message, "message", ["string", "bigint", "number", "Uint8Array"])
|
|
49
|
+
requireTypes(scope, "scope", ["string", "bigint", "number", "Uint8Array"])
|
|
48
50
|
|
|
49
51
|
if (merkleTreeDepth) {
|
|
50
52
|
requireNumber(merkleTreeDepth, "merkleTreeDepth")
|
|
@@ -73,8 +75,8 @@ export default async function generateProof(
|
|
|
73
75
|
const merkleProofLength = merkleProof.siblings.length
|
|
74
76
|
|
|
75
77
|
if (merkleTreeDepth !== undefined) {
|
|
76
|
-
if (merkleTreeDepth <
|
|
77
|
-
throw new TypeError(
|
|
78
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
79
|
+
throw new TypeError(`The tree depth must be a number between ${MIN_DEPTH} and ${MAX_DEPTH}`)
|
|
78
80
|
}
|
|
79
81
|
} else {
|
|
80
82
|
merkleTreeDepth = merkleProofLength
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* istanbul ignore file */
|
|
2
|
-
import { requireNumber } from "@
|
|
2
|
+
import { requireNumber } from "@zk-kit/utils/error-handlers"
|
|
3
3
|
import { createWriteStream, existsSync, readdirSync } from "node:fs"
|
|
4
4
|
import { mkdir } from "node:fs/promises"
|
|
5
5
|
import os from "node:os"
|
package/src/hash.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import type { BigNumberish } from "ethers"
|
|
1
2
|
import { keccak256 } from "ethers/crypto"
|
|
2
3
|
import { toBeHex } from "ethers/utils"
|
|
3
4
|
import { NumericString } from "snarkjs"
|
|
4
|
-
import { BigNumberish } from "./types"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
package/src/index.ts
CHANGED
package/src/to-bigint.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { BigNumberish } from "ethers"
|
|
1
2
|
import { encodeBytes32String } from "ethers/abi"
|
|
2
3
|
import { toBigInt as _toBigInt } from "ethers/utils"
|
|
3
|
-
import { BigNumberish } from "./types"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Converts a bignumberish to a bigint.
|
|
6
|
+
* Converts a bignumberish or a text to a bigint.
|
|
7
7
|
* @param value The value to be converted to bigint.
|
|
8
8
|
* @return The value converted to bigint.
|
|
9
9
|
*/
|
package/src/types/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { NumericString } from "snarkjs"
|
|
2
|
-
|
|
3
|
-
export type BigNumberish = string | number | bigint
|
|
2
|
+
import type { PackedGroth16Proof } from "@zk-kit/utils"
|
|
4
3
|
|
|
5
4
|
export type SnarkArtifacts = {
|
|
6
5
|
wasmFilePath: string
|
|
@@ -13,16 +12,5 @@ export type SemaphoreProof = {
|
|
|
13
12
|
message: NumericString
|
|
14
13
|
nullifier: NumericString
|
|
15
14
|
scope: NumericString
|
|
16
|
-
points:
|
|
15
|
+
points: PackedGroth16Proof
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
export type PackedPoints = [
|
|
20
|
-
NumericString,
|
|
21
|
-
NumericString,
|
|
22
|
-
NumericString,
|
|
23
|
-
NumericString,
|
|
24
|
-
NumericString,
|
|
25
|
-
NumericString,
|
|
26
|
-
NumericString,
|
|
27
|
-
NumericString
|
|
28
|
-
]
|
package/src/verify-proof.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
requireNumber,
|
|
5
|
-
requireObject,
|
|
6
|
-
requireString
|
|
7
|
-
} from "@semaphore-protocol/utils/errors"
|
|
1
|
+
import { MAX_DEPTH, MIN_DEPTH } from "@semaphore-protocol/utils/constants"
|
|
2
|
+
import { requireArray, requireDefined, requireNumber, requireObject, requireString } from "@zk-kit/utils/error-handlers"
|
|
3
|
+
import { unpackGroth16Proof } from "@zk-kit/utils/proof-packing"
|
|
8
4
|
import { groth16 } from "snarkjs"
|
|
9
|
-
import { unpackGroth16Proof } from "@zk-kit/utils"
|
|
10
5
|
import hash from "./hash"
|
|
11
6
|
import { SemaphoreProof } from "./types"
|
|
12
7
|
import verificationKeys from "./verification-keys.json"
|
|
@@ -30,9 +25,8 @@ export default async function verifyProof(proof: SemaphoreProof): Promise<boolea
|
|
|
30
25
|
requireString(scope, "proof.scope")
|
|
31
26
|
requireArray(points, "proof.points")
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
throw new TypeError("The tree depth must be a number between 1 and 12")
|
|
28
|
+
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
|
|
29
|
+
throw new TypeError(`The tree depth must be a number between ${MIN_DEPTH} and ${MAX_DEPTH}`)
|
|
36
30
|
}
|
|
37
31
|
|
|
38
32
|
const verificationKey = {
|