Skip to content

Commit

Permalink
fix jmpn, reduntant jump and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
krlosMata committed Mar 2, 2023
1 parent 3d42356 commit 5e4b9bc
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 66 deletions.
2 changes: 1 addition & 1 deletion counters/countersConstants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0
CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 8
; opCREATE - COMPLEX - hardcoded values at test
CONST %OPCREATE_STEP = 100
CONST %OPCREATE_CNT_BINARY = 21
CONST %OPCREATE_CNT_BINARY = 22
CONST %OPCREATE_CNT_ARITH = 4
CONST %OPCREATE_CNT_KECCAK_F = 0
CONST %OPCREATE_CNT_MEM_ALIGN = 2
Expand Down
11 changes: 9 additions & 2 deletions main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ loadTx_rlp:
:CALL(addBatchHashData)
A - 0xc0 :JMPN(invalidTxRLP)
A - 0xf8 :JMPN(shortList)
; do not allow lists over 2**24 bytes
; Transaction could not have more than 300.000 due to smart contract limitation (keccaks counters)
; meaning that the RLP encoding is wrong
A - 0xfb :JMPN(longList, invalidTxRLP)

longList:
A - 0xf7 => D :CALL(addHashTx)
Expand Down Expand Up @@ -167,7 +171,10 @@ dataREAD:
A - 0x80 :JMPN(veryShortData)
A - 0x81 :JMPN(endData)
A - 0xb8 :JMPN(shortData)
A - 0xc0 :JMPN(longData, invalidTxRLP)
; do not allow string over 2**24 bytes
; Transaction could not have more than 300.000 due to smart contract limitation (keccaks counters)
; meaning that the RLP encoding is wrong
A - 0xbb :JMPN(longData, invalidTxRLP)

veryShortData:
1 :MSTORE(txCalldataLen)
Expand Down Expand Up @@ -238,7 +245,7 @@ endChainId:
setPreEIP155Flag:
1 :MSTORE(isPreEIP155)
;; size verification
; checks RLP lenght read at the RLP header with bytes read during RLP parsing
; checks RLP length read at the RLP header with bytes read during RLP parsing
sizeVerification:
C => A
$ => B :MLOAD(txRLPLength)
Expand Down
5 changes: 2 additions & 3 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ INCLUDE "vars.zkasm"

start: ; main zkROM entry point
;;;;;;;;;;;;;;;;;;
;; A - Load input variabales
;; A - Load input variables
;;;;;;;;;;;;;;;;;;
STEP => A
0 :ASSERT ; Ensure it is the beginning of the execution

; it is assumed that forkID is less than 32 bits
; hence, 'JMPZ' instruction is safe
CTX :MSTORE(forkID)
CTX - %FORK_ID :JMPNZ(failAssert)

Expand Down Expand Up @@ -65,6 +63,7 @@ setGlobalExitRoot:

; read timestamp given the globalExitRoot
; skip overwrite timestamp if it is different than 0
; Since timestamp is enforced by the smart contract it is safe to compare only 32 bits in 'op0' with JMPNZ
$ => D :SLOAD, JMPNZ(skipSetGlobalExitRoot)

$ => D :MLOAD(timestamp)
Expand Down
30 changes: 13 additions & 17 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,28 @@
*/
opBLOCKHASH:
; checks zk-counters
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)

; check out-of-gas
GAS - %GAS_EXT_STEP => GAS :JMPN(outOfGas)

; Get last tx count
$ => B :MLOAD(txCount)
B + 1 => B
$ => A :MLOAD(SP) ; [blockNumber => A]
; Check batch block is lt current block number, else return 0
B - A - 1 :JMPN(opBLOCKHASHzero)
$ => B :MLOAD(SP) ; [blockNumber => B]
; If block number does not exist in the smart conract system, it will return 0

; Create key for the batch hash mapping key
; set bytes length to D
32 => D
; A new hash with position 0 is started
0 => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
E+1 => E :MSTORE(lastHashKIdUsed)
A :HASHK(E)
B :HASHK(E)
%STATE_ROOT_STORAGE_POS :HASHK(E)
HASHPOS :HASHKLEN(E)
; blockhash key = hash(blockNumber, STATE_ROOT_STORAGE_POS)
Expand All @@ -47,10 +44,6 @@ opBLOCKHASH:
; store result value in the stack
E :MSTORE(SP++), JMP(readCode); [hash(E) => SP]

; Handle when is asking for the hash of a not computed batch, return 0
opBLOCKHASHzero:
0 :MSTORE(SP++), JMP(readCode); [0 => SP]

/**
* @link [https://www.evm.codes/#41?fork=berlin]
* @zk-counters
Expand Down Expand Up @@ -105,8 +98,11 @@ opNUMBER:
; check out-of-gas
GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; Get current tx count
$ => D :MLOAD(txCount)
D + 1 :MSTORE(SP++); [blockNumber => SP]
$ => A :MLOAD(txCount)
1 => B

; call binary:add state machine and push to the stack
$ :ADD, MSTORE(SP++); [blockNumber => SP]
; check stack overflow
%CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode)

Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ opCALLDATALOAD2:
$ => A :MLOAD(SP++)
; reading beyond the calldata length will result in reading 0
; calldata memory reservation in zkEVM (2**16 * 32 bytes) is larger than maximum bytes allowed as calldata fixed in the smart contract (300.000 bytes)
; therefore it is assured that remaining memory space will be always 0 and never writen or over exceeded
; therefore it is assured that remaining memory space will be always 0 and never writen or over exceed
$ => B :MLOAD(SP)
$ => A :MEM_ALIGN_RD
; recover SP
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ opSAR:
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)

; read two items form the stack
; read two items from the stack
SP - 1 => SP
$ => C :MLOAD(SP--) ; [shift (bits) => C]
$ => A :MLOAD(SP) ; [value => A]
Expand Down
21 changes: 12 additions & 9 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ opSTOPend:
opCREATE:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*3 :JMPN(outOfCountersPoseidon)

; check out-of-gas
Expand Down Expand Up @@ -96,7 +96,7 @@ opCREATE:
D => B
$ :LT,JMPC(opCreateFail)

; check nonce overlow
; check nonce overflow
$ => A :MLOAD(storageAddr)
; set key for smt nonce query
%SMT_KEY_NONCE => B
Expand Down Expand Up @@ -125,7 +125,8 @@ opCREATE:
:CALL(getLenBytes); in: [B: value] out: [A: value bytes length]
A :MSTORE(lengthNonce)
; increase nonce
B + 1 => D
1 => A
$ => D :ADD
C => A
; set key for smt nonce query
%SMT_KEY_NONCE => B
Expand Down Expand Up @@ -263,7 +264,7 @@ opCALLend:
$ => C :LT, CALL(computeGasSendCall); in: [gasCall: gas sent to call] out: [A: min( requested_gas , all_but_one_64th(63/64))]
$ => CTX :MLOAD(originCTX)
GAS - A :MSTORE(gasCTX)
; add estipend (2300) in case of tx with value
; add stipend (2300) in case of tx with value
A + %CALL_STIPEND*C => A
; move is static bool value to following CTX
$ => D :MLOAD(isStaticCall)
Expand Down Expand Up @@ -371,7 +372,7 @@ opCALLCODEend:
$ => C :LT, CALL(computeGasSendCall); in: [gasCall: gas sent to call] out: [A: min( requested_gas , all_but_one_64th(63/64))]
$ => CTX :MLOAD(originCTX)
GAS - A :MSTORE(gasCTX)
; Add estipend (2300) in case of tx with value
; Add stipend (2300) in case of tx with value
A + %CALL_STIPEND*C => A
; move is static bool value to following CTX
$ => D :MLOAD(isStaticCall)
Expand Down Expand Up @@ -508,7 +509,7 @@ opRETURNcreate:
$ => D :MLOAD(txNonce)

; set bytecode length and read bytecode first byte
$ => A :MLOAD(createContractAddress), CALL(checkBytecodeStartsEF)
$ => A :MLOAD(createContractAddress), CALL(checkBytecodeStartsEF) ; in: [memOffset], out: [startsWithEFn

; check bytecode first byte != 0xEF
$ :MLOAD(startsWithEF), JMPNZ(invalidCodeStartsEF)
Expand Down Expand Up @@ -687,7 +688,7 @@ opCREATE2:
A - B => GAS
GAS :MSTORE(gasCall)

; check correctness of Create: depth, can transfer, nonce overlfow,
; check correctness of Create: depth, can transfer, nonce overflow,
; can transfer
$ => A :MLOAD(storageAddr)
; set key for smt balance query
Expand All @@ -699,7 +700,7 @@ opCREATE2:
$ => B :MLOAD(valueCall)
$ :LT,JMPC(opCreateFail)

; check nonce overlow
; check nonce overflow
$ => A :MLOAD(storageAddr)
; set key for smt nonce query
%SMT_KEY_NONCE => B
Expand Down Expand Up @@ -729,7 +730,9 @@ opCREATE2:
B :MSTORE(txNonce), CALL(getLenBytes); in: [B: value] out: [A: value bytes length]
A :MSTORE(lengthNonce)
; increase nonce
B + 1 => D
1 => A
$ => D :ADD

C => A
; set key for smt nonce query
%SMT_KEY_NONCE => B
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ opSSTOREinit:
; set key for smt storage query
%SMT_KEY_SC_STORAGE => B
$ => E :SLOAD
; change context to check sotrage original value
; change context to check storage original value
SR :MSTORE(auxSR)
$ => SR :MLOAD(originSR)
$ => B :SLOAD ; origin value
Expand Down
12 changes: 10 additions & 2 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ endCheckChainId:
$ => A :SLOAD
$ => B :MLOAD(txNonce)
$ => C :EQ, JMPNC(invalidIntrinsicTxNonce) ; Compare nonce state tree with nonce transaction
A + 1 => D
; increase nonce by 1
1 => B
$ => D :ADD ; compute [nonce + 1 => D]
E => A
%SMT_KEY_NONCE => B
0 => C
Expand Down Expand Up @@ -152,6 +154,7 @@ endCalldataIntrinsicGas:
$ => B :MLOAD(txValue)
D :MSTORE(arithA)
B :MSTORE(arithB), CALL(addARITH)
$ :MLOAD(addArithOverflow), JMPNZ(invalidIntrinsicTxBalance)
$ => B :MLOAD(arithRes1)
; Comparison
C => A
Expand Down Expand Up @@ -214,6 +217,11 @@ getContractAddress:
$ :LT,JMPC(nonce1byte)
$ => C :MLOAD(lengthNonce)
; 1 byte length address + 20 bytes address + 1 byte length nonce + C bytes nonce
; RLPlist[RLP(address):RLP(nonce)] --> 1 byte length address + 20 bytes address + 1 byte length nonce + C bytes nonce
; --> 0xc0 + address RLP bytes(1 + 20) + nonce RLP bytes (variable)
; RLP(address) --> 20 bytes header + address value --> 0x94 + address value
; RLP(nonce) --> nonce < 128 --> 1 byte --> value itself
; --> nonce > 128 --> (1 byte + length nonce) + value itself
0xc0 + 22 + C :HASHK1(E)
0x94 :HASHK1(E)
20 => D
Expand Down Expand Up @@ -435,7 +443,7 @@ endDeploy:

; save offset memory and length to compute hash bytecode. Read bytecode first byte
E :MSTORE(memOffsetLinearPoseidon)
C :MSTORE(memSizeLinearPoseidon), CALL(checkBytecodeStartsEF)
C :MSTORE(memSizeLinearPoseidon), CALL(checkBytecodeStartsEF) ; in: [memOffset], out: [startsWithEFn]

; check bytecode first byte != 0xEF
$ :MLOAD(startsWithEF), JMPNZ(invalidCodeStartsEF)
Expand Down
Loading

0 comments on commit 5e4b9bc

Please sign in to comment.