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 authored and laisolizq committed Mar 2, 2023
1 parent 3d42356 commit 77f381e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion counters/countersConstants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ CONST %OPSIGNEXTEND_CNT_PADDING_PG = 0
CONST %OPSIGNEXTEND_CNT_POSEIDON_G = 0
; opBLOCKHASH
CONST %OPBLOCKHASH_STEP = 100
CONST %OPBLOCKHASH_CNT_BINARY = 0
CONST %OPBLOCKHASH_CNT_BINARY = 1
CONST %OPBLOCKHASH_CNT_ARITH = 0
CONST %OPBLOCKHASH_CNT_KECCAK_F = 1
CONST %OPBLOCKHASH_CNT_MEM_ALIGN = 0
Expand Down
2 changes: 1 addition & 1 deletion main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,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
2 changes: 1 addition & 1 deletion main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
Expand Down
25 changes: 14 additions & 11 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
*/
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)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
Expand All @@ -22,11 +23,10 @@ opBLOCKHASH:
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)
$ => A :MLOAD(txCount)
$ => B :MLOAD(SP) ; [blockNumber => A]
; Check batch number is less than current block number, else return 0
$ :LT ,JMPC(opBLOCKHASHzero)
; Create key for the batch hash mapping key
; set bytes length to D
32 => D
Expand Down Expand Up @@ -105,8 +105,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
10 changes: 5 additions & 5 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
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 @@ -263,7 +263,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 +371,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 @@ -687,7 +687,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 +699,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
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
42 changes: 22 additions & 20 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ copySP:
RR :MSTORE(tmpZkPCcopy)
; store current ctx
CTX :MSTORE(currentCTX)
; set SP at the begginning of calldata memory allocation
; set SP at the beginning of calldata memory allocation
%CALLDATA_OFFSET => SP
; retrieve previous context
$ => CTX :MLOAD(originCTX)
Expand Down Expand Up @@ -200,7 +200,7 @@ initMSTORE:
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 memoty
; load from memory
$ => A :MLOAD(MEM:E)
32 - D => D
; shift loaded bytes
Expand Down Expand Up @@ -317,7 +317,7 @@ MLOADX:
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)

32 - C :JMPN(errorMLOADMSTORE) ; TDDO Should be unreachable! check it
32 - C :JMPN(errorMLOADMSTORE)
32 - C - 1 :JMPN(MLOAD32)
1 :MSTORE(isMLOADX)

Expand Down Expand Up @@ -353,7 +353,7 @@ initMLOAD:
memAlignOptionMLOAD:
$ => A :MLOAD(MEM:E)
$ => B :MLOAD(MEM:E+1)
; get memeory value
; get memory value
$ => A :MEM_ALIGN_RD
E*32 + C => E
$ => B :MLOAD(isMLOADX)
Expand Down Expand Up @@ -635,7 +635,7 @@ mulARITH:
; @info arithmetic division
; @in: arithA: dividend value
; @in: arithB: divisor value
; @out: arithRes1: quotien of division arithA / arithB
; @out: arithRes1: quotient of division arithA / arithB
; @out: arithRes2: remainder of division arithA % arithB
divARITH:
; check zk-counters
Expand All @@ -657,17 +657,17 @@ divARITH:
$ :LT, JMPC(divisorSmallerDiv)
C => A

${E%A} => C ; reminder
${E%A} => C ; remainder
${E/A} => B
0 => D
E :ARITH

B :MSTORE(arithRes1)
C :MSTORE(arithRes2)

; check divisor > reminder
; check divisor > remainder
A => B ; divisor
C => A ; reminder
C => A ; remainder
$ => A :LT
1 :ASSERT,CALL(loadTmp)
$ => RR :MLOAD(tmpZkPCArith)
Expand Down Expand Up @@ -1043,7 +1043,7 @@ readPush:
D => A
0x04 => B
$ :LT,JMPNC(failAssert)
0 => B :JMP(readPushBlock)
0 => B

readPushBlock:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
Expand All @@ -1066,10 +1066,10 @@ readPushBlock:

$ => A :HASHP1(E)
HASHPOS - 2 => HASHPOS
A*16777216 + C => C :JMP(doRotate)
A*16777216 + C => C

doRotate:
B - 1 => A :JMP(doRotateLoop)
B - 1 => A

doRotateLoop:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
Expand Down Expand Up @@ -1156,7 +1156,7 @@ utilsAddBatchHashBytebyByteLoop:
1 => D :CALL(addBatchHashData); in:[D: length of the hash]
; check loop
B => D
; D + 1 => D we set 33 in stead of 32 to earn 1 step
; D + 1 => D, we set 33 instead of 32 to earn 1 step
33 - D => D
$ => B :MLOAD(tmpVarDaddB)
D - B - 1 :JMPN(utilsAddBatchHashBytebyByteLoop)
Expand Down Expand Up @@ -1187,7 +1187,7 @@ checkBytecodeStartsEF:
$ => E :MLOAD(memOffsetLinearPoseidon)

; get 1 byte from memory
1 => C :CALL(MLOADX) ; in: [E: offset, C: lenght] out: [A: value , E: new offset]
1 => C :CALL(MLOADX) ; in: [E: offset, C: length] out: [A: value , E: new offset]
31 => D :CALL(SHRarith) ; in: [A: value, D: #bytes to right shift] out: [A: shifted result]

; check if byte read is equal to 0xEF
Expand Down Expand Up @@ -1285,7 +1285,7 @@ maskAddress:
$ => A :AND
$ => B :MLOAD(tmpVarBmask), RETURN

;@info: updates the address sytem storage with current batch and state root
;@info: updates the address system storage with current batch and state root
updateSystemData:
; check keccak counters
$ => A :MLOAD(cntKeccakPreProcess)
Expand All @@ -1294,12 +1294,14 @@ updateSystemData:
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon)
; Get last tx count
$ => A :MLOAD(txCount)
;Update last tx Count at system storage
1 => B
$ => D :ADD, MSTORE(txCount)

%LAST_TX_STORAGE_POS => C
%ADDRESS_SYSTEM => A
%SMT_KEY_SC_STORAGE => B
$ => D :MLOAD(txCount)
;Update last tx Count at system storage
D + 1 => D :MSTORE(txCount)
$ => SR :SSTORE

;Update state root mapping
Expand Down Expand Up @@ -1342,8 +1344,8 @@ utilMULMOD:
; A * B + 0 = D*2^256 + E
; K * N + mulModResult = D*2^256 + E

; Since the k can be bigger than 2²⁵⁶ and therefore does not fit in a register we divedit in the
; most significan and less significant part:
; Since the k can be bigger than 2²⁵⁶ and therefore does not fit in a register, we split it in the
; most significant and less significant part:

; (k.l + k.h * 2²⁵⁶) * N + mulModResult = (D1 + D2) * 2²⁵⁶ + E
; And divide this operation in 2 which fits in 2²⁵⁶ digits
Expand Down Expand Up @@ -1511,7 +1513,7 @@ expADend:
$ => RR :MLOAD(tmpZkPCexp)
$ => E :MLOAD(tmpVarEexp), RETURN

;@info function to force a failed assertt
;@info function to force a failed assert
failAssert:
1 => A
2 :ASSERT
14 changes: 7 additions & 7 deletions main/vars.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
VAR GLOBAL oldStateRoot ; Previous state-tree root
VAR GLOBAL oldAccInputHash ; Previous accumulated input hash
VAR GLOBAL globalExitRoot ; Global exit-tree root
VAR GLOBAL oldNumBatch ; Previous batch processsed
VAR GLOBAL oldNumBatch ; Previous batch processed
VAR GLOBAL sequencerAddr ; Coinbase address which will receive the fees
VAR GLOBAL batchHashData ; batchHashData = H_keccak( transactions )
VAR GLOBAL timestamp ; Current batch timestamp
Expand All @@ -12,14 +12,14 @@ VAR GLOBAL forkID ; Fork identifier
; Output variables
VAR GLOBAL newAccInputHash ; Final accumulated input hash. newAccInputHash = H_keccak( oldAccInputHash | batchHashData | globalExitRoot | timestamp | sequencerAddr )
VAR GLOBAL newLocalExitRoot ; Updated local exit tree root
VAR GLOBAL newNumBatch ; Current batch processsed
VAR GLOBAL newNumBatch ; Current batch processed

VAR GLOBAL batchL2DataParsed ; Number of bytes read when decoding RLP transactions. Computed during RLP loop
VAR GLOBAL pendingTxs ; Number of transactions decoded in RLP block
VAR GLOBAL lastCtxUsed ; Last context that has been used
VAR GLOBAL ctxTxToUse ; First context to be used when processing transactions
VAR GLOBAL lastHashKIdUsed ; Last hash address used
VAR GLOBAL nextHashPId ; Next posidon hash address available
VAR GLOBAL nextHashPId ; Next poseidon hash address available

VAR GLOBAL batchL2DataLength ; Transactions bytes read from the input
VAR GLOBAL batchHashDataId ; hash address used when adding bytes to batchHashData
Expand All @@ -31,7 +31,7 @@ VAR GLOBAL gasCalldata ; gas spent by the calldata
VAR GLOBAL gasCall ; total gas forwarded when creating a new context
VAR GLOBAL addrCall ; address parameter when creating a new context
VAR GLOBAL valueCall ; value parameter when creating a new context
VAR GLOBAL argsLengthCall ; size af the calldata creating a new context
VAR GLOBAL argsLengthCall ; size of the calldata creating a new context
VAR GLOBAL txSrcOriginAddr ; origin address of a tx
VAR GLOBAL txGasPrice ; transaction parameter: 'gasPrice' global var
VAR GLOBAL depth ; Current depth execution
Expand All @@ -44,10 +44,10 @@ VAR GLOBAL txCount ; Current transaction count

VAR GLOBAL touchedSR ; touched tree root
VAR GLOBAL numTopics ; number of topics depending on LOG opcode call
VAR GLOBAL SPw ; aux variable to store Stack poimnter 'SP'
VAR GLOBAL auxSR ; auxiliaty variable. Temporary state root
VAR GLOBAL SPw ; aux variable to store Stack pointer 'SP'
VAR GLOBAL auxSR ; auxiliary variable. Temporary state root
VAR GLOBAL txRLPLength ; transaction RLP list length
VAR GLOBAL txDataRead ; aux varible to check transaction 'data' left that needs to be read
VAR GLOBAL txDataRead ; aux variable to check transaction 'data' left that needs to be read

VAR CTX txGasLimit ; transaction parameter: 'gas limit'
VAR CTX txDestAddr ; transaction parameter: 'to'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xpolygonhermez/zkrom",
"version": "0.7.0.0",
"version": "0.8.0.0",
"description": "zkROM source code",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 77f381e

Please sign in to comment.