Skip to content

Commit

Permalink
Add foundy tests for huff
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jan 12, 2024
1 parent 8cf19b4 commit b57fab8
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/foundry-huff"]
path = lib/foundry-huff
url = https://github.com/huff-language/foundry-huff
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ src = 'contracts'
out = 'foundry_artifacts'
libs = ["node_modules", "lib"]
test = 'foundry_test'
ffi = true
48 changes: 48 additions & 0 deletions foundry_test/modules/utils/L2CompressorHuff.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "foundry_test/base/AdvTest.sol";

import { HuffConfig } from "foundry-huff/HuffConfig.sol";
import { HuffDeployer } from "foundry-huff/HuffDeployer.sol";

interface L2CompressorImps {
function testLoadDynamicSize(
bytes32 _a,
bytes32 _b,
uint256 rindex,
uint256 size
) external pure returns (uint256, uint256);
}

contract L2CompressorHuffTests is Test {
L2CompressorImps public imp;

function setUp() public {
imp = L2CompressorImps(
HuffDeployer
.config()
.with_evm_version("paris")
.deploy("imps/L2CompressorImps")
);
}

function test_load_dynamic_size() external {
(uint256 size, uint256 rindex) = imp.testLoadDynamicSize(
bytes32(0x082366f82de6ef3a1d439d0adbfa2ee606f86c2774b4d946f895dfc1f88443a2),
bytes32(0),
4,
1
);

assertEq(size, 8);
assertEq(rindex, 5);

(size, rindex) = imp.testLoadDynamicSize(
bytes32(0x082366f82de6ef3a1d439d0adbfa2ee606f86c2774b4d946f895dfc1f88443a2),
bytes32(0),
4 + 32 - 3,
2219024896
);
}
}
1 change: 1 addition & 0 deletions lib/foundry-huff
Submodule foundry-huff added at 7648fa
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[FREE_MEMORY_START] // [windex, rindex]

0x01 // [flag, windex, rindex]

FLAG_READ_BYTES32() // [windex, rindex]
}

Expand Down Expand Up @@ -298,16 +297,10 @@

swap1 // [size, cdata[rindex], size + rindex]
0x08 mul // [size bits, cdata[rindex], size + rindex]
0x0100 // [0x0100, size bits, cdata[rindex], size + rindex]
sub // [0x0100 - size bits, cdata[rindex], size + rindex]

shr // [cdata[rindex] >> size bits, size + rindex]

// output stack: [cdata[rindex] >> size bits, size + rindex]
}

#[calldata("0x0005")]
#define test TEST_LOAD_DYNAMIC_SIZE() = {
0x02 0x00 // [size, rindex]
LOAD_DYNAMIC_SIZE() // [val, rindex]

0x0005 eq ASSERT() // [rindex]
0x02 eq ASSERT() // []
}
29 changes: 29 additions & 0 deletions src/imps/L2CompressorImps.huff
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../L2Compressor.huff"

#define function testLoadDynamicSize(bytes32 _a, bytes32 _b, uint256, uint256) view returns (uint256, uint256)

// Function Dispatching
#define macro MAIN() = takes (1) returns (1) {
// Identify which function is being called.
0x00 calldataload 0xE0 shr // [func_sig]

dup1 __FUNC_SIG(testLoadDynamicSize) eq testLoadDynamicSize jumpi

// Revert if no match is found.
0x00 dup1 revert

testLoadDynamicSize:
IMP_LOAD_DYNAMIC_SIZE()
}

#define macro IMP_LOAD_DYNAMIC_SIZE() = takes (2) returns (0) {
0x04 0x40 add calldataload // [rindex]
0x04 0x60 add calldataload // [size, rindex]

LOAD_DYNAMIC_SIZE() // [size bits, rindex + size]

0x00 mstore // [rindex + size]
0x20 mstore // []

0x40 0x00 return
}

0 comments on commit b57fab8

Please sign in to comment.