Skip to content

Commit 3f15377

Browse files
committed
Read flag fixes
1 parent 66180b5 commit 3f15377

File tree

3 files changed

+551
-174
lines changed

3 files changed

+551
-174
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
import "foundry_test/base/AdvTest.sol";
5+
6+
import "forge-std/console.sol";
7+
import "forge-std/console2.sol";
8+
9+
import { HuffConfig } from "foundry-huff/HuffConfig.sol";
10+
import { HuffDeployer } from "foundry-huff/HuffDeployer.sol";
11+
12+
contract L2CompressorHuffTests is AdvTest {
13+
address public imp;
14+
15+
function setUp() public {
16+
imp = address(
17+
HuffDeployer
18+
.config()
19+
.with_evm_version("paris")
20+
.deploy("imps/L2CompressorReadFlag")
21+
);
22+
}
23+
24+
function test_read_flag_bytes32_zero(bytes32 _data) external {
25+
(bool s, bytes memory r) = imp.staticcall(
26+
abi.encodePacked(hex"00", _data)
27+
);
28+
29+
assertTrue(s);
30+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
31+
assertEq(rindex, 1);
32+
assertEq(windex, 0x80 + 32);
33+
34+
assertEq(abi.encode(uint256(0)), res);
35+
}
36+
37+
function test_read_flag_bytes32_one(uint8 _val) external {
38+
(bool s, bytes memory r) = imp.staticcall(
39+
abi.encodePacked(hex"01", _val)
40+
);
41+
42+
assertTrue(s);
43+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
44+
assertEq(rindex, 2);
45+
assertEq(windex, 0x80 + 32);
46+
47+
assertEq(abi.encode(uint256(_val)), res);
48+
}
49+
50+
function test_read_flag_bytes32_two(uint16 _val) external {
51+
(bool s, bytes memory r) = imp.staticcall(
52+
abi.encodePacked(hex"02", _val)
53+
);
54+
55+
assertTrue(s);
56+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
57+
assertEq(rindex, 3);
58+
assertEq(windex, 0x80 + 32);
59+
60+
assertEq(abi.encode(uint256(_val)), res);
61+
}
62+
63+
function test_read_flag_bytes32_x(uint256 _size, bytes memory _content) public {
64+
_size = bound(_size, 0, 32);
65+
66+
(bool s, bytes memory r) = imp.staticcall(
67+
abi.encodePacked(uint8(_size), _content)
68+
);
69+
70+
assertTrue(s);
71+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
72+
assertEq(rindex, 1 + _size);
73+
assertEq(windex, 0x80 + 32);
74+
75+
_content = abi.encodePacked(_content, bytes32(0));
76+
uint256 expected;
77+
assembly {
78+
expected := mload(add(_content, 0x20))
79+
expected := shr(sub(256, mul(_size, 8)), expected)
80+
}
81+
82+
assertEq(abi.encode(uint256(expected)), res);
83+
}
84+
85+
function test_read_flag_save_and_read_address(address _addr2) external {
86+
address _addr = address(this);
87+
(bool s, bytes memory r) = imp.call(
88+
abi.encodePacked(hex"21", _addr)
89+
);
90+
91+
assertTrue(s);
92+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
93+
94+
assertEq(rindex, 1 + 20);
95+
assertEq(windex, 0x80 + 32);
96+
97+
assertEq(abi.encode(_addr), res);
98+
99+
// Read the address at index 1
100+
(s, r) = imp.staticcall(
101+
abi.encodePacked(hex"23", uint16(1))
102+
);
103+
104+
assertTrue(s);
105+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
106+
assertEq(rindex, 3);
107+
assertEq(windex, 0x80 + 32);
108+
109+
assertEq(abi.encode(_addr), res);
110+
111+
// Save a second address
112+
(s, r) = imp.call(
113+
abi.encodePacked(hex"21", _addr2)
114+
);
115+
116+
assertTrue(s);
117+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
118+
119+
assertEq(rindex, 1 + 20);
120+
assertEq(windex, 0x80 + 32);
121+
122+
assertEq(abi.encode(_addr2), res);
123+
124+
// Read second address using 3 bytes, 4 bytes and 5 bytes
125+
(s, r) = imp.staticcall(
126+
abi.encodePacked(hex"24", uint24(2))
127+
);
128+
129+
assertTrue(s);
130+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
131+
assertEq(rindex, 4);
132+
assertEq(windex, 0x80 + 32);
133+
134+
assertEq(abi.encode(_addr2), res);
135+
136+
(s, r) = imp.staticcall(
137+
abi.encodePacked(hex"25", uint32(2))
138+
);
139+
140+
assertTrue(s);
141+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
142+
assertEq(rindex, 5);
143+
assertEq(windex, 0x80 + 32);
144+
145+
assertEq(abi.encode(_addr2), res);
146+
147+
(s, r) = imp.staticcall(
148+
abi.encodePacked(hex"26", uint40(2))
149+
);
150+
151+
assertTrue(s);
152+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
153+
assertEq(rindex, 6);
154+
assertEq(windex, 0x80 + 32);
155+
156+
assertEq(abi.encode(_addr2), res);
157+
}
158+
159+
function test_read_flag_save_and_read_bytes32(bytes32 _b1, bytes32 _b2) external {
160+
(bool s, bytes memory r) = imp.call(
161+
abi.encodePacked(hex"22", _b1)
162+
);
163+
164+
assertTrue(s);
165+
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
166+
167+
assertEq(rindex, 1 + 32);
168+
assertEq(windex, 0x80 + 32);
169+
170+
assertEq(abi.encode(_b1), res);
171+
172+
// Read the address at index 1
173+
(s, r) = imp.staticcall(
174+
abi.encodePacked(hex"27", uint16(1))
175+
);
176+
177+
assertTrue(s);
178+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
179+
assertEq(rindex, 3);
180+
assertEq(windex, 0x80 + 32);
181+
182+
assertEq(abi.encode(_b1), res);
183+
184+
// Save a second address
185+
(s, r) = imp.call(
186+
abi.encodePacked(hex"22", _b2)
187+
);
188+
189+
assertTrue(s);
190+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
191+
192+
assertEq(rindex, 1 + 32);
193+
assertEq(windex, 0x80 + 32);
194+
195+
assertEq(abi.encode(_b2), res);
196+
197+
// Read second address using 3 bytes, 4 bytes and 5 bytes
198+
(s, r) = imp.staticcall(
199+
abi.encodePacked(hex"28", uint24(2))
200+
);
201+
202+
assertTrue(s);
203+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
204+
assertEq(rindex, 4);
205+
assertEq(windex, 0x80 + 32);
206+
207+
assertEq(abi.encode(_b2), res);
208+
209+
(s, r) = imp.staticcall(
210+
abi.encodePacked(hex"29", uint32(2))
211+
);
212+
213+
assertTrue(s);
214+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
215+
assertEq(rindex, 5);
216+
assertEq(windex, 0x80 + 32);
217+
218+
assertEq(abi.encode(_b2), res);
219+
220+
(s, r) = imp.staticcall(
221+
abi.encodePacked(hex"2a", uint40(2))
222+
);
223+
224+
assertTrue(s);
225+
(rindex, windex, res) = abi.decode(r, (uint256, uint256, bytes));
226+
assertEq(rindex, 6);
227+
assertEq(windex, 0x80 + 32);
228+
229+
assertEq(abi.encode(_b2), res);
230+
}
231+
232+
// function test_read_flag_bytes_n(bytes calldata _data, bytes calldata _extra) external {
233+
// (bool s, bytes memory r) = imp.staticcall(
234+
// abi.encodePacked(hex"2b", _data, _extra)
235+
// );
236+
237+
// assertTrue(s);
238+
// (uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes));
239+
240+
// assertEq(rindex, _data.length + 1);
241+
242+
// }
243+
}

0 commit comments

Comments
 (0)