-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathutils.zkasm
1557 lines (1388 loc) · 52.8 KB
/
utils.zkasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; @info Get absolute value and sign
; @in A => number to convert
; @out A => Absolut value of A
; @out B => Sign of A [1 if negative, 0 positive]
abs:
; check zk-counters
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
0 => B
; if is negative, change sign
$ => B :SLT, JMPC(absIsNeg)
:RETURN
absIsNeg:
A => B
0 => A
; 0 - (-A) = A
$ => A :SUB
1 => B :RETURN
VAR GLOBAL tmpZkPCcopy
; @info copy calldata from previous context to current context
; @in argsOffsetCall: offset to copy from te calldata
; @in argsLengthCall: length to copy from the calldata
copySP:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
RR :MSTORE(tmpZkPCcopy)
; store current ctx
CTX :MSTORE(currentCTX)
; set SP at the beginning of calldata memory allocation
%CALLDATA_OFFSET => SP
; retrieve previous context
$ => CTX :MLOAD(originCTX)
$ => E :MLOAD(argsOffsetCall)
$ => C :MLOAD(argsLengthCall)
copySPloop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep)
C :JMPZ(copySPEnd)
C - 32 :JMPN(copySPFinal)
; load 32 bytes from previous context
zkPC+1 => RR :JMP(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
; restore current context
$ => CTX :MLOAD(currentCTX)
; insert 32 bytes from previous context's calldata to current context's calldata
A :MSTORE(SP++)
; load previous context for next iteration
$ => CTX :MLOAD(originCTX)
C - 32 => C :JMP(copySPloop)
copySPFinal:
; update RR to call a function from a call and load X bytes from previous context
zkPC+1 => RR :JMP(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
; restore current context
$ => CTX :MLOAD(currentCTX)
; insert X bytes from previous context's calldata to current context's calldata
A :MSTORE(SP++)
; load previous context
$ => CTX :MLOAD(originCTX)
copySPEnd:
; restore RR to return to initial call correctly
$ => RR :MLOAD(tmpZkPCcopy)
$ => CTX :MLOAD(currentCTX), RETURN
VAR GLOBAL tmpVarBgetLen
VAR GLOBAL tmpVarCgetLen
VAR GLOBAL tmpVarDgetLen
VAR GLOBAL tmpZkPCgetLen
; @info byte length of B
; @in B => number
; @out A => bytes length
getLenBytes:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
; store current registries
RR :MSTORE(tmpZkPCgetLen)
B :MSTORE(tmpVarBgetLen)
C :MSTORE(tmpVarCgetLen)
D :MSTORE(tmpVarDgetLen)
; set C as counter to 0
0 => C
B => A
getLenBytesLoop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
0 => B
; if A is zero, finish counter
$ :EQ,JMPC(getLenEnd)
; right shift one byte
1 => D
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; increase counter
C + 1 => C :JMP(getLenBytesLoop)
getLenEnd:
; counter to A
C => A
; recover registries
$ => B :MLOAD(tmpVarBgetLen)
$ => C :MLOAD(tmpVarCgetLen)
$ => RR :MLOAD(tmpZkPCgetLen)
$ => D :MLOAD(tmpVarDgetLen), RETURN
; @info bits length of B
; @in B => number
; @out A => bits length
getLenBits:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
; store current registries
RR :MSTORE(tmpZkPCgetLen)
B :MSTORE(tmpVarBgetLen)
C :MSTORE(tmpVarCgetLen)
D :MSTORE(tmpVarDgetLen)
; set C as counter to 0
0 => C
B => A
getLenBitsLoop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
0 => B
; if B is zero, finish counter
$ :EQ,JMPC(getLenBitsEnd)
; divide value by 2
A :MSTORE(arithA)
2 :MSTORE(arithB)
zkPC+1 => RR :JMP(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1)
; increase counter
C + 1 => C :JMP(getLenBitsLoop)
getLenBitsEnd:
; counter to A
C => A
; recover registries
$ => B :MLOAD(tmpVarBgetLen)
$ => C :MLOAD(tmpVarCgetLen)
$ => RR :MLOAD(tmpZkPCgetLen)
$ => D :MLOAD(tmpVarDgetLen), RETURN
VAR GLOBAL tmpVarAmstore
VAR GLOBAL tmpVarBmstore
VAR GLOBAL tmpVarCmstore
VAR GLOBAL tmpVarDmstore
VAR GLOBAL tmpVarEmstore
VAR GLOBAL tmpZkPCmstore
VAR GLOBAL bytesToStore
VAR GLOBAL isMSTOREX
; @info save value to memory < 32 bytes with offset
; @in bytesToStore => bytes to store in memory
; @in E => offset
; @in C => length
; @out E => new offset
MSTOREX:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
C :JMPZ(endMSTOREX)
32 - C :JMPN(errorMLOADMSTORE)
32 - C - 1 :JMPN(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset]
1 :MSTORE(isMSTOREX)
; @info save value to memory 32 bytes with offset
; @in bytesToStore => bytes to store in memory
; @in E => offset
; @out E => new offset
MSTORE32:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
; store current registries
RR :MSTORE(tmpZkPCmstore)
A :MSTORE(tmpVarAmstore)
B :MSTORE(tmpVarBmstore)
C :MSTORE(tmpVarCmstore)
D :MSTORE(tmpVarDmstore)
; check offset is lower than max memory
E => A
%MAX_MEM_EXPANSION_BYTES => B
$ :LT,JMPC(initMSTORE, errorMLOADMSTORE)
initMSTORE:
zkPC+1 => RR :JMP(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
; is storing <32 bytes, jump to store last bytes or finish if 0 bytes left
$ => B :MLOAD(isMSTOREX), JMPZ(finalMSTORE)
; if C has value, offset != 0
C :JMPNZ(MSTOREX2)
$ => C :MLOAD(tmpVarCmstore)
; load bytes to store
$ => A :MLOAD(bytesToStore)
32 - C => D
; shift bytes to store
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
A => B
; load from memory
$ => A :MLOAD(MEM:E)
32 - D => D
; shift loaded bytes
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
A + B :MSTORE(bytesToStore)
A + B => A
0 => C :JMP(finalMSTORE)
; used if bytesToStore.length < 32 && offset != 0
MSTOREX2:
$ => D :MLOAD(tmpVarCmstore)
C + D => D
32 - D :JMPN(MSTOREX3)
; if bytesToStore.length < 32 && memory to load is allocated in two different slots
; load memory from slot E
$ => A :MLOAD(MEM:E)
; shift loaded memory from slot E
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
$ => D :MLOAD(tmpVarCmstore)
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
A => B
; load memory from slot E+1
$ => A :MLOAD(MEM:E+1)
32 - C => D
; right shift loaded memory from slot E+1
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; join both results
A + B => B
$ => A :MLOAD(bytesToStore)
$ => D :MLOAD(tmpVarCmstore)
32 - D => D
; shift bytes to store
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
; append loaded bytes from concurrent slots
A + B => A :MSTORE(bytesToStore)
:JMP(finalMSTORE)
; used if bytesToStore.length < 32 && memory to load is allocated in one slot
MSTOREX3:
D - 32 => D
; load memory from slot E+1
$ => A :MLOAD(MEM:E+1)
; shift bytes to store
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
$ => D :MLOAD(tmpVarCmstore)
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; join both results
A => B
$ => A :MLOAD(bytesToStore)
$ => D :MLOAD(tmpVarCmstore)
32 - D => D
; shift bytes to store
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
; append loaded bytes from concurrent slots
A + B => A :MSTORE(bytesToStore)
finalMSTORE:
C :JMPNZ(memAlignOptionMSTORE)
$ => A :MLOAD(bytesToStore)
A :MSTORE(MEM:E)
E*32 => E
$ :MLOAD(isMSTOREX),JMPZ(offsetMSTORE32)
$ => C :MLOAD(tmpVarCmstore)
E + C => E
0 :MSTORE(isMSTOREX),JMP(endMSTORE)
memAlignOptionMSTORE:
E :MSTORE(tmpVarEmstore)
$ => A :MLOAD(MEM:E)
$ => B :MLOAD(MEM:E+1)
${memAlignWR_W0(A,mem.bytesToStore,C)} => D ; no trust calculate W0
${memAlignWR_W1(B,mem.bytesToStore,C)} => E ; no trust calculate W1
$ :MEM_ALIGN_WR,MLOAD(bytesToStore)
E => A
$ => E :MLOAD(tmpVarEmstore)
D :MSTORE(MEM:E) ; write W0
A :MSTORE(MEM:E+1) ; write W1
E*32 + C => E
$ => A :MLOAD(isMSTOREX), JMPZ(offsetMSTORE32)
$ => C :MLOAD(tmpVarCmstore)
E + C => E
0 :MSTORE(isMSTOREX), JMP(endMSTORE)
offsetMSTORE32:
E + 32 => E
endMSTORE:
$ => A :MLOAD(tmpVarAmstore)
$ => B :MLOAD(tmpVarBmstore)
$ => C :MLOAD(tmpVarCmstore)
$ => RR :MLOAD(tmpZkPCmstore)
$ => D :MLOAD(tmpVarDmstore), RETURN
endMSTOREX:
:RETURN
VAR GLOBAL tmpVarAmload
VAR GLOBAL tmpVarBmload
VAR GLOBAL tmpVarCmload
VAR GLOBAL tmpVarDmload
VAR GLOBAL tmpVarEmload
VAR GLOBAL tmpZkPCmload
VAR GLOBAL isMLOADX
; @info get value from memory (< 32 bytes)
; @in E => offset
; @in C => length
; @out A => value
; @out E => new offset
MLOADX:
; check zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
32 - C :JMPN(errorMLOADMSTORE)
32 - C - 1 :JMPN(MLOAD32)
1 :MSTORE(isMLOADX)
; @info get value from memory (32 bytes)
; @in E => offset
; @out A => value
; @out E => new offset
MLOAD32:
; check zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
; store current registries
RR :MSTORE(tmpZkPCmload)
B :MSTORE(tmpVarBmload)
C :MSTORE(tmpVarCmload)
D :MSTORE(tmpVarDmload)
; check offset is lower than max memory
E => A
%MAX_MEM_EXPANSION_BYTES => B
$ :LT,JMPC(initMLOAD, errorMLOADMSTORE)
initMLOAD:
:CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
; if C has value, bytes splitted in two memory slots
C :JMPNZ(memAlignOptionMLOAD)
; load memory from one slot
$ => A :MLOAD(MEM:E)
$ => B :MLOAD(isMLOADX)
E*32 => E
B :JMPZ(offsetMLOAD32, sliceA)
memAlignOptionMLOAD:
$ => A :MLOAD(MEM:E)
$ => B :MLOAD(MEM:E+1)
; get memory value
$ => A :MEM_ALIGN_RD
E*32 + C => E
$ => B :MLOAD(isMLOADX)
B :JMPZ(offsetMLOAD32)
sliceA:
; if is mloadx, slice the result by the length
$ => C :MLOAD(tmpVarCmload)
32 - C => D
zkPC+1 => RR :JMP(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
zkPC+1 => RR :JMP(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
0 :MSTORE(isMLOADX)
E + C => E :JMP(endMLOAD)
offsetMLOAD32:
E + 32 => E
endMLOAD:
; restore stored values
$ => B :MLOAD(tmpVarBmload)
$ => C :MLOAD(tmpVarCmload)
$ => RR :MLOAD(tmpZkPCmload)
$ => D :MLOAD(tmpVarDmload), RETURN
errorMLOADMSTORE:
:JMP(outOfGas)
VAR GLOBAL tmpVarAemptyAcc
VAR GLOBAL tmpVarBemptyAcc
VAR GLOBAL tmpVarCemptyAcc
VAR GLOBAL tmpVarDemptyAcc
; @info check account is empty ( balance == nonce == code == 0x )
; @in E => address
; @out E => isEmpty => 1 = true, 0 = false
isEmptyAccount:
; check zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*3 :JMPN(outOfCountersPoseidon)
; store current registries
A :MSTORE(tmpVarAemptyAcc)
B :MSTORE(tmpVarBemptyAcc)
C :MSTORE(tmpVarCemptyAcc)
D :MSTORE(tmpVarDemptyAcc)
E => A
; read balance
; set key for smt balance query.
%SMT_KEY_BALANCE => B
0 => C
$ => B :SLOAD
; balance in A
0 => A
; if balance is not zero, is not empty
$ :LT,JMPC(isNotEmptyAccount)
; check nonce
E => A
; set key for smt nonce query
%SMT_KEY_NONCE => B
; nonce in B
$ => B :SLOAD
0 => A
; if nonce is not zero, is not empty
$ :LT,JMPC(isNotEmptyAccount)
; read bytecode
E => A
; set key for smt smart contract query
%SMT_KEY_SC_CODE => B
; sc in B
$ => B :SLOAD
0 => A
; if code is not zero, is not empty
$ :LT,JMPC(isNotEmptyAccount)
1 => E :JMP(ISEMPTYEnd)
isNotEmptyAccount:
0 => E :JMP(ISEMPTYEnd)
ISEMPTYEnd:
; recover registries
$ => A :MLOAD(tmpVarAemptyAcc)
$ => B :MLOAD(tmpVarBemptyAcc)
$ => C :MLOAD(tmpVarCemptyAcc)
$ => D :MLOAD(tmpVarDemptyAcc), RETURN
VAR GLOBAL tmpVarBcompGas
VAR GLOBAL tmpVarCcompGas
VAR GLOBAL tmpVarDcompGas
VAR GLOBAL tmpVarEcompGas
; @info Compute gas to send to call following EIP 150
; @in gasCall: gas sent to call
; @out A => min( requested_gas , all_but_one_64th(63/64) )
computeGasSendCall:
; check zk-counters
%MAX_CNT_STEPS - STEP - 30 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
; save tmp vars
B :MSTORE(tmpVarBcompGas)
C :MSTORE(tmpVarCcompGas)
D :MSTORE(tmpVarDcompGas)
E :MSTORE(tmpVarEcompGas)
; compute all_but_one_64th gas
GAS => A
; C = [c7, c6, ..., c0]
; JMPN instruction assures c0 is within the range [0, 2**32 - 1]
${GAS >> 6} => C :JMPN(failAssert)
${GAS & 0x3f} => D
; since D is assured to be less than 0x40
; it is enforced that [c7, c6, ..., c1] are 0 since there is no value multiplied by 64
; that equals the field
; Since e0 is assured to be less than 32 bits, c0 * 64 + d0 could not overflow the field
C * 64 + D :ASSERT
D => A
0x40 => B
$ :LT,JMPNC(failAssert)
GAS - C => A
$ => B :MLOAD(gasCall)
; gas_sent_with_call = min(requested_gas, all_but_one_64th)
$ :LT,JMPC(computeGasSendCallEnd)
; gas to substract
B => A
computeGasSendCallEnd:
; restore stored values
$ => B :MLOAD(tmpVarBcompGas)
$ => C :MLOAD(tmpVarCcompGas)
$ => D :MLOAD(tmpVarDcompGas)
$ => E :MLOAD(tmpVarEcompGas), RETURN
VAR GLOBAL tmpVarAsaveMem
VAR GLOBAL tmpVarBsaveMem
VAR GLOBAL tmpVarCsaveMem
VAR GLOBAL tmpVarDsaveMem
VAR GLOBAL tmpVarEsaveMem
VAR GLOBAL tmpZkPCsaveMem
; @info compute memory expansion gas cost
; @in: lastMemOffset: offset to copy bytes
; @in: lastMemLength: size of the bytes to copy
saveMem:
; check zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary)
RR :MSTORE(tmpZkPCsaveMem)
A :MSTORE(tmpVarAsaveMem)
B :MSTORE(tmpVarBsaveMem)
C :MSTORE(tmpVarCsaveMem)
D :MSTORE(tmpVarDsaveMem)
E :MSTORE(tmpVarEsaveMem)
$ => A :MLOAD(lastMemLength)
; If no len, no memory expansion
0 => B
$ :EQ, JMPC(saveMemEnd)
$ => B :MLOAD(lastMemOffset)
; If the binary has a carry, means the mem expansion is very big. We can jump to oog directly
; offset + length in B
$ => B :ADD, JMPC(outOfGas)
; check new memory length is lower than 2**22 - 31 - 1 (max supported memory expansion for %TX_GAS_LIMIT of gas)
%MAX_MEM_EXPANSION_BYTES => A
$ :LT,JMPC(outOfGas)
; load old memory length, if is greater than new memory length, no expansion cost
$ => A :MLOAD(memLength)
$ :LT, JMPC(saveMemGAS, saveMemEnd)
saveMemGAS:
; store new memory length
B :MSTORE(memLength)
B => E
; memory_size_word = (memory_byte_size + 31) / 32 in E
; ${(B+31)/32} => E
E + 31 => A
; E = [e7, e6, ..., e0]
; JMPN instruction assures e0 is within the range [0, 2**32 - 1]
${A >> 5} => E :JMPN(failAssert)
${A & 0x1f} => D
; since D is assured to be less than 0x20
; it is enforced that [e7, e6, ..., e1] are 0 since there is no value multiplied by 32
; that equals the field
; Since e0 is assured to be less than 32 bits, e0 * 32 + d0 could not overflow the field
E * 32 + D :ASSERT
D => A
0x20 => B
$ :LT,JMPNC(failAssert)
; memory_cost = (memory_size_word ** 2) / 512 + (3 * memory_size_word) in A
; ${E*E/512} + 3*E=> A
E :MSTORE(arithA)
E :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB, mulFlagOverflow: value overflow, mulArithOverflowFlag: set to 1 if operation causes overflow]
$ => C :MLOAD(arithRes1)
$ :MLOAD(mulArithOverflowFlag), JMPNZ(outOfGas)
C :MSTORE(arithA)
512 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1)
A + 3*E => A
$ => B :MLOAD(lastMemoryExpansionCost)
A :MSTORE(lastMemoryExpansionCost)
; memory_expansion_cost = new_memory_cost - last_memory_cost
A - B => A
; update new gas
GAS - A => GAS :JMPN(outOfGas)
saveMemEnd:
; restore stored values
$ => A :MLOAD(tmpVarAsaveMem)
$ => B :MLOAD(tmpVarBsaveMem)
$ => C :MLOAD(tmpVarCsaveMem)
$ => D :MLOAD(tmpVarDsaveMem)
$ => RR :MLOAD(tmpZkPCsaveMem)
$ => E :MLOAD(tmpVarEsaveMem), RETURN
VAR GLOBAL tmpVarAArith
VAR GLOBAL tmpVarBArith
VAR GLOBAL tmpVarCArith
VAR GLOBAL tmpVarDArith
VAR GLOBAL tmpVarEArith
VAR GLOBAL tmpZkPCArith
VAR GLOBAL arithA
VAR GLOBAL arithB
VAR GLOBAL arithRes1
VAR GLOBAL arithRes2
VAR GLOBAL addArithOverflow
; @info binary addition
; @in: arithA: addend value
; @in: arithB: addend value
; @out: arithRes1: arithA + arithB
addARITH:
RR :MSTORE(tmpZkPCArith), CALL(storeTmp)
$ => A :MLOAD(arithA)
$ => B :MLOAD(arithB)
$ => E :ADD, MSTORE(arithRes1), JMPC(setAddArithOverflow)
0 :MSTORE(addArithOverflow), JMP(finishAddArith)
setAddArithOverflow:
1 :MSTORE(addArithOverflow)
finishAddArith:
$ => RR :MLOAD(tmpZkPCArith), JMP(loadTmp)
; @info binary substraction
; @in: arithA: minuend value
; @in: arithB: subtrahend value
; @out: arithRes1: arithA - arithB
subARITH:
RR :MSTORE(tmpZkPCArith),CALL(storeTmp)
$ => A :MLOAD(arithA)
$ => B :MLOAD(arithB)
$ => A :SUB,MSTORE(arithRes1)
$ => RR :MLOAD(tmpZkPCArith),JMP(loadTmp)
VAR GLOBAL mulArithOverflowValue
VAR GLOBAL mulArithOverflowFlag
; @info arithmetic multiplication
; @in: arithA: multiplier value
; @in: arithB: multiplicand value
; @out: arithRes1: product of multiplication arithA * arithB
; @out: mulArithOverflowValue: overflow value
; @out: mulArithOverflowFlag: set to 1 if operation causes overflow
mulARITH:
; check zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
RR :MSTORE(tmpZkPCArith), CALL(storeTmp)
$ => A :MLOAD(arithA)
$ => B :MLOAD(arithB)
0 => C
$${var _mulArith = A * B}
${_mulArith >> 256} => D
${_mulArith} => E :ARITH
E :MSTORE(arithRes1)
D :MSTORE(mulArithOverflowValue)
D => A
0 => B
$ :EQ,JMPNC(setMulArithOverflow)
0 :MSTORE(mulArithOverflowFlag), JMP(finishMulArith)
setMulArithOverflow:
1 :MSTORE(mulArithOverflowFlag)
finishMulArith:
$ => RR :MLOAD(tmpZkPCArith),JMP(loadTmp)
; @info arithmetic division
; @in: arithA: dividend value
; @in: arithB: divisor value
; @out: arithRes1: quotient of division arithA / arithB
; @out: arithRes2: remainder of division arithA % arithB
divARITH:
; check zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
RR :MSTORE(tmpZkPCArith), CALL(storeTmp)
$ => E :MLOAD(arithA)
$ => A :MLOAD(arithB)
; Check denominator(A) is not zero
0 => B
$ :EQ, JMPC(zeroDiv)
; Check if divisor (E) is smaller than denominator E < A
A => C ; store temporally A in C
E => A ; divisor
C => B ; denominator
$ :LT, JMPC(divisorSmallerDiv)
C => A
${E%A} => C ; remainder
${E/A} => B
0 => D
E :ARITH
B :MSTORE(arithRes1)
C :MSTORE(arithRes2)
; check divisor > remainder
A => B ; divisor
C => A ; remainder
$ => A :LT
1 :ASSERT,CALL(loadTmp)
$ => RR :MLOAD(tmpZkPCArith)
:RETURN
zeroDiv:
0 :MSTORE(arithRes1)
0 :MSTORE(arithRes2), CALL(loadTmp)
$ => RR :MLOAD(tmpZkPCArith)
:RETURN
divisorSmallerDiv:
0 :MSTORE(arithRes1)
E :MSTORE(arithRes2), CALL(loadTmp)
$ => RR :MLOAD(tmpZkPCArith)
:RETURN
loadTmp:
$ => A :MLOAD(tmpVarAArith)
$ => B :MLOAD(tmpVarBArith)
$ => C :MLOAD(tmpVarCArith)
$ => D :MLOAD(tmpVarDArith)
$ => E :MLOAD(tmpVarEArith), RETURN
storeTmp:
A :MSTORE(tmpVarAArith)
B :MSTORE(tmpVarBArith)
C :MSTORE(tmpVarCArith)
D :MSTORE(tmpVarDArith)
E :MSTORE(tmpVarEArith), RETURN
VAR GLOBAL tmpSHXZkPC
VAR GLOBAL tmpSHXZkPC2
VAR GLOBAL tmpVarBSHX
VAR GLOBAL tmpVarCSHX
VAR GLOBAL tmpVarDSHX
VAR GLOBAL tmpVarESHX
VAR GLOBAL result
;@info Shift right D bytes to A
;@in A - (A >> D)
;@in D - (A >> D) D bytes
;@out A - A >> D => A
SHRarith:
; check zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
RR :MSTORE(tmpSHXZkPC2)
B :MSTORE(tmpVarBSHX)
C :MSTORE(tmpVarCSHX)
D :MSTORE(tmpVarDSHX)
E :MSTORE(tmpVarESHX)
; E init number
A => E
; A bytes
D => A
; B = 8 (1 byte = 8 bits)
8 => B
0 => C
0 => D
; A * B = op --> D = op (D bits)
${A*B} => D :ARITH
; A init number
E => A :JMP(SHRarithinit)
;@in A - (A >> D)
;@in D - (A >> D) D bits
;@out A - A >> D => A
SHRarithBit:
; check zk-counters
%MAX_CNT_STEPS - STEP - 30 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
RR :MSTORE(tmpSHXZkPC2)
B :MSTORE(tmpVarBSHX)
C :MSTORE(tmpVarCSHX)
D :MSTORE(tmpVarDSHX)
E :MSTORE(tmpVarESHX)
SHRarithinit:
0 => B
; if A == 0 --> no shift
$ :EQ,JMPC(SHRarithfinal)
; E init number
A => E
; B bits
D => B
255 => A
; A < B, 255 < bits
$ :LT,JMPC(SHRarith0)
D => RR
E => A :MSTORE(arithA)
:CALL(@exp_num + RR); out:[B: 2**RR]
B :MSTORE(arithB),CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1),JMP(SHRarithfinal)
SHRarith0:
0 => A
SHRarithfinal:
$ => B :MLOAD(tmpVarBSHX)
$ => C :MLOAD(tmpVarCSHX)
$ => D :MLOAD(tmpVarDSHX)
$ => RR :MLOAD(tmpSHXZkPC2)
$ => E :MLOAD(tmpVarESHX), RETURN
;@info Shift left D bytes to A
;@in A - (A << D)
;@in D - (A << D) D bytes
;@out A - A << D => A
SHLarith:
; check zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 2 :JMPN(outOfCountersArith)
RR :MSTORE(tmpSHXZkPC2)
B :MSTORE(tmpVarBSHX)
C :MSTORE(tmpVarCSHX)
D :MSTORE(tmpVarDSHX)
E :MSTORE(tmpVarESHX)
; E init number
A => E
; A bytes
D => A
8 => B
0 => C, D
; D = A * 8, D bits
${A*B} => D :ARITH
; A init number
E => A
:JMP(SHLarithinit)
SHLarithBit:
; check zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
RR :MSTORE(tmpSHXZkPC2)
B :MSTORE(tmpVarBSHX)
C :MSTORE(tmpVarCSHX)
D :MSTORE(tmpVarDSHX)
E :MSTORE(tmpVarESHX)
SHLarithinit:
; E init number
A => E
0 => A
; D --> B bits
D => B
; if D == 0 --> no shift
$ :EQ,JMPC(SHLarithfinal)
255 => A
; A < B, 255 < bits
$ :LT,JMPC(SHLarith0)
D => RR
; A init number and calculate B = 2**D
E => A :CALL(@exp_num + RR); out:[B: 2**RR]
; E = init number * 2**D (result)
${A*B} => E
E :MSTORE(result)
; D = 256 - D
256 - D => D, RR
B => C :CALL(@exp_num + RR); out:[B: 2**RR]
${A/B} => D
C => B
0 => C
E :ARITH, JMP(SHLarithfinal)
SHLarith0:
0 => E
:JMP(SHLarithfinal)
SHLarithfinal:
E => A
$ => B :MLOAD(tmpVarBSHX)
$ => C :MLOAD(tmpVarCSHX)
$ => D :MLOAD(tmpVarDSHX)
$ => RR :MLOAD(tmpSHXZkPC2)
$ => E :MLOAD(tmpVarESHX), RETURN
; out of counters full tracer event trigger
outOfCountersStep:
$${eventLog(onError, OOCS)} :JMP(handleBatchError)
outOfCountersKeccak:
$${eventLog(onError, OOCK)} :JMP(handleBatchError)
outOfCountersBinary:
$${eventLog(onError, OOCB)} :JMP(handleBatchError)
outOfCountersMemalign:
$${eventLog(onError, OOCM)} :JMP(handleBatchError)
outOfCountersArith:
$${eventLog(onError, OOCA)} :JMP(handleBatchError)
outOfCountersPadding:
$${eventLog(onError, OOCPA)} :JMP(handleBatchError)
outOfCountersPoseidon:
$${eventLog(onError, OOCPO)} :JMP(handleBatchError)
outOfGas:
$${eventLog(onError, OOG)} :JMP(handleError)
invalidJump:
$${eventLog(onError, invalidJump)} :JMP(handleError)
invalidOpcode:
$${eventLog(onError, invalidOpcode)} :JMP(handleError)
stackUnderflow:
$${eventLog(onError, underflow)} :JMP(handleError)
stackOverflow:
$${eventLog(onError, overflow)} :JMP(handleError)
deployAddressCollision:
$${eventLog(onError, invalidAddressCollision)} :JMP(handleError)
invalidStaticTx:
$${eventLog(onError, invalidStaticTx)} :JMP(handleError)
invalidCodeSize:
$${eventLog(onError, invalidCodeSize)} :JMP(handleError)
invalidCodeStartsEF:
$${eventLog(onError, invalidCodeStartsEF)} :JMP(handleError)
handleError:
%MAX_CNT_STEPS - STEP - 500 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
;revert all state changes
$ => SR :MLOAD(initSR), CALL(revertTouched)
;remaining gas = 0
$ => A :MLOAD(originCTX)
0 => B
$ :EQ,JMPC(firstContextInvalid)
A => CTX
; Add return data context value to origin context
; Clear return data context
0 :MSTORE(retDataCTX)
CTX :MSTORE(currentCTX)
$ => GAS :MLOAD(gasCTX)
$ => SP :MLOAD(lastSP)
$ => PC :MLOAD(lastPC)
0 :MSTORE(SP++)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth), JMP(readCode)
handleBatchError:
; restore init state root and finish batch
$ => SR :MLOAD(batchSR)
$${eventLog(onFinishTx)}
$${eventLog(onFinishBatch)} :JMP(processTxsEnd)
firstContextInvalid:
;save Root and jump to send gas to sequencer
0 :MSTORE(gasRefund)
0 => GAS :JMP(sendGasSeq)
VAR GLOBAL tmpVarAoffsetUtil
VAR GLOBAL tmpVarBoffsetUtil
;Get offset/32 & offset%32
;@in A offset (offset is assumed to be less than %MAX_MEM_EXPANSION_BYTES)
;@out E offset/32
;@out C offset%32
offsetUtil:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
A :MSTORE(tmpVarAoffsetUtil)
B :MSTORE(tmpVarBoffsetUtil)
; E = [e7, e6, ..., e0]
; JMPN instruction assures e0 is within the range [0, 2**32 - 1]
${A >> 5} => E :JMPN(failAssert)
${A & 0x1F} => C
; since C is assured to be less than 0x20
; it is enforced that [e7, e6, ..., e1] are 0 since there is no value multiplied by 32
; that equals the field
; Since e0 is assured to be less than 32 bits, e0 * 32 + c0 could not overflow the field
E * 32 + C :ASSERT
C => A
0x20 => B
$ :LT,JMPNC(failAssert)
$ => A :MLOAD(tmpVarAoffsetUtil)
$ => B :MLOAD(tmpVarBoffsetUtil), RETURN
;@info: move balances between two accounts
;@in: txSrcAddr: source address
;@in: storageAddr: destination address
;@in: txValue: transaction value
moveBalances:
;;;;;;;;
; evmCALL (Move Balances)
;;;;;;;;
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*4 :JMPN(outOfCountersPoseidon)
;Check if is a delegate call
$ => A :MLOAD(isDelegateCall), JMPNZ(endMoveBalances)
; Decrement source balance
$ => A :MLOAD(txSrcAddr)
; set key for smt balance query
%SMT_KEY_BALANCE => B
0 => C
; Balance in A
$ => A :SLOAD
; value in B
$ => B :MLOAD(txValue)
; Check has enough balance to pay the value. In case not, means we are in a CALL/CALLCODE
$ :LT,JMPC(invalidCall)
; sourceBalance - value in D
$ => D :SUB
; update source balance
$ => A :MLOAD(txSrcAddr)
; set key for smt balance query
%SMT_KEY_BALANCE => B
$ => SR :SSTORE
; Increment destination balance
$ => A :MLOAD(storageAddr)