@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 Verifier28 {
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
- [4553625243522856553165922942982108474187282402890756796515747778282922584601, 16835654219229187428071649241190746119082269636345872682107941472241044260584],
168
- [3272293478534046729728233267765357195255129499603632413158978822084188871854, 873742823867191038535544062852920538566418819521732785500614249239215175476]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 7856986171681248404396064225772749784181602218562773063185003409958949630985,
176
- 11707218736744382138692483591389641607570557654489363179025201039696228471230
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 2902255937308264958973169948617099471543255757887963647238093192858290079050,
181
- 4092153880227661899721872164083575597602963673456107552146583620177664115673
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 18380478859138320895837407377103009470968863533040661874531861881638854174636,
186
- 14502773952184441371657781525836310753176308880224816843041318743809785835984
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 2781117248053224106149213822307598926495461873135153638774638501111353469325,
191
- 3500056595279027698683405880585654897391289317486204483344715855049598477604
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 8880120765926282932795149634761705738498809569874317407549203808931092257005,
196
- 19080036326648068547894941015038877788526324720587349784852594495705578761000
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 Verifier29 {
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
- [7252337675475138150830402909353772156046809729627064992143762325769537840623, 7601443214415704135008588588192028557655441716696726549510699770097979655628],
168
- [436607343827794507835462908831699962173244647704538949914686722631806931932, 18500126298578278987997086114400065402270866280547473913420536595663876273004]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 18427701611614193839908361166447988195308352665132182219164437649866377475111,
176
- 5299493942596042045861137432338955179078182570752746487573709678936617478454
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 4188155714164125069834512529839479682516489319499446390214266838952761728656,
181
- 2720966082507704094346897998659841489771837229143573083003847010258396944787
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 13256461570028177373135283778770729308216900804505379897951455548375840027026,
186
- 10722074030307391322177899534114921764931623271723882054692012663305322382747
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 9824147497244652955949696442395586567974424828238608972020527958186701134273,
191
- 15755269950882650791869946186461432242513999576056199368058858215068920022191
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 21172488506061181949536573476893375313339715931330476837156243346077173297265,
196
- 13892434487977776248366965108031841947713544939953824768291380177301871559945
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
- }