|
1 | 1 | #include "./Errors.huff"
|
2 | 2 |
|
3 | 3 | #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 |
5 | 6 |
|
6 | 7 | #define constant FLAG_READ_BYTES32_2_BYTES = 0x27
|
7 | 8 | #define constant FLAG_READ_ADDRESS_2_BYTES = 0x23
|
|
14 | 15 |
|
15 | 16 | // #define macro MAIN() = takes (0) returns (0) {
|
16 | 17 | // 0x00 // [rindex]
|
17 |
| -// [FREE_MEMORY_START] // [windex, rindex] |
| 18 | +// [FMS] // [windex, rindex] |
18 | 19 |
|
19 | 20 | // 0x01 // [flag, windex, rindex]
|
20 | 21 | // READ_FLAG() // [windex, rindex]
|
|
132 | 133 | JUMP_READ_POWER_OF_2 // 0x2c
|
133 | 134 | }
|
134 | 135 |
|
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: |
136 | 140 | // input stack: [windex, rindex]
|
137 | 141 |
|
138 | 142 | dup2 // [rindex, windex, rindex]
|
|
143 | 147 | 0x01 add // [rindex + 1, windex, flag]
|
144 | 148 | swap2 // [flag, windex, rindex + 1]
|
145 | 149 |
|
| 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 | + |
146 | 154 | __tablesize(FLAG_TABLE) // [table_size, flag, windex, rindex + 1]
|
147 | 155 | __tablestart(FLAG_TABLE) // [table_start, table_size, flag, windex, rindex + 1]
|
148 | 156 | 0x00 // [0x00, table_start, table_size, flag, windex, rindex + 1]
|
149 | 157 | codecopy // [flag, windex, rindex + 1]
|
150 | 158 |
|
151 |
| - dup1 // [flag, flag, windex, rindex + 1] |
152 |
| - 0x2b lt // [0x2b < flag, flag, windex, rindex + 1] |
153 |
| - default jumpi // [flag, windex, rindex + 1] |
154 |
| - |
155 | 159 | dup1 // [flag, flag, windex, rindex + 1]
|
156 | 160 | 0x01 shl // [flag << 0x01, flag, windex, rindex + 1]
|
157 | 161 | mload // [word, flag, windex, rindex + 1]
|
|
281 | 285 | end jump
|
282 | 286 |
|
283 | 287 | JUMP_READ_N_BYTES:
|
284 |
| - READ_N_BYTES() // [windex, rindex] |
| 288 | + READ_N_BYTES(nested_read_flag_start) // [windex, rindex] |
285 | 289 | end jump
|
286 | 290 |
|
287 | 291 | JUMP_READ_POWER_OF_2:
|
288 | 292 | READ_POWER_OF_2() // [windex, rindex]
|
289 | 293 | end jump
|
290 | 294 |
|
291 | 295 | 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] |
292 | 303 |
|
293 | 304 | 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] |
294 | 335 | }
|
295 | 336 |
|
296 | 337 | #[calldata("0x02f1f2")]
|
297 | 338 | #define test TEST_READ_FLAG_2_BYTES() = {
|
298 |
| - 0x00 // [rindex] |
299 |
| - 0x00 // [windex, rindex] |
| 339 | + 0x00 // [rindex] |
| 340 | + [FMS] // [windex, rindex] |
300 | 341 |
|
301 |
| - // READ_FLAG() // [windex, rindex] |
| 342 | + READ_FLAG() // [windex, rindex] |
302 | 343 |
|
303 |
| - // 0x20 eq ASSERT() // [rindex] |
304 |
| - // 0x02 eq ASSERT() // [] |
| 344 | + 0x20 [FMS] add eq ASSERT() // [rindex] |
| 345 | + 0x03 eq ASSERT() // [] |
305 | 346 |
|
306 |
| - // 0x00 mload 0x01f1 eq ASSERT() // [] |
| 347 | + [FMS] mload 0xf1f2 eq ASSERT() // [] |
307 | 348 | }
|
308 | 349 |
|
309 | 350 | #[calldata("0x1000")]
|
310 | 351 | #define test TEST_READ_FLAG_0_BYTES() = {
|
311 |
| - 0x02 // [rindex] |
312 |
| - 0x00 // [windex, rindex] |
| 352 | + 0x02 // [rindex] |
| 353 | + [FMS] // [windex, rindex] |
313 | 354 |
|
314 | 355 | // Store something
|
315 |
| - 0x10 0x00 mstore |
| 356 | + 0x10 [FMS] mstore |
316 | 357 |
|
317 | 358 | READ_FLAG() // [windex, rindex]
|
318 | 359 |
|
319 |
| - 0x20 eq ASSERT() // [rindex] |
320 |
| - 0x03 eq ASSERT() // [] |
| 360 | + 0x20 [FMS] add eq ASSERT() // [rindex] |
| 361 | + 0x03 eq ASSERT() // [] |
321 | 362 |
|
322 |
| - 0x00 mload 0x00 eq ASSERT() // [] |
| 363 | + [FMS] mload 0x00 eq ASSERT() // [] |
323 | 364 | }
|
324 | 365 |
|
325 | 366 | #define test TEST_NUMS() = {
|
|
429 | 470 |
|
430 | 471 | #[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
|
431 | 472 | #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] |
435 | 476 |
|
436 |
| - READ_BYTES32(0xf0, 0x02) // [windex, rindex] |
| 477 | + READ_BYTES32(0xf0, 0x02) // [windex, rindex] |
437 | 478 |
|
438 |
| - 0x40 eq ASSERT() // [rindex] |
439 |
| - 0x03 eq ASSERT() // [] |
| 479 | + [FMS] 0x60 add eq ASSERT() // [rindex] |
| 480 | + 0x03 eq ASSERT() // [] |
440 | 481 |
|
441 |
| - 0x20 mload 0xd10e eq ASSERT() // [] |
| 482 | + [FMS] 0x40 add mload 0xd10e eq ASSERT() // [] |
442 | 483 |
|
443 |
| - 0x00 // [rindex] |
444 |
| - 0x00 // [windex, rindex] |
445 |
| - 0xff // [flag, windex, rindex] |
| 484 | + 0x00 // [rindex] |
| 485 | + [FMS] // [windex, rindex] |
| 486 | + 0xff // [flag, windex, rindex] |
446 | 487 |
|
447 | 488 | READ_BYTES32(0x00, 0x20) // [windex, rindex]
|
448 | 489 |
|
449 |
| - 0x20 eq ASSERT() // [rindex] |
450 |
| - 0x20 eq ASSERT() // [] |
| 490 | + [FMS] 0x20 add eq ASSERT() // [rindex] |
| 491 | + 0x20 eq ASSERT() // [] |
451 | 492 |
|
452 |
| - 0x00 mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // [] |
| 493 | + [FMS] mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // [] |
453 | 494 |
|
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] |
457 | 498 |
|
458 | 499 | READ_BYTES32_WORD() // [windex, rindex]
|
459 | 500 |
|
460 |
| - 0x60 eq ASSERT() // [rindex] |
461 |
| - 0x20 eq ASSERT() // [] |
| 501 | + [FMS] 0x60 add eq ASSERT() // [rindex] |
| 502 | + 0x20 eq ASSERT() // [] |
462 | 503 |
|
463 |
| - 0x40 mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // [] |
| 504 | + [FMS] 0x40 add |
| 505 | + mload 0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8 eq ASSERT() // [] |
464 | 506 |
|
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] |
468 | 510 |
|
469 | 511 | READ_BYTES32_EMPTY() // [windex, rindex]
|
470 | 512 |
|
471 |
| - 0x30 eq ASSERT() // [rindex] |
472 |
| - 0x15 eq ASSERT() // [] |
| 513 | + [FMS] 0x30 add eq ASSERT() // [rindex] |
| 514 | + 0x15 eq ASSERT() // [] |
473 | 515 |
|
474 |
| - 0x10 mload 0x00 eq ASSERT() // [] |
| 516 | + [FMS] 0x10 add mload 0x00 eq ASSERT() // [] |
475 | 517 | }
|
476 | 518 |
|
477 | 519 | #define macro SAVE_ADDRESS() = takes (3) returns (2) {
|
|
505 | 547 | // 0xd10eb37ef5838bb835ea71bbd4053daf8de7bd8e
|
506 | 548 | #[calldata("0xb2d10eb37ef5838bb835ea71bbd4053daf8de7bd8ecdf638451a2bc966a145a8"), value(0x01)]
|
507 | 549 | #define test TEST_SAVE_ADDRESS() = {
|
508 |
| - 0x01 // [rindex] |
509 |
| - 0x20 // [windex, rindex] |
| 550 | + 0x01 // [rindex] |
| 551 | + [FMS] // [windex, rindex] |
510 | 552 |
|
511 | 553 | 0x02 // [flag, windex, rindex]
|
512 | 554 |
|
513 | 555 | SAVE_ADDRESS() // [windex, rindex]
|
514 | 556 |
|
515 |
| - 0x40 eq ASSERT() // [rindex] |
516 |
| - 0x15 eq ASSERT() // [] |
| 557 | + [FMS] 0x20 add eq ASSERT() // [rindex] |
| 558 | + 0x15 eq ASSERT() // [] |
517 | 559 |
|
518 | 560 | // Validate that memory was written correctly
|
519 | 561 |
|
520 |
| - 0x20 mload // [mem[0x20]] () |
| 562 | + [FMS] mload // [mem[0x20]] () |
521 | 563 | 0x000000000000000000000000d10eb37ef5838bb835ea71bbd4053daf8de7bd8e
|
522 |
| - eq ASSERT() // [] |
| 564 | + eq ASSERT() // [] |
523 | 565 |
|
524 | 566 | // Validate that the written address is correct
|
525 | 567 | 0x02 sload // [addr]
|
|
602 | 644 | swap2 // [rindex, size, windex]
|
603 | 645 | swap1 // [size, rindex, windex]
|
604 | 646 |
|
605 |
| - LOAD_DYNAMIC_SIZE() // [index, nrindex + size, windex] |
| 647 | + LOAD_DYNAMIC_SIZE() // [index, nrindex + size, windex] |
606 | 648 | <smv> <smc> sload // [bytes32, nrindex + size, windex]
|
607 | 649 |
|
608 | 650 | dup3 // [windex, bytes32, nrindex + size, windex]
|
|
808 | 850 | 0x10 mload 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff eq ASSERT() // []
|
809 | 851 | }
|
810 | 852 |
|
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] |
816 | 855 |
|
817 |
| - 0x00 byte // [size, windex, rindex] |
| 856 | + pop // [windex, rindex] |
818 | 857 |
|
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] |
822 | 860 |
|
823 | 861 | dup2 // [windex, size, windex, rindex + 1]
|
824 | 862 | dup2 add // [windex + size, size, windex, rindex + 1]
|
|
832 | 870 |
|
833 | 871 | calldatacopy // [windex, rindex + 1 + size]
|
834 | 872 |
|
835 |
| - // output stack: [windex + size, rindex + 1 + size] |
| 873 | + // output stack: [windex + size, rindex + size] |
836 | 874 | }
|
837 | 875 |
|
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 |
| -} |
862 | 876 |
|
863 | 877 | #define macro LOAD_DYNAMIC_SIZE() = takes (2) returns (2) {
|
864 | 878 | // input stack: [size, rindex]
|
|
0 commit comments