-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProvethVerifier.sol
423 lines (375 loc) · 14.4 KB
/
ProvethVerifier.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
pragma solidity ^0.5.0;
import "./Solidity-RLP/contracts/RLPReader.sol";
contract ProvethVerifier {
using RLPReader for RLPReader.RLPItem;
using RLPReader for bytes;
uint256 constant TX_ROOT_HASH_INDEX = 4;
struct UnsignedTransaction {
uint256 nonce;
uint256 gasprice;
uint256 startgas;
address to;
uint256 value;
bytes data;
bool isContractCreation;
}
struct SignedTransaction {
uint256 nonce;
uint256 gasprice;
uint256 startgas;
address to;
uint256 value;
bytes data;
uint256 v;
uint256 r;
uint256 s;
bool isContractCreation;
}
function isEmpty(RLPReader.RLPItem memory item) internal pure returns (bool) {
if (item.len != 1) {
return false;
}
uint8 b;
uint memPtr = item.memPtr;
assembly {
b := byte(0, mload(memPtr))
}
return b == 0x80 /* empty byte string */ || b == 0xc0 /* empty list */;
}
function isEmptyBytesequence(RLPReader.RLPItem memory item) internal pure returns (bool) {
if (item.len != 1) {
return false;
}
uint8 b;
uint memPtr = item.memPtr;
assembly {
b := byte(0, mload(memPtr))
}
return b == 0x80 /* empty byte string */;
}
function decodeUnsignedTx(bytes memory rlpUnsignedTx) internal pure returns (UnsignedTransaction memory t) {
RLPReader.RLPItem[] memory fields = rlpUnsignedTx.toRlpItem().toList();
require(fields.length == 6);
address potentialAddress;
bool isContractCreation;
if(isEmpty(fields[3])) {
potentialAddress = 0x0000000000000000000000000000000000000000;
isContractCreation = true;
} else {
potentialAddress = fields[3].toAddress();
isContractCreation = false;
}
t = UnsignedTransaction(
fields[0].toUint(), // nonce
fields[1].toUint(), // gasprice
fields[2].toUint(), // startgas
potentialAddress, // to
fields[4].toUint(), // value
fields[5].toBytes(), // data
isContractCreation
);
}
function decodeSignedTx(bytes memory rlpSignedTx) internal pure returns (SignedTransaction memory t) {
RLPReader.RLPItem[] memory fields = rlpSignedTx.toRlpItem().toList();
address potentialAddress;
bool isContractCreation;
if(isEmpty(fields[3])) {
potentialAddress = 0x0000000000000000000000000000000000000000;
isContractCreation = true;
} else {
potentialAddress = fields[3].toAddress();
isContractCreation = false;
}
t = SignedTransaction(
fields[0].toUint(),
fields[1].toUint(),
fields[2].toUint(),
potentialAddress,
fields[4].toUint(),
fields[5].toBytes(),
fields[6].toUint(),
fields[7].toUint(),
fields[8].toUint(),
isContractCreation
);
}
function decodeNibbles(bytes memory compact, uint skipNibbles) internal pure returns (bytes memory nibbles) {
require(compact.length > 0);
uint length = compact.length * 2;
require(skipNibbles <= length);
length -= skipNibbles;
nibbles = new bytes(length);
uint nibblesLength = 0;
for (uint i = skipNibbles; i < skipNibbles + length; i += 1) {
if (i % 2 == 0) {
nibbles[nibblesLength] = bytes1((uint8(compact[i/2]) >> 4) & 0xF);
} else {
nibbles[nibblesLength] = bytes1((uint8(compact[i/2]) >> 0) & 0xF);
}
nibblesLength += 1;
}
assert(nibblesLength == nibbles.length);
}
function merklePatriciaCompactDecode(bytes memory compact) internal pure returns (bool isLeaf, bytes memory nibbles) {
require(compact.length > 0);
uint first_nibble = uint8(compact[0]) >> 4 & 0xF;
uint skipNibbles;
if (first_nibble == 0) {
skipNibbles = 2;
isLeaf = false;
} else if (first_nibble == 1) {
skipNibbles = 1;
isLeaf = false;
} else if (first_nibble == 2) {
skipNibbles = 2;
isLeaf = true;
} else if (first_nibble == 3) {
skipNibbles = 1;
isLeaf = true;
} else {
// Not supposed to happen!
revert();
}
return (isLeaf, decodeNibbles(compact, skipNibbles));
}
function sharedPrefixLength(uint xsOffset, bytes memory xs, bytes memory ys) internal pure returns (uint) {
uint i;
for (i = 0; i + xsOffset < xs.length && i < ys.length; i++) {
if (xs[i + xsOffset] != ys[i]) {
return i;
}
}
return i;
}
struct Proof {
uint256 kind;
bytes rlpBlockHeader;
bytes32 txRootHash;
bytes rlpTxIndex;
uint txIndex;
bytes mptKey;
RLPReader.RLPItem[] stack;
}
function decodeProofBlob(bytes memory proofBlob) internal pure returns (Proof memory proof) {
RLPReader.RLPItem[] memory proofFields = proofBlob.toRlpItem().toList();
bytes memory rlpTxIndex = proofFields[2].toRlpBytes();
proof = Proof(
proofFields[0].toUint(),
proofFields[1].toRlpBytes(),
bytes32(proofFields[1].toList()[TX_ROOT_HASH_INDEX].toUint()),
rlpTxIndex,
proofFields[2].toUint(),
decodeNibbles(rlpTxIndex, 0),
proofFields[3].toList()
);
}
uint8 constant public TX_PROOF_RESULT_PRESENT = 1;
uint8 constant public TX_PROOF_RESULT_ABSENT = 2;
function txProof(
bytes32 blockHash,
bytes memory proofBlob
) public pure returns (
uint8 result, // see TX_PROOF_RESULT_*
uint256 index,
uint256 nonce,
uint256 gasprice,
uint256 startgas,
address to, // 20 byte address for "regular" tx,
// empty for contract creation tx
uint256 value,
bytes memory data,
uint256 v,
uint256 r,
uint256 s,
bool isContractCreation
) {
SignedTransaction memory t;
(result, index, t) = validateTxProof(blockHash, proofBlob);
nonce = t.nonce;
gasprice = t.gasprice;
startgas = t.startgas;
to = t.to;
value = t.value;
data = t.data;
v = t.v;
r = t.r;
s = t.s;
isContractCreation = t.isContractCreation;
}
function validateTxProof(
bytes32 blockHash,
bytes memory proofBlob
) internal pure returns (uint8 result, uint256 index, SignedTransaction memory t) {
result = 0;
index = 0;
Proof memory proof = decodeProofBlob(proofBlob);
if (proof.kind != 1) {
revert();
}
if (keccak256(proof.rlpBlockHeader) != blockHash) {
revert();
}
bytes memory rlpTx = validateMPTProof(proof.txRootHash, proof.mptKey, proof.stack);
if (rlpTx.length == 0) {
// empty node
return (
TX_PROOF_RESULT_ABSENT,
proof.txIndex,
t
);
} else {
return (
TX_PROOF_RESULT_PRESENT,
proof.txIndex,
decodeSignedTx(rlpTx)
);
}
}
/// @dev Computes the hash of the Merkle-Patricia-Trie hash of the input.
/// Merkle-Patricia-Tries use a weird "hash function" that outputs
/// *variable-length* hashes: If the input is shorter than 32 bytes,
/// the MPT hash is the input. Otherwise, the MPT hash is the
/// Keccak-256 hash of the input.
/// The easiest way to compare variable-length byte sequences is
/// to compare their Keccak-256 hashes.
/// @param input The byte sequence to be hashed.
/// @return Keccak-256(MPT-hash(input))
function mptHashHash(bytes memory input) internal pure returns (bytes32) {
if (input.length < 32) {
return keccak256(input);
} else {
return keccak256(abi.encodePacked(keccak256(abi.encodePacked(input))));
}
}
/// @dev Validates a Merkle-Patricia-Trie proof.
/// If the proof proves the inclusion of some key-value pair in the
/// trie, the value is returned. Otherwise, i.e. if the proof proves
/// the exclusion of a key from the trie, an empty byte array is
/// returned.
/// @param rootHash is the Keccak-256 hash of the root node of the MPT.
/// @param mptKey is the key (consisting of nibbles) of the node whose
/// inclusion/exclusion we are proving.
/// @param stack is the stack of MPT nodes (starting with the root) that
/// need to be traversed during verification.
/// @return value whose inclusion is proved or an empty byte array for
/// a proof of exclusion
function validateMPTProof(
bytes32 rootHash,
bytes memory mptKey,
RLPReader.RLPItem[] memory stack
) internal pure returns (bytes memory value) {
uint mptKeyOffset = 0;
bytes32 nodeHashHash;
bytes memory rlpNode;
RLPReader.RLPItem[] memory node;
RLPReader.RLPItem memory rlpValue;
if (stack.length == 0) {
// Root hash of empty Merkle-Patricia-Trie
require(rootHash == 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421);
return new bytes(0);
}
// Traverse stack of nodes starting at root.
for (uint i = 0; i < stack.length; i++) {
// We use the fact that an rlp encoded list consists of some
// encoding of its length plus the concatenation of its
// *rlp-encoded* items.
rlpNode = stack[i].toRlpBytes();
// The root node is hashed with Keccak-256 ...
if (i == 0 && rootHash != keccak256(rlpNode)) {
revert();
}
// ... whereas all other nodes are hashed with the MPT
// hash function.
if (i != 0 && nodeHashHash != mptHashHash(rlpNode)) {
revert();
}
// We verified that stack[i] has the correct hash, so we
// may safely decode it.
node = stack[i].toList();
if (node.length == 2) {
// Extension or Leaf node
bool isLeaf;
bytes memory nodeKey;
(isLeaf, nodeKey) = merklePatriciaCompactDecode(node[0].toBytes());
uint prefixLength = sharedPrefixLength(mptKeyOffset, mptKey, nodeKey);
mptKeyOffset += prefixLength;
if (prefixLength < nodeKey.length) {
// Proof claims divergent extension or leaf. (Only
// relevant for proofs of exclusion.)
// An Extension/Leaf node is divergent iff it "skips" over
// the point at which a Branch node should have been had the
// excluded key been included in the trie.
// Example: Imagine a proof of exclusion for path [1, 4],
// where the current node is a Leaf node with
// path [1, 3, 3, 7]. For [1, 4] to be included, there
// should have been a Branch node at [1] with a child
// at 3 and a child at 4.
// Sanity check
if (i < stack.length - 1) {
// divergent node must come last in proof
revert();
}
return new bytes(0);
}
if (isLeaf) {
// Sanity check
if (i < stack.length - 1) {
// leaf node must come last in proof
revert();
}
if (mptKeyOffset < mptKey.length) {
return new bytes(0);
}
rlpValue = node[1];
return rlpValue.toBytes();
} else { // extension
// Sanity check
if (i == stack.length - 1) {
// shouldn't be at last level
revert();
}
if (!node[1].isList()) {
// rlp(child) was at least 32 bytes. node[1] contains
// Keccak256(rlp(child)).
nodeHashHash = keccak256(node[1].toBytes());
} else {
// rlp(child) was at less than 32 bytes. node[1] contains
// rlp(child).
nodeHashHash = keccak256(node[1].toRlpBytes());
}
}
} else if (node.length == 17) {
// Branch node
if (mptKeyOffset != mptKey.length) {
// we haven't consumed the entire path, so we need to look at a child
uint8 nibble = uint8(mptKey[mptKeyOffset]);
mptKeyOffset += 1;
if (nibble >= 16) {
// each element of the path has to be a nibble
revert();
}
if (isEmptyBytesequence(node[nibble])) {
// Sanity
if (i != stack.length - 1) {
// leaf node should be at last level
revert();
}
return new bytes(0);
} else if (!node[nibble].isList()) {
nodeHashHash = keccak256(node[nibble].toBytes());
} else {
nodeHashHash = keccak256(node[nibble].toRlpBytes());
}
} else {
// we have consumed the entire mptKey, so we need to look at what's contained in this node.
// Sanity
if (i != stack.length - 1) {
// should be at last level
revert();
}
return node[16].toBytes();
}
}
}
}
}