Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix save mem calls #95

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 50 additions & 84 deletions main/opcodes.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ opBALANCE:
$ => D :SLOAD
D :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:JMP(readCode)

Expand Down Expand Up @@ -766,7 +766,7 @@ opGASPRICE:
opEXTCODESIZE:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
%SMT_KEY_SC_LENGTH => B
0 => C
$ => A :SLOAD
Expand All @@ -787,7 +787,7 @@ opEXTCODECOPY:
SP - 4 :JMPN(stackUnderflow)
SP - 1 => SP
$ => A :MLOAD(SP--) ;addr
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:CALL(opEXTCODECOPYCheckHash)
$ => C :MLOAD(SP--) ;destOffset, memory offset
Expand Down Expand Up @@ -960,7 +960,7 @@ opRETURNDATACOPYfinal:
opEXTCODEHASH:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
%SMT_KEY_SC_CODE => B
0 => C
Expand All @@ -974,7 +974,7 @@ opEXTCODEHASH:
;opEXTCODEHASHL2:
; SP - 1 => SP :JMPN(stackUnderflow)
; $ => A :MLOAD(SP)
; ${touchedAddress(A,CTX)} => D
; ${touchedAddress(A)} => D
; GAS-100-D*2500 => GAS :JMPN(outOfGas)
; %SMT_KEY_SC_CODE => B
; 0 => C
Expand Down Expand Up @@ -1156,7 +1156,7 @@ opSLOAD:
$ => E :SLOAD ;current value
E :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
${touchedStorageSlots(A,C,CTX)} => A
${touchedStorageSlots(A,C)} => A
GAS - %SLOAD_GAS - A*2000 => GAS :JMPN(outOfGas)
:JMP(readCode)

Expand All @@ -1183,7 +1183,7 @@ opSSTOREinit:
$ => SR :MLOAD(originSR)
$ => B :SLOAD ;origin value
$ => SR :MLOAD(auxSR)
${touchedStorageSlots(A,C,CTX)} => A
${touchedStorageSlots(A,C)} => A
B => C ;origin value
GAS - A*2100 => GAS :JMPN(outOfGas)
E => A
Expand Down Expand Up @@ -2175,37 +2175,27 @@ opCALL2:
; Store bytes offset int the memory, the calldata of the subcontext
$ => A :MLOAD(SP--)
A :MSTORE(argsOffsetCall)
A :MSTORE(lastMemOffset)

; Byte size to copy, size of the calldata of the subcontext
$ => C :MLOAD(SP--)
C :MSTORE(argsLengthCall)
C :MSTORE(lastMemLength)
; Mem expansion gas cost call args
:CALL(saveMem)

; Store byte offset in the memory in bytes, where to store the return data of the sub context.
$ => B :MLOAD(SP--)
B :MSTORE(retOffset)
B :MSTORE(lastMemOffset)

; Store byte size to copy (size of the return data).
$ => D :MLOAD(SP)
D :MSTORE(retLength)
D :MSTORE(lastMemLength)
; Access with zero length will not extend the memory
; Add operation with Arith
D :MSTORE(arithA)
C :MSTORE(arithB)
:CALL(addARITH)
$ => A :MLOAD(arithRes1)
0 => B
$ :EQ, JMPC(opCALL4)

$ => A :MLOAD(argsOffsetCall)
${A + C} => A ; last memory pointer call
$ => B :MLOAD(retOffset)
${B + D} => B ; last memory pointer return
$ => C :LT, JMPC(opCALL3)
B => A

opCALL3:
; Mem expansion gas cost return args
:CALL(saveMem)

opCALL4:
$ => B :MLOAD(txDestAddr)
$ => D :MLOAD(txSrcAddr)
$ => C :MLOAD(storageAddr)
Expand All @@ -2219,14 +2209,12 @@ opCALL4:
A :MSTORE(originCTX)
C :MSTORE(txSrcAddr)
SR :MSTORE(initSR)
${copyTouchedAddress(A,CTX)} ; // TODO: Check security copy same addresses
; // TODO: copyTouchedAddress once the depth CTX is returned
$ => A :MLOAD(addrCall)
A :MSTORE(txDestAddr)
A :MSTORE(storageAddr)

; If addr in touched addres D = 0, else D = 1
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D

;gas_cost = memory_expansion_cost + code_execution_cost + address_access_cost + positive_value_cost + value_to_empty_account_cost
; Calculate address_access_cost: 100 for warm account, 2600 for cold account
Expand All @@ -2235,8 +2223,11 @@ opCALL4:
$ => B :MLOAD(valueCall)
B :MSTORE(txValue)
0 => A
$ => C :LT
GAS - 9000*C => GAS :JMPN(outOfGas)
0 => C
$ :EQ,JMPC(opCALLend)
1 => C

GAS - 9000 => GAS :JMPN(outOfGas)
; If destAddress is an empty account, we should add 25k of gas to base cost
$ => E :MLOAD(txDestAddr)
:CALL(isEmptyAccount)
Expand All @@ -2245,9 +2236,9 @@ opCALL4:
opCALLend:
:CALL(computeGasSendCall)
; Add estipend (2300) in case of tx with value
A + 2300*C => A
$ => CTX :MLOAD(originCTX)
GAS - A + 2300*C :MSTORE(gasCTX)
GAS - A :MSTORE(gasCTX)
A + 2300*C => A
$ => C :MLOAD(txGasPrice)
$ => CTX :MLOAD(currentCTX)
C :MSTORE(txGasPrice)
Expand Down Expand Up @@ -2282,9 +2273,14 @@ opCALLCODE2:
; Store bytes offset int the memory, the calldata of the subcontext
$ => A :MLOAD(SP--)
A :MSTORE(argsOffsetCall)
A :MSTORE(lastMemOffset)
; Byte size to copy, size of the calldata of the subcontext
$ => C :MLOAD(SP--)
C :MSTORE(argsLengthCall)
C :MSTORE(lastMemLength)
; Mem expansion gas cost call args
:CALL(saveMem)

; Store byte offset in the memory in bytes, where to store the return data of the sub context.
$ => B :MLOAD(SP--)
B :MSTORE(retOffset)
Expand All @@ -2293,21 +2289,7 @@ opCALLCODE2:
$ => D :MLOAD(SP)
D :MSTORE(retLength)
D :MSTORE(lastMemLength)
; Access with zero length will not extend the memory
; Add operation with Arith
D :MSTORE(arithA)
C :MSTORE(arithB)
:CALL(addARITH)
$ => A :MLOAD(arithRes1)
0 => B
$ :EQ, JMPC(opCALLCODE4)

$ => A :MLOAD(argsOffsetCall)
${A + C} => A ; last memory pointer call
$ => B :MLOAD(retOffset)
${B + D} => B ; last memory pointer return
$ => C :LT, JMPC(opCALLCODE3)
B => A
:CALL(saveMem)

opCALLCODE3:
:CALL(saveMem)
Expand All @@ -2327,13 +2309,11 @@ opCALLCODE4:
E :MSTORE(txSrcAddr)
E :MSTORE(storageAddr)
SR :MSTORE(initSR)
${copyTouchedAddress(A,CTX)} ;// TODO: Check security copy same addresses
; // TODO: copyTouchedAddress once the depth CTX is returned
$ => A :MLOAD(addrCall)
A :MSTORE(txDestAddr)

; If addr in touched addres D = 0, else D = 1
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D

;gas_cost = memory_expansion_cost + code_execution_cost + address_access_cost + positive_value_cost
; Calculate address_access_cost: 100 for warm account, 2600 for cold account
Expand Down Expand Up @@ -2472,7 +2452,6 @@ opRETURNcreateEnd:
$ => SP :MLOAD(lastSP)
$ => PC :MLOAD(lastPC)
A :MSTORE(SP++)
${touchedAddress(A,CTX)}
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)

Expand All @@ -2490,10 +2469,15 @@ opDELEGATECALL:
; Store bytes offset int the memory, the calldata of the subcontext
$ => A :MLOAD(SP--)
A :MSTORE(argsOffsetCall)
A :MSTORE(lastMemOffset)

; Byte size to copy, size of the calldata of the subcontext
$ => C :MLOAD(SP--)
C :MSTORE(argsLengthCall)
${A + B} => A
C :MSTORE(lastMemLength)
; Mem expansion gas cost call args
:CALL(saveMem)

; Store byte offset in the memory in bytes, where to store the return data of the sub context.
$ => B :MLOAD(SP--)
B :MSTORE(retOffset)
Expand All @@ -2502,26 +2486,9 @@ opDELEGATECALL:
$ => D :MLOAD(SP)
D :MSTORE(retLength)
D :MSTORE(lastMemLength)
; Access with zero length will not extend the memory
; Add operation with Arith
D :MSTORE(arithA)
C :MSTORE(arithB)
:CALL(addARITH)
$ => A :MLOAD(arithRes1)
0 => B
$ :EQ, JMPC(opDELEGATECALL3)

$ => A :MLOAD(argsOffsetCall)
${A + C} => A ; last memory pointer call
$ => B :MLOAD(retOffset)
${B + D} => B ; last memory pointer return
$ => C :LT, JMPC(opDELEGATECALL2)
B => A

opDELEGATECALL2:
; Mem expansion gas cost return args
:CALL(saveMem)

opDELEGATECALL3:
$ => B :MLOAD(txValue)
$ => C :MLOAD(txSrcAddr)
$ => E :MLOAD(storageAddr)
Expand All @@ -2537,11 +2504,10 @@ opDELEGATECALL3:
E :MSTORE(storageAddr)
1 :MSTORE(isDelegateCall)
SR :MSTORE(initSR)
${copyTouchedAddress(A,CTX)}
B :MSTORE(txValue)
$ => A :MLOAD(addrCall)
A :MSTORE(txDestAddr)
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
;gas_cost = base_gas + gas_sent_with_call
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:CALL(computeGasSendCall)
Expand Down Expand Up @@ -2572,6 +2538,7 @@ opCREATE2:
$ => C :MLOAD(SP--) ;length
C :MSTORE(argsLengthCall)
$ => D :MLOAD(SP) ;salt

; Mem expansion gas cost
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength)
Expand Down Expand Up @@ -2634,24 +2601,24 @@ opSTATICCALL:
A :MSTORE(addrCall)
$ => A :MLOAD(SP--)
A :MSTORE(argsOffsetCall)
A :MSTORE(lastMemOffset)

$ => B :MLOAD(SP--)
B :MSTORE(argsLengthCall)
${A + B} => A
B :MSTORE(argsLengthCall)
B :MSTORE(lastMemLength)
; Mem expansion gas cost call args
:CALL(saveMem)

$ => C :MLOAD(SP--)
C :MSTORE(retOffset)
C :MSTORE(lastMemOffset)

$ => B :MLOAD(SP)
B :MSTORE(retLength)
B :MSTORE(lastMemLength)
${C + B} => B
$ => C :LT
C - 1 :JMPN(opSTATICCALL2)
B => A

opSTATICCALL2:
; Mem expansion gas cost return args
:CALL(saveMem)

opSTATICCALL3:
$ => B :MLOAD(txDestAddr)
SR :MSTORE(initSR)
SP :MSTORE(lastSP)
Expand All @@ -2665,11 +2632,10 @@ opSTATICCALL3:
0 :MSTORE(txValue)
1 :MSTORE(isStaticCall)
B :MSTORE(txSrcAddr)
${copyTouchedAddress(A,CTX)}
$ => A :MLOAD(addrCall)
A :MSTORE(txDestAddr)
A :MSTORE(storageAddr)
${touchedAddress(A,CTX)} => D
${touchedAddress(A)} => D
;gas_cost = base_gas + gas_sent_with_call
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:CALL(computeGasSendCall)
Expand Down Expand Up @@ -2751,7 +2717,7 @@ opSELFDESTRUCT:
GAS - 25000 * E => GAS :JMPN(outOfGas)

; gas: check touched address
${touchedAddress(A,CTX)} => E
${touchedAddress(A)} => E
GAS - 2600 * E => GAS :JMPN(outOfGas)

; read previous balance receiver
Expand Down
8 changes: 5 additions & 3 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ endCheckChainId:
; Touched Addresses
;;;;;;;;
$ => A :MLOAD(txSrcOriginAddr)
${touchedAddress(A,CTX)}
${resetTouchedAddress()}
${resetStorageSlots()}
${touchedAddress(A)}

;;;;;;;;
; Check and update Nonce --> //TODO: same as chainID
Expand Down Expand Up @@ -179,7 +181,7 @@ txType:
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(getContractAddress)
$ => A :MLOAD(txDestAddr)
${touchedAddress(A,CTX)}
${touchedAddress(A)}
; // TODO: check "0x000..000" when coming from a CALL
10 => B
$ :LT,JMPC(selectorPrecompiled) ; precompiled smart contracts
Expand Down Expand Up @@ -298,7 +300,7 @@ create2end:
A :MSTORE(storageAddr)

deploy:
${touchedAddress(A,CTX)}
${touchedAddress(A)}
; set contract nonce to 1
0 => C
1 => D
Expand Down
Loading