forked from harmony-one/horizon
/
EthereumProver.sol
297 lines (264 loc) · 10.3 KB
/
EthereumProver.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "./lib/RLPReader.sol";
library EthereumProver {
using RLPReader for RLPReader.RLPItem;
using RLPReader for bytes;
function isEmpty(RLPReader.RLPItem memory item)
internal
pure
returns (bool)
{
if (item.len != 1) {
return false;
}
uint8 b;
uint256 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;
uint256 memPtr = item.memPtr;
assembly {
b := byte(0, mload(memPtr))
}
return b == 0x80; /* empty byte string */
}
function decodeNibbles(
bytes memory compact,
uint256 skipNibbles,
uint256 append16
) internal pure returns (bytes memory nibbles) {
require(compact.length > 0);
uint256 length = compact.length * 2;
require(skipNibbles <= length);
length -= skipNibbles;
nibbles = new bytes(length + append16);
if (append16 == 1) nibbles[nibbles.length - 1] = bytes1(uint8(0x10));
uint256 nibblesLength = 0;
for (uint256 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 + append16 == nibbles.length);
}
function merklePatriciaCompactDecode(bytes memory compact)
internal
pure
returns (bool isLeaf, bytes memory nibbles)
{
require(compact.length > 0);
uint256 first_nibble = (uint8(compact[0]) >> 4) & 0xF;
uint256 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, isLeaf ? 1 : 0));
}
function sharedPrefixLength(
uint256 xsOffset,
bytes memory xs,
bytes memory ys
) internal pure returns (uint256) {
uint256 i;
for (i = 0; i + xsOffset < xs.length && i < ys.length; i++) {
if (xs[i + xsOffset] != ys[i]) {
return i;
}
}
return i;
}
/// @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 proof is decoded to 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,
bytes memory proof
) internal pure returns (bytes memory value) {
RLPReader.RLPItem[] memory stack = proof.toRlpItem().toList();
mptKey = decodeNibbles(mptKey, 0, 1);
uint256 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 (uint256 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()
);
uint256 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();
}
}
}
}
}