-
Notifications
You must be signed in to change notification settings - Fork 594
Expand file tree
/
Copy pathTxsDecoder.sol
More file actions
609 lines (534 loc) · 25 KB
/
TxsDecoder.sol
File metadata and controls
609 lines (534 loc) · 25 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;
// Libraries
import {Errors} from "../Errors.sol";
import {Constants} from "../ConstantsGen.sol";
import {Hash} from "../Hash.sol";
/**
* @title Txs Decoder Library
* @author Aztec Labs
* @notice Decoding a L2 block body and computing the TxsHash.
* Concerned with readability and velocity of development not giving a damn about gas costs.
* @dev Assumes the input trees to be padded.
*
* -------------------
* L2 Body Data Specification
* -------------------
* | byte start | num bytes | name
* | --- | --- | ---
* | 0x0 | 0x4 | len(numTxs) (denoted t)
* | | | TxEffect 0 {
* | 0x4 | 0x1 | revertCode
* | 0x5 | 0x20 | transactionFee
* | 0x25 | 0x1 | len(noteHashes) (denoted b)
* | 0x25 + 0x1 | b * 0x20 | noteHashes
* | 0x25 + 0x1 + b * 0x20 | 0x1 | len(nullifiers) (denoted c)
* | 0x25 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | nullifiers
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(l2ToL1Msgs) (denoted d)
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | l2ToL1Msgs
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(publicDataUpdateRequests) (denoted e)
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | publicDataUpdateRequests
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(noteEncryptedLogs) (denoted f)
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | noteEncryptedLogs
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(encryptedLogs) (denoted g)
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | encryptedLogs
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 + g | 0x04 | byteLen(unencryptedLogs) (denoted h)
* | 0x25 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 + g + 0x04| h | unencryptedLogs
* | | | },
* | | | TxEffect 1 {
* | | | ...
* | | | },
* | | | ...
* | | | TxEffect (t - 1) {
* | | | ...
* | | | },
*/
library TxsDecoder {
struct ArrayOffsets {
uint256 revertCode;
uint256 transactionFee;
uint256 noteHash;
uint256 nullifier;
uint256 l2ToL1Msgs;
uint256 publicData;
uint256 noteEncryptedLogsLength;
uint256 encryptedLogsLength;
uint256 unencryptedLogsLength;
}
struct Counts {
uint256 noteHash;
uint256 nullifier;
uint256 l2ToL1Msgs;
uint256 publicData;
}
// Note: Used in `computeConsumables` to get around stack too deep errors.
struct ConsumablesVars {
bytes32[] baseLeaves;
bytes baseLeaf;
uint256 kernelNoteEncryptedLogsLength;
uint256 kernelEncryptedLogsLength;
uint256 kernelUnencryptedLogsLength;
bytes32 noteEncryptedLogsHash;
bytes32 encryptedLogsHash;
bytes32 unencryptedLogsHash;
}
/**
* @notice Computes txs effects hash
* @param _body - The L2 block body calldata.
* @return The txs effects hash.
*/
function decode(bytes calldata _body) internal pure returns (bytes32) {
ArrayOffsets memory offsets;
Counts memory counts;
ConsumablesVars memory vars;
uint256 offset = 0;
uint32 numTxEffects = uint32(read4(_body, offset));
uint256 numTxEffectsToPad = computeNumTxEffectsToPad(numTxEffects);
offset += 0x4;
vars.baseLeaves = new bytes32[](numTxEffects + numTxEffectsToPad);
// Data starts after header. Look at L2 Block Data specification at the top of this file.
{
for (uint256 i = 0; i < numTxEffects; i++) {
/*
* Compute the leaf to insert.
* Leaf_i = (
* revertCode,
* transactionFee,
* noteHashesKernel,
* nullifiersKernel,
* l2ToL1MsgsKernel,
* publicDataUpdateRequestsKernel,
* noteEncryptedLogsLength,
* encryptedLogsLength,
* unencryptedLogsLength,
* noteEncryptedLogsHash, |
* encryptedLogsHash, |
* unencryptedLogsHash, ____|=> Computed below from logs' preimages.
* );
* Note that we always read data, the l2Block (atm) must therefore include dummy or zero-notes for
* Zero values.
*/
// REVERT CODE
offsets.revertCode = offset;
offset += 0x1;
// TX FEE
offsets.transactionFee = offset;
offset += 0x20;
// NOTE HASHES
uint256 count = read1(_body, offset);
offset += 0x1;
counts.noteHash = count;
offsets.noteHash = offset;
offset += count * 0x20; // each note hash is 0x20 bytes long
// NULLIFIERS
count = read1(_body, offset);
offset += 0x1;
counts.nullifier = count;
offsets.nullifier = offset;
offset += count * 0x20; // each nullifier is 0x20 bytes long
// L2 TO L1 MESSAGES
count = read1(_body, offset);
offset += 0x1;
counts.l2ToL1Msgs = count;
offsets.l2ToL1Msgs = offset;
offset += count * 0x20; // each l2 to l1 message is 0x20 bytes long
// PUBLIC DATA UPDATE REQUESTS
count = read1(_body, offset);
offset += 0x1;
counts.publicData = count;
offsets.publicData = offset;
offset += count * 0x40; // each public data update request is 0x40 bytes long
// NOTE ENCRYPTED LOGS LENGTH
offsets.noteEncryptedLogsLength = offset;
offset += 0x20;
// ENCRYPTED LOGS LENGTH
offsets.encryptedLogsLength = offset;
offset += 0x20;
// UNENCRYPTED LOGS LENGTH
offsets.unencryptedLogsLength = offset;
offset += 0x20;
/**
* Compute note, encrypted and unencrypted logs hashes corresponding to the current leaf.
* Note: will advance offsets by the number of bytes processed.
*/
// NOTE ENCRYPTED LOGS HASH
(vars.noteEncryptedLogsHash, offset, vars.kernelNoteEncryptedLogsLength) =
computeKernelNoteEncryptedLogsHash(offset, _body);
// ENCRYPTED LOGS HASH
(vars.encryptedLogsHash, offset, vars.kernelEncryptedLogsLength) =
computeKernelEncryptedLogsHash(offset, _body);
// UNENCRYPTED LOGS HASH
(vars.unencryptedLogsHash, offset, vars.kernelUnencryptedLogsLength) =
computeKernelUnencryptedLogsHash(offset, _body);
// We throw to ensure that the byte len we charge for DA gas in the kernels matches the actual chargable log byte len
// Without this check, the user may provide the kernels with a lower log len than reality
if (
uint256(bytes32(slice(_body, offsets.noteEncryptedLogsLength, 0x20)))
!= vars.kernelNoteEncryptedLogsLength
) {
revert Errors.TxsDecoder__InvalidLogsLength(
uint256(bytes32(slice(_body, offsets.noteEncryptedLogsLength, 0x20))),
vars.kernelNoteEncryptedLogsLength
);
}
if (
uint256(bytes32(slice(_body, offsets.encryptedLogsLength, 0x20)))
!= vars.kernelEncryptedLogsLength
) {
revert Errors.TxsDecoder__InvalidLogsLength(
uint256(bytes32(slice(_body, offsets.encryptedLogsLength, 0x20))),
vars.kernelEncryptedLogsLength
);
}
if (
uint256(bytes32(slice(_body, offsets.unencryptedLogsLength, 0x20)))
!= vars.kernelUnencryptedLogsLength
) {
revert Errors.TxsDecoder__InvalidLogsLength(
uint256(bytes32(slice(_body, offsets.unencryptedLogsLength, 0x20))),
vars.kernelUnencryptedLogsLength
);
}
// Insertions are split into multiple `bytes.concat` to work around stack too deep.
vars.baseLeaf = bytes.concat(
// pad the revert code to 32 bytes to match the hash preimage
bytes.concat(
sliceAndPadLeft(_body, offsets.revertCode, 0x1, 0x20),
slice(_body, offsets.transactionFee, 0x20)
),
bytes.concat(
sliceAndPadRight(
_body,
offsets.noteHash,
counts.noteHash * 0x20,
Constants.NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPadRight(
_body,
offsets.nullifier,
counts.nullifier * 0x20,
Constants.NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPadRight(
_body,
offsets.l2ToL1Msgs,
counts.l2ToL1Msgs * 0x20,
Constants.L2_TO_L1_MSGS_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPadRight(
_body,
offsets.publicData,
counts.publicData * 0x40,
Constants.PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP
)
),
bytes.concat(
slice(_body, offsets.noteEncryptedLogsLength, 0x20),
slice(_body, offsets.encryptedLogsLength, 0x20),
slice(_body, offsets.unencryptedLogsLength, 0x20)
),
bytes.concat(vars.noteEncryptedLogsHash, vars.encryptedLogsHash, vars.unencryptedLogsHash)
);
vars.baseLeaves[i] = Hash.sha256ToField(vars.baseLeaf);
}
// We pad base leaves with hashes of empty tx effect.
for (uint256 i = numTxEffects; i < vars.baseLeaves.length; i++) {
// Value taken from tx_effect.test.ts "hash of empty tx effect matches snapshot" test case
vars.baseLeaves[i] = hex"0071c642b31e5890a15ef92f3cbeba34edfb6e2e9f63079ecbda13a89d426f7d";
}
}
return computeRoot(vars.baseLeaves);
}
/**
* @notice Computes logs hash as is done in the kernel and app circuits.
* @param _offsetInBlock - The offset of kernel's logs in a block.
* @param _body - The L2 block calldata.
* @return The hash of the logs and offset in a block after processing the logs.
* @dev We have logs preimages on the input and we need to perform the same hashing process as is done in the app
* circuit (hashing the logs) and in the kernel circuit (accumulating the logs hashes). The tail kernel
* circuit flat hashes all the app log hashes.
*
* E.g. for resulting logs hash of a kernel with 3 iterations would be computed as:
*
* kernelPublicInputsLogsHash = sha256((sha256(I1_LOGS), sha256(I2_LOGS)), sha256(I3_LOGS))
*
* where I1_LOGS, I2_LOGS and I3_LOGS are logs emitted in the first, second and third function call.
*
* Note that `sha256(I1_LOGS)`, `sha256(I2_LOGS)` and `sha256(I3_LOGS)` are computed in the app circuit and not
* in the kernel circuit. The kernel circuit only accumulates the hashes.
*
* @dev For the example above, the logs are encoded in the following way:
*
* || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS | I2_LOGS_LEN | I2_LOGS | I3_LOGS_LEN | I3_LOGS ||
* 4 bytes 4 bytes i bytes 4 bytes j bytes 4 bytes k bytes
*
* K_LOGS_LEN is the total length of the logs in the kernel.
* I1_LOGS_LEN (i) is the length of the logs in the first iteration.
* I1_LOGS are all the logs emitted in the first iteration.
* I2_LOGS_LEN (j) ...
* @dev The circuit outputs a total logs len based on the byte length that the user pays DA gas for.
* In terms of the encoding above, this is the raw log length (i, j, or k) + 4 for each log.
* For the example above, kernelLogsLength = (i + 4) + (j + 4) + (k + 4). Since we already track
* the total remainingLogsLength, we just remove the bytes holding function logs length.
*
* @dev Link to a relevant discussion:
* https://discourse.aztec.network/t/proposal-forcing-the-sequencer-to-actually-submit-data-to-l1/426/9
*/
function computeKernelNoteEncryptedLogsHash(uint256 _offsetInBlock, bytes calldata _body)
internal
pure
returns (bytes32, uint256, uint256)
{
uint256 offset = _offsetInBlock;
uint256 remainingLogsLength = read4(_body, offset);
uint256 kernelLogsLength = remainingLogsLength;
offset += 0x4;
bytes memory flattenedLogHashes; // The hash input
// Iterate until all the logs were processed
while (remainingLogsLength > 0) {
// The length of the logs emitted by Aztec.nr from the function call corresponding to this kernel iteration
uint256 privateCircuitPublicInputLogsLength = read4(_body, offset);
offset += 0x4;
// Decrease remaining logs length by this privateCircuitPublicInputsLogs's length (len(I?_LOGS)) and 4 bytes for I?_LOGS_LEN
remainingLogsLength -= (privateCircuitPublicInputLogsLength + 0x4);
kernelLogsLength -= 0x4;
while (privateCircuitPublicInputLogsLength > 0) {
uint256 singleCallLogsLength = read4(_body, offset);
offset += 0x4;
bytes32 singleLogHash = Hash.sha256ToField(slice(_body, offset, singleCallLogsLength));
offset += singleCallLogsLength;
flattenedLogHashes = bytes.concat(flattenedLogHashes, singleLogHash);
privateCircuitPublicInputLogsLength -= (singleCallLogsLength + 0x4);
}
}
// Not having a 0 value hash for empty logs causes issues with empty txs used for padding.
if (flattenedLogHashes.length == 0) {
return (0, offset, 0);
}
// padded to MAX_LOGS * 32 bytes
flattenedLogHashes = bytes.concat(
flattenedLogHashes,
new bytes(Constants.MAX_NOTE_ENCRYPTED_LOGS_PER_TX * 32 - flattenedLogHashes.length)
);
bytes32 kernelPublicInputsLogsHash = Hash.sha256ToField(flattenedLogHashes);
return (kernelPublicInputsLogsHash, offset, kernelLogsLength);
}
/**
* @notice Computes encrypted logs hash as is done in the kernel circuits.
* @param _offsetInBlock - The offset of kernel's logs in a block.
* @param _body - The L2 block calldata.
* @return The hash of the logs and offset in a block after processing the logs.
* @dev See above for full details. Non-note encrypted logs hashes are siloed with
* their (hidden) contract address:
* singleLogsHash = sha256ToField(encryptedBuffer)
* siloedLogsHash = sha256ToField(maskedContractAddress, singleLogsHash)
* where maskedContractAddress = pedersen(contract_address, randomness) is provided as part
* of the block bytes, prepended to each encrypted log.
* We don't currently count the maskedContractAddress as part of the
* chargable DA length of the log.
*/
function computeKernelEncryptedLogsHash(uint256 _offsetInBlock, bytes calldata _body)
internal
pure
returns (bytes32, uint256, uint256)
{
uint256 offset = _offsetInBlock;
uint256 remainingLogsLength = read4(_body, offset);
uint256 kernelLogsLength = remainingLogsLength;
offset += 0x4;
bytes memory flattenedLogHashes; // The hash input
// Iterate until all the logs were processed
while (remainingLogsLength > 0) {
// The length of the logs emitted by Aztec.nr from the function call corresponding to this kernel iteration
uint256 privateCircuitPublicInputLogsLength = read4(_body, offset);
offset += 0x4;
// Decrease remaining logs length by this privateCircuitPublicInputsLogs's length (len(I?_LOGS)) and 4 bytes for I?_LOGS_LEN
remainingLogsLength -= (privateCircuitPublicInputLogsLength + 0x4);
kernelLogsLength -= 0x4;
while (privateCircuitPublicInputLogsLength > 0) {
uint256 singleCallLogsLengthWithMaskedAddress = read4(_body, offset);
offset += 0x4;
// The first 32 bytes of the provided encrypted log are its masked address (see EncryptedL2Log.toBuffer())
bytes32 maskedContractAddress = bytes32(slice(_body, offset, 0x20));
offset += 0x20;
// We don't currently include the masked contract address as part of the DA length
kernelLogsLength -= 0x20;
uint256 singleCallLogsLength = singleCallLogsLengthWithMaskedAddress - 0x20;
bytes32 singleLogHash = Hash.sha256ToField(slice(_body, offset, singleCallLogsLength));
bytes32 siloedLogHash =
Hash.sha256ToField(bytes.concat(maskedContractAddress, singleLogHash));
offset += singleCallLogsLength;
flattenedLogHashes = bytes.concat(flattenedLogHashes, siloedLogHash);
privateCircuitPublicInputLogsLength -= (singleCallLogsLengthWithMaskedAddress + 0x4);
}
}
// Not having a 0 value hash for empty logs causes issues with empty txs used for padding.
if (flattenedLogHashes.length == 0) {
return (0, offset, 0);
}
// padded to MAX_LOGS * 32 bytes
flattenedLogHashes = bytes.concat(
flattenedLogHashes,
new bytes(Constants.MAX_ENCRYPTED_LOGS_PER_TX * 32 - flattenedLogHashes.length)
);
bytes32 kernelPublicInputsLogsHash = Hash.sha256ToField(flattenedLogHashes);
return (kernelPublicInputsLogsHash, offset, kernelLogsLength);
}
/**
* @notice Computes unencrypted logs hash as is done in the kernel circuits.
* @param _offsetInBlock - The offset of kernel's logs in a block.
* @param _body - The L2 block calldata.
* @return The hash of the logs and offset in a block after processing the logs.
* @dev See above for details. The only difference here is that unencrypted logs are
* siloed with their contract address.
*/
function computeKernelUnencryptedLogsHash(uint256 _offsetInBlock, bytes calldata _body)
internal
pure
returns (bytes32, uint256, uint256)
{
uint256 offset = _offsetInBlock;
uint256 remainingLogsLength = read4(_body, offset);
uint256 kernelLogsLength = remainingLogsLength;
offset += 0x4;
bytes memory flattenedLogHashes; // The hash input
// Iterate until all the logs were processed
while (remainingLogsLength > 0) {
// The length of the logs emitted by Aztec.nr from the function call corresponding to this kernel iteration
uint256 privateCircuitPublicInputLogsLength = read4(_body, offset);
offset += 0x4;
// Decrease remaining logs length by this privateCircuitPublicInputsLogs's length (len(I?_LOGS)) and 4 bytes for I?_LOGS_LEN
remainingLogsLength -= (privateCircuitPublicInputLogsLength + 0x4);
kernelLogsLength -= 0x4;
while (privateCircuitPublicInputLogsLength > 0) {
uint256 singleCallLogsLength = read4(_body, offset);
offset += 0x4;
bytes32 singleLogHash = Hash.sha256ToField(slice(_body, offset, singleCallLogsLength));
// The first 32 bytes of an unencrypted log buffer are its address (see UnencryptedL2Log.toBuffer())
bytes32 siloedLogHash =
Hash.sha256ToField(bytes.concat(slice(_body, offset, 0x20), singleLogHash));
offset += singleCallLogsLength;
flattenedLogHashes = bytes.concat(flattenedLogHashes, siloedLogHash);
privateCircuitPublicInputLogsLength -= (singleCallLogsLength + 0x4);
}
}
// Not having a 0 value hash for empty logs causes issues with empty txs used for padding.
if (flattenedLogHashes.length == 0) {
return (0, offset, 0);
}
// padded to MAX_LOGS * 32 bytes
flattenedLogHashes = bytes.concat(
flattenedLogHashes,
new bytes(Constants.MAX_UNENCRYPTED_LOGS_PER_TX * 32 - flattenedLogHashes.length)
);
bytes32 kernelPublicInputsLogsHash = Hash.sha256ToField(flattenedLogHashes);
return (kernelPublicInputsLogsHash, offset, kernelLogsLength);
}
/**
* @notice Computes the root for a binary Merkle-tree given the leafs.
* @dev Uses sha256.
* @param _leafs - The 32 bytes leafs to build the tree of.
* @return The root of the Merkle tree.
*/
function computeRoot(bytes32[] memory _leafs) internal pure returns (bytes32) {
// @todo Must pad the tree
uint256 treeDepth = 0;
while (2 ** treeDepth < _leafs.length) {
treeDepth++;
}
uint256 treeSize = 2 ** treeDepth;
assembly {
mstore(_leafs, treeSize)
}
for (uint256 i = 0; i < treeDepth; i++) {
for (uint256 j = 0; j < treeSize; j += 2) {
_leafs[j / 2] = Hash.sha256ToField(bytes.concat(_leafs[j], _leafs[j + 1]));
}
treeSize /= 2;
}
return _leafs[0];
}
/**
* @notice Wrapper around the slicing to avoid some stack too deep
* @param _data - The data to slice
* @param _start - The start of the slice
* @param _length - The length of the slice
* @return The slice
*/
function slice(bytes calldata _data, uint256 _start, uint256 _length)
internal
pure
returns (bytes memory)
{
return _data[_start:_start + _length];
}
/**
* @notice Wrapper around the slicing and padding to avoid some stack too deep
* @param _data - The data to slice
* @param _start - The start of the slice
* @param _length - The length of the slice
* @param _targetLength - The length of the padded array
* @return The slice
*/
function sliceAndPadLeft(
bytes calldata _data,
uint256 _start,
uint256 _length,
uint256 _targetLength
) internal pure returns (bytes memory) {
return bytes.concat(new bytes(_targetLength - _length), _data[_start:_start + _length]);
}
/**
* @notice Wrapper around the slicing and padding to avoid some stack too deep
* @param _data - The data to slice
* @param _start - The start of the slice
* @param _length - The length of the slice
* @param _targetLength - The length of the padded array
* @return The slice
*/
function sliceAndPadRight(
bytes calldata _data,
uint256 _start,
uint256 _length,
uint256 _targetLength
) internal pure returns (bytes memory) {
return bytes.concat(_data[_start:_start + _length], new bytes(_targetLength - _length));
}
/**
* @notice Reads 1 bytes from the data
* @param _data - The data to read from
* @param _offset - The offset to read from
* @return The 1 byte as a uint256
*/
function read1(bytes calldata _data, uint256 _offset) internal pure returns (uint256) {
return uint256(uint8(bytes1(slice(_data, _offset, 1))));
}
/**
* @notice Reads 4 bytes from the data
* @param _data - The data to read from
* @param _offset - The offset to read from
* @return The 4 bytes read as a uint256
*/
function read4(bytes calldata _data, uint256 _offset) internal pure returns (uint256) {
return uint256(uint32(bytes4(slice(_data, _offset, 4))));
}
function computeNumTxEffectsToPad(uint32 _numTxEffects) internal pure returns (uint32) {
// 2 is the minimum number of tx effects so we have to handle the following 2 cases separately
if (_numTxEffects == 0) {
return 2;
} else if (_numTxEffects == 1) {
return 1;
}
uint32 v = _numTxEffects;
// the following rounds _numTxEffects up to the next power of 2 (works only for 4 bytes value!)
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v - _numTxEffects;
}
}