@semaphore-protocol/contracts 2.6.1 → 3.0.0-beta.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/LICENSE +21 -0
- package/Semaphore.sol +51 -34
- package/base/Pairing.sol +151 -0
- package/base/SemaphoreGroups.sol +17 -22
- package/base/SemaphoreVerifier.sol +113 -0
- package/extensions/SemaphoreVoting.sol +19 -37
- package/extensions/SemaphoreWhistleblowing.sol +21 -29
- package/interfaces/ISemaphore.sol +21 -16
- package/interfaces/ISemaphoreGroups.sol +1 -3
- package/interfaces/ISemaphoreVerifier.sol +37 -0
- package/interfaces/ISemaphoreVoting.sol +4 -4
- package/interfaces/ISemaphoreWhistleblowing.sol +3 -4
- package/package.json +2 -2
- package/base/SemaphoreConstants.sol +0 -4
- package/base/SemaphoreCore.sol +0 -45
- package/interfaces/ISemaphoreCore.sol +0 -10
- package/interfaces/ISemaphoreNullifiers.sol +0 -14
- package/interfaces/IVerifier.sol +0 -13
- package/verifiers/Verifier16.sol +0 -237
- package/verifiers/Verifier17.sol +0 -237
- package/verifiers/Verifier18.sol +0 -237
- package/verifiers/Verifier19.sol +0 -237
- package/verifiers/Verifier20.sol +0 -237
- package/verifiers/Verifier21.sol +0 -237
- package/verifiers/Verifier22.sol +0 -237
- package/verifiers/Verifier23.sol +0 -237
- package/verifiers/Verifier24.sol +0 -237
- package/verifiers/Verifier25.sol +0 -237
- package/verifiers/Verifier26.sol +0 -237
- package/verifiers/Verifier27.sol +0 -237
- package/verifiers/Verifier28.sol +0 -237
- package/verifiers/Verifier29.sol +0 -237
- package/verifiers/Verifier30.sol +0 -237
- package/verifiers/Verifier31.sol +0 -237
- package/verifiers/Verifier32.sol +0 -237
package/verifiers/Verifier32.sol
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2017 Christian Reitwiessner
|
|
3
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
5
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
6
|
-
//
|
|
7
|
-
// 2019 OKIMS
|
|
8
|
-
// ported to solidity 0.6
|
|
9
|
-
// fixed linter warnings
|
|
10
|
-
// added requiere error messages
|
|
11
|
-
//
|
|
12
|
-
// 2021 Remco Bloemen
|
|
13
|
-
// cleaned up code
|
|
14
|
-
// added InvalidProve() error
|
|
15
|
-
// always revert with InvalidProof() on invalid proof
|
|
16
|
-
// make Pairing strict
|
|
17
|
-
//
|
|
18
|
-
// SPDX-License-Identifier: GPL-3.0
|
|
19
|
-
pragma solidity ^0.8.4;
|
|
20
|
-
|
|
21
|
-
library Pairing {
|
|
22
|
-
error InvalidProof();
|
|
23
|
-
|
|
24
|
-
// The prime q in the base field F_q for G1
|
|
25
|
-
uint256 constant BASE_MODULUS = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
|
|
26
|
-
|
|
27
|
-
// The prime moludus of the scalar field of G1.
|
|
28
|
-
uint256 constant SCALAR_MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
|
29
|
-
|
|
30
|
-
struct G1Point {
|
|
31
|
-
uint256 X;
|
|
32
|
-
uint256 Y;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Encoding of field elements is: X[0] * z + X[1]
|
|
36
|
-
struct G2Point {
|
|
37
|
-
uint256[2] X;
|
|
38
|
-
uint256[2] Y;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/// @return the generator of G1
|
|
42
|
-
function P1() internal pure returns (G1Point memory) {
|
|
43
|
-
return G1Point(1, 2);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/// @return the generator of G2
|
|
47
|
-
function P2() internal pure returns (G2Point memory) {
|
|
48
|
-
return
|
|
49
|
-
G2Point(
|
|
50
|
-
[
|
|
51
|
-
11559732032986387107991004021392285783925812861821192530917403151452391805634,
|
|
52
|
-
10857046999023057135944570762232829481370756359578518086990519993285655852781
|
|
53
|
-
],
|
|
54
|
-
[
|
|
55
|
-
4082367875863433681332203403145435568316851327593401208105741076214120093531,
|
|
56
|
-
8495653923123431417604973247489272438418190587263600148770280649306958101930
|
|
57
|
-
]
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/// @return r the negation of p, i.e. p.addition(p.negate()) should be zero.
|
|
62
|
-
function negate(G1Point memory p) internal pure returns (G1Point memory r) {
|
|
63
|
-
if (p.X == 0 && p.Y == 0) return G1Point(0, 0);
|
|
64
|
-
// Validate input or revert
|
|
65
|
-
if (p.X >= BASE_MODULUS || p.Y >= BASE_MODULUS) revert InvalidProof();
|
|
66
|
-
// We know p.Y > 0 and p.Y < BASE_MODULUS.
|
|
67
|
-
return G1Point(p.X, BASE_MODULUS - p.Y);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/// @return r the sum of two points of G1
|
|
71
|
-
function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) {
|
|
72
|
-
// By EIP-196 all input is validated to be less than the BASE_MODULUS and form points
|
|
73
|
-
// on the curve.
|
|
74
|
-
uint256[4] memory input;
|
|
75
|
-
input[0] = p1.X;
|
|
76
|
-
input[1] = p1.Y;
|
|
77
|
-
input[2] = p2.X;
|
|
78
|
-
input[3] = p2.Y;
|
|
79
|
-
bool success;
|
|
80
|
-
// solium-disable-next-line security/no-inline-assembly
|
|
81
|
-
assembly {
|
|
82
|
-
success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60)
|
|
83
|
-
}
|
|
84
|
-
if (!success) revert InvalidProof();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/// @return r the product of a point on G1 and a scalar, i.e.
|
|
88
|
-
/// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p.
|
|
89
|
-
function scalar_mul(G1Point memory p, uint256 s) internal view returns (G1Point memory r) {
|
|
90
|
-
// By EIP-196 the values p.X and p.Y are verified to less than the BASE_MODULUS and
|
|
91
|
-
// form a valid point on the curve. But the scalar is not verified, so we do that explicitelly.
|
|
92
|
-
if (s >= SCALAR_MODULUS) revert InvalidProof();
|
|
93
|
-
uint256[3] memory input;
|
|
94
|
-
input[0] = p.X;
|
|
95
|
-
input[1] = p.Y;
|
|
96
|
-
input[2] = s;
|
|
97
|
-
bool success;
|
|
98
|
-
// solium-disable-next-line security/no-inline-assembly
|
|
99
|
-
assembly {
|
|
100
|
-
success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60)
|
|
101
|
-
}
|
|
102
|
-
if (!success) revert InvalidProof();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/// Asserts the pairing check
|
|
106
|
-
/// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1
|
|
107
|
-
/// For example pairing([P1(), P1().negate()], [P2(), P2()]) should succeed
|
|
108
|
-
function pairingCheck(G1Point[] memory p1, G2Point[] memory p2) internal view {
|
|
109
|
-
// By EIP-197 all input is verified to be less than the BASE_MODULUS and form elements in their
|
|
110
|
-
// respective groups of the right order.
|
|
111
|
-
if (p1.length != p2.length) revert InvalidProof();
|
|
112
|
-
uint256 elements = p1.length;
|
|
113
|
-
uint256 inputSize = elements * 6;
|
|
114
|
-
uint256[] memory input = new uint256[](inputSize);
|
|
115
|
-
for (uint256 i = 0; i < elements; i++) {
|
|
116
|
-
input[i * 6 + 0] = p1[i].X;
|
|
117
|
-
input[i * 6 + 1] = p1[i].Y;
|
|
118
|
-
input[i * 6 + 2] = p2[i].X[0];
|
|
119
|
-
input[i * 6 + 3] = p2[i].X[1];
|
|
120
|
-
input[i * 6 + 4] = p2[i].Y[0];
|
|
121
|
-
input[i * 6 + 5] = p2[i].Y[1];
|
|
122
|
-
}
|
|
123
|
-
uint256[1] memory out;
|
|
124
|
-
bool success;
|
|
125
|
-
// solium-disable-next-line security/no-inline-assembly
|
|
126
|
-
assembly {
|
|
127
|
-
success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
|
|
128
|
-
}
|
|
129
|
-
if (!success || out[0] != 1) revert InvalidProof();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
contract Verifier32 {
|
|
134
|
-
using Pairing for *;
|
|
135
|
-
|
|
136
|
-
struct VerifyingKey {
|
|
137
|
-
Pairing.G1Point alfa1;
|
|
138
|
-
Pairing.G2Point beta2;
|
|
139
|
-
Pairing.G2Point gamma2;
|
|
140
|
-
Pairing.G2Point delta2;
|
|
141
|
-
Pairing.G1Point[] IC;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
struct Proof {
|
|
145
|
-
Pairing.G1Point A;
|
|
146
|
-
Pairing.G2Point B;
|
|
147
|
-
Pairing.G1Point C;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
|
|
151
|
-
vk.alfa1 = Pairing.G1Point(
|
|
152
|
-
20491192805390485299153009773594534940189261866228447918068658471970481763042,
|
|
153
|
-
9383485363053290200918347156157836566562967994039712273449902621266178545958
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
vk.beta2 = Pairing.G2Point(
|
|
157
|
-
[4252822878758300859123897981450591353533073413197771768651442665752259397132, 6375614351688725206403948262868962793625744043794305715222011528459656738731],
|
|
158
|
-
[21847035105528745403288232691147584728191162732299865338377159692350059136679, 10505242626370262277552901082094356697409835680220590971873171140371331206856]
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
vk.gamma2 = Pairing.G2Point(
|
|
162
|
-
[11559732032986387107991004021392285783925812861821192530917403151452391805634, 10857046999023057135944570762232829481370756359578518086990519993285655852781],
|
|
163
|
-
[4082367875863433681332203403145435568316851327593401208105741076214120093531, 8495653923123431417604973247489272438418190587263600148770280649306958101930]
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
vk.delta2 = Pairing.G2Point(
|
|
167
|
-
[12315240965742683516581565369496371929586281338862761742109651525191835544242, 18994803742708336446369128568423705404354655742604689352630273180469431952708],
|
|
168
|
-
[18019403342409608922812569436317484250134945386869657285229378095251425778096, 12707009780301102830224094192984906206920666691015255692741008594808694787917]
|
|
169
|
-
);
|
|
170
|
-
|
|
171
|
-
vk.IC = new Pairing.G1Point[](5);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
vk.IC[0] = Pairing.G1Point(
|
|
175
|
-
2592407181901686208061988776764501828311271519595797153264758207470081204331,
|
|
176
|
-
11847594161160074962679125411562687287595382335410213641115001866587988494499
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
vk.IC[1] = Pairing.G1Point(
|
|
180
|
-
3346927026869562921166545684451290646273836362895645367665514203662899621366,
|
|
181
|
-
15758185693543979820528128025093553492246135914029575732836221618882836493143
|
|
182
|
-
);
|
|
183
|
-
|
|
184
|
-
vk.IC[2] = Pairing.G1Point(
|
|
185
|
-
20528686657810499188368147206002308531447185877994439397529705707372170337045,
|
|
186
|
-
18025396678079701612906003769476076600196287001844168390936182972248852818155
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
vk.IC[3] = Pairing.G1Point(
|
|
190
|
-
9799815250059685769827017947834627563597884023490186073806184882963949644596,
|
|
191
|
-
4998495094322372762314630336611134866447406022687118703953312157819349892603
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
vk.IC[4] = Pairing.G1Point(
|
|
195
|
-
16176535527670849161173306151058200762642157343823553073439957507563856439772,
|
|
196
|
-
21877331533292960470552563236986670222564955589137303622102707801351340670855
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/// @dev Verifies a Semaphore proof. Reverts with InvalidProof if the proof is invalid.
|
|
202
|
-
function verifyProof(
|
|
203
|
-
uint[2] memory a,
|
|
204
|
-
uint[2][2] memory b,
|
|
205
|
-
uint[2] memory c,
|
|
206
|
-
uint[4] memory input
|
|
207
|
-
) public view {
|
|
208
|
-
// If the values are not in the correct range, the Pairing contract will revert.
|
|
209
|
-
Proof memory proof;
|
|
210
|
-
proof.A = Pairing.G1Point(a[0], a[1]);
|
|
211
|
-
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
|
|
212
|
-
proof.C = Pairing.G1Point(c[0], c[1]);
|
|
213
|
-
|
|
214
|
-
VerifyingKey memory vk = verifyingKey();
|
|
215
|
-
|
|
216
|
-
// Compute the linear combination vk_x of inputs times IC
|
|
217
|
-
if (input.length + 1 != vk.IC.length) revert Pairing.InvalidProof();
|
|
218
|
-
Pairing.G1Point memory vk_x = vk.IC[0];
|
|
219
|
-
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[1], input[0]));
|
|
220
|
-
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[2], input[1]));
|
|
221
|
-
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[3], input[2]));
|
|
222
|
-
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[4], input[3]));
|
|
223
|
-
|
|
224
|
-
// Check pairing
|
|
225
|
-
Pairing.G1Point[] memory p1 = new Pairing.G1Point[](4);
|
|
226
|
-
Pairing.G2Point[] memory p2 = new Pairing.G2Point[](4);
|
|
227
|
-
p1[0] = Pairing.negate(proof.A);
|
|
228
|
-
p2[0] = proof.B;
|
|
229
|
-
p1[1] = vk.alfa1;
|
|
230
|
-
p2[1] = vk.beta2;
|
|
231
|
-
p1[2] = vk_x;
|
|
232
|
-
p2[2] = vk.gamma2;
|
|
233
|
-
p1[3] = proof.C;
|
|
234
|
-
p2[3] = vk.delta2;
|
|
235
|
-
Pairing.pairingCheck(p1, p2);
|
|
236
|
-
}
|
|
237
|
-
}
|