-
Notifications
You must be signed in to change notification settings - Fork 3
/
contractNipopow.sol
477 lines (419 loc) · 14.2 KB
/
contractNipopow.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
pragma solidity ^0.6.2;
//import "strings.sol";
contract Crosschain {
constructor(bytes32 genesis, uint _m, uint _k) public {
genesisBlockHash = genesis;
m = _m;
k = _k;
}
// The genesis block hash
bytes32 genesisBlockHash;
// Collateral to pay.
uint256 constant z = 0.1 ether;
mapping(uint256 => uint256) levelCounter;
struct Event {
address payable author;
uint256 expire;
bytes32 proofHash;
bytes32 hashedProofHash;
bytes32 siblingsHash;
}
// the block header hash.
mapping(bytes32 => Event) events;
mapping(bytes32 => bool) finalizedEvents;
// Security parameters.
uint256 m;
uint256 k;
//TOOO: Move this to another file
function memcpy(uint256 dest, uint256 src, uint256 len) private pure {
// Copy word-length chunks while possible
for (; len >= 32; len -= 32) {
assembly {
mstore(dest, mload(src))
}
dest += 32;
src += 32;
}
// Copy remaining bytes.
uint256 mask = 256**(32 - len) - 1;
assembly {
let srcpart := and(mload(src), not(mask))
let destpart := and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
}
}
// TODO: move this to another file
// Hash the header using double SHA256
function hashHeader(bytes32[4] memory header)
internal
pure
returns (bytes32)
{
// Compute the hash of 112-byte header.
string memory s = new string(112);
uint256 sptr;
uint256 hptr;
assembly {
sptr := add(s, 32)
}
assembly {
hptr := add(header, 0)
}
memcpy(sptr, hptr, 112);
return sha256(abi.encodePacked(sha256(abi.encodePacked(s))));
}
// TODO: Implement the O(log(maxLevel)) algorithm.
function getLevel(bytes32 hashedHeader) internal pure returns (uint256) {
uint256 hash = uint256(hashedHeader);
for (uint256 i = 0; i <= 255; i++) {
// Change endianess.
uint256 pow = (i / 8) * 8 + 8 - (i % 8) - 1;
uint256 mask = 2**pow;
if ((hash & mask) != 0) {
return uint8(i) - 1;
}
}
return 0;
}
function argAtLevel(bytes32[] memory proof, uint256 level)
internal
pure
returns (uint256)
{
uint256 blocksOfLevel = 0;
for (uint256 i = 0; i < proof.length; i++) {
if (getLevel(proof[i]) >= level) {
blocksOfLevel++;
}
}
uint256 score = uint256(blocksOfLevel * 2**level);
return score;
}
// TODO: lca can be very close to the tip of submited proof so that
// score(existing[lca:]) < score(contesting[lca:]) because
// |existing[:lca]| < m
function bestArg(bytes32[] memory proof, uint256 lca)
internal
returns (uint256)
{
uint256 maxLevel = 0;
uint256 maxScore = 0;
uint256 curLevel = 0;
// Count the frequency of the levels.
for (uint256 i = 0; i < lca; i++) {
curLevel = getLevel(proof[i]);
// Superblocks of level m are also superblocks of level m - 1.
for (uint256 j = 0; j <= curLevel; j++) {
levelCounter[j]++;
}
if (maxLevel < curLevel) {
maxLevel = curLevel;
}
}
for (uint256 i = 0; i <= maxLevel; i++) {
uint256 curScore = uint256(levelCounter[i] * 2**i);
if (levelCounter[i] >= m && curScore > maxScore) {
maxScore = levelCounter[i] * 2**i;
}
// clear the map.
levelCounter[i] = 0;
}
return maxScore;
}
function verifyMerkle(
bytes32 roothash,
bytes32 leaf,
uint8 mu,
bytes32[] memory siblings
) internal pure returns (bool) {
bytes32 h = leaf;
for (uint256 i = 0; i < siblings.length; i++) {
uint8 bit = mu & 0x1;
if (bit == 1) {
h = sha256(
abi.encodePacked(
sha256(
abi.encodePacked(
siblings[siblings.length - i - 1],
h
)
)
)
);
} else {
h = sha256(
abi.encodePacked(
sha256(
abi.encodePacked(
h,
siblings[siblings.length - i - 1]
)
)
)
);
}
mu >>= 1;
}
return h == roothash;
}
// shift bits to the most segnificant byte (256-8 = 248)
// and cast it to a 8-bit uint
function b32ToUint8(bytes32 b) private pure returns (uint8) {
return uint8(bytes1(b << 248));
}
function findSiblingsOffset(
bytes32[4][] memory headers,
uint256 proofIndex
) internal pure returns (uint256) {
uint256 ptr;
for (uint256 i = 1; i < proofIndex; i++) {
// hold the 3rd and 4th least significant bytes
uint8 branchLength = b32ToUint8(
(headers[i][3] >> 8) & bytes32(uint256(0xff))
);
require(branchLength <= 5, "Branch length too big");
ptr += branchLength;
}
return ptr;
}
function validateSingleInterlink(
bytes32[4][] memory headers,
bytes32[] memory siblings,
uint256 validateIndex
) internal pure returns (bool) {
uint256 siblingsOffset = findSiblingsOffset(
headers,
validateIndex
);
uint8 branchLength = b32ToUint8(
(headers[validateIndex][3] >> 8) & bytes32(uint256(0xff))
);
uint8 merkleIndex = b32ToUint8(
(headers[validateIndex][3] >> 0) & bytes32(uint256(0xff))
);
bytes32[] memory reversedSiblings = new bytes32[](branchLength);
for (uint8 j = 0; j < branchLength; j++)
reversedSiblings[j] = siblings[siblingsOffset + j];
return
verifyMerkle(
headers[validateIndex - 1][0],
hashHeader(headers[validateIndex]),
merkleIndex,
reversedSiblings
);
}
function validateInterlinks(
bytes32[4][] memory headers,
bytes32[] memory hashedHeaders,
bytes32[] memory siblings
) internal pure returns (bool) {
uint256 ptr = 0; // Index of the current sibling
for (uint256 i = 1; i < headers.length; i++) {
// hold the 3rd and 4th least significant bytes
uint8 branchLength = b32ToUint8(
(headers[i][3] >> 8) & bytes32(uint256(0xff))
);
uint8 merkleIndex = b32ToUint8(
(headers[i][3] >> 0) & bytes32(uint256(0xff))
);
require(branchLength <= 5, "Branch length too big");
require(merkleIndex <= 32, "Merkle index too big");
// Copy siblings.
bytes32[] memory reversedSiblings = new bytes32[](branchLength);
for (uint8 j = 0; j < branchLength; j++)
reversedSiblings[j] = siblings[ptr + j];
ptr += branchLength;
// Verify the merkle tree proof
if (
!verifyMerkle(
headers[i - 1][0],
hashedHeaders[i],
merkleIndex,
reversedSiblings
)
) {
return false;
}
}
return true;
}
function submitEventProof(
bytes32[4][] memory headers,
bytes32[] memory siblings,
uint256 blockOfInterestIndex
) public payable returns (bool) {
require(msg.value >= z, "insufficient collateral");
require(
headers.length > blockOfInterestIndex && blockOfInterestIndex >= 0,
"Block of interest index is out of range"
);
bytes32 hashedBlock = hashHeader(headers[blockOfInterestIndex]);
require(
events[hashedBlock].expire == 0,
"The submission period has expired"
);
require(
events[hashedBlock].proofHash == 0,
"A proof with this evens exists"
);
require(
hashHeader(headers[headers.length - 1]) == genesisBlockHash,
"Proof does not include the genesis block"
);
bytes32[] memory hashedHeaders = new bytes32[](headers.length);
for (uint256 i = 0; i < headers.length; i++) {
hashedHeaders[i] = hashHeader(headers[i]);
}
events[hashedBlock].proofHash = sha256(abi.encodePacked(headers));
events[hashedBlock].hashedProofHash = sha256(
abi.encodePacked(hashedHeaders)
);
events[hashedBlock].siblingsHash = sha256(abi.encodePacked(siblings));
events[hashedBlock].expire = block.number + k;
events[hashedBlock].author = msg.sender;
return true;
}
function finalizeEvent(bytes32[4] memory blockOfInterest)
public
returns (bool)
{
bytes32 hashedBlock = hashHeader(blockOfInterest);
if (
events[hashedBlock].expire == 0 ||
block.number < events[hashedBlock].expire
) {
return false;
}
finalizedEvents[hashedBlock] = true;
events[hashedBlock].expire = 0;
events[hashedBlock].author.transfer(z);
return true;
}
// If this will be expensive, check memory mapping
// Check if existing[0:lca] is disjoint from contesting[:-1]
// existing[0] is the tip of existing, existing[-1] is the genesis
// contesting[0] is the tip of contesting, contestin[-1] is the lca
function disjointProofs(
bytes32[] memory existing,
bytes32[] memory contesting,
uint256 lca
) internal pure returns (bool) {
for (uint256 i = 0; i < lca; i++) {
for (uint256 j = 0; j < contesting.length - 1; j++) {
if (existing[i] == contesting[j]) {
return false;
}
}
}
return true;
}
function disputeExistingProof(
bytes32[4][] memory existingHeaders,
bytes32[] memory siblings,
uint256 blockOfInterestIndex,
uint256 disputeIndex
) public returns (bool) {
bytes32 blockOfInterestHash = hashHeader(
existingHeaders[blockOfInterestIndex]
);
require(
events[blockOfInterestHash].expire > block.number,
"Contesting period has expired"
);
require(
events[blockOfInterestHash].proofHash ==
sha256(abi.encodePacked(existingHeaders)),
"Wrong existing proof"
);
require(
events[blockOfInterestHash].siblingsHash ==
sha256(abi.encodePacked(siblings)),
"Wrong siblings"
);
require(
1 <= disputeIndex && disputeIndex < existingHeaders.length,
"Dispute index out of range"
);
require(
!validateSingleInterlink(existingHeaders, siblings, disputeIndex),
"Existing proof is valid at this index"
);
// If you reached this point, contesting was successful
events[blockOfInterestHash].expire = 0;
msg.sender.transfer(z);
return true;
}
function submitContestingProof(
bytes32[] memory existingHeadersHashed,
uint256 lca,
bytes32[4][] memory contestingHeaders,
bytes32[] memory contestingSiblings,
uint256 bestLevel,
uint256 blockOfInterestIndex
) public returns (bool) {
require(
existingHeadersHashed.length > blockOfInterestIndex &&
blockOfInterestIndex >= 0,
"Block of interest index is out of range"
);
require(
existingHeadersHashed.length >= m, "Security parameter m violated"
);
bytes32 blockOfInterestHash
= existingHeadersHashed[blockOfInterestIndex];
require(
events[blockOfInterestHash].expire > block.number,
"Contesting period has expired"
);
require(existingHeadersHashed.length > lca, "Lca out of range");
require(
lca > blockOfInterestIndex,
"Block of interest exists in sub-chain"
);
require(
events[blockOfInterestHash].hashedProofHash ==
sha256(abi.encodePacked(existingHeadersHashed)),
"Wrong existing proof"
);
bytes32[] memory contestingHeadersHashed = new bytes32[](
contestingHeaders.length
);
for (uint256 i = 0; i < contestingHeaders.length; i++) {
contestingHeadersHashed[i] = hashHeader(contestingHeaders[i]);
}
require(
validateInterlinks(
contestingHeaders,
contestingHeadersHashed,
contestingSiblings
),
"Merkle verification failed"
);
require(
existingHeadersHashed[lca] ==
contestingHeadersHashed[contestingHeaders.length - 1],
"Wrong lca"
);
require(
disjointProofs(existingHeadersHashed, contestingHeadersHashed, lca),
"Contesting proof[1:] is not different from existing[lca+1:]"
);
require(
bestArg(existingHeadersHashed, lca) <
argAtLevel(contestingHeadersHashed, bestLevel),
"Existing proof has greater score"
);
// If you reached this point, contesting was successful
events[blockOfInterestHash].expire = 0;
msg.sender.transfer(z);
return true;
}
function eventExists(bytes32[4] memory blockHeader)
public
view
returns (bool)
{
bytes32 hashedBlock = hashHeader(blockHeader);
return finalizedEvents[hashedBlock];
}
}