-
Notifications
You must be signed in to change notification settings - Fork 4
/
SSZ.sol
311 lines (273 loc) · 10.2 KB
/
SSZ.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
// SPDX-FileCopyrightText: 2024 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.24;
import { BeaconBlockHeader, Withdrawal, Validator } from "./Types.sol";
import { GIndex } from "./GIndex.sol";
library SSZ {
error BranchHasMissingItem();
error BranchHasExtraItem();
error InvalidProof();
function hashTreeRoot(
BeaconBlockHeader memory header
) internal view returns (bytes32 root) {
bytes32[8] memory nodes = [
toLittleEndian(header.slot.unwrap()),
toLittleEndian(header.proposerIndex),
header.parentRoot,
header.stateRoot,
header.bodyRoot,
bytes32(0),
bytes32(0),
bytes32(0)
];
/// @solidity memory-safe-assembly
assembly {
// Count of nodes to hash
let count := 8
// Loop over levels
// prettier-ignore
for { } 1 { } {
// Loop over nodes at the given depth
// Initialize `offset` to the offset of `proof` elements in memory.
let target := nodes
let source := nodes
let end := add(source, shl(5, count))
// prettier-ignore
for { } 1 { } {
// Read next two hashes to hash
mcopy(0x00, source, 0x40)
// Call sha256 precompile
let result := staticcall(
gas(),
0x02,
0x00,
0x40,
0x00,
0x20
)
if iszero(result) {
// Precompiles returns no data on OutOfGas error.
revert(0, 0)
}
// Store the resulting hash at the target location
mstore(target, mload(0x00))
// Advance the pointers
target := add(target, 0x20)
source := add(source, 0x40)
if iszero(lt(source, end)) {
break
}
}
count := shr(1, count)
if eq(count, 1) {
root := mload(0x00)
break
}
}
}
}
function hashTreeRoot(
Validator memory validator
) internal view returns (bytes32 root) {
bytes32 pubkeyRoot;
assembly {
// Dynamic data types such as bytes are stored at the specified offset.
let offset := mload(validator)
// Copy the pubkey to the scratch space.
mcopy(0x00, add(offset, 32), 48)
// Clear the last 16 bytes.
mcopy(48, 0x60, 16)
// Call sha256 precompile.
let result := staticcall(gas(), 0x02, 0x00, 0x40, 0x00, 0x20)
if iszero(result) {
// Precompiles returns no data on OutOfGas error.
revert(0, 0)
}
pubkeyRoot := mload(0x00)
}
bytes32[8] memory nodes = [
pubkeyRoot,
validator.withdrawalCredentials,
toLittleEndian(validator.effectiveBalance),
toLittleEndian(validator.slashed),
toLittleEndian(validator.activationEligibilityEpoch),
toLittleEndian(validator.activationEpoch),
toLittleEndian(validator.exitEpoch),
toLittleEndian(validator.withdrawableEpoch)
];
/// @solidity memory-safe-assembly
assembly {
// Count of nodes to hash
let count := 8
// Loop over levels
// prettier-ignore
for { } 1 { } {
// Loop over nodes at the given depth
// Initialize `offset` to the offset of `proof` elements in memory.
let target := nodes
let source := nodes
let end := add(source, shl(5, count))
// prettier-ignore
for { } 1 { } {
// Read next two hashes to hash
mcopy(0x00, source, 0x40)
// Call sha256 precompile
let result := staticcall(
gas(),
0x02,
0x00,
0x40,
0x00,
0x20
)
if iszero(result) {
// Precompiles returns no data on OutOfGas error.
revert(0, 0)
}
// Store the resulting hash at the target location
mstore(target, mload(0x00))
// Advance the pointers
target := add(target, 0x20)
source := add(source, 0x40)
if iszero(lt(source, end)) {
break
}
}
count := shr(1, count)
if eq(count, 1) {
root := mload(0x00)
break
}
}
}
}
/// @notice Modified version of `verify` from Solady `MerkleProofLib` to support generalized indices and sha256 precompile.
/// @dev Reverts if `leaf` doesn't exist in the Merkle tree with `root`, given `proof`.
function verifyProof(
bytes32[] calldata proof,
bytes32 root,
bytes32 leaf,
GIndex gI
) internal view {
uint256 index = gI.index();
/// @solidity memory-safe-assembly
assembly {
// Check if `proof` is empty.
if iszero(proof.length) {
// revert InvalidProof()
mstore(0x00, 0x09bde339)
revert(0x1c, 0x04)
}
// Left shift by 5 is equivalent to multiplying by 0x20.
let end := add(proof.offset, shl(5, proof.length))
// Initialize `offset` to the offset of `proof` in the calldata.
let offset := proof.offset
// Iterate over proof elements to compute root hash.
// prettier-ignore
for { } 1 { } {
// Slot of `leaf` in scratch space.
// If the condition is true: 0x20, otherwise: 0x00.
let scratch := shl(5, and(index, 1))
index := shr(1, index)
if iszero(index) {
// revert BranchHasExtraItem()
mstore(0x00, 0x5849603f)
// 0x1c = 28 => offset in 32-byte word of a slot 0x00
revert(0x1c, 0x04)
}
// Store elements to hash contiguously in scratch space.
// Scratch space is 64 bytes (0x00 - 0x3f) and both elements are 32 bytes.
mstore(scratch, leaf)
mstore(xor(scratch, 0x20), calldataload(offset))
// Call sha256 precompile.
let result := staticcall(
gas(),
0x02,
0x00,
0x40,
0x00,
0x20
)
if iszero(result) {
// Precompile returns no data on OutOfGas error.
revert(0, 0)
}
// Reuse `leaf` to store the hash to reduce stack operations.
leaf := mload(0x00)
offset := add(offset, 0x20)
if iszero(lt(offset, end)) {
break
}
}
if iszero(eq(index, 1)) {
// revert BranchHasMissingItem()
mstore(0x00, 0x1b6661c3)
revert(0x1c, 0x04)
}
if iszero(eq(leaf, root)) {
// revert InvalidProof()
mstore(0x00, 0x09bde339)
revert(0x1c, 0x04)
}
}
}
// Inspired by https://github.com/succinctlabs/telepathy-contracts/blob/5aa4bb7/src/libraries/SimpleSerialize.sol#L59
function hashTreeRoot(
Withdrawal memory withdrawal
) internal pure returns (bytes32) {
return
sha256(
bytes.concat(
sha256(
bytes.concat(
toLittleEndian(withdrawal.index),
toLittleEndian(withdrawal.validatorIndex)
)
),
sha256(
bytes.concat(
bytes20(withdrawal.withdrawalAddress),
bytes12(0),
toLittleEndian(withdrawal.amount)
)
)
)
);
}
// See https://github.com/succinctlabs/telepathy-contracts/blob/5aa4bb7/src/libraries/SimpleSerialize.sol#L17-L28
function toLittleEndian(uint256 v) internal pure returns (bytes32) {
v =
((v &
0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >>
8) |
((v &
0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) <<
8);
v =
((v &
0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >>
16) |
((v &
0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) <<
16);
v =
((v &
0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >>
32) |
((v &
0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) <<
32);
v =
((v &
0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >>
64) |
((v &
0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) <<
64);
v = (v >> 128) | (v << 128);
return bytes32(v);
}
function toLittleEndian(bool v) internal pure returns (bytes32) {
return bytes32(v ? 1 << 248 : 0);
}
}