Skip to content

Commit

Permalink
Custom macro to load 1 byte
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jan 25, 2024
1 parent 62a38de commit 07f7b99
Showing 1 changed file with 56 additions and 41 deletions.
97 changes: 56 additions & 41 deletions src/L2Compressor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@
#define macro READ_POW_10_AND_SELF_EXECUTE(nrfs) = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [exp_word, rindex, windex]
swap1 // [rindex, windex]
LOAD_1_BYTE() // [exp_word, rindex, windex]

// We didn't had any more pleace for READ_SELF_EXECUTE without expanding
// the jumptable! so it has to live here. Sorry about that.
Expand Down Expand Up @@ -531,16 +531,16 @@

end_pow_10 jumpi // [windex, rindex]

BACKREAD_SINGLE_VALUE() // [pow_result, windex, rindex]
swap2 // [rindex, windex, pow_result]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [factor, rindex, windex, pow_result]
swap1 // [rindex, factor, windex, pow_result]
swap3 // [pow_result, factor, windex, rindex]
mul // [(pow_result * factor), windex, rindex]
dup2 // [windex, (pow_result * factor), windex, rindex]
mstore // [windex, rindex]
0x20 // [0x20, windex, rindex]
add // [(0x20 + windex), rindex]
BACKREAD_SINGLE_VALUE() // [pow_result, windex, rindex]
swap2 // [rindex, windex, pow_result]
LOAD_1_BYTE() // [factor, rindex, windex, pow_result]
swap1 // [rindex, factor, windex, pow_result]
swap3 // [pow_result, factor, windex, rindex]
mul // [(pow_result * factor), windex, rindex]
dup2 // [windex, (pow_result * factor), windex, rindex]
mstore // [windex, rindex]
0x20 // [0x20, windex, rindex]
add // [(0x20 + windex), rindex]

end_pow_10: // [windex, rindex]

Expand Down Expand Up @@ -588,7 +588,7 @@
LOAD_DYNAMIC_SIZE(0x02, 0xf0) // [location, rindex, windex]
swap1 // [rindex, location, windex]

LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [size, rindex, location, windex]
LOAD_1_BYTE() // [size, rindex, location, windex]

dup1 // [size, size, rindex, location, windex]
swap3 // [location, size, rindex, size, windex]
Expand Down Expand Up @@ -667,8 +667,8 @@
#define macro READ_NESTED_N_FLAGS_8(nrfs) = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [size, rindex, windex]
swap1 // [rindex, windex]
LOAD_1_BYTE() // [size, rindex, windex]

swap2 // [windex, rindex, size]
READ_NESTED_N_FLAGS(<nrfs>)
Expand Down Expand Up @@ -809,7 +809,7 @@
swap1 // [rindex, windex]

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

dup3 // [windex, weight, rindex, windex]
mstore8 // [rindex, windex]
Expand Down Expand Up @@ -872,11 +872,11 @@
#define macro READ_SIGNATURE_W0() = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
swap1 // [rindex, windex]

LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [weight, rindex, windex]
LOAD_1_BYTE() // [weight, rindex, windex]

swap2 // [windex, rindex, weight]
swap2 // [windex, rindex, weight]

READ_SIGNATURE() // [windex, rindex]

Expand Down Expand Up @@ -930,9 +930,9 @@
#define macro READ_ADDRESS_W0(nrfs) = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
swap1 // [rindex, windex]

LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [weight, rindex, windex]
LOAD_1_BYTE() // [weight, rindex, windex]

swap2 // [windex, rindex, weight]

Expand Down Expand Up @@ -1057,21 +1057,21 @@
// only supports 1 byte for it. If the wallet is not compatible, READ_NESTED
// can't be used, and READ_N_BYTES must be used instead

swap1 // [rindex, windex]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [weight, rindex, windex]
swap1 // [rindex, weight, windex]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [threshold, rindex, weight, windex]
0xf0 shl // [threshold << 0xf0, rindex, weight, windex]
swap2 // [weight, rindex, threshold, windex]

dup4 // [windex, weight, rindex, threshold, windex]
mstore8 // [rindex, threshold, windex]
swap2 // [windex, threshold, rindex]
0x01 add // [windex, threshold, rindex]
swap1 // [threshold, windex, rindex]
dup2 // [windex, threshold, windex, rindex]
mstore // [windex, rindex]
0x02 add // [windex, rindex]
swap1 // [rindex, windex]
LOAD_1_BYTE() // [weight, rindex, windex]
swap1 // [rindex, weight, windex]
LOAD_1_BYTE() // [threshold, rindex, weight, windex]
0xf0 shl // [threshold << 0xf0, rindex, weight, windex]
swap2 // [weight, rindex, threshold, windex]

dup4 // [windex, weight, rindex, threshold, windex]
mstore8 // [rindex, threshold, windex]
swap2 // [windex, threshold, rindex]
0x01 add // [windex, threshold, rindex]
swap1 // [threshold, windex, rindex]
dup2 // [windex, threshold, windex, rindex]
mstore // [windex, rindex]
0x02 add // [windex, rindex]

// Now we just proceed by reading the branch
// the only important part is that we need to
Expand Down Expand Up @@ -1653,8 +1653,8 @@
#define macro READ_ABI_4_BYTES() = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [index, rindex, windex]
swap1 // [rindex, windex]
LOAD_1_BYTE() // [index, rindex, windex]

// The 4 bytes may be provided either as an index (1 byte)
// of the known 4bytes table, or as 00 and the real 4 bytes
Expand Down Expand Up @@ -2231,9 +2231,9 @@
#define macro READ_POW_2() = takes (2) returns (2) {
// input stack: [windex, rindex]

swap1 // [rindex, windex]
swap1 // [rindex, windex]

LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [exp, rindex, windex]
LOAD_1_BYTE() // [exp, rindex, windex]

// an exp value 0 means that the formula will be 2 ** exp - 1
// and we need to read a second exponent
Expand All @@ -2244,7 +2244,7 @@
//sub_1:
pop // [rindex, windex]

LOAD_DYNAMIC_SIZE(0x01, 0xf8) // [exp, rindex, windex]
LOAD_1_BYTE() // [exp, rindex, windex]

0x01 // [0x01, exp, rindex, windex]
dup1 // [0x01, 0x01, exp, rindex, windex]
Expand Down Expand Up @@ -2369,6 +2369,21 @@
// output stack: [windex + size, rindex + size]
}

#define macro LOAD_1_BYTE() = takes (1) returns (2) {
// input stack: [rindex]

dup1 // [rindex, rindex]
0x01 // [0x01, rindex, rindex]
add // [(0x01 + rindex), rindex]
swap1 // [rindex, (0x01 + rindex)]

calldataload // [data[rindex], (0x01 + rindex)]

callvalue // [0x00, data[rindex], (0x01 + rindex)]
byte // [byte[0x00], (0x01 + rindex)]

// output stack: [value, rindex]
}

#define macro LOAD_DYNAMIC_SIZE(read_bytes, read_bits_shift) = takes (1) returns (2) {
// input stack: [rindex]
Expand Down

0 comments on commit 07f7b99

Please sign in to comment.