Skip to content

Commit

Permalink
Add read dyamic signature op
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jan 23, 2024
1 parent 6a242a4 commit faa80f3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions foundry_test/modules/utils/L2CompressorEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,7 @@ function encode_subdigest(bytes32 _subdigest) pure returns (bytes memory) {
function encode_nested(uint8 _weight, uint8 _threshold, bytes memory _nested) pure returns (bytes memory) {
return abi.encodePacked(uint8(0x43), uint8(_weight), uint8(_threshold), encode_bytes_n(_nested));
}

function encode_dynamic_signature(uint8 _weight, address _signer, bytes memory _signature) pure returns (bytes memory) {
return abi.encodePacked(uint8(0x44), uint8(_weight), encodeWord(uint256(uint160(_signer))), encode_bytes_n(_signature));
}
17 changes: 17 additions & 0 deletions foundry_test/modules/utils/L2CompressorHuffReadFlag.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,21 @@ contract L2CompressorHuffReadFlagTests is AdvTest {

assertEq(res, abi.encodePacked(uint8(6), uint8(_weight), uint16(_threshold), uint24(_data.length), _data));
}

function test_read_dynamic_signature(uint8 _weight, address _signer, bytes memory _signature) external {
vm.assume(_signature.length <= type(uint24).max - 1);

bytes memory encoded = encode_dynamic_signature(_weight, _signer, _signature);

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

assertTrue(s);

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

assertEq(rindex, encoded.length);
assertEq(windex, FMS + res.length);

assertEq(res, abi.encodePacked(uint8(2), uint8(_weight), _signer, uint24(_signature.length + 1), _signature, uint8(3)));
}
}
75 changes: 73 additions & 2 deletions src/L2Compressor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
FLAG_BRANCH // 0x41
FLAG_SUBDIGEST // 0x42
FLAG_NESTED // 0x43
FLAG_DYNAMIC_SIGNATURE // 0x44
}

#define constant HIGHEST_FLAG = 0x44
Expand Down Expand Up @@ -410,7 +411,10 @@
end jump
FLAG_NESTED:
READ_NESTED(nrfs) // [windex, rindex]
end jump // [end jump, windex, rindex]
end jump
FLAG_DYNAMIC_SIGNATURE:
READ_DYNAMIC_SIGNATURE(nrfs) // [windex, rindex]
end jump

default:
// The default just pushes the flag as a byte (padded to 32 bytes)
Expand Down Expand Up @@ -518,6 +522,74 @@
// output stack: [windex, rindex]
}

#define macro READ_DYNAMIC_SIGNATURE(nrfs) = takes (2) returns (2) {
// input stack: [windex, rindex]

WRITE_SEQUENCE_FLAG(0x02)

swap1 // [rindex, windex]

// Read the weight, it is always 1 byte
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [weight, rindex, windex]

dup3 // [windex, weight, rindex, windex]
mstore8 // [rindex, windex]
swap1 // [windex, rindex]
0x01 add // [windex, rindex]

// Read the address, use read flag as it may use a pointer
PERFORM_NESTED_READ_FLAG(nrfs) // [windex, rindex]
BACKREAD_SINGLE_VALUE() // [word, windex, rindex]
0x60 shl // [address, windex, rindex]

dup2 // [windex, word, windex, rindex]
mstore // [windex, rindex]
0x14 add // [windex, rindex]

// Now read another nested flag, as-is, but leave
// the space to demark the size

// Clear the memory here, that way we don't need to mask it
// when we write the size
callvalue // [0x00, windex, rindex]
dup2 mstore // [windex, rindex]

// Reserve 3 bytes for the size
dup1 // [windex, size_pointer, rindex]
0x03 add // [windex, size_pointer, rindex]

swap2 // [rindex, size_pointer, windex]
dup3 // [windex, rindex, size_pointer, prev_windex]

PERFORM_NESTED_READ_FLAG(nrfs) // [windex, rindex, size_pointer, prev_windex]

// Need to go back and write the size now

swap3 // [prev_windex, rindex, size_pointer, windex]
dup4 // [windex, prev_windex, rindex, size_pointer, windex]
sub // [size, rindex, size_pointer, windex]

// The size is +1 since we also need to include the suffix "03" for EIP1271
0x01 add // [size + 1, rindex, size_pointer, windex]

0xe8 shl // [size << 0xe8, rindex, size_pointer, windex]
dup3 // [size_pointer, size, rindex, size_pointer, windex]
mload // [mload[size_pointer], size, rindex, size_pointer, windex]
or // [(mload[size_pointer] | size), rindex, size_pointer, windex]
swap1 // [rindex, (mload[size_pointer] | size), size_pointer, windex]
swap2 // [size_pointer, (mload[size_pointer] | size), rindex, windex]
mstore // [rindex, windex]
swap1 // [windex, rindex]

// Write the suffix, just 03
0x03 // [0x03, windex, rindex]
dup2 // [windex, 0x03, windex, rindex]
mstore8 // [windex, rindex]
0x01 add // [windex, rindex]

// output stack: [windex, rindex]
}

#define macro READ_SIGNATURE_W0() = takes (2) returns (2) {
// input stack: [windex, rindex]

Expand Down Expand Up @@ -621,7 +693,6 @@

PERFORM_NESTED_READ_FLAG(nrfs) // [windex, rindex]
BACKREAD_SINGLE_VALUE() // [word, windex, rindex]

0x60 shl // [address, windex, rindex]

dup2 // [windex, word, windex, rindex]
Expand Down

0 comments on commit faa80f3

Please sign in to comment.