-
Notifications
You must be signed in to change notification settings - Fork 48
/
storage-memory.zkasm
318 lines (286 loc) · 10.1 KB
/
storage-memory.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
/**
* @link [https://www.evm.codes/#51?fork=berlin]
* @zk-counters
* - 32 arith
* - 32 binary
* - 1 mem align
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: [offset]
* - stack output: [value]
*/
opMLOAD:
; checks zk-counters
%MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
$ => E :MLOAD(SP); [offset => E]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset), CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
; store lastMemLength for memory expansion gas cost
; compute memory expansion gas cost
32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; store stack output
A :MSTORE(SP++), JMP(readCode); [value(A) => SP]
/**
* @link [https://www.evm.codes/#52?fork=berlin]
* @zk-counters
* - 32 arith
* - 32 binary
* - 1 mem align
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: [offset, value]
* - stack output: none
*/
opMSTORE:
; checks zk-counters
%MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 2 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
$ => E :MLOAD(SP+1); [offset => E]
$ => B :MLOAD(SP); [value => B]
; store bytesToStore for MSTORE32 execution
B :MSTORE(bytesToStore)
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [A: value]
; store lastMemOffset for memory expansion gas cost
32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
:JMP(readCode)
/**
* @link [https://www.evm.codes/#53?fork=berlin]
* @zk-counters
* - 32 binary
* - 1 mem align
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: [offset, value]
* - stack output: none
*/
opMSTORE8:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 2 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
$ => B :MLOAD(SP+1); [offset => B]
; store lastMemOffset for memory expansion gas cost
B :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost. In case of MSTORE8, always 1 byte
1 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
B => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
$ => B :MLOAD(SP); [value => B]
; read from memory position E
$ => A :MLOAD(MEM:E)
${memAlignWR8_W0(A,B,C)} => D ; no trust calculate W0
B :MEM_ALIGN_WR8 ; only use LSB of B, rest of bytes could be non zero.
; write at memory position E
D :MSTORE(MEM:E), JMP(readCode)
/**
* @link [https://www.evm.codes/#59?fork=berlin]
* @zk-counters
* - 1 arith
* - 3 binary
* - 200 steps
* @process-opcode
* - stack input: none
* - stack output: [size]
*/
opMSIZE:
; checks zk-counters
%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check out-of-gas
GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; load current memory length
$ => E :MLOAD(memLength)
; MSIZE should be multiple of a word (32 bytes)
; Div operation with Arith
E :MSTORE(arithA)
32 :MSTORE(arithB)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => C :MLOAD(arithRes1)
$ => B :MLOAD(arithRes2)
; check arithRes2 is 0, no need to round in this case
0 => A
$ :EQ, JMPC(MSIZEend)
; Round size to 32bytes multiple
C + 1 => C
C * 32 => E
MSIZEend:
E :MSTORE(SP++); [size(E) => SP]
; check stack overflow
%CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode)
/**
* @link [https://www.evm.codes/#54?fork=berlin]
* @zk-counters
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: [key]
* - stack output: [value]
*/
opSLOAD:
; checks zk-counters
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
$ => C :MLOAD(SP); [key => C]
; get current storage address
$ => A :MLOAD(storageAddr)
; set key for smt storage query
%SMT_KEY_SC_STORAGE => B
$ => E :SLOAD
; set key(C) as warmed storage for address(A)
E :MSTORE(SP++), CALL(isColdSlot); [value(E) => SP]
; check out-of-gas
GAS - %SLOAD_GAS - A * %COLD_SLOAD_COST_RED => GAS :JMPN(outOfGas, readCode)
/**
* @link [https://www.evm.codes/#55?fork=berlin]
* @zk-counters
* - 10 binary
* - 1 mem align
* - 765 poseidon
* - 120 steps
* @process-opcode
* - stack input: [key, value]
* - stack output: none
*/
opSSTORE:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 765 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
; check stack underflow
SP - 2 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %SSTORE_ENTRY_EIP_2200_GAS - 1 :JMPN(outOfGas)
; check is static call
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
$ => C :MLOAD(SP+1) ; [key => C]
C :MSTORE(tmpVarC)
$ => D :MLOAD(SP) ; [value => D]
; check if is a create call
$ => A :MLOAD(isCreateContract), JMPNZ(deploymentSSTORE)
; load current storage address
$ => A :MLOAD(storageAddr), JMP(opSSTOREinit)
deploymentSSTORE:
; in case of create, the storage address is the create contract address
$ => A :MLOAD(createContractAddress)
opSSTOREinit:
; set key for smt storage query
%SMT_KEY_SC_STORAGE => B
$ => E :SLOAD
; change context to check sotrage original value
SR :MSTORE(auxSR)
$ => SR :MLOAD(originSR)
$ => B :SLOAD ; origin value
; set key(C) as warmed storage for address(A)
$ => SR :MLOAD(auxSR), CALL(isColdSlot)
B => C ; origin value
; check out-of-gas
GAS - A*%COLD_SLOAD_COST => GAS :JMPN(outOfGas)
E => A
D => B
$ => A :EQ, JMPNC(opSSTOREdif)
; if new_val == current_val
; check out-of-gas
GAS - %SSTORE_DYNAMIC_GAS => GAS:JMPN(outOfGas, opSSTOREend)
opSSTOREdif:
; if new_val != current_val
C => A
E => B
$ => A :EQ, JMPNC(opSSTOREdifA)
; if current_val == orig_val
0 => A
C => B
$ => A :EQ, JMPNC(opSSTOREdifB)
; if origin_val == 0
; check out-of-gas
GAS - %SSTORE_SET_GAS => GAS :JMPN(outOfGas, opSSTOREend)
opSSTOREdifA:
; if current_val != orig_val
GAS - %SSTORE_DYNAMIC_GAS => GAS:JMPN(outOfGas)
0 => A
C => B
$ => A :EQ, JMPNC(opSSTOREdifA1)
; if origin_value == 0
opSSTOREdifAB:
; if origin_value == 0
D => A
C => B
$ => A :EQ, JMPNC(opSSTOREend)
; if new_val == orig_val
0 => A
$ => A :EQ, JMPNC(opSSTOREdifA2)
; if orig_val == 0
$ => A :MLOAD(gasRefund)
A + 19900 => A :MSTORE(gasRefund), JMP(opSSTOREend)
opSSTOREdifA1:
; if orig_val != 0
0 => A
E => B
$ => A :EQ, JMPNC(opSSTOREdifA12)
; if current_val == 0
; compute gas refund
$ => A :MLOAD(gasRefund)
A - %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund), JMP(opSSTOREdifAB)
opSSTOREdifA12:
;if current_val != 0
0 => A
D => B
$ => A :EQ, JMPNC(opSSTOREdifAB)
; if new_val == 0
; compute gas refund
$ => A :MLOAD(gasRefund)
A + %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund), JMP(opSSTOREdifAB)
opSSTOREdifA2:
; if orig_val != 0
; compute gas refund
$ => A :MLOAD(gasRefund)
A + 2800 => A :MSTORE(gasRefund), JMP(opSSTOREend)
opSSTOREdifB:
; if orig_val != 0
; check out-of-gas
GAS - %SSTORE_RESET_GAS => GAS :JMPN(outOfGas)
0 => A
D => B
$ => A :EQ, JMPNC(opSSTOREend)
; if new_val == 0
; compute gas refund
$ => A :MLOAD(gasRefund)
A + %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund)
:JMP(opSSTOREend)
opSSTOREend:
$ => A :MLOAD(isCreateContract), JMPNZ(mloadContract)
$ => A :MLOAD(storageAddr), JMP(opSSTOREsr)
mloadContract:
; if is a create, use create contract address storage
$ => A :MLOAD(createContractAddress)
opSSTOREsr:
; set key for smt storage query
%SMT_KEY_SC_STORAGE => B
$ => C :MLOAD(tmpVarC); key => C
$ => SR :SSTORE, JMP(readCode)