-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistry.sol
302 lines (255 loc) · 9.39 KB
/
Registry.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import {BLS} from "./lib/BLS.sol";
import {MerkleUtils} from "./lib/Merkelize.sol";
contract Registry {
using BLS for *;
struct Registration {
BLS.G1Point pubkey;
BLS.G2Point signature;
}
struct Operator {
bytes32 commitmentKey; // compressed ecdsa key without prefix
address withdrawalAddress; // can be a multisig or same as commitment key
uint56 collateral; // amount in gwei
uint32 registeredAt; // block number
uint32 unregisteredAt; // block number
uint16 unregistrationDelay; // block number
}
mapping(bytes32 operatorCommitment => Operator) public commitments;
// Constants
uint256 constant MIN_COLLATERAL = 0.1 ether;
uint256 constant TWO_EPOCHS = 64;
uint256 constant FRAUD_PROOF_WINDOW = 7200;
BLS.G1Point public G1_GENERATOR;
BLS.G1Point public NEGATED_G1_GENERATOR;
// Errors
error InsufficientCollateral();
error WrongOperator();
error AlreadyUnregistered();
error NotUnregistered();
error UnregistrationDelayNotMet();
error NoCollateralToClaim();
error FraudProofWindowExpired();
error FraudProofMerklePathInvalid();
error FraudProofChallengeInvalid();
error UnregistrationDelayTooShort();
// Events
event OperatorRegistered(bytes32 operatorCommitment, uint32 registeredAt);
event OperatorUnregistered(
bytes32 operatorCommitment,
uint32 unregisteredAt
);
event OperatorDeleted(bytes32 operatorCommitment, uint72 amountToReturn);
constructor() {
/// @notice The generator point in G1 (P1).
G1_GENERATOR = BLS.G1Point(
BLS.Fp(
31827880280837800241567138048534752271,
88385725958748408079899006800036250932223001591707578097800747617502997169851
),
BLS.Fp(
11568204302792691131076548377920244452,
114417265404584670498511149331300188430316142484413708742216858159411894806497
)
);
/// @notice The negated generator point in G1 (-P1).
NEGATED_G1_GENERATOR = BLS.G1Point(
BLS.Fp(
31827880280837800241567138048534752271,
88385725958748408079899006800036250932223001591707578097800747617502997169851
),
BLS.Fp(
22997279242622214937712647648895181298,
46816884707101390882112958134453447585552332943769894357249934112654335001290
)
);
}
function register(
Registration[] calldata registrations,
bytes32 commitmentKey,
address withdrawalAddress,
uint16 unregistrationDelay,
uint256 height
) external payable {
// check collateral
if (msg.value < MIN_COLLATERAL) {
revert InsufficientCollateral();
}
if (unregistrationDelay < TWO_EPOCHS) {
revert UnregistrationDelayTooShort();
}
// merklize registrations
bytes32 operatorCommitment = createCommitment(
registrations,
commitmentKey,
height
);
// add operatorCommitment to mapping
commitments[operatorCommitment] = Operator({
withdrawalAddress: withdrawalAddress,
commitmentKey: commitmentKey,
collateral: uint56(msg.value), // todo save as GWEI
registeredAt: uint32(block.number),
unregistrationDelay: unregistrationDelay,
unregisteredAt: 0
});
// emit events
}
function createCommitment(
Registration[] calldata registrations,
bytes32 commitmentKey,
uint256 height
) internal pure returns (bytes32 operatorCommitment) {
uint256 batchSize = 1 << height; // guaranteed pow of 2
require(
registrations.length <= batchSize,
"Batch size must be at least as big"
);
// Create leaves array with padding
bytes32[] memory leaves = new bytes32[](batchSize);
// Create leaf nodes
for (uint256 i = 0; i < registrations.length; i++) {
// Create registration commitment by hashing signature and metadata
// Flatten the signature
BLS.G2Point memory signature = registrations[i].signature;
uint256[8] memory signatureBytes = [
signature.x.c0.a,
signature.x.c0.b,
signature.x.c1.a,
signature.x.c1.b,
signature.y.c0.a,
signature.y.c0.b,
signature.y.c1.a,
signature.y.c1.b
];
bytes32 registrationCommitment = sha256(
abi.encodePacked(signatureBytes, commitmentKey)
);
// Create leaf node by hashing pubkey and commitment
BLS.G1Point memory pubkey = registrations[i].pubkey;
leaves[i] = sha256(
abi.encodePacked(
[pubkey.x.a, pubkey.x.b, pubkey.y.a, pubkey.y.b],
registrationCommitment
)
);
// emit event
}
// Fill remaining leaves with empty hashes for padding
for (uint256 i = registrations.length; i < batchSize; i++) {
leaves[i] = bytes32(0);
}
operatorCommitment = MerkleUtils.merkleize(leaves);
//emit final event
}
function slashRegistration(
bytes32 operatorCommitment,
BLS.G1Point calldata pubkey,
BLS.G2Point calldata signature,
bytes32 commitmentKey,
bytes32[] calldata proof,
uint256 leafIndex
) external view {
Operator storage operator = commitments[operatorCommitment];
if (block.number > operator.registeredAt + FRAUD_PROOF_WINDOW) {
revert FraudProofWindowExpired();
}
uint256[4] memory pubkeyBytes = [
pubkey.x.a,
pubkey.x.b,
pubkey.y.a,
pubkey.y.b
];
uint256[8] memory signatureBytes = [
signature.x.c0.a,
signature.x.c0.b,
signature.x.c1.a,
signature.x.c1.b,
signature.y.c0.a,
signature.y.c0.b,
signature.y.c1.a,
signature.y.c1.b
];
// reconstruct leaf
bytes32 leaf = sha256(
abi.encodePacked(
pubkeyBytes,
sha256(abi.encodePacked(signatureBytes, commitmentKey))
)
);
// verify proof against operatorCommitment
if (
MerkleUtils.verifyProof(proof, operatorCommitment, leaf, leafIndex)
) {
revert FraudProofMerklePathInvalid();
}
// reconstruct message
// todo what exactly are they signing?
bytes memory message = bytes("");
// verify signature
bytes memory domainSeparator = bytes("");
if (verifySignature(message, signature, pubkey, domainSeparator)) {
revert FraudProofChallengeInvalid();
}
}
function unregister(bytes32 operatorCommitment) external {
Operator storage operator = commitments[operatorCommitment];
if (operator.withdrawalAddress != msg.sender) {
revert WrongOperator();
}
// Check that they haven't already unregistered
if (operator.unregisteredAt != 0) {
revert AlreadyUnregistered();
}
// Set unregistration timestamp
operator.unregisteredAt = uint32(block.number);
emit OperatorUnregistered(operatorCommitment, operator.unregisteredAt);
}
function claimCollateral(bytes32 operatorCommitment) external {
Operator storage operator = commitments[operatorCommitment];
// Check that they've unregistered
if (operator.unregisteredAt == 0) {
revert NotUnregistered();
}
// Check that enough time has passed
if (
block.number <
operator.unregisteredAt + operator.unregistrationDelay
) {
revert UnregistrationDelayNotMet();
}
// Check there's collateral to claim
if (operator.collateral == 0) {
revert NoCollateralToClaim();
}
uint72 amountToReturn = operator.collateral;
// TODO safe transfer for rentrancy
(bool success, ) = operator.withdrawalAddress.call{
value: amountToReturn
}("");
require(success, "Transfer failed");
emit OperatorDeleted(operatorCommitment, amountToReturn);
// Clear operator info
delete commitments[operatorCommitment];
}
function verifySignature(
bytes memory message,
BLS.G2Point memory signature,
BLS.G1Point memory publicKey,
bytes memory domainSeparator
) public view returns (bool) {
// Hash the message bytes into a G2 point
BLS.G2Point memory messagePoint = BLS.MapFp2ToG2(
BLS.Fp2(BLS.Fp(0, 0), BLS.Fp(0, uint256(keccak256(message))))
);
// Invoke the pairing check to verify the signature.
BLS.G1Point[] memory g1Points = new BLS.G1Point[](2);
g1Points[0] = NEGATED_G1_GENERATOR;
g1Points[1] = publicKey;
BLS.G2Point[] memory g2Points = new BLS.G2Point[](2);
g2Points[0] = signature;
g2Points[1] = messagePoint;
return BLS.Pairing(g1Points, g2Points);
}
}