-
Notifications
You must be signed in to change notification settings - Fork 4
/
MerklePatriciaProof.sol
198 lines (168 loc) · 7.11 KB
/
MerklePatriciaProof.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
/*
* @title MerklePatriciaVerifier
* @author Sam Mayo (sammayo888@gmail.com)
*
* @dev Library for verifing merkle patricia proofs.
*/
pragma solidity ^0.4.19;
import "./RLP.sol";
library MerklePatriciaProof {
/*
* @dev Verifies a merkle patricia proof.
* @param value The terminating value in the trie.
* @param encodedPath The path in the trie leading to value.
* @param rlpParentNodes The rlp encoded stack of nodes.
* @param root The root hash of the trie.
* @return The boolean validity of the proof.
*/
function verify(bytes32 value, bytes encodedPath, bytes rlpParentNodes, bytes32 root) internal pure returns (bool) {
RLP.RLPItem memory item = RLP.toRLPItem(rlpParentNodes);
RLP.RLPItem[] memory parentNodes = RLP.toList(item);
bytes memory currentNode;
RLP.RLPItem[] memory currentNodeList;
bytes32 nodeKey = root;
uint pathPtr = 0;
bytes memory path = _getNibbleArray2(encodedPath);
if(path.length == 0) {return false;}
for (uint i=0; i<parentNodes.length; i++) {
if(pathPtr > path.length) {return false;}
currentNode = RLP.toBytes(parentNodes[i]);
if(nodeKey != keccak256(currentNode)) {return false;}
currentNodeList = RLP.toList(parentNodes[i]);
if(currentNodeList.length == 17) {
if(pathPtr == path.length) {
if(keccak256(RLP.toBytes(currentNodeList[16])) == value) {
return true;
} else {
return false;
}
}
uint8 nextPathNibble = uint8(path[pathPtr]);
if(nextPathNibble > 16) {return false;}
nodeKey = RLP.toBytes32(currentNodeList[nextPathNibble]);
pathPtr += 1;
} else if(currentNodeList.length == 2) {
pathPtr += _nibblesToTraverse(RLP.toData(currentNodeList[0]), path, pathPtr);
if(pathPtr == path.length) {//leaf node
if(keccak256(RLP.toData(currentNodeList[1])) == value) {
return true;
} else {
return false;
}
}
//extension node ... test if means that it is empty value
if(_nibblesToTraverse(RLP.toData(currentNodeList[0]), path, pathPtr) == 0) {
return (keccak256() == value);
}
nodeKey = RLP.toBytes32(currentNodeList[1]);
} else {
return false;
}
}
}
function verifyDebug(bytes32 value, bytes not_encodedPath, bytes rlpParentNodes, bytes32 root) internal pure returns (bool res, uint loc, bytes path_debug) {
RLP.RLPItem memory item = RLP.toRLPItem(rlpParentNodes);
RLP.RLPItem[] memory parentNodes = RLP.toList(item);
bytes memory currentNode;
RLP.RLPItem[] memory currentNodeList;
bytes32 nodeKey = root;
uint pathPtr = 0;
bytes memory path = _getNibbleArray2(not_encodedPath);
path_debug = path;
if(path.length == 0) { loc = 0; res = false; return;}
for (uint i=0; i<parentNodes.length; i++) {
if(pathPtr > path.length) {loc = 1; res = false; return;}
currentNode = RLP.toBytes(parentNodes[i]);
if(nodeKey != keccak256(currentNode)) { res = false; loc = 100+i; return;}
currentNodeList = RLP.toList(parentNodes[i]);
loc = currentNodeList.length;
if(currentNodeList.length == 17) {
if(pathPtr == path.length) {
if(keccak256(RLP.toBytes(currentNodeList[16])) == value) {
res = true; return;
} else {
loc = 3;
return;
}
}
uint8 nextPathNibble = uint8(path[pathPtr]);
if(nextPathNibble > 16) {
loc = 4;
return; }
nodeKey = RLP.toBytes32(currentNodeList[nextPathNibble]);
pathPtr += 1;
} else if(currentNodeList.length == 2) {
pathPtr += _nibblesToTraverse(RLP.toData(currentNodeList[0]), path, pathPtr);
if(pathPtr == path.length) {//leaf node
if(keccak256(RLP.toData(currentNodeList[1])) == value) {
res = true; return;
} else {
loc = 5;
return;
}
}
//extension node
if(_nibblesToTraverse(RLP.toData(currentNodeList[0]), path, pathPtr) == 0) {
loc = 6;
res = (keccak256() == value);
return;
}
nodeKey = RLP.toBytes32(currentNodeList[1]);
} else {
loc = 7;
return;
}
}
loc = 8;
return;
}
function _nibblesToTraverse(bytes encodedPartialPath, bytes path, uint pathPtr) private pure returns (uint) {
uint len;
// encodedPartialPath has elements that are each two hex characters (1 byte), but partialPath
// and slicedPath have elements that are each one hex character (1 nibble)
bytes memory partialPath = _getNibbleArray(encodedPartialPath);
bytes memory slicedPath = new bytes(partialPath.length);
// pathPtr counts nibbles in path
// partialPath.length is a number of nibbles
for(uint i=pathPtr; i<pathPtr+partialPath.length; i++) {
byte pathNibble = path[i];
slicedPath[i-pathPtr] = pathNibble;
}
if(keccak256(partialPath) == keccak256(slicedPath)) {
len = partialPath.length;
} else {
len = 0;
}
return len;
}
// bytes b must be hp encoded
function _getNibbleArray(bytes b) private pure returns (bytes) {
bytes memory nibbles;
if(b.length>0) {
uint8 offset;
uint8 hpNibble = uint8(_getNthNibbleOfBytes(0,b));
if(hpNibble == 1 || hpNibble == 3) {
nibbles = new bytes(b.length*2-1);
byte oddNibble = _getNthNibbleOfBytes(1,b);
nibbles[0] = oddNibble;
offset = 1;
} else {
nibbles = new bytes(b.length*2-2);
offset = 0;
}
for(uint i=offset; i<nibbles.length; i++) {
nibbles[i] = _getNthNibbleOfBytes(i-offset+2,b);
}
}
return nibbles;
}
// normal byte array, no encoding used
function _getNibbleArray2(bytes b) private pure returns (bytes) {
bytes memory nibbles = new bytes(b.length*2);
for (uint i = 0; i < nibbles.length; i++) nibbles[i] = _getNthNibbleOfBytes(i, b);
return nibbles;
}
function _getNthNibbleOfBytes(uint n, bytes str) private pure returns (byte) {
return byte(n%2==0 ? uint8(str[n/2])/0x10 : uint8(str[n/2])%0x10);
}
}