@semaphore-protocol/contracts 2.6.1 → 3.0.0-beta.6

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.
@@ -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 Verifier18 {
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
- [9218320951536642499143228327011901814587826948504871816273184688188019956292, 19717684456458906358368865507225121991585492363133107109865920739019288468011],
168
- [16717590750910963405756115910371408378114896008824240863060392362901176601412, 18221695645112467945186983098720611586049108689347006136423489099202471884089]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 4691595252082380256698158158199364410440273386659834000993210659508747323919,
176
- 9205801980459323513061837717352821162780471027241700646145937351740096374660
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 16150531426263112884093068164597994126623437929929609532055221646496813246000,
181
- 20245743178241899668170758952526381872637304119026868520579207157118516761827
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 6063536446992770713985314309889717594240410784717230886576072989709763902848,
186
- 18258781411255795973918859665416013869184055573057512603788635470145328981347
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 10109932964756104512054045207253535333686585863745296080906925765480296575285,
191
- 4174640428253153601540284363759502713687021920150940723252842152556151210349
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 18049428534741480832385046397049175120355008065781483226058177421025493210952,
196
- 591730261265040164434889324846001338201068482543108348317417391345612814922
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
- }
@@ -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 Verifier19 {
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
- [3995128789564535587814512245259203300137618476815456454931286633947953135662, 15953239752392927777442331623182226063776310198012173504208557434319753428770],
168
- [20957319343912866335583737646657534123362052690050674068142580221965936605075, 2523786679709693946058523307330825034772478122295850507521258983130425334580]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 9877211178693075145402462781884120278654771727348087433632224794894486095150,
176
- 19972682062587174829535281061580296764150591339640180868104711395548066529340
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 6324578424031095537345184040149690238371517387586958921377481904541316423724,
181
- 15513931720576048544404512239839508014664224085062729779520992909505663748296
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 11371337652479737143800707796204655130812036287859296372695832558127430723628,
186
- 11757275188600040111649009832378343123994225623498773406233261322165903848967
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 13282496583564708104981015168203451877588903263486398132954741568835583461335,
191
- 1746144324840370907926720490289700342734912534857331743685374514401176014195
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 7993952462467372951144011615584426050192046712674662254138390197508963352374,
196
- 5156942148925224345709309361345680948125600198010285179548841917923439945819
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
- }