-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathabi_encode_calldata_slice.sol
69 lines (63 loc) · 2.45 KB
/
abi_encode_calldata_slice.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
pragma abicoder v2;
contract C {
function enc_packed_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(data[start:end]);
}
function enc_packed_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(bytes(data[start:end]));
}
function enc_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(data[start:end]);
}
function enc_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(bytes(data[start:end]));
}
function enc_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_packed_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function enc_packed_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function compare(bytes memory x, bytes memory y) internal {
assert(x.length == y.length);
for (uint i = 0; i < x.length; ++i)
assert(x[i] == y[i]);
}
function test_bytes() public {
bytes memory test = new bytes(3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_bytes(test, i, j), this.enc_packed_bytes_reference(test, i, j));
compare(this.enc_bytes(test, i, j), this.enc_bytes_reference(test, i, j));
}
}
function test_uint256() public {
uint256[] memory test = new uint256[](3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_uint256(test, i, j), this.enc_packed_uint256_reference(test, i, j));
compare(this.enc_uint256(test, i, j), this.enc_uint256_reference(test, i, j));
}
}
}
// ====
// EVMVersion: >homestead
// ----
// test_bytes() ->
// gas irOptimized: 302904
// gas legacy: 305816
// gas legacyOptimized: 244887
// test_uint256() ->
// gas irOptimized: 428338
// gas legacy: 421304
// gas legacyOptimized: 337032