@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
package/README.md
CHANGED
|
@@ -93,13 +93,13 @@ const message = utils.formatBytes32String("Hello world")
|
|
|
93
93
|
|
|
94
94
|
group.addMembers([...identityCommitments, identity.generateCommitment()])
|
|
95
95
|
|
|
96
|
-
const
|
|
96
|
+
const proof1 = await generateProof(identity, group, message, scope)
|
|
97
97
|
|
|
98
98
|
// You can also specify the maximum tree depth supported by the proof.
|
|
99
|
-
const
|
|
99
|
+
const proof2 = await generateProof(identity, group, message, scope, 20)
|
|
100
100
|
|
|
101
101
|
// You can also specify the default zkey/wasm files.
|
|
102
|
-
const
|
|
102
|
+
const proof3 = await generateProof(identity, group, message, scope, 20, {
|
|
103
103
|
wasmFilePath: "./semaphore.wasm",
|
|
104
104
|
zkeyFilePath: "./semaphore.zkey"
|
|
105
105
|
})
|
|
@@ -110,5 +110,5 @@ const fullProof3 = await generateProof(identity, group, message, scope, 20, {
|
|
|
110
110
|
```typescript
|
|
111
111
|
import { verifyProof } from "@semaphore-protocol/proof"
|
|
112
112
|
|
|
113
|
-
await verifyProof(
|
|
113
|
+
await verifyProof(proof1)
|
|
114
114
|
```
|
|
@@ -1,6 +1,6 @@
|
|
|
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
|
|
@@ -25,7 +25,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
25
25
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
26
26
|
PERFORMANCE OF THIS SOFTWARE.
|
|
27
27
|
***************************************************************************** */
|
|
28
|
-
/* global Reflect, Promise */
|
|
28
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
var __assign = function() {
|
|
@@ -75,133 +75,138 @@ function __generator(thisArg, body) {
|
|
|
75
75
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
76
76
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
77
77
|
}
|
|
78
|
-
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
81
|
+
var e = new Error(message);
|
|
82
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
83
|
+
};
|
|
79
84
|
|
|
80
|
-
function getSnarkArtifacts(treeDepth) {
|
|
81
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
-
return __generator(this, function (_a) {
|
|
83
|
-
return [2 /*return*/, {
|
|
84
|
-
wasmFilePath: "https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"),
|
|
85
|
-
zkeyFilePath: "https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey")
|
|
86
|
-
}];
|
|
87
|
-
});
|
|
88
|
-
});
|
|
85
|
+
function getSnarkArtifacts(treeDepth) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
return [2 /*return*/, {
|
|
89
|
+
wasmFilePath: "https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"),
|
|
90
|
+
zkeyFilePath: "https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey")
|
|
91
|
+
}];
|
|
92
|
+
});
|
|
93
|
+
});
|
|
89
94
|
}
|
|
90
95
|
|
|
91
|
-
/**
|
|
92
|
-
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
|
93
|
-
* @param message The message to be hashed.
|
|
94
|
-
* @returns The message digest.
|
|
95
|
-
*/
|
|
96
|
-
function hash(message) {
|
|
97
|
-
message = BigNumber.from(message).toTwos(256).toHexString();
|
|
98
|
-
message = zeroPad(message, 32);
|
|
99
|
-
return (BigInt(keccak256(message)) >> BigInt(8)).toString();
|
|
96
|
+
/**
|
|
97
|
+
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
|
98
|
+
* @param message The message to be hashed.
|
|
99
|
+
* @returns The message digest.
|
|
100
|
+
*/
|
|
101
|
+
function hash(message) {
|
|
102
|
+
message = BigNumber.from(message).toTwos(256).toHexString();
|
|
103
|
+
message = zeroPad(message, 32);
|
|
104
|
+
return (BigInt(keccak256(message)) >> BigInt(8)).toString();
|
|
100
105
|
}
|
|
101
106
|
|
|
102
|
-
/**
|
|
103
|
-
* Packs the Groth16 proof points into a format compatible with Semaphore.
|
|
104
|
-
* @param proof The proof points generated with SnarkJS.
|
|
105
|
-
* @returns The proof points compatible with Semaphore.
|
|
106
|
-
*/
|
|
107
|
-
function packPoints(proof) {
|
|
108
|
-
return [
|
|
109
|
-
proof.pi_a[0],
|
|
110
|
-
proof.pi_a[1],
|
|
111
|
-
proof.pi_b[0][1],
|
|
112
|
-
proof.pi_b[0][0],
|
|
113
|
-
proof.pi_b[1][1],
|
|
114
|
-
proof.pi_b[1][0],
|
|
115
|
-
proof.pi_c[0],
|
|
116
|
-
proof.pi_c[1]
|
|
117
|
-
];
|
|
107
|
+
/**
|
|
108
|
+
* Packs the Groth16 proof points into a format compatible with Semaphore.
|
|
109
|
+
* @param proof The proof points generated with SnarkJS.
|
|
110
|
+
* @returns The proof points compatible with Semaphore.
|
|
111
|
+
*/
|
|
112
|
+
function packPoints(proof) {
|
|
113
|
+
return [
|
|
114
|
+
proof.pi_a[0],
|
|
115
|
+
proof.pi_a[1],
|
|
116
|
+
proof.pi_b[0][1],
|
|
117
|
+
proof.pi_b[0][0],
|
|
118
|
+
proof.pi_b[1][1],
|
|
119
|
+
proof.pi_b[1][0],
|
|
120
|
+
proof.pi_c[0],
|
|
121
|
+
proof.pi_c[1]
|
|
122
|
+
];
|
|
118
123
|
}
|
|
119
124
|
|
|
120
|
-
/**
|
|
121
|
-
* Generates a Semaphore proof.
|
|
122
|
-
* @param identity The Semaphore identity.
|
|
123
|
-
* @param groupOrMerkleProof The Semaphore group or its Merkle proof.
|
|
124
|
-
* @param scope The external nullifier.
|
|
125
|
-
* @param message The Semaphore signal.
|
|
126
|
-
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
127
|
-
* @param snarkArtifacts The SNARK artifacts.
|
|
128
|
-
* @returns The Semaphore proof ready to be verified.
|
|
129
|
-
*/
|
|
130
|
-
function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeDepth, snarkArtifacts) {
|
|
131
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
132
|
-
var merkleProof, leafIndex, merkleProofLength, merkleProofIndices, merkleProofSiblings, i, _a, proof, publicSignals;
|
|
133
|
-
return __generator(this, function (_b) {
|
|
134
|
-
switch (_b.label) {
|
|
135
|
-
case 0:
|
|
136
|
-
if ("siblings" in groupOrMerkleProof) {
|
|
137
|
-
merkleProof = groupOrMerkleProof;
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
leafIndex = groupOrMerkleProof.indexOf(identity.commitment);
|
|
141
|
-
merkleProof = groupOrMerkleProof.generateMerkleProof(leafIndex);
|
|
142
|
-
}
|
|
143
|
-
merkleProofLength = merkleProof.siblings.length;
|
|
144
|
-
if (merkleTreeDepth !== undefined) {
|
|
145
|
-
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
146
|
-
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
merkleTreeDepth = merkleProofLength;
|
|
151
|
-
}
|
|
152
|
-
if (!!snarkArtifacts) return [3 /*break*/, 2];
|
|
153
|
-
return [4 /*yield*/, getSnarkArtifacts(merkleTreeDepth)];
|
|
154
|
-
case 1:
|
|
155
|
-
snarkArtifacts = _b.sent();
|
|
156
|
-
_b.label = 2;
|
|
157
|
-
case 2:
|
|
158
|
-
merkleProofIndices = [];
|
|
159
|
-
merkleProofSiblings = merkleProof.siblings;
|
|
160
|
-
for (i = 0; i < merkleTreeDepth; i += 1) {
|
|
161
|
-
merkleProofIndices.push((merkleProof.index >> i) & 1);
|
|
162
|
-
if (merkleProofSiblings[i] === undefined) {
|
|
163
|
-
merkleProofSiblings[i] = "0";
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return [4 /*yield*/, groth16.fullProve({
|
|
167
|
-
secret: identity.secretScalar,
|
|
168
|
-
merkleProofLength: merkleProofLength,
|
|
169
|
-
merkleProofIndices: merkleProofIndices,
|
|
170
|
-
merkleProofSiblings: merkleProofSiblings,
|
|
171
|
-
scope: hash(scope),
|
|
172
|
-
message: hash(message)
|
|
173
|
-
}, snarkArtifacts.wasmFilePath, snarkArtifacts.zkeyFilePath)];
|
|
174
|
-
case 3:
|
|
175
|
-
_a = _b.sent(), proof = _a.proof, publicSignals = _a.publicSignals;
|
|
176
|
-
return [2 /*return*/, {
|
|
177
|
-
merkleTreeDepth: merkleTreeDepth,
|
|
178
|
-
merkleTreeRoot: publicSignals[0],
|
|
179
|
-
nullifier: publicSignals[1],
|
|
180
|
-
message: BigNumber.from(message).toString(),
|
|
181
|
-
scope: BigNumber.from(scope).toString(),
|
|
182
|
-
points: packPoints(proof)
|
|
183
|
-
}];
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
});
|
|
125
|
+
/**
|
|
126
|
+
* Generates a Semaphore proof.
|
|
127
|
+
* @param identity The Semaphore identity.
|
|
128
|
+
* @param groupOrMerkleProof The Semaphore group or its Merkle proof.
|
|
129
|
+
* @param scope The external nullifier.
|
|
130
|
+
* @param message The Semaphore signal.
|
|
131
|
+
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
132
|
+
* @param snarkArtifacts The SNARK artifacts.
|
|
133
|
+
* @returns The Semaphore proof ready to be verified.
|
|
134
|
+
*/
|
|
135
|
+
function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeDepth, snarkArtifacts) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
+
var merkleProof, leafIndex, merkleProofLength, merkleProofIndices, merkleProofSiblings, i, _a, proof, publicSignals;
|
|
138
|
+
return __generator(this, function (_b) {
|
|
139
|
+
switch (_b.label) {
|
|
140
|
+
case 0:
|
|
141
|
+
if ("siblings" in groupOrMerkleProof) {
|
|
142
|
+
merkleProof = groupOrMerkleProof;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
leafIndex = groupOrMerkleProof.indexOf(identity.commitment);
|
|
146
|
+
merkleProof = groupOrMerkleProof.generateMerkleProof(leafIndex);
|
|
147
|
+
}
|
|
148
|
+
merkleProofLength = merkleProof.siblings.length;
|
|
149
|
+
if (merkleTreeDepth !== undefined) {
|
|
150
|
+
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
151
|
+
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
merkleTreeDepth = merkleProofLength;
|
|
156
|
+
}
|
|
157
|
+
if (!!snarkArtifacts) return [3 /*break*/, 2];
|
|
158
|
+
return [4 /*yield*/, getSnarkArtifacts(merkleTreeDepth)];
|
|
159
|
+
case 1:
|
|
160
|
+
snarkArtifacts = _b.sent();
|
|
161
|
+
_b.label = 2;
|
|
162
|
+
case 2:
|
|
163
|
+
merkleProofIndices = [];
|
|
164
|
+
merkleProofSiblings = merkleProof.siblings;
|
|
165
|
+
for (i = 0; i < merkleTreeDepth; i += 1) {
|
|
166
|
+
merkleProofIndices.push((merkleProof.index >> i) & 1);
|
|
167
|
+
if (merkleProofSiblings[i] === undefined) {
|
|
168
|
+
merkleProofSiblings[i] = "0";
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return [4 /*yield*/, groth16.fullProve({
|
|
172
|
+
secret: identity.secretScalar,
|
|
173
|
+
merkleProofLength: merkleProofLength,
|
|
174
|
+
merkleProofIndices: merkleProofIndices,
|
|
175
|
+
merkleProofSiblings: merkleProofSiblings,
|
|
176
|
+
scope: hash(scope),
|
|
177
|
+
message: hash(message)
|
|
178
|
+
}, snarkArtifacts.wasmFilePath, snarkArtifacts.zkeyFilePath)];
|
|
179
|
+
case 3:
|
|
180
|
+
_a = _b.sent(), proof = _a.proof, publicSignals = _a.publicSignals;
|
|
181
|
+
return [2 /*return*/, {
|
|
182
|
+
merkleTreeDepth: merkleTreeDepth,
|
|
183
|
+
merkleTreeRoot: publicSignals[0],
|
|
184
|
+
nullifier: publicSignals[1],
|
|
185
|
+
message: BigNumber.from(message).toString(),
|
|
186
|
+
scope: BigNumber.from(scope).toString(),
|
|
187
|
+
points: packPoints(proof)
|
|
188
|
+
}];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
187
192
|
}
|
|
188
193
|
|
|
189
|
-
/**
|
|
190
|
-
* Unpacks the Groth16 proof points into their original form.
|
|
191
|
-
* @param proof The proof points compatible with Semaphore.
|
|
192
|
-
* @returns The proof points compatible with SnarkJS.
|
|
193
|
-
*/
|
|
194
|
-
function unpackPoints(proof) {
|
|
195
|
-
return {
|
|
196
|
-
pi_a: [proof[0], proof[1]],
|
|
197
|
-
pi_b: [
|
|
198
|
-
[proof[3], proof[2]],
|
|
199
|
-
[proof[5], proof[4]]
|
|
200
|
-
],
|
|
201
|
-
pi_c: [proof[6], proof[7]],
|
|
202
|
-
protocol: "groth16",
|
|
203
|
-
curve: "bn128"
|
|
204
|
-
};
|
|
194
|
+
/**
|
|
195
|
+
* Unpacks the Groth16 proof points into their original form.
|
|
196
|
+
* @param proof The proof points compatible with Semaphore.
|
|
197
|
+
* @returns The proof points compatible with SnarkJS.
|
|
198
|
+
*/
|
|
199
|
+
function unpackPoints(proof) {
|
|
200
|
+
return {
|
|
201
|
+
pi_a: [proof[0], proof[1]],
|
|
202
|
+
pi_b: [
|
|
203
|
+
[proof[3], proof[2]],
|
|
204
|
+
[proof[5], proof[4]]
|
|
205
|
+
],
|
|
206
|
+
pi_c: [proof[6], proof[7]],
|
|
207
|
+
protocol: "groth16",
|
|
208
|
+
curve: "bn128"
|
|
209
|
+
};
|
|
205
210
|
}
|
|
206
211
|
|
|
207
212
|
var protocol = "groth16";
|
|
@@ -778,25 +783,24 @@ var verificationKeys = {
|
|
|
778
783
|
IC: IC
|
|
779
784
|
};
|
|
780
785
|
|
|
781
|
-
/**
|
|
782
|
-
* Verifies a Semaphore proof.
|
|
783
|
-
* @param
|
|
784
|
-
* @
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
});
|
|
786
|
+
/**
|
|
787
|
+
* Verifies a Semaphore proof.
|
|
788
|
+
* @param proof The Semaphore proof.
|
|
789
|
+
* @returns True if the proof is valid, false otherwise.
|
|
790
|
+
*/
|
|
791
|
+
function verifyProof(_a) {
|
|
792
|
+
var merkleTreeDepth = _a.merkleTreeDepth, merkleTreeRoot = _a.merkleTreeRoot, nullifier = _a.nullifier, message = _a.message, scope = _a.scope, points = _a.points;
|
|
793
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
794
|
+
var verificationKey;
|
|
795
|
+
return __generator(this, function (_b) {
|
|
796
|
+
// TODO: support all tree depths after trusted-setup.
|
|
797
|
+
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
798
|
+
throw new TypeError("The tree depth must be a number between 1 and 12");
|
|
799
|
+
}
|
|
800
|
+
verificationKey = __assign(__assign({}, verificationKeys), { vk_delta_2: verificationKeys.vk_delta_2[merkleTreeDepth - 1], IC: verificationKeys.IC[merkleTreeDepth - 1] });
|
|
801
|
+
return [2 /*return*/, groth16.verify(verificationKey, [merkleTreeRoot, nullifier, hash(message), hash(scope)], unpackPoints(points))];
|
|
802
|
+
});
|
|
803
|
+
});
|
|
800
804
|
}
|
|
801
805
|
|
|
802
806
|
export { generateProof, getSnarkArtifacts, packPoints, unpackPoints, verifyProof };
|