Skip to content

Commit

Permalink
Fix read literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jan 25, 2024
1 parent 6b18d20 commit 95da97e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions foundry_test/modules/utils/L2CompressorEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ function encodeWord(bytes32 _value) pure returns (bytes memory) {
}

function encodeWord(uint256 _value) pure returns (bytes memory) {
uint256 highestFlag = 0x4f;

if (_value < type(uint8).max - highestFlag) {
return abi.encodePacked(uint8(_value + highestFlag + 1));
}

uint8 b = requiredBytesFor(_value);
return abi.encodePacked(b, packToBytes(_value, b));
}
Expand Down
13 changes: 13 additions & 0 deletions foundry_test/modules/utils/L2CompressorHuffReadFlag.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,17 @@ contract L2CompressorHuffReadFlagTests is AdvTest {
assertEq(rindex, encoded.length);
assertEq(res, abi.encode(_addr, _addr));
}

function test_read_literal(uint256 _val) external {
bytes memory encoded = encodeWord(_val);

(bool s, bytes memory r) = imp.call(encoded);
assertTrue(s);

(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));

assertEq(windex, FMS + res.length);
assertEq(rindex, encoded.length);
assertEq(res, abi.encode(_val));
}
}
17 changes: 9 additions & 8 deletions src/L2Compressor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
}

#define constant HIGHEST_FLAG = 0x4f
#define constant HIGHEST_FLAG_PLUS_ONE = 0x50

#define macro READ_FLAG() = takes (2) returns (2) {
nrfs:
Expand Down Expand Up @@ -467,11 +468,11 @@
default:
// The default just pushes the flag as a byte (padded to 32 bytes)
// notice that we start at 0x01 since 0x00 can be pushed with the flag 0x00
[HIGHEST_FLAG] // [HIGHEST_FLAG, flag, windex, rindex]
swap1 sub // [flag - HIGHEST_FLAG, windex, rindex]
dup2 // [windex, flag - HIGHEST_FLAG, windex, rindex]
mstore // [windex, rindex]
0x20 add // [windex + 0x20, rindex]
[HIGHEST_FLAG_PLUS_ONE] // [HIGHEST_FLAG_PLUS_ONE, flag, windex, rindex]
swap1 sub // [flag - HIGHEST_FLAG_PLUS_ONE, windex, rindex]
dup2 // [windex, flag - HIGHEST_FLAG_PLUS_ONE, windex, rindex]
mstore // [windex, rindex]
0x20 add // [windex + 0x20, rindex]

end:

Expand Down Expand Up @@ -1861,7 +1862,7 @@
// output stack: [windex + 0x20, rindex + 0x20]
}

#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8")]
#define test TEST_FLAG_READ_BYTES32() = {
0x01 // [rindex]
[FMS] 0x40 add // [windex, rindex]
Expand Down Expand Up @@ -1932,7 +1933,7 @@
}

// 0xd10eb37ef5838bb835ea71bbd4053daf8de7bd8e
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8")]
#define test TEST_SAVE_ADDRESS() = {
0x01 // [rindex]
[FMS] // [windex, rindex]
Expand Down Expand Up @@ -1982,7 +1983,7 @@
// output stack: [windex + 32, rindex + 32]
}

#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a899"), value(0x01)]
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a899")]
#define test TEST_SAVE_BYTES32() = {
0x01 // [rindex]
0x20 // [windex, rindex]
Expand Down

0 comments on commit 95da97e

Please sign in to comment.