Skip to content

Commit cefebf5

Browse files
committed
Expand scratch space and implement nested read
1 parent 3f15377 commit cefebf5

File tree

2 files changed

+110
-94
lines changed

2 files changed

+110
-94
lines changed

src/L2Compressor.huff

Lines changed: 100 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "./Errors.huff"
22

33
#define constant ADDR_BYTES_STORAGE = 0x00
4-
#define constant FREE_MEMORY_START = 0x40
4+
#define constant FMS = 0xa0
5+
#define constant NESTED_READ_FLAG_RETURN_MSLOT = 0x80
56

67
#define constant FLAG_READ_BYTES32_2_BYTES = 0x27
78
#define constant FLAG_READ_ADDRESS_2_BYTES = 0x23
@@ -14,7 +15,7 @@
1415

1516
// #define macro MAIN() = takes (0) returns (0) {
1617
// 0x00 // [rindex]
17-
// [FREE_MEMORY_START] // [windex, rindex]
18+
// [FMS] // [windex, rindex]
1819

1920
// 0x01 // [flag, windex, rindex]
2021
// READ_FLAG() // [windex, rindex]
@@ -132,7 +133,10 @@
132133
JUMP_READ_POWER_OF_2 // 0x2c
133134
}
134135

135-
#define macro READ_FLAG(nrfj) = takes (2) returns (2) {
136+
#define constant HIGHEST_FLAG = 0x2c
137+
138+
#define macro READ_FLAG() = takes (2) returns (2) {
139+
nested_read_flag_start:
136140
// input stack: [windex, rindex]
137141

138142
dup2 // [rindex, windex, rindex]
@@ -143,15 +147,15 @@
143147
0x01 add // [rindex + 1, windex, flag]
144148
swap2 // [flag, windex, rindex + 1]
145149

150+
dup1 // [flag, flag, windex, rindex + 1]
151+
[HIGHEST_FLAG] lt // [HIGHEST_FLAG < flag, flag, windex, rindex + 1]
152+
default jumpi // [flag, windex, rindex + 1]
153+
146154
__tablesize(FLAG_TABLE) // [table_size, flag, windex, rindex + 1]
147155
__tablestart(FLAG_TABLE) // [table_start, table_size, flag, windex, rindex + 1]
148156
0x00 // [0x00, table_start, table_size, flag, windex, rindex + 1]
149157
codecopy // [flag, windex, rindex + 1]
150158

151-
dup1 // [flag, flag, windex, rindex + 1]
152-
0x2b lt // [0x2b < flag, flag, windex, rindex + 1]
153-
default jumpi // [flag, windex, rindex + 1]
154-
155159
dup1 // [flag, flag, windex, rindex + 1]
156160
0x01 shl // [flag << 0x01, flag, windex, rindex + 1]
157161
mload // [word, flag, windex, rindex + 1]
@@ -281,45 +285,82 @@
281285
end jump
282286

283287
JUMP_READ_N_BYTES:
284-
READ_N_BYTES() // [windex, rindex]
288+
READ_N_BYTES(nested_read_flag_start) // [windex, rindex]
285289
end jump
286290

287291
JUMP_READ_POWER_OF_2:
288292
READ_POWER_OF_2() // [windex, rindex]
289293
end jump
290294

291295
default:
296+
// The default just pushes the flag as a byte (padded to 32 bytes)
297+
// notice that we start at 0x01 since 0x00 can be pushed with the flag 0x00
298+
[HIGHEST_FLAG] // [HIGHEST_FLAG, flag, windex, rindex]
299+
swap1 sub // [flag - HIGHEST_FLAG, windex, rindex]
300+
dup2 // [windex, flag - HIGHEST_FLAG, windex, rindex]
301+
mstore // [windex, rindex]
302+
0x20 add // [windex + 0x20, rindex]
292303

293304
end:
305+
306+
// If the NESTED memory slot is not 0, then we need to jump there
307+
// it means that this READ_FLAG() is nested
308+
309+
[NESTED_READ_FLAG_RETURN_MSLOT] mload // [nrfr, windex, rindex]
310+
dup1 jumpi // [windex, rindex]
311+
}
312+
313+
#define macro PERFORM_NESTED_READ_FLAG(nrfs) = takes(0) returns (0) {
314+
back // [back]
315+
[NESTED_READ_FLAG_RETURN_MSLOT] // [nrfr, back]
316+
mstore // []
317+
318+
<nrfs> jump // []
319+
320+
back:
321+
322+
// Clear the back pointer!
323+
0x00 [NESTED_READ_FLAG_RETURN_MSLOT] mstore
324+
}
325+
326+
#define macro BACKREAD_SINGLE_VALUE() = takes (1) returns (2) {
327+
// input stack: [windex]
328+
329+
0x20 swap1 sub // [windex - 0x20]
330+
dup1 // [windex - 0x20, windex - 0x20]
331+
332+
mload // [mem[windex - 0x20], windex - 0x20]
333+
334+
// output stack: [mem[windex - 0x20], windex - 0x20]
294335
}
295336

296337
#[calldata("0x02f1f2")]
297338
#define test TEST_READ_FLAG_2_BYTES() = {
298-
0x00 // [rindex]
299-
0x00 // [windex, rindex]
339+
0x00 // [rindex]
340+
[FMS] // [windex, rindex]
300341

301-
// READ_FLAG() // [windex, rindex]
342+
READ_FLAG() // [windex, rindex]
302343

303-
// 0x20 eq ASSERT() // [rindex]
304-
// 0x02 eq ASSERT() // []
344+
0x20 [FMS] add eq ASSERT() // [rindex]
345+
0x03 eq ASSERT() // []
305346

306-
// 0x00 mload 0x01f1 eq ASSERT() // []
347+
[FMS] mload 0xf1f2 eq ASSERT() // []
307348
}
308349

309350
#[calldata("0x1000")]
310351
#define test TEST_READ_FLAG_0_BYTES() = {
311-
0x02 // [rindex]
312-
0x00 // [windex, rindex]
352+
0x02 // [rindex]
353+
[FMS] // [windex, rindex]
313354

314355
// Store something
315-
0x10 0x00 mstore
356+
0x10 [FMS] mstore
316357

317358
READ_FLAG() // [windex, rindex]
318359

319-
0x20 eq ASSERT() // [rindex]
320-
0x03 eq ASSERT() // []
360+
0x20 [FMS] add eq ASSERT() // [rindex]
361+
0x03 eq ASSERT() // []
321362

322-
0x00 mload 0x00 eq ASSERT() // []
363+
[FMS] mload 0x00 eq ASSERT() // []
323364
}
324365

325366
#define test TEST_NUMS() = {
@@ -429,49 +470,50 @@
429470

430471
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
431472
#define test TEST_FLAG_READ_BYTES32() = {
432-
0x01 // [rindex]
433-
0x20 // [windex, rindex]
434-
0x00 // [flag, windex, rindex]
473+
0x01 // [rindex]
474+
[FMS] 0x40 add // [windex, rindex]
475+
0x00 // [flag, windex, rindex]
435476

436-
READ_BYTES32(0xf0, 0x02) // [windex, rindex]
477+
READ_BYTES32(0xf0, 0x02) // [windex, rindex]
437478

438-
0x40 eq ASSERT() // [rindex]
439-
0x03 eq ASSERT() // []
479+
[FMS] 0x60 add eq ASSERT() // [rindex]
480+
0x03 eq ASSERT() // []
440481

441-
0x20 mload 0xd10e eq ASSERT() // []
482+
[FMS] 0x40 add mload 0xd10e eq ASSERT() // []
442483

443-
0x00 // [rindex]
444-
0x00 // [windex, rindex]
445-
0xff // [flag, windex, rindex]
484+
0x00 // [rindex]
485+
[FMS] // [windex, rindex]
486+
0xff // [flag, windex, rindex]
446487

447488
READ_BYTES32(0x00, 0x20) // [windex, rindex]
448489

449-
0x20 eq ASSERT() // [rindex]
450-
0x20 eq ASSERT() // []
490+
[FMS] 0x20 add eq ASSERT() // [rindex]
491+
0x20 eq ASSERT() // []
451492

452-
0x00 mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // []
493+
[FMS] mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // []
453494

454-
0x00 // [rindex]
455-
0x40 // [windex, rindex]
456-
0x00 // [flag, windex, rindex]
495+
0x00 // [rindex]
496+
[FMS] 0x40 add // [windex, rindex]
497+
0x00 // [flag, windex, rindex]
457498

458499
READ_BYTES32_WORD() // [windex, rindex]
459500

460-
0x60 eq ASSERT() // [rindex]
461-
0x20 eq ASSERT() // []
501+
[FMS] 0x60 add eq ASSERT() // [rindex]
502+
0x20 eq ASSERT() // []
462503

463-
0x40 mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // []
504+
[FMS] 0x40 add
505+
mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // []
464506

465-
0x15 // [rindex]
466-
0x10 // [windex, rindex]
467-
0xf1 // [flag, windex, rindex]
507+
0x15 // [rindex]
508+
[FMS] 0x10 add // [windex, rindex]
509+
0xf1 // [flag, windex, rindex]
468510

469511
READ_BYTES32_EMPTY() // [windex, rindex]
470512

471-
0x30 eq ASSERT() // [rindex]
472-
0x15 eq ASSERT() // []
513+
[FMS] 0x30 add eq ASSERT() // [rindex]
514+
0x15 eq ASSERT() // []
473515

474-
0x10 mload 0x00 eq ASSERT() // []
516+
[FMS] 0x10 add mload 0x00 eq ASSERT() // []
475517
}
476518

477519
#define macro SAVE_ADDRESS() = takes (3) returns (2) {
@@ -505,21 +547,21 @@
505547
// 0xd10eb37ef5838bb835ea71bbd4053daf8de7bd8e
506548
#[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
507549
#define test TEST_SAVE_ADDRESS() = {
508-
0x01 // [rindex]
509-
0x20 // [windex, rindex]
550+
0x01 // [rindex]
551+
[FMS] // [windex, rindex]
510552

511553
0x02 // [flag, windex, rindex]
512554

513555
SAVE_ADDRESS() // [windex, rindex]
514556

515-
0x40 eq ASSERT() // [rindex]
516-
0x15 eq ASSERT() // []
557+
[FMS] 0x20 add eq ASSERT() // [rindex]
558+
0x15 eq ASSERT() // []
517559

518560
// Validate that memory was written correctly
519561

520-
0x20 mload // [mem[0x20]] ()
562+
[FMS] mload // [mem[0x20]] ()
521563
0x000000000000000000000000d10eb37ef5838bb835ea71bbd4053daf8de7bd8e
522-
eq ASSERT() // []
564+
eq ASSERT() // []
523565

524566
// Validate that the written address is correct
525567
0x02 sload // [addr]
@@ -602,7 +644,7 @@
602644
swap2 // [rindex, size, windex]
603645
swap1 // [size, rindex, windex]
604646

605-
LOAD_DYNAMIC_SIZE() // [index, nrindex + size, windex]
647+
LOAD_DYNAMIC_SIZE() // [index, nrindex + size, windex]
606648
<smv> <smc> sload // [bytes32, nrindex + size, windex]
607649

608650
dup3 // [windex, bytes32, nrindex + size, windex]
@@ -808,17 +850,13 @@
808850
0x10 mload 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff eq ASSERT() // []
809851
}
810852

811-
#define macro READ_N_BYTES() = takes (2) returns (2) {
812-
// input stack: [windex, rindex]
813-
814-
dup2 // [rindex, windex, rindex]
815-
calldataload // [cdata[rindex], windex, rindex]
853+
#define macro READ_N_BYTES(nrfs) = takes (2) returns (2) {
854+
// input stack: [flag, windex, rindex]
816855

817-
0x00 byte // [size, windex, rindex]
856+
pop // [windex, rindex]
818857

819-
swap2 // [rindex, windex, size]
820-
0x01 add // [rindex + 1, windex, size]
821-
swap2 // [size, windex, rindex + 1]
858+
PERFORM_NESTED_READ_FLAG(<nrfs>) // [windex, rindex]
859+
BACKREAD_SINGLE_VALUE() // [size, windex, rindex]
822860

823861
dup2 // [windex, size, windex, rindex + 1]
824862
dup2 add // [windex + size, size, windex, rindex + 1]
@@ -832,33 +870,9 @@
832870

833871
calldatacopy // [windex, rindex + 1 + size]
834872

835-
// output stack: [windex + size, rindex + 1 + size]
873+
// output stack: [windex + size, rindex + size]
836874
}
837875

838-
#[calldata("0x02f1f240a3b3dcc26b3a2b584cf0427ac8ef901401c89b22613407ddfc6790209bf4151a2ed3d1275d5f8f13a8cbe8adda0193aaf438230c921b2d717dc314592b9f53fc")]
839-
#define test TEST_READ_N_BYTES() = {
840-
0x00 // [rindex]
841-
0x00 // [windex, rindex]
842-
843-
READ_N_BYTES() // [windex, rindex]
844-
845-
0x02 eq ASSERT() // [rindex]
846-
0x03 eq ASSERT() // []
847-
848-
0x00 mload 0x00 byte 0xf1 eq ASSERT() // []
849-
0x00 mload 0x01 byte 0xf2 eq ASSERT() // []
850-
851-
0x03 // [rindex]
852-
0x20 // [windex, rindex]
853-
854-
READ_N_BYTES() // [windex, rindex]
855-
856-
0x60 eq ASSERT() // [rindex]
857-
0x03 0x40 add 0x01 add eq ASSERT() // []
858-
859-
0x20 mload 0xa3b3dcc26b3a2b584cf0427ac8ef901401c89b22613407ddfc6790209bf4151a eq ASSERT() // []
860-
0x40 mload 0x2ed3d1275d5f8f13a8cbe8adda0193aaf438230c921b2d717dc314592b9f53fc eq ASSERT() // []
861-
}
862876

863877
#define macro LOAD_DYNAMIC_SIZE() = takes (2) returns (2) {
864878
// input stack: [size, rindex]

src/imps/L2CompressorReadFlag.huff

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
#include "../L2Compressor.huff"
22

3+
#define constant FMS = 0xa0
4+
35
// Function Dispatching
46
#define macro MAIN() = takes (1) returns (1) {
57
// readAdvanced with whatever calldata is passed
68
// first 32 bytes returns the new rindex and the next 32 bytes returns the new windex
79

810
0x00 // [rindex]
9-
0x80 // [windex, rindex]
11+
[FMS] // [windex, rindex]
1012

1113
READ_FLAG() // [windex, rindex]
1214

13-
0x80 // [0x80, windex, rindex]
14-
dup2 // [windex, 0x80, windex, rindex]
15+
[FMS] // [0xa0, windex, rindex]
16+
dup2 // [windex, 0xa0, windex, rindex]
1517
sub // [len, windex, rindex]
1618

1719
swap2 // [rindex, windex, len]
1820

19-
0x00 mstore // [windex, len]
20-
0x20 mstore // [len]
21+
0x80 [FMS] sub mstore // [windex, len]
22+
0x60 [FMS] sub mstore // [len]
2123

22-
0x60 0x40 mstore // [len]
23-
dup1 0x60 mstore // [len]
24+
0x60 0x40 [FMS] sub mstore // [len]
25+
dup1 0x20 [FMS] sub mstore // [len]
2426

2527
0x80 add // [len + 0x80]
2628

27-
0x00 return
29+
0x80 [FMS] sub return
2830
}

0 commit comments

Comments
 (0)