@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 Verifier30 {
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
- [10202326166286888893675634318107715186834588694714750762952081034135561546271, 15028154694713144242204861571552635520290993855826554325002991692907421516918],
168
- [18486039841380105976272577521609866666900576498507352937328726490052296469859, 12766289885372833812620582632847872978085960777075662988932200910695848591357]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 1452272927738590248356371174422184656932731110936062990115610832462181634644,
176
- 3608050114233210789542189629343107890943266759827387991788718454179833288695
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 14798240452388909327945424685903532333765637883272751382037716636327236955001,
181
- 10773894897711848209682368488916121016695006898681985691467605219098835500201
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 17204267933132009093604099819536245144503489322639121825381131096467570698650,
186
- 7704298975420304156332734115679983371345754866278811368869074990486717531131
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 8060465662017324080560848316478407038163145149983639907596180500095598669247,
191
- 20475082166427284188002500222093571716651248980245637602667562336751029856573
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 7457566682692308112726332096733260585025339741083447785327706250123165087868,
196
- 11904519443874922292602150685069370036383697877657723976244907400392778002614
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 Verifier31 {
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
- [14930624777162656776068112402283260602512252179767747308433194885322661150422, 13682963731073238132274278610660469286329368216526659590944079211949686450402],
168
- [18705481657148807016785305378773304476425591636333098330324049960258682574070, 21315724107376627085778492378001676935454590984229146391746301404292016287653]
169
- );
170
-
171
- vk.IC = new Pairing.G1Point[](5);
172
-
173
-
174
- vk.IC[0] = Pairing.G1Point(
175
- 12628427235010608529869146871556870477182704310235373946877240509680742038961,
176
- 15093298104438768585559335868663959710321348106117735180051519837845319121254
177
- );
178
-
179
- vk.IC[1] = Pairing.G1Point(
180
- 6593907467779318957599440584793099005109789224774644007604434924706249001015,
181
- 18549596630007199540674697114946251030815675677713256327810772799104711621483
182
- );
183
-
184
- vk.IC[2] = Pairing.G1Point(
185
- 6271101737045248834759003849256661059806617144229427987717476992610974162336,
186
- 355748132218964841305454070022507122319085542484477110563322753565651576458
187
- );
188
-
189
- vk.IC[3] = Pairing.G1Point(
190
- 2116139772133141967317791473319540620104888687412078412336248003979594158546,
191
- 4004400204967325849492155713520296687406035356901102254880522534085890616486
192
- );
193
-
194
- vk.IC[4] = Pairing.G1Point(
195
- 4206647028595764233995379982714022410660284578620723510907006350595207905228,
196
- 19380634286337609988098517090003334645113675227742745065381519159322795845003
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
- }