Skip to content

Commit

Permalink
reduce size within kernel of revert code from Field to u8
Browse files Browse the repository at this point in the history
  • Loading branch information
just-mitch committed Mar 19, 2024
1 parent 74ac9c5 commit 7b7273b
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 102 deletions.
28 changes: 14 additions & 14 deletions l1-contracts/src/core/libraries/decoders/MessagesDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import {Hash} from "../Hash.sol";
* | 0x4 | a * 0x20 | newL1ToL2Msgs
* | 0x4 + a * 0x20 = tx0Start | 0x4 | len(numTxs) (denoted t)
* | | | TxEffect 0 {
* | tx0Start | 0x20 | revertCode
* | tx0Start + 0x20 | 0x1 | len(newNoteHashes) (denoted b)
* | tx0Start + 0x20 + 0x1 | b * 0x20 | newNoteHashes
* | tx0Start + 0x20 + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | tx0Start | 0x1 | revertCode
* | tx0Start + 0x1 | 0x1 | len(newNoteHashes) (denoted b)
* | tx0Start + 0x1 + 0x1 | b * 0x20 | newNoteHashes
* | tx0Start + 0x1 + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | | | },
* | | | TxEffect 1 {
* | | | ...
Expand Down Expand Up @@ -91,7 +91,7 @@ library MessagesDecoder {
// Now we iterate over the tx effects
for (uint256 i = 0; i < numTxs; i++) {
// revertCode
offset += 0x20;
offset += 0x1;

// Note hashes
count = read1(_body, offset);
Expand Down
57 changes: 37 additions & 20 deletions l1-contracts/src/core/libraries/decoders/TxsDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ import {Hash} from "../Hash.sol";
* | 0x4 | a * 0x20 | newL1ToL2Msgs
* | 0x4 + a * 0x20 = tx0Start | 0x4 | len(numTxs) (denoted t)
* | | | TxEffect 0 {
* | tx0Start | 0x20 | revertCode
* | tx0Start + 0x20 | 0x1 | len(newNoteHashes) (denoted b)
* | tx0Start + 0x20 + 0x1 | b * 0x20 | newNoteHashes
* | tx0Start + 0x20 + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | tx0Start + 0x20 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | tx0Start | 0x1 | revertCode
* | tx0Start + 0x1 | 0x1 | len(newNoteHashes) (denoted b)
* | tx0Start + 0x1 + 0x1 | b * 0x20 | newNoteHashes
* | tx0Start + 0x1 + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | tx0Start + 0x1 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | | | },
* | | | TxEffect 1 {
* | | | ...
Expand Down Expand Up @@ -115,7 +115,7 @@ library TxsDecoder {

// Revert Code
offsets.revertCode = offset;
offset += 0x20;
offset += 0x1;

// Note hashes
uint256 count = read1(_body, offset);
Expand Down Expand Up @@ -154,27 +154,28 @@ library TxsDecoder {

// Insertions are split into multiple `bytes.concat` to work around stack too deep.
vars.baseLeaf = bytes.concat(
bytes32(slice(_body, offsets.revertCode, 0x20)),
// pad the revert code to 32 bytes to match the hash preimage
sliceAndPadLeft(_body, offsets.revertCode, 0x1, 0x20),
bytes.concat(
sliceAndPad(
sliceAndPadRight(
_body,
offsets.noteHash,
counts.noteHash * 0x20,
Constants.NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPad(
sliceAndPadRight(
_body,
offsets.nullifier,
counts.nullifier * 0x20,
Constants.NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPad(
sliceAndPadRight(
_body,
offsets.l2ToL1Msgs,
counts.l2ToL1Msgs * 0x20,
Constants.L2_TO_L1_MSGS_NUM_BYTES_PER_BASE_ROLLUP
),
sliceAndPad(
sliceAndPadRight(
_body,
offsets.publicData,
counts.publicData * 0x40,
Expand Down Expand Up @@ -305,7 +306,23 @@ library TxsDecoder {
* @param _targetLength - The length of the padded array
* @return The slice
*/
function sliceAndPad(bytes calldata _data, uint256 _start, uint256 _length, uint256 _targetLength)
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)
Expand Down
12 changes: 6 additions & 6 deletions l1-contracts/test/fixtures/empty_block_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
]
},
"block": {
"archive": "0x06469dfc367020dff702cf28ec39ab16b30039cd891f4875b8454a7c15124266",
"body": "0x000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"archive": "0x0b89607cab1873fbd39f9235ea7124e12d2fdc50a8d74267624f47da4db190c1",
"body": "0x0000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"txsEffectsHash": "0xf0712fd0e716f7d0c3ce0986086fcf5ade6d8205e8ffa2c84160ae2dca4fd0cc",
"decodedHeader": {
"contentCommitment": {
Expand All @@ -32,8 +32,8 @@
"chainId": 31337,
"timestamp": 0,
"version": 1,
"coinbase": "0x066778087af46651b679415994007ea595786cea",
"feeRecipient": "0x04404372eb53c6fad1344bb6caff65029dfb32000e9e81908d0a06f8239b3054"
"coinbase": "0x89a752d909f418d6424f1b560b0ca18dcad29bed",
"feeRecipient": "0x0b3bef33e2b7e9bdfb2253ac8e6bef188347fd78418661fe950ca15b6cab557f"
},
"lastArchive": {
"nextAvailableLeafIndex": 1,
Expand All @@ -60,7 +60,7 @@
}
}
},
"header": "0x012a86560737adb075e12af8253fb09abf17aa841fb56d180bc89f0d2d473c7f000000010000000000000000000000000000000000000000000000000000000000000002f0712fd0e716f7d0c3ce0986086fcf5ade6d8205e8ffa2c84160ae2dca4fd0cc536d98837f2dd165a55d5eeae91485954472d56f246df256bf3cae19352a123cc78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000066778087af46651b679415994007ea595786cea04404372eb53c6fad1344bb6caff65029dfb32000e9e81908d0a06f8239b3054",
"publicInputsHash": "0x02dd0d23315b3b22b240716b07ffebd9c3e8ddd85ba6a4998129e14e4723c8d7"
"header": "0x012a86560737adb075e12af8253fb09abf17aa841fb56d180bc89f0d2d473c7f000000010000000000000000000000000000000000000000000000000000000000000002f0712fd0e716f7d0c3ce0986086fcf5ade6d8205e8ffa2c84160ae2dca4fd0cc536d98837f2dd165a55d5eeae91485954472d56f246df256bf3cae19352a123cc78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000089a752d909f418d6424f1b560b0ca18dcad29bed0b3bef33e2b7e9bdfb2253ac8e6bef188347fd78418661fe950ca15b6cab557f",
"publicInputsHash": "0x131b764dd6cc048f80014477523bec0b69c2374243f49fd18197a6be46f7168c"
}
}
Loading

0 comments on commit 7b7273b

Please sign in to comment.