From 85f4a7fe7bb37913c62eb8e7115cf7b3442aa659 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Mon, 19 Dec 2022 19:07:44 +0100 Subject: [PATCH 01/60] update tmpvars & expAD counters --- main/opcodes/calldata-returndata-code.zkasm | 5 +- main/opcodes/storage-memory.zkasm | 5 +- main/precompiled/selector.zkasm | 1 - main/utils.zkasm | 396 +++++++++++--------- 4 files changed, 225 insertions(+), 182 deletions(-) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index ff550d86..aac27615 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -470,6 +470,7 @@ opEXTCODECOPY2: C :MSTORE(memOffset) E :MSTORE(remainingBytes), JMP(opCODECOPYinit) +VAR GLOBAL tmpZkPCext opEXTCODECOPYCheckHash: ; set key for smt smart contract length query %SMT_KEY_SC_LENGTH => B @@ -483,10 +484,10 @@ opEXTCODECOPYCheckHash: $ :EQ, JMPC(opEXTCODECOPYCheckHashEnd) ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - RR :MSTORE(tmpZkPC) + RR :MSTORE(tmpZkPCext) B + 1 :MSTORE(arithA) 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => RR :MLOAD(tmpZkPC) + $ => RR :MLOAD(tmpZkPCext) $ => B :MLOAD(arithRes1) %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A $ :LT, JMPC(outOfCountersPoseidon) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 091d3cb0..a68d5369 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -175,6 +175,7 @@ opSLOAD: ; check out-of-gas GAS - %SLOAD_GAS - A * %COLD_SLOAD_COST_RED => GAS :JMPN(outOfGas, readCode) +VAR GLOBAL tmpVarCsstore /** * @link [https://www.evm.codes/#55?fork=berlin] * @zk-counters @@ -201,7 +202,7 @@ opSSTORE: $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx) $ => C :MLOAD(SP+1) ; [key => C] - C :MSTORE(tmpVarC) + C :MSTORE(tmpVarCsstore) $ => D :MLOAD(SP) ; [value => D] ; check if is a create call $ => A :MLOAD(isCreateContract), JMPNZ(deploymentSSTORE) @@ -314,5 +315,5 @@ mloadContract: opSSTOREsr: ; set key for smt storage query %SMT_KEY_SC_STORAGE => B - $ => C :MLOAD(tmpVarC); key => C + $ => C :MLOAD(tmpVarCsstore); key => C $ => SR :SSTORE, JMP(readCode) diff --git a/main/precompiled/selector.zkasm b/main/precompiled/selector.zkasm index cd2f5984..21e60ffb 100644 --- a/main/precompiled/selector.zkasm +++ b/main/precompiled/selector.zkasm @@ -1,6 +1,5 @@ INCLUDE "pre-ecrecover.zkasm" INCLUDE "identity.zkasm" -INCLUDE "modexp.zkasm" INCLUDE "end.zkasm" /** diff --git a/main/utils.zkasm b/main/utils.zkasm index f74d7bbe..01e4e1ef 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -1,24 +1,3 @@ -VAR GLOBAL tmpVarA -VAR GLOBAL tmpVarB -VAR GLOBAL tmpVarC -VAR GLOBAL tmpVarD -VAR GLOBAL tmpVarE -VAR GLOBAL tmpVarA2 -VAR GLOBAL tmpVarB2 -VAR GLOBAL tmpVarC2 -VAR GLOBAL tmpVarD2 -VAR GLOBAL tmpVarE2 -VAR GLOBAL tmpVarA3 -VAR GLOBAL tmpVarB3 -VAR GLOBAL tmpVarC3 -VAR GLOBAL tmpVarD3 -VAR GLOBAL tmpVarE3 -VAR GLOBAL tmpZkPC -VAR GLOBAL tmpZkPC2 -VAR GLOBAL tmpZkPC3 -VAR GLOBAL tmpZkPC4 -VAR GLOBAL result - ; @info Get absolute value and sign ; @in A => number to convert ; @out A => Absolut value of A @@ -36,11 +15,12 @@ absIsNeg: $ => 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: - RR :MSTORE(tmpZkPC2) + RR :MSTORE(tmpZkPCcopy) ; store current ctx CTX :MSTORE(currentCTX) ; set SP at the begginning of calldata memory allocation @@ -78,18 +58,22 @@ copyFinal: copyEnd: ; restore RR to return to initial call correctly - $ => RR :MLOAD(tmpZkPC2) + $ => 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: ; store current registries - RR :MSTORE(tmpZkPC) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) + RR :MSTORE(tmpZkPCgetLen) + B :MSTORE(tmpVarBgetLen) + C :MSTORE(tmpVarCgetLen) + D :MSTORE(tmpVarDgetLen) ; set C as counter to 0 0 => C B => A @@ -113,20 +97,20 @@ getLenEnd: ; counter to A C => A ; recover registries - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => RR :MLOAD(tmpZkPC) - $ => D :MLOAD(tmpVarD), RETURN + $ => 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: ; store current registries - RR :MSTORE(tmpZkPC) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) + RR :MSTORE(tmpZkPCgetLen) + B :MSTORE(tmpVarBgetLen) + C :MSTORE(tmpVarCgetLen) + D :MSTORE(tmpVarDgetLen) ; set C as counter to 0 0 => C B => A @@ -151,10 +135,17 @@ getLenBitsEnd: ; counter to A C => A ; recover registries - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => RR :MLOAD(tmpZkPC) - $ => D :MLOAD(tmpVarD), RETURN + $ => 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 @@ -174,12 +165,12 @@ MSTOREX: ; @out E => new offset MSTORE32: ; store current registries - RR :MSTORE(tmpZkPC) - A :MSTORE(tmpVarA) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) - E :MSTORE(tmpVarE) + RR :MSTORE(tmpZkPCmstore) + A :MSTORE(tmpVarAmstore) + B :MSTORE(tmpVarBmstore) + C :MSTORE(tmpVarCmstore) + D :MSTORE(tmpVarDmstore) + E :MSTORE(tmpVarEmstore) ; check offset is lower than max memory E => A %MEMORY_LENGTH => B @@ -191,7 +182,7 @@ initMSTORE: $ => B :MLOAD(isMSTOREX), JMPZ(finalMSTORE) ; if C has value, bytes splitted in two memory slots C :JMPNZ(MSTOREX2) - $ => C :MLOAD(tmpVarC) + $ => C :MLOAD(tmpVarCmstore) ; load bytes to store $ => A :MLOAD(bytesToStore) 32 - C => D @@ -212,13 +203,13 @@ initMSTORE: ; used if memory to load is allocated in two different slots MSTOREX2: - $ => D :MLOAD(tmpVarC) + $ => D :MLOAD(tmpVarCmstore) C + D => D ; 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(tmpVarC) + $ => 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 @@ -229,7 +220,7 @@ MSTOREX2: ; join both results A + B => B $ => A :MLOAD(bytesToStore) - $ => D :MLOAD(tmpVarC) + $ => 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] @@ -245,7 +236,7 @@ finalMSTORE: E*32 => E :JMP(offsetMSTORE32) memAlignOptionMSTORE: - E :MSTORE(tmpVarE) + E :MSTORE(tmpVarEmstore) $ => A :MLOAD(MEM:E) $ => B :MLOAD(MEM:E+1) @@ -254,12 +245,12 @@ memAlignOptionMSTORE: ${memAlignWR_W1(B,mem.bytesToStore,C)} => E ; no trust calculate W1 $ :MEM_ALIGN_WR,MLOAD(bytesToStore) E => A - $ => E :MLOAD(tmpVarE) + $ => 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(tmpVarC) + $ => C :MLOAD(tmpVarCmstore) E*32 + C => E 0 :MSTORE(isMSTOREX), JMP(endMSTORE) @@ -267,11 +258,18 @@ offsetMSTORE32: E + 32 => E endMSTORE: - $ => A :MLOAD(tmpVarA) - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => RR :MLOAD(tmpZkPC) - $ => D :MLOAD(tmpVarD), RETURN + $ => A :MLOAD(tmpVarAmstore) + $ => B :MLOAD(tmpVarBmstore) + $ => C :MLOAD(tmpVarCmstore) + $ => RR :MLOAD(tmpZkPCmstore) + $ => D :MLOAD(tmpVarDmstore), 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) @@ -290,10 +288,10 @@ MLOADX: ; @out E => new offset MLOAD32: ; store current registries - RR :MSTORE(tmpZkPC) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) + RR :MSTORE(tmpZkPCmload) + B :MSTORE(tmpVarBmload) + C :MSTORE(tmpVarCmload) + D :MSTORE(tmpVarDmload) ; check offset is lower than max memory E => A %MEMORY_LENGTH => B @@ -320,7 +318,7 @@ memAlignOptionMLOAD: sliceA: ; if is mloadx, slice the result by the length - $ => C :MLOAD(tmpVarC) + $ => 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] @@ -332,23 +330,28 @@ offsetMLOAD32: endMLOAD: ; restore stored values - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => RR :MLOAD(tmpZkPC) - $ => D :MLOAD(tmpVarD), RETURN + $ => B :MLOAD(tmpVarBmload) + $ => C :MLOAD(tmpVarCmload) + $ => RR :MLOAD(tmpZkPCmload) + $ => D :MLOAD(tmpVarDmload), RETURN errorMLOADMSTORE: :JMP(handleError) +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: ; store current registries - A :MSTORE(tmpVarA) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) + A :MSTORE(tmpVarAemptyAcc) + B :MSTORE(tmpVarBemptyAcc) + C :MSTORE(tmpVarCemptyAcc) + D :MSTORE(tmpVarDemptyAcc) E => A ; read balance @@ -385,20 +388,25 @@ isNotEmptyAccount: ISEMPTYEnd: ; recover registries - $ => A :MLOAD(tmpVarA) - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => D :MLOAD(tmpVarD), RETURN + $ => 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: ; save tmp vars - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - D :MSTORE(tmpVarD) - E :MSTORE(tmpVarE) + B :MSTORE(tmpVarBcompGas) + C :MSTORE(tmpVarCcompGas) + D :MSTORE(tmpVarDcompGas) + E :MSTORE(tmpVarEcompGas) ; compute all_but_one_64th gas GAS - ${GAS/64} => A @@ -410,19 +418,23 @@ computeGasSendCall: computeGasSendCallEnd: ; restore stored values - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => D :MLOAD(tmpVarD) - $ => E :MLOAD(tmpVarE), RETURN - + $ => B :MLOAD(tmpVarBcompGas) + $ => C :MLOAD(tmpVarCcompGas) + $ => D :MLOAD(tmpVarDcompGas) + $ => E :MLOAD(tmpVarEcompGas), RETURN + +VAR GLOBAL tmpVarAsaveMem +VAR GLOBAL tmpVarBsaveMem +VAR GLOBAL tmpVarCsaveMem +VAR GLOBAL tmpVarEsaveMem ; @info compute memory expansion gas cost ; @in: lastMemOffset: offset to copy bytes ; @in: lastMemLength: size of the bytes to copy saveMem: - A :MSTORE(tmpVarA) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - E :MSTORE(tmpVarE) + A :MSTORE(tmpVarAsaveMem) + B :MSTORE(tmpVarBsaveMem) + C :MSTORE(tmpVarCsaveMem) + E :MSTORE(tmpVarEsaveMem) $ => E :MLOAD(lastMemOffset) $ => A :MLOAD(lastMemLength) 0 => B @@ -457,10 +469,10 @@ saveMemGAS: saveMemEnd: ; restore stored values - $ => A :MLOAD(tmpVarA) - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => E :MLOAD(tmpVarE), RETURN + $ => A :MLOAD(tmpVarAsaveMem) + $ => B :MLOAD(tmpVarBsaveMem) + $ => C :MLOAD(tmpVarCsaveMem) + $ => E :MLOAD(tmpVarEsaveMem), RETURN VAR GLOBAL tmpVarAArith VAR GLOBAL tmpVarBArith @@ -584,16 +596,22 @@ storeTmp: 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: RR :MSTORE(tmpSHXZkPC2) - B :MSTORE(tmpVarB2) - C :MSTORE(tmpVarC2) - D :MSTORE(tmpVarD2) - E :MSTORE(tmpVarE2) + B :MSTORE(tmpVarBSHX) + C :MSTORE(tmpVarCSHX) + D :MSTORE(tmpVarDSHX) + E :MSTORE(tmpVarESHX) ; E init number A => E ; A bytes @@ -612,10 +630,10 @@ SHRarith: ;@out A - A >> D => A SHRarithBit: RR :MSTORE(tmpSHXZkPC2) - B :MSTORE(tmpVarB2) - C :MSTORE(tmpVarC2) - D :MSTORE(tmpVarD2) - E :MSTORE(tmpVarE2) + B :MSTORE(tmpVarBSHX) + C :MSTORE(tmpVarCSHX) + D :MSTORE(tmpVarDSHX) + E :MSTORE(tmpVarESHX) SHRarithinit: 0 => B @@ -638,11 +656,11 @@ SHRarith0: 0 => A SHRarithfinal: - $ => B :MLOAD(tmpVarB2) - $ => C :MLOAD(tmpVarC2) - $ => D :MLOAD(tmpVarD2) + $ => B :MLOAD(tmpVarBSHX) + $ => C :MLOAD(tmpVarCSHX) + $ => D :MLOAD(tmpVarDSHX) $ => RR :MLOAD(tmpSHXZkPC2) - $ => E :MLOAD(tmpVarE2), RETURN + $ => E :MLOAD(tmpVarESHX), RETURN ;@info Shift left D bytes to A ;@in A - (A << D) @@ -650,10 +668,10 @@ SHRarithfinal: ;@out A - A << D => A SHLarith: RR :MSTORE(tmpSHXZkPC2) - B :MSTORE(tmpVarB2) - C :MSTORE(tmpVarC2) - D :MSTORE(tmpVarD2) - E :MSTORE(tmpVarE2) + B :MSTORE(tmpVarBSHX) + C :MSTORE(tmpVarCSHX) + D :MSTORE(tmpVarDSHX) + E :MSTORE(tmpVarESHX) ; E init number A => E ; A bytes @@ -668,10 +686,10 @@ SHLarith: :JMP(SHLarithinit) SHLarithBit: RR :MSTORE(tmpSHXZkPC2) - B :MSTORE(tmpVarB2) - C :MSTORE(tmpVarC2) - D :MSTORE(tmpVarD2) - E :MSTORE(tmpVarE2) + B :MSTORE(tmpVarBSHX) + C :MSTORE(tmpVarCSHX) + D :MSTORE(tmpVarDSHX) + E :MSTORE(tmpVarESHX) SHLarithinit: ; E init number @@ -728,11 +746,11 @@ SHLarithBig: SHLarithfinal: E => A - $ => B :MLOAD(tmpVarB2) - $ => C :MLOAD(tmpVarC2) - $ => D :MLOAD(tmpVarD2) + $ => B :MLOAD(tmpVarBSHX) + $ => C :MLOAD(tmpVarCSHX) + $ => D :MLOAD(tmpVarDSHX) $ => RR :MLOAD(tmpSHXZkPC2) - $ => E :MLOAD(tmpVarE2), RETURN + $ => E :MLOAD(tmpVarESHX), RETURN ; out of counters full tracer event trigger outOfCountersStep: @@ -871,15 +889,15 @@ invalidCall: $ => PC :MLOAD(lastPC) 0 :MSTORE(SP++), JMP(readCode) +VAR GLOBAL pushBytes +VAR GLOBAL numBlocks +VAR GLOBAL leftBytes +VAR GLOBAL accumulator ; @info Read bytes opcode PUSHX ; @internalParam {numBlocks} 4 bytes blocks to read ; @internalParam {leftBytes} remaining bytes ; @in D => bytes to read ; @out E => value read -VAR GLOBAL pushBytes -VAR GLOBAL numBlocks -VAR GLOBAL leftBytes -VAR GLOBAL accumulator readPush: D :MSTORE(pushBytes) D => A @@ -978,15 +996,17 @@ finalPush: $ => E :MLOAD(accumulator) $ => D :MLOAD(pushBytes), RETURN +VAR GLOBAL tmpVarDaddB +VAR GLOBAL tmpZkPCaddB VAR GLOBAL auxBytes ;@info: adds data to batchHashdata byte by byte ;@in: A: bytes to add ;@in D: bytes length addBatchHashByteByByte: %MAX_CNT_BINARY - CNT_BINARY - 200 :JMPN(handleOOCBatRLP) - RR :MSTORE(tmpZkPC) + RR :MSTORE(tmpZkPCaddB) A :MSTORE(auxBytes) - D :MSTORE(tmpVarD) + D :MSTORE(tmpVarDaddB) 1 => D utilsAddBatchHashBytebyByte: @@ -1002,23 +1022,29 @@ utilsAddBatchHashBytebyByte: B => D ; D + 1 => D we set 33 in stead of 32 to earn 1 step 33 - D => D - $ => B :MLOAD(tmpVarD) + $ => B :MLOAD(tmpVarDaddB) D - B - 1 :JMPN(utilsAddBatchHashBytebyByte) - $ => RR :MLOAD(tmpZkPC) + $ => RR :MLOAD(tmpZkPCaddB) :RETURN +VAR GLOBAL startsWithEF +VAR GLOBAL tmpVarAEF +VAR GLOBAL tmpVarBEF +VAR GLOBAL tmpVarCEF +VAR GLOBAL tmpVarDEF +VAR GLOBAL tmpVarEEF +VAR GLOBAL tmpZkPCEF ; @info check bytecode first byte != 0xEF as defined in EIP-3541 (https://eips.ethereum.org/EIPS/eip-3541) ; @internalParam {memOffset} memory offset to read bytes from ; @internalParam {startsWithEF} flag to indicate if first deployed byte on the bytecode is 0xEF -VAR GLOBAL startsWithEF checkBytecodeStartsEF: ; save temporary registers - A :MSTORE(tmpVarA3) - B :MSTORE(tmpVarB3) - C :MSTORE(tmpVarC3) - D :MSTORE(tmpVarD3) - E :MSTORE(tmpVarE3) - RR :MSTORE(tmpZkPC2) + A :MSTORE(tmpVarAEF) + B :MSTORE(tmpVarBEF) + C :MSTORE(tmpVarCEF) + D :MSTORE(tmpVarDEF) + E :MSTORE(tmpVarEEF) + RR :MSTORE(tmpZkPCEF) ; load memOffset $ => E :MLOAD(memOffsetLinearPoseidon) @@ -1033,25 +1059,30 @@ checkBytecodeStartsEF: checkBytecodeStartsEFend: ; set back tmp registers - $ => RR :MLOAD(tmpZkPC2) - $ => A :MLOAD(tmpVarA3) - $ => B :MLOAD(tmpVarB3) - $ => C :MLOAD(tmpVarC3) - $ => D :MLOAD(tmpVarD3) - $ => E :MLOAD(tmpVarE3), RETURN - + $ => RR :MLOAD(tmpZkPCEF) + $ => A :MLOAD(tmpVarAEF) + $ => B :MLOAD(tmpVarBEF) + $ => C :MLOAD(tmpVarCEF) + $ => D :MLOAD(tmpVarDEF) + $ => E :MLOAD(tmpVarEEF), RETURN + +VAR GLOBAL tmpVarAhashP +VAR GLOBAL tmpVarBhashP +VAR GLOBAL tmpVarChashP +VAR GLOBAL tmpVarEhashP +VAR GLOBAL tmpZkPChashP +VAR GLOBAL memOffsetLinearPoseidon +VAR GLOBAL memSizeLinearPoseidon ; @info Computes hash bytecode from memory bytes ; @internalParam {memOffset} memory offset to read bytes from ; @internalParam {memSize} memory size to read bytes from ; @out D => resulting linear poseidon -VAR GLOBAL memOffsetLinearPoseidon -VAR GLOBAL memSizeLinearPoseidon hashPoseidonLinearFromMemory: - A :MSTORE(tmpVarA) - B :MSTORE(tmpVarB) - C :MSTORE(tmpVarC) - E :MSTORE(tmpVarE) - RR :MSTORE(tmpZkPC2) + A :MSTORE(tmpVarAhashP) + B :MSTORE(tmpVarBhashP) + C :MSTORE(tmpVarChashP) + E :MSTORE(tmpVarEhashP) + RR :MSTORE(tmpZkPChashP) $ => E :MLOAD(memOffsetLinearPoseidon) $ => C :MLOAD(memSizeLinearPoseidon) 0 => D @@ -1099,21 +1130,21 @@ hashPoseidonEnd: $${saveContractBytecode(E)} hashPoseidonReturn: - $ => RR :MLOAD(tmpZkPC2) - $ => A :MLOAD(tmpVarA) - $ => B :MLOAD(tmpVarB) - $ => C :MLOAD(tmpVarC) - $ => E :MLOAD(tmpVarE), RETURN - + $ => RR :MLOAD(tmpZkPChashP) + $ => A :MLOAD(tmpVarAhashP) + $ => B :MLOAD(tmpVarBhashP) + $ => C :MLOAD(tmpVarChashP) + $ => E :MLOAD(tmpVarEhashP), RETURN +VAR GLOBAL tmpVarBmask ; @info Mask address to 20 bytes ; @in A => address not masked ; @out A => masked address maskAddress: - B :MSTORE(tmpVarB) + B :MSTORE(tmpVarBmask) 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn => B $ => A :AND - $ => B :MLOAD(tmpVarB), RETURN + $ => B :MLOAD(tmpVarBmask), RETURN ;@info: updates the address sytem storage with current batch and state root updateSystemData: @@ -1144,6 +1175,11 @@ updateSystemData: SR => D $ => SR :SSTORE, RETURN +VAR GLOBAL tmpVarAmulmod +VAR GLOBAL tmpVarBmulmod +VAR GLOBAL tmpVarDmulmod +VAR GLOBAL tmpVarEmulmod +VAR GLOBAL tmpZkPCmulmod ; @info (A*B)%C => C ; @in A ; @in B @@ -1155,11 +1191,11 @@ utilMULMOD: %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - RR :MSTORE(tmpZkPC4) - A :MSTORE(tmpVarA) - B :MSTORE(tmpVarB) - D :MSTORE(tmpVarD) - E :MSTORE(tmpVarE) + RR :MSTORE(tmpZkPCmulmod) + A :MSTORE(tmpVarAmulmod) + B :MSTORE(tmpVarBmulmod) + D :MSTORE(tmpVarDmulmod) + E :MSTORE(tmpVarEmulmod) ; The following approach will be followed in order to verify the mulmod operation ; A * B + 0 = D*2^256 + E @@ -1200,7 +1236,7 @@ utilMULMOD: ; D2 must be less than 2²⁵⁶ C => A ; Modulus 0 => C, D - ${B * A} => E :MSTORE(tmpVarD), ARITH ; D2 + ${B * A} => E :MSTORE(tmpVarDmulmod), ARITH ; D2 ; k.l * N + mulModResult = D1 * 2²⁵⁶ + E ; B * A + C = D*2^256 + E @@ -1223,7 +1259,7 @@ utilMULMOD: ; Assert D1 + D2 = D ; ADD ;ASSERT D => A ; D1 - $ => B :MLOAD(tmpVarD) ;D2 + $ => B :MLOAD(tmpVarDmulmod) ;D2 $ => A :ADD $ :MLOAD(arithOverflow), ASSERT, JMP(utilMULMODend) @@ -1247,20 +1283,25 @@ zeroOneMod: 0 => C utilMULMODend: - $ => A :MLOAD(tmpVarA) - $ => B :MLOAD(tmpVarB) - $ => D :MLOAD(tmpVarD) - $ => RR :MLOAD(tmpZkPC4) - $ => E :MLOAD(tmpVarE), RETURN + $ => A :MLOAD(tmpVarAmulmod) + $ => B :MLOAD(tmpVarBmulmod) + $ => D :MLOAD(tmpVarDmulmod) + $ => RR :MLOAD(tmpZkPCmulmod) + $ => E :MLOAD(tmpVarEmulmod), RETURN + +VAR GLOBAL tmpVarBexp +VAR GLOBAL tmpVarCexp +VAR GLOBAL tmpVarEexp +VAR GLOBAL tmpZkPCexp ;@info exp(A,D) --> A^D ;@in A, D => A^D ;@out A => result expAD: - RR :MSTORE(tmpZkPC3) - B :MSTORE(tmpVarB3) - C :MSTORE(tmpVarC3) - E :MSTORE(tmpVarE3) + RR :MSTORE(tmpZkPCexp) + B :MSTORE(tmpVarBexp) + C :MSTORE(tmpVarCexp) + E :MSTORE(tmpVarEexp) ;E base A => E ;B exp @@ -1274,16 +1315,17 @@ expAD: ;if base == 0 --> expA0D $ :EQ,JMPC(expA0D) 1 => C + D => B :CALL(getLenBits) ;A exp bits length -expADloop: - ; check zk-counters on each loop + ; check zk-counters on each loop (A loops) ; - 3 ARITH: 1 in divARITH + 1 in 2*mulARITH ; - 5 BINARIES: 5 in divARITH ; - 100 steps (rounded up): 37 in divARITH + 11 in 2*mulARITH + 18 self - %MAX_CNT_ARITH - CNT_ARITH - 3 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_ARITH - CNT_ARITH - 3*A :JMPN(outOfCountersArith) + %MAX_CNT_BINARY - CNT_BINARY - 5*A :JMPN(outOfCountersBinary) + %MAX_CNT_STEPS - STEP - 100*A :JMPN(outOfCountersStep) +expADloop: ;A exp D => A 0 => B @@ -1322,7 +1364,7 @@ expAD0: expADend: C => A - $ => B :MLOAD(tmpVarB3) - $ => C :MLOAD(tmpVarC3) - $ => RR :MLOAD(tmpZkPC3) - $ => E :MLOAD(tmpVarE3), RETURN \ No newline at end of file + $ => B :MLOAD(tmpVarBexp) + $ => C :MLOAD(tmpVarCexp) + $ => RR :MLOAD(tmpZkPCexp) + $ => E :MLOAD(tmpVarEexp), RETURN \ No newline at end of file From 6b070654db63463c017c95584f8ec981390a8ba7 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Thu, 22 Dec 2022 09:52:53 +0100 Subject: [PATCH 02/60] Fix jmpn --- main/load-tx-rlp.zkasm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 644efc17..11531188 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -104,7 +104,7 @@ gasLimitREAD: A - 0xa1 :JMPN(shortGasLimit, invalidTxRLP) gasLimit0: - 0 => A :JMPN(endGasLimit) + 0 => A :JMP(endGasLimit) shortGasLimit: A - 0x80 => D @@ -147,8 +147,7 @@ valueREAD: A - 0xa1 :JMPN(shortValue, invalidTxRLP) value0: - 0 => A - :JMPN(endValue) + 0 => A :JMP(endValue) shortValue: A - 0x80 => D From 171827a309ab6b021b2d791fe7e09d1b1c3ea361 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 22 Dec 2022 12:56:01 +0100 Subject: [PATCH 03/60] read entire bytecode program at once --- main/opcodes/calldata-returndata-code.zkasm | 59 ++++++--------------- main/process-tx.zkasm | 22 ++------ main/touched.zkasm | 2 + main/utils.zkasm | 1 - main/vars.zkasm | 1 - package.json | 2 +- 6 files changed, 22 insertions(+), 65 deletions(-) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index aac27615..3c9fc357 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -419,24 +419,29 @@ VAR GLOBAL tmpContractLength opEXTCODECOPY: ; checks zk-counters %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_PADDING_PG - CNT_PADDING_PG - 11 :JMPN(outOfCountersPadding) + %MAX_CNT_PADDING_PG - CNT_PADDING_PG - 11 :JMPN(outOfCountersPadding) %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + ; check stack underflow - SP - 4 => SP :JMPN(stackUnderflow) + SP - 4 => SP :JMPN(stackUnderflow) $ => A :MLOAD(SP+3), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address] :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] + ; check out-of-gas GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) - :CALL(opEXTCODECOPYCheckHash) + :CALL(opEXTCODECOPYLoadBytecode) $ => C :MLOAD(SP+2); [destOffset => C] $ => D :MLOAD(SP+1); [offset => D] $ => E :MLOAD(SP); [size => E] + ; store lastMemOffset for memory expansion gas cost C :MSTORE(lastMemOffset) + ; store lastMemLength for memory expansion gas cost E :MSTORE(lastMemLength) + ; check out-of-gas ;${3*((E+31)/32)} E+31 => A @@ -471,7 +476,7 @@ opEXTCODECOPY2: E :MSTORE(remainingBytes), JMP(opCODECOPYinit) VAR GLOBAL tmpZkPCext -opEXTCODECOPYCheckHash: +opEXTCODECOPYLoadBytecode: ; set key for smt smart contract length query %SMT_KEY_SC_LENGTH => B 0 => C @@ -482,6 +487,7 @@ opEXTCODECOPYCheckHash: 0 => A D => B $ :EQ, JMPC(opEXTCODECOPYCheckHashEnd) + ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length RR :MSTORE(tmpZkPCext) @@ -492,56 +498,21 @@ opEXTCODECOPYCheckHash: %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A $ :LT, JMPC(outOfCountersPoseidon) - E => A ; set key for smt smart contract code query + E => A %SMT_KEY_SC_CODE => B 0 => C $ => A :SLOAD + ; get a new hashPId $ => E :MLOAD(nextHashPId) E :MSTORE(tmpContractHashId) E + 1 :MSTORE(nextHashPId) - ; set vars prior to loop - D => B - 0 => HASHPOS :JMP(opEXTCODECOPYCheckHashLoop) - -; @info bytes are inserted byte by byte -opEXTCODECOPYCheckHashLoop: - %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) - ; finish reading bytecode - B :JMPZ(opEXTCODECOPYCheckHashLoopEnd) - ; add bytes to hash contract bytecode - ${getBytecode(A, HASHPOS, 1)} :HASHP1(E) - B - 1 => B :JMP(opEXTCODECOPYCheckHashLoop) - -opEXTCODECOPYCheckHashLoopEnd: - HASHPOS :HASHPLEN(E) - $ => E :HASHPDIGEST(E) - ; check hash computed matches hash in the smt leaf - E :ASSERT + ; load contract bytecode + A :HASHPDIGEST(E) opEXTCODECOPYCheckHashEnd: - :RETURN - -opEXTCODECOPYinit: - ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - C :JMPZ(readCode) - C - 32 :JMPN(opEXTCODECOPYfinal) - ${getBytecode(A,B,32)} => D - ; set bytesToStore with value to use in MSTORE - D :MSTORE(bytesToStore) - :CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] - C - 32 => C - B + 32 => B :JMP(opEXTCODECOPYinit) - -opEXTCODECOPYfinal: - ; copy last bytes - ${getBytecode(A,B,C)} => A - 32 - C => D :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result] - ; set bytesToStore with value to use in MSTORE - A :MSTORE(bytesToStore), CALL(MSTOREX); in: [bytesToStore, E: offset, C: length] out: [E: new offset] - :JMP(readCode) + :RETURN /** * @link [https://www.evm.codes/#3D?fork=berlin] diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 468cbb5d..c443adba 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -368,7 +368,7 @@ readDeployBytecodeCreateDefault: ;;;;;;;;;;;;;;;;;; ;; F.1 - Call contract -;; - Check bytecode to process against state-tree hash bytecode +;; - Load bytecode from its state-tree hash ;; - Process bytecode ;; - End deploy: add state-tree hash bytecode and bytecode length ;;;;;;;;;;;;;;;;;; @@ -399,32 +399,18 @@ callContract: %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A $ :LT, JMPC(outOfCountersPoseidon) - $ => A :MLOAD(txDestAddr) ; get hash contract + $ => A :MLOAD(txDestAddr) %SMT_KEY_SC_CODE => B $ => A :SLOAD - A :MSTORE(hashContractTxDestAddr) - 0 => HASHPOS - $ => B :MLOAD(bytecodeLength) ; get a new hashPId $ => E :MLOAD(nextHashPId) E :MSTORE(contractHashId) E+1 :MSTORE(nextHashPId) - ; check steps used by REPEAT - %MAX_CNT_STEPS - STEP - B - 4 :JMPN(outOfCountersStep) - - ; set repeat itarations: RCX register + 1 - B - 1 => RCX :JMPN(checkHashBytecodeEnd) - ${getBytecode(A, HASHPOS, 1)} :HASHP1(E), REPEAT(RCX) ; hash contract bytecode - -checkHashBytecodeEnd: - HASHPOS :HASHPLEN(E) - $ => E :HASHPDIGEST(E) - ; check hash computed matches hash in the smt leaf - $ => A :MLOAD(hashContractTxDestAddr) - E :ASSERT, JMP(readCode) + ; load contract bytecode + A :HASHPDIGEST(E) readByteCode: $ => E :MLOAD(contractHashId) ; hash index diff --git a/main/touched.zkasm b/main/touched.zkasm index 2aadcdda..18c021f3 100644 --- a/main/touched.zkasm +++ b/main/touched.zkasm @@ -50,6 +50,7 @@ checkWarmed: ; set address to warm and return 0 markWarmAddress: 1 => D + $${eventLog(onTouchedAddress(A))} $ => SR :SSTORE ; address is cold. D = 1, set before @@ -95,6 +96,7 @@ isColdSlot: ; set address to warm and return 0 markWarmSlot: 1 => D + $${eventLog(onTouchedSlot(A, C))} $ => SR :SSTORE ; slot is cold. A = 1 1 => A diff --git a/main/utils.zkasm b/main/utils.zkasm index 01e4e1ef..8606bb7d 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -1127,7 +1127,6 @@ hashPoseidonEnd: $ => E :MLOAD(tmpContractHashId) HASHPOS :HASHPLEN(E) $ => D :HASHPDIGEST(E) - $${saveContractBytecode(E)} hashPoseidonReturn: $ => RR :MLOAD(tmpZkPChashP) diff --git a/main/vars.zkasm b/main/vars.zkasm index 919e8a24..8e4f2cb9 100644 --- a/main/vars.zkasm +++ b/main/vars.zkasm @@ -44,7 +44,6 @@ 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 hashContractTxDestAddr ; state-tree hash bytecode leaf value of the 'to' address VAR GLOBAL auxSR ; auxiliaty 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 diff --git a/package.json b/package.json index 6772d4f1..5d50ac24 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#v0.6.0.0", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/last-changes", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.6.0.0", "chai": "^4.3.6", "chalk": "^3.0.0", From 1c627c437a53ae9aa11edbb164777d2df59b3384 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Wed, 4 Jan 2023 16:30:33 +0100 Subject: [PATCH 04/60] Fix computeGasSendCall Fix saveMem free input fix --- .gitignore | 1 + main/utils.zkasm | 59 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index edfa729c..6262be5f 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,4 @@ build # testvectors files tools/parallel-tests + diff --git a/main/utils.zkasm b/main/utils.zkasm index 8606bb7d..94117ad7 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -409,7 +409,14 @@ computeGasSendCall: E :MSTORE(tmpVarEcompGas) ; compute all_but_one_64th gas - GAS - ${GAS/64} => A + GAS => A + ${A >> 6} => C + ${A & 0x3f} => D + 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) @@ -426,14 +433,18 @@ computeGasSendCallEnd: 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: + RR :MSTORE(tmpZkPCsaveMem) A :MSTORE(tmpVarAsaveMem) B :MSTORE(tmpVarBsaveMem) C :MSTORE(tmpVarCsaveMem) + D :MSTORE(tmpVarDsaveMem) E :MSTORE(tmpVarEsaveMem) $ => E :MLOAD(lastMemOffset) $ => A :MLOAD(lastMemLength) @@ -450,19 +461,45 @@ saveMem: saveMemGAS: ; store new memory length B :MSTORE(memLength) + A => D ; new memory size word= (memory_byte_size + 31) / 32 in E - ${(B+31)/32} => E + ; ${(B+31)/32} => E + 31 => A + $ => C :ADD,JMPC(outOfGas) + C :MSTORE(arithA) + 32 :MSTORE(arithB),CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => E :MLOAD(arithRes1) ; old memory size word = (memory_byte_size + 31) / 32 in C - ${(A+31)/32} => C + ; ${(A+31)/32} => C + D => B + $ => C :ADD,JMPC(outOfGas) + C :MSTORE(arithA) + 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => C :MLOAD(arithRes1) ; memory_cost = (memory_size_word ** 2) / 512 + (3 * memory_size_word) - GAS + 3*C + ${C*C/512} => GAS + ; ${C*C/512} => A + C :MSTORE(arithA) + C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB, arithOverflow: set to 1 if operation causes overflow] + $ => A :MLOAD(arithRes1) + $ :MLOAD(arithOverflow), JMPNZ(outOfGas) + A :MSTORE(arithA) + 512 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => A :MLOAD(arithRes1) + GAS + 3*C + A => GAS ; memory_expansion_cost = new_memory_cost - last_memory_cost GAS => A 3*E => B ; check enough gas for exapnsion cost $ :LT,JMPC(outOfGas) A - B => A - ${E*E/512} => B + ; ${E*E/512} => B + E :MSTORE(arithA) + E :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB, arithOverflow: set to 1 if operation causes overflow] + $ => E :MLOAD(arithRes1) + $ :MLOAD(arithOverflow), JMPNZ(outOfGas) + E :MSTORE(arithA) + 512 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => B :MLOAD(arithRes1) $ :LT,JMPC(outOfGas) ; update new gas A - B => GAS @@ -472,6 +509,8 @@ saveMemEnd: $ => A :MLOAD(tmpVarAsaveMem) $ => B :MLOAD(tmpVarBsaveMem) $ => C :MLOAD(tmpVarCsaveMem) + $ => D :MLOAD(tmpVarDsaveMem) + $ => RR :MLOAD(tmpZkPCsaveMem) $ => E :MLOAD(tmpVarEsaveMem), RETURN VAR GLOBAL tmpVarAArith @@ -910,8 +949,7 @@ readPush: B*4 + D :ASSERT D => A 0x04 => B - $ => A :LT - 1 :ASSERT + $ => A :LT,JMPNC(failAssert) 0 => B :JMP(readPushBlock) readPushBlock: @@ -1366,4 +1404,9 @@ expADend: $ => B :MLOAD(tmpVarBexp) $ => C :MLOAD(tmpVarCexp) $ => RR :MLOAD(tmpZkPCexp) - $ => E :MLOAD(tmpVarEexp), RETURN \ No newline at end of file + $ => E :MLOAD(tmpVarEexp), RETURN + +;@info function to force a failed assertt +failAssert: + 1 => A + 2 :ASSERT From 2987a823906ad210d2cd40fff36e88d1710c27ce Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Thu, 5 Jan 2023 15:06:19 +0100 Subject: [PATCH 05/60] Performance improvement --- main/utils.zkasm | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/main/utils.zkasm b/main/utils.zkasm index 94117ad7..aca17c18 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -410,8 +410,8 @@ computeGasSendCall: ; compute all_but_one_64th gas GAS => A - ${A >> 6} => C - ${A & 0x3f} => D + ${GAS >> 6} => C + ${GAS & 0x3f} => D C * 64 + D :ASSERT D => A 0x40 => B @@ -461,21 +461,32 @@ saveMem: saveMemGAS: ; store new memory length B :MSTORE(memLength) - A => D + B => E + A => C + ; check new memory length is lower than 2**32 - 31 + 0xffffffe1 => A + $ :LT,JMPC(outOfGas) + ; new memory size word= (memory_byte_size + 31) / 32 in E ; ${(B+31)/32} => E - 31 => A - $ => C :ADD,JMPC(outOfGas) - C :MSTORE(arithA) - 32 :MSTORE(arithB),CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => E :MLOAD(arithRes1) + E + 31 => A + ${A >> 5} => E + ${A & 0x1f} => D + E * 32 + D :ASSERT + D => A + 0x20 => B + $ :LT,JMPNC(failAssert) + ; old memory size word = (memory_byte_size + 31) / 32 in C + ; old memory length is always lower than 32 bits ; ${(A+31)/32} => C - D => B - $ => C :ADD,JMPC(outOfGas) - C :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => C :MLOAD(arithRes1) + C + 31 => A + ${A >> 5} => C + ${A & 0x1f} => D + C * 32 + D :ASSERT + D => A + 0x20 => B + $ :LT,JMPNC(failAssert) ; memory_cost = (memory_size_word ** 2) / 512 + (3 * memory_size_word) ; ${C*C/512} => A C :MSTORE(arithA) From 4c2763ab4aa2fdfa630b4e1f51c4521a5954a5c3 Mon Sep 17 00:00:00 2001 From: zkronos73 Date: Mon, 9 Jan 2023 08:50:10 +0100 Subject: [PATCH 06/60] remove obsolete TODO comment --- main/ecrecover/mulPointEc.zkasm | 1 - 1 file changed, 1 deletion(-) diff --git a/main/ecrecover/mulPointEc.zkasm b/main/ecrecover/mulPointEc.zkasm index d6d65784..00d82048 100644 --- a/main/ecrecover/mulPointEc.zkasm +++ b/main/ecrecover/mulPointEc.zkasm @@ -17,7 +17,6 @@ VAR GLOBAL mulPointEc_p12_x VAR GLOBAL mulPointEc_p12_y VAR GLOBAL mulPointEc_HASHPOS -; TODO protect all cases mulPointEc: RR :MSTORE(mulPointEc_RR) HASHPOS :MSTORE(mulPointEc_HASHPOS) From 56c1d1b832e4c56e10f666c7afc51a2017b34e9b Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Thu, 5 Jan 2023 16:13:26 +0100 Subject: [PATCH 07/60] Refactor saveMem --- main/constants.zkasm | 1 + main/process-tx.zkasm | 1 - main/utils.zkasm | 66 ++++++++++++++----------------------------- main/vars.zkasm | 1 + 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index 9d12b293..49527664 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -9,6 +9,7 @@ CONST %GLOBAL_EXIT_ROOT_STORAGE_POS = 0 CONST %LOCAL_EXIT_ROOT_STORAGE_POS = 1 CONST %LAST_TX_STORAGE_POS = 0 CONST %STATE_ROOT_STORAGE_POS = 1 +CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0 ; SMT STATE-TREE CONSTANT KEYS CONST %SMT_KEY_BALANCE = 0 diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index c443adba..9945535b 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -29,7 +29,6 @@ processTx: ; Store initial state at the beginning of the transaction SR :MSTORE(originSR) SR :MSTORE(initSR) - ; Minimum of 100000 steps left to process a tx %MAX_CNT_STEPS - STEP - 100000 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 100 :JMPN(outOfCountersBinary) diff --git a/main/utils.zkasm b/main/utils.zkasm index aca17c18..67ef65b6 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -446,15 +446,18 @@ saveMem: C :MSTORE(tmpVarCsaveMem) D :MSTORE(tmpVarDsaveMem) E :MSTORE(tmpVarEsaveMem) - $ => E :MLOAD(lastMemOffset) $ => A :MLOAD(lastMemLength) - 0 => B - ; If no len, no memory expansion - $ :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 - E => B + ; offset + length in B $ => B :ADD, JMPC(outOfGas) - ; load old memory length + ; If no len, no memory expansion + A :JMPZ(saveMemEnd) + ; check new memory length is lower than 2**22 - 31 - 1 (max supported memory expansion for 30M 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) @@ -462,12 +465,7 @@ saveMemGAS: ; store new memory length B :MSTORE(memLength) B => E - A => C - ; check new memory length is lower than 2**32 - 31 - 0xffffffe1 => A - $ :LT,JMPC(outOfGas) - - ; new memory size word= (memory_byte_size + 31) / 32 in E + ; memory_size_word = (memory_byte_size + 31) / 32 in E ; ${(B+31)/32} => E E + 31 => A ${A >> 5} => E @@ -475,45 +473,23 @@ saveMemGAS: E * 32 + D :ASSERT D => A 0x20 => B - $ :LT,JMPNC(failAssert) - - ; old memory size word = (memory_byte_size + 31) / 32 in C - ; old memory length is always lower than 32 bits - ; ${(A+31)/32} => C - C + 31 => A - ${A >> 5} => C - ${A & 0x1f} => D - C * 32 + D :ASSERT - D => A - 0x20 => B - $ :LT,JMPNC(failAssert) - ; memory_cost = (memory_size_word ** 2) / 512 + (3 * memory_size_word) - ; ${C*C/512} => A - C :MSTORE(arithA) - C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB, arithOverflow: set to 1 if operation causes overflow] - $ => A :MLOAD(arithRes1) + $ :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, arithOverflow: set to 1 if operation causes overflow] + $ => C :MLOAD(arithRes1) $ :MLOAD(arithOverflow), JMPNZ(outOfGas) - A :MSTORE(arithA) + C :MSTORE(arithA) 512 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => A :MLOAD(arithRes1) - GAS + 3*C + A => GAS + A + 3*E => A + $ => B :MLOAD(lastMemoryExpansionCost) + A :MSTORE(lastMemoryExpansionCost) ; memory_expansion_cost = new_memory_cost - last_memory_cost - GAS => A - 3*E => B - ; check enough gas for exapnsion cost - $ :LT,JMPC(outOfGas) A - B => A - ; ${E*E/512} => B - E :MSTORE(arithA) - E :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB, arithOverflow: set to 1 if operation causes overflow] - $ => E :MLOAD(arithRes1) - $ :MLOAD(arithOverflow), JMPNZ(outOfGas) - E :MSTORE(arithA) - 512 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - $ :LT,JMPC(outOfGas) ; update new gas - A - B => GAS + GAS - A => GAS :JMPN(outOfGas) saveMemEnd: ; restore stored values diff --git a/main/vars.zkasm b/main/vars.zkasm index 8e4f2cb9..9b13f471 100644 --- a/main/vars.zkasm +++ b/main/vars.zkasm @@ -68,6 +68,7 @@ VAR CTX gasRefund ; keeps track of the transaction gas refund VAR CTX initSR ; state-tree once the initial upfront cost is substracted and nonce is increased VAR CTX memLength ; current memory size VAR CTX lastMemLength ; length of bytes to copy to memory +VAR CTX lastMemoryExpansionCost ; cost of the last memory expansion VAR CTX lastMemOffset ; offset to copy to memory VAR CTX retCallOffset ; initial pointer to begin store the return data VAR CTX retCallLength ; size of the return data From 8938e62ea2b31b45bfaf8979c430a228f1338d50 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Mon, 9 Jan 2023 13:11:24 +0100 Subject: [PATCH 08/60] Fix offset utils + others --- main/opcodes/create-terminate-context.zkasm | 4 ++-- main/utils.zkasm | 21 ++++++++++++++------- test/rotate.zkasm | 5 ++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index cde1f194..aed458bd 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -720,11 +720,11 @@ opCREATE2: CTX :MSTORE(originAuxCTX) ; cost to hash the initialisation code - ${C+31} :MSTORE(arithA) + C + 31 :MSTORE(arithA) 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => C :MLOAD(arithRes1) GAS => A - 6*C => B + 6 * C => B $ :LT,JMPC(outOfGas) A - B => GAS GAS :MSTORE(gasCall) diff --git a/main/utils.zkasm b/main/utils.zkasm index 67ef65b6..8576fe2b 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -453,7 +453,7 @@ saveMem: $ => B :ADD, JMPC(outOfGas) ; If no len, no memory expansion A :JMPZ(saveMemEnd) - ; check new memory length is lower than 2**22 - 31 - 1 (max supported memory expansion for 30M of gas) + ; 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) @@ -842,18 +842,25 @@ firstContextInvalid: 0 :MSTORE(gasRefund) 0 => GAS :JMP(sendGasSeq) +VAR GLOBAL tmpVarAoffsetUtil +VAR GLOBAL tmpVarBoffsetUtil ;Get offset/32 & offset%32 ;@in A offset ;@out E offset/32 ;@out C offset%32 offsetUtil: - ; ${A >> 5} -> E (*) + A :MSTORE(tmpVarAoffsetUtil) + B :MSTORE(tmpVarAoffsetUtil) ${A >> 5} => E - ; ${A & 0x1F} -> C ${A & 0x1F} => C + E * 32 + C :ASSERT + C => A + 0x20 => B + $ :LT,JMPNC(failAssert) 0x0FFFF - E :JMPN(stackUnderflow) - 31-C :JMPN(stackUnderflow) - E*32+C :ASSERT, RETURN + 31 - C :JMPN(stackUnderflow) + $ => A :MLOAD(tmpVarAoffsetUtil) + $ => B :MLOAD(tmpVarBoffsetUtil), RETURN ;@info: move balances between two accounts ;@in: txSrcAddr: source address @@ -933,10 +940,10 @@ readPush: B :MSTORE(numBlocks) ${A & 0x03} => D D :MSTORE(leftBytes) - B*4 + D :ASSERT + B * 4 + D :ASSERT D => A 0x04 => B - $ => A :LT,JMPNC(failAssert) + $ :LT,JMPNC(failAssert) 0 => B :JMP(readPushBlock) readPushBlock: diff --git a/test/rotate.zkasm b/test/rotate.zkasm index da3e5cc3..bacdb7e6 100644 --- a/test/rotate.zkasm +++ b/test/rotate.zkasm @@ -22,7 +22,10 @@ readPush: PC + D - 1 => PC ${A >> 2} => B ${A & 0x03} => D - B*4 + D :ASSERT + B * 4 + D :ASSERT + D => A + 0x04 => B + $ :LT,JMPNC(failAssert) D :MSTORE(leftBytes) B :MSTORE(numBlock) 0 => B From ad8770b5678eabe5f63af775abafbee9183c719f Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Tue, 10 Jan 2023 12:49:01 +0100 Subject: [PATCH 09/60] First approach Apply jmpnz More functions Finished counters tests from utils Setup gha for counter tests Arith counters tests --- .github/workflows/main.yaml | 3 + counters/counters-executor.js | 65 +++++ counters/countersConstants.zkasm | 257 ++++++++++++++++++++ counters/endIncludes.zkasm | 7 + counters/initIncludes.zkasm | 1 + counters/tests/MLOAD32.zkasm | 27 ++ counters/tests/MLOADX.zkasm | 30 +++ counters/tests/MSTORE32.zkasm | 32 +++ counters/tests/MSTOREX.zkasm | 36 +++ counters/tests/SHLarith.zkasm | 28 +++ counters/tests/SHLarithBit.zkasm | 28 +++ counters/tests/SHRarith.zkasm | 28 +++ counters/tests/SHRarithBit.zkasm | 28 +++ counters/tests/abs.zkasm | 29 +++ counters/tests/addBatchHashByteByByte.zkasm | 33 +++ counters/tests/computeGasSendCall.zkasm | 30 +++ counters/tests/copySP.zkasm | 38 +++ counters/tests/divArith.zkasm | 27 ++ counters/tests/expAD.zkasm | 30 +++ counters/tests/getLenBits.zkasm | 30 +++ counters/tests/getLenBytes.zkasm | 32 +++ counters/tests/isEmptyAccount.zkasm | 30 +++ counters/tests/mulARITH.zkasm | 28 +++ counters/tests/offsetUtil.zkasm | 29 +++ counters/tests/opADDMOD.zkasm | 28 +++ counters/tests/opAdd.zkasm | 27 ++ counters/tests/opDIV.zkasm | 27 ++ counters/tests/opEXP.zkasm | 29 +++ counters/tests/opMOD.zkasm | 27 ++ counters/tests/opMUL.zkasm | 27 ++ counters/tests/opMULMOD.zkasm | 28 +++ counters/tests/opSDIV.zkasm | 28 +++ counters/tests/opSIGNEXTEND.zkasm | 27 ++ counters/tests/opSMOD.zkasm | 28 +++ counters/tests/opSUB.zkasm | 27 ++ counters/tests/saveMem.zkasm | 31 +++ main/load-tx-rlp-utils.zkasm | 2 +- main/load-tx-rlp.zkasm | 2 + main/opcodes/arithmetic.zkasm | 41 ++-- main/process-tx.zkasm | 1 + main/utils.zkasm | 231 ++++++++++++------ package.json | 1 + 42 files changed, 1415 insertions(+), 103 deletions(-) create mode 100644 counters/counters-executor.js create mode 100644 counters/countersConstants.zkasm create mode 100644 counters/endIncludes.zkasm create mode 100644 counters/initIncludes.zkasm create mode 100644 counters/tests/MLOAD32.zkasm create mode 100644 counters/tests/MLOADX.zkasm create mode 100644 counters/tests/MSTORE32.zkasm create mode 100644 counters/tests/MSTOREX.zkasm create mode 100644 counters/tests/SHLarith.zkasm create mode 100644 counters/tests/SHLarithBit.zkasm create mode 100644 counters/tests/SHRarith.zkasm create mode 100644 counters/tests/SHRarithBit.zkasm create mode 100644 counters/tests/abs.zkasm create mode 100644 counters/tests/addBatchHashByteByByte.zkasm create mode 100644 counters/tests/computeGasSendCall.zkasm create mode 100644 counters/tests/copySP.zkasm create mode 100644 counters/tests/divArith.zkasm create mode 100644 counters/tests/expAD.zkasm create mode 100644 counters/tests/getLenBits.zkasm create mode 100644 counters/tests/getLenBytes.zkasm create mode 100644 counters/tests/isEmptyAccount.zkasm create mode 100644 counters/tests/mulARITH.zkasm create mode 100644 counters/tests/offsetUtil.zkasm create mode 100644 counters/tests/opADDMOD.zkasm create mode 100644 counters/tests/opAdd.zkasm create mode 100644 counters/tests/opDIV.zkasm create mode 100644 counters/tests/opEXP.zkasm create mode 100644 counters/tests/opMOD.zkasm create mode 100644 counters/tests/opMUL.zkasm create mode 100644 counters/tests/opMULMOD.zkasm create mode 100644 counters/tests/opSDIV.zkasm create mode 100644 counters/tests/opSIGNEXTEND.zkasm create mode 100644 counters/tests/opSMOD.zkasm create mode 100644 counters/tests/opSUB.zkasm create mode 100644 counters/tests/saveMem.zkasm diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a26e4b31..27a8fa8d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,6 +21,9 @@ jobs: run: | npm i npm run build + - name: run counters tests + run: | + npm run test:counters - name: Generate tests run: | npm run test:gen diff --git a/counters/counters-executor.js b/counters/counters-executor.js new file mode 100644 index 00000000..e24d5534 --- /dev/null +++ b/counters/counters-executor.js @@ -0,0 +1,65 @@ +const path = require('path'); +const fs = require('fs'); +const smMain = require('@0xpolygonhermez/zkevm-proverjs/src/sm/sm_main/sm_main'); +const fileCachePil = path.join(__dirname, '../node_modules/@0xpolygonhermez/zkevm-proverjs/cache-main-pil.json'); +const empty_input = require('@0xpolygonhermez/zkevm-proverjs/test/inputs/empty_input.json') +const buildPoseidon = require('@0xpolygonhermez/zkevm-commonjs').getPoseidon; +const pathMainPil = path.join(__dirname, '../node_modules/@0xpolygonhermez/zkevm-proverjs/pil/main.pil'); +const { newCommitPolsArray } = require('pilcom'); +const { compile } = require('pilcom'); +const zkasm = require("@0xpolygonhermez/zkasmcom"); +const testFilesDir = path.join(__dirname, './tests'); +const { argv } = require('yargs'); + +async function main() { + + // Compile pil + const cmPols = await compilePil(); + + // Run all zkasm files + const files = getTestFiles(); + + for (let file of files) { + await runTest(file, cmPols) + } +} + +async function runTest(testName, cmPols) { + const zkasmFile = `${testFilesDir}/${testName}`; + // Compile rom + const rom = await zkasm.compile(zkasmFile); + const config = { + debug: true, + stepsN: 8388608, + } + console.log(`Running ${testName}`) + const res = await smMain.execute(cmPols.Main, empty_input, rom, config); + console.log(res.counters) +} + +function getTestFiles() { + if(argv.test){ + return [`${argv.test}.zkasm`] + } + const files = fs.readdirSync(testFilesDir).filter(name => name.endsWith('.zkasm')) + return files +} + +async function compilePil() { + if (!fs.existsSync(fileCachePil)) { + const poseidon = await buildPoseidon(); + const { F } = poseidon; + const pilConfig = { + defines: { N: 4096 }, + namespaces: ['Main', 'Global'], + disableUnusedError: true + }; + const p = await compile(F, pathMainPil, null, pilConfig); + fs.writeFileSync(fileCachePil, `${JSON.stringify(p, null, 1)}\n`, 'utf8'); + } + + const pil = JSON.parse(fs.readFileSync(fileCachePil)); + return newCommitPolsArray(pil); +} + +main() \ No newline at end of file diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm new file mode 100644 index 00000000..4644031a --- /dev/null +++ b/counters/countersConstants.zkasm @@ -0,0 +1,257 @@ +;divArith +CONST %DIVARITH_STEP = 50 +CONST %DIVARITH_CNT_BINARY = 3 +CONST %DIVARITH_CNT_ARITH = 1 +CONST %DIVARITH_CNT_KECCAK_F = 0 +CONST %DIVARITH_CNT_MEM_ALIGN = 0 +CONST %DIVARITH_CNT_PADDING_PG = 0 +CONST %DIVARITH_CNT_POSEIDON_G = 0 +; SHRarith +CONST %SHRARITH_STEP = 50 + %DIVARITH_STEP +CONST %SHRARITH_CNT_BINARY = 2 + %DIVARITH_CNT_BINARY +CONST %SHRARITH_CNT_ARITH = 1 + %DIVARITH_CNT_ARITH +CONST %SHRARITH_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F +CONST %SHRARITH_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN +CONST %SHRARITH_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG +CONST %SHRARITH_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G +; SHRarithBit +CONST %SHRARITHBIT_STEP = 30 + %DIVARITH_STEP +CONST %SHRARITHBIT_CNT_BINARY = 2 + %DIVARITH_CNT_BINARY +CONST %SHRARITHBIT_CNT_ARITH = 0 + %DIVARITH_CNT_ARITH +CONST %SHRARITHBIT_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F +CONST %SHRARITHBIT_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN +CONST %SHRARITHBIT_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG +CONST %SHRARITHBIT_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G +;offsetUtil +CONST %OFFSETUTIL_STEP = 20 +CONST %OFFSETUTIL_CNT_BINARY = 1 +CONST %OFFSETUTIL_CNT_ARITH = 0 +CONST %OFFSETUTIL_CNT_KECCAK_F = 0 +CONST %OFFSETUTIL_CNT_MEM_ALIGN = 0 +CONST %OFFSETUTIL_CNT_PADDING_PG = 0 +CONST %OFFSETUTIL_CNT_POSEIDON_G = 0 +; SHLarith +CONST %SHLARITH_STEP = 100 +CONST %SHLARITH_CNT_BINARY = 4 +CONST %SHLARITH_CNT_ARITH = 2 +CONST %SHLARITH_CNT_KECCAK_F = 0 +CONST %SHLARITH_CNT_MEM_ALIGN = 0 +CONST %SHLARITH_CNT_PADDING_PG = 0 +CONST %SHLARITH_CNT_POSEIDON_G = 0 +; SHLarithBit +CONST %SHLARITHBIT_STEP = 100 +CONST %SHLARITHBIT_CNT_BINARY = 4 +CONST %SHLARITHBIT_CNT_ARITH = 1 +CONST %SHLARITHBIT_CNT_KECCAK_F = 0 +CONST %SHLARITHBIT_CNT_MEM_ALIGN = 0 +CONST %SHLARITHBIT_CNT_PADDING_PG = 0 +CONST %SHLARITHBIT_CNT_POSEIDON_G = 0 +; MLOADX +CONST %MLOADX_STEP = 100 + %SHRARITH_STEP + %SHLARITH_STEP + %OFFSETUTIL_STEP +CONST %MLOADX_CNT_BINARY = 1 + %SHRARITH_CNT_BINARY + %SHLARITH_CNT_BINARY + %OFFSETUTIL_CNT_BINARY +CONST %MLOADX_CNT_ARITH = 0 + %SHRARITH_CNT_ARITH + %SHLARITH_CNT_ARITH + %OFFSETUTIL_CNT_ARITH +CONST %MLOADX_CNT_KECCAK_F = 0 + %SHRARITH_CNT_KECCAK_F + %SHLARITH_CNT_KECCAK_F + %OFFSETUTIL_CNT_KECCAK_F +CONST %MLOADX_CNT_MEM_ALIGN = 1 + %SHRARITH_CNT_MEM_ALIGN + %SHLARITH_CNT_MEM_ALIGN + %OFFSETUTIL_CNT_MEM_ALIGN +CONST %MLOADX_CNT_PADDING_PG = 0 + %SHRARITH_CNT_PADDING_PG + %SHLARITH_CNT_PADDING_PG + %OFFSETUTIL_CNT_PADDING_PG +CONST %MLOADX_CNT_POSEIDON_G = 0 + %SHRARITH_CNT_POSEIDON_G + %SHLARITH_CNT_POSEIDON_G + %OFFSETUTIL_CNT_POSEIDON_G +; MLOAD32 +CONST %MLOAD32_STEP = 100 + %OFFSETUTIL_STEP +CONST %MLOAD32_CNT_BINARY = 1 + %OFFSETUTIL_CNT_BINARY +CONST %MLOAD32_CNT_ARITH = 0 + %OFFSETUTIL_CNT_ARITH +CONST %MLOAD32_CNT_KECCAK_F = 0 + %OFFSETUTIL_CNT_KECCAK_F +CONST %MLOAD32_CNT_MEM_ALIGN = 1 + %OFFSETUTIL_CNT_MEM_ALIGN +CONST %MLOAD32_CNT_PADDING_PG = 0 + %OFFSETUTIL_CNT_PADDING_PG +CONST %MLOAD32_CNT_POSEIDON_G = 0 + %OFFSETUTIL_CNT_POSEIDON_G +; abs +CONST %ABS_STEP = 20 +CONST %ABS_CNT_BINARY = 2 +CONST %ABS_CNT_ARITH = 0 +CONST %ABS_CNT_KECCAK_F = 0 +CONST %ABS_CNT_MEM_ALIGN = 0 +CONST %ABS_CNT_PADDING_PG = 0 +CONST %ABS_CNT_POSEIDON_G = 0 +; copySP +CONST %COPYSP_STEP = 20 + %MLOADX_STEP +CONST %COPYSP_CNT_BINARY = 0 + %MLOADX_CNT_BINARY +CONST %COPYSP_CNT_ARITH = 0 + %MLOADX_CNT_ARITH +CONST %COPYSP_CNT_KECCAK_F = 0 + %MLOADX_CNT_KECCAK_F +CONST %COPYSP_CNT_MEM_ALIGN = 0 + %MLOADX_CNT_MEM_ALIGN +CONST %COPYSP_CNT_PADDING_PG = 0 + %MLOADX_CNT_PADDING_PG +CONST %COPYSP_CNT_POSEIDON_G = 0 + %MLOADX_CNT_POSEIDON_G +; mulARITH +CONST %MULARITH_STEP = 50 +CONST %MULARITH_CNT_BINARY = 0 +CONST %MULARITH_CNT_ARITH = 1 +CONST %MULARITH_CNT_KECCAK_F = 0 +CONST %MULARITH_CNT_MEM_ALIGN = 0 +CONST %MULARITH_CNT_PADDING_PG = 0 +CONST %MULARITH_CNT_POSEIDON_G = 0 +; getLenBytes +CONST %GETLENBYTES_STEP = 20 +CONST %GETLENBYTES_CNT_BINARY = 0 +CONST %GETLENBYTES_CNT_ARITH = 0 +CONST %GETLENBYTES_CNT_KECCAK_F = 0 +CONST %GETLENBYTES_CNT_MEM_ALIGN = 0 +CONST %GETLENBYTES_CNT_PADDING_PG = 0 +CONST %GETLENBYTES_CNT_POSEIDON_G = 0 +; getLenBits +CONST %GETLENBITS_STEP = 20 +CONST %GETLENBITS_CNT_BINARY = 0 +CONST %GETLENBITS_CNT_ARITH = 0 +CONST %GETLENBITS_CNT_KECCAK_F = 0 +CONST %GETLENBITS_CNT_MEM_ALIGN = 0 +CONST %GETLENBITS_CNT_PADDING_PG = 0 +CONST %GETLENBITS_CNT_POSEIDON_G = 0 +;expAD +CONST %EXPAD_STEP = 20 + %GETLENBITS_STEP +CONST %EXPAD_CNT_BINARY = 2 + %GETLENBITS_CNT_BINARY +CONST %EXPAD_CNT_ARITH = 0 + %GETLENBITS_CNT_ARITH +CONST %EXPAD_CNT_KECCAK_F = 0 + %GETLENBITS_CNT_KECCAK_F +CONST %EXPAD_CNT_MEM_ALIGN = 0 + %GETLENBITS_CNT_MEM_ALIGN +CONST %EXPAD_CNT_PADDING_PG = 0 + %GETLENBITS_CNT_PADDING_PG +CONST %EXPAD_CNT_POSEIDON_G = 0 + %GETLENBITS_CNT_POSEIDON_G +;opAdd +CONST %OPADD_STEP = 20 +CONST %OPADD_CNT_BINARY = 1 +CONST %OPADD_CNT_ARITH = 0 +CONST %OPADD_CNT_KECCAK_F = 0 +CONST %OPADD_CNT_MEM_ALIGN = 0 +CONST %OPADD_CNT_PADDING_PG = 0 +CONST %OPADD_CNT_POSEIDON_G = 0 +;opMUL +CONST %OPMUL_STEP = 20 + %MULARITH_STEP +CONST %OPMUL_CNT_BINARY = 0 + %MULARITH_CNT_BINARY +CONST %OPMUL_CNT_ARITH = 0 + %MULARITH_CNT_ARITH +CONST %OPMUL_CNT_KECCAK_F = 0 + %MULARITH_CNT_KECCAK_F +CONST %OPMUL_CNT_MEM_ALIGN = 0 + %MULARITH_CNT_MEM_ALIGN +CONST %OPMUL_CNT_PADDING_PG = 0 + %MULARITH_CNT_PADDING_PG +CONST %OPMUL_CNT_POSEIDON_G = 0 + %MULARITH_CNT_POSEIDON_G +;opSUB +CONST %OPSUB_STEP = 20 +CONST %OPSUB_CNT_BINARY = 1 +CONST %OPSUB_CNT_ARITH = 0 +CONST %OPSUB_CNT_KECCAK_F = 0 +CONST %OPSUB_CNT_MEM_ALIGN = 0 +CONST %OPSUB_CNT_PADDING_PG = 0 +CONST %OPSUB_CNT_POSEIDON_G = 0 +;opDIV +CONST %OPDIV_STEP = 20 + %DIVARITH_STEP +CONST %OPDIV_CNT_BINARY = 0 + %DIVARITH_CNT_BINARY +CONST %OPDIV_CNT_ARITH = 0 + %DIVARITH_CNT_ARITH +CONST %OPDIV_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F +CONST %OPDIV_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN +CONST %OPDIV_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG +CONST %OPDIV_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G +;opSDIV +CONST %OPSDIV_STEP = 20 + %DIVARITH_STEP + %ABS_STEP*2 +CONST %OPSDIV_CNT_BINARY = 1 + %DIVARITH_CNT_BINARY + %ABS_CNT_BINARY*2 +CONST %OPSDIV_CNT_ARITH = 0 + %DIVARITH_CNT_ARITH + %ABS_CNT_ARITH*2 +CONST %OPSDIV_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F + %ABS_CNT_KECCAK_F*2 +CONST %OPSDIV_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN + %ABS_CNT_MEM_ALIGN*2 +CONST %OPSDIV_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG + %ABS_CNT_PADDING_PG*2 +CONST %OPSDIV_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G + %ABS_CNT_POSEIDON_G*2 +;opMOD +CONST %OPMOD_STEP = 20 + %DIVARITH_STEP +CONST %OPMOD_CNT_BINARY = 0 + %DIVARITH_CNT_BINARY +CONST %OPMOD_CNT_ARITH = 0 + %DIVARITH_CNT_ARITH +CONST %OPMOD_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F +CONST %OPMOD_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN +CONST %OPMOD_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG +CONST %OPMOD_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G +;opSMOD +CONST %OPSMOD_STEP = 20 + %DIVARITH_STEP + %ABS_STEP*2 +CONST %OPSMOD_CNT_BINARY = 1 + %DIVARITH_CNT_BINARY + %ABS_CNT_BINARY*2 +CONST %OPSMOD_CNT_ARITH = 0 + %DIVARITH_CNT_ARITH + %ABS_CNT_ARITH*2 +CONST %OPSMOD_CNT_KECCAK_F = 0 + %DIVARITH_CNT_KECCAK_F + %ABS_CNT_KECCAK_F*2 +CONST %OPSMOD_CNT_MEM_ALIGN = 0 + %DIVARITH_CNT_MEM_ALIGN + %ABS_CNT_MEM_ALIGN*2 +CONST %OPSMOD_CNT_PADDING_PG = 0 + %DIVARITH_CNT_PADDING_PG + %ABS_CNT_PADDING_PG*2 +CONST %OPSMOD_CNT_POSEIDON_G = 0 + %DIVARITH_CNT_POSEIDON_G + %ABS_CNT_POSEIDON_G*2 +;opADDMOD +CONST %OPADDMOD_STEP = 50 +CONST %OPADDMOD_CNT_BINARY = 3 +CONST %OPADDMOD_CNT_ARITH = 1 +CONST %OPADDMOD_CNT_KECCAK_F = 0 +CONST %OPADDMOD_CNT_MEM_ALIGN = 0 +CONST %OPADDMOD_CNT_PADDING_PG = 0 +CONST %OPADDMOD_CNT_POSEIDON_G = 0 +;opMULMOD +CONST %OPMULMOD_STEP = 120 + %MULARITH_STEP +CONST %OPMULMOD_CNT_BINARY = 3 + %MULARITH_CNT_BINARY +CONST %OPMULMOD_CNT_ARITH = 2 + %MULARITH_CNT_ARITH +CONST %OPMULMOD_CNT_KECCAK_F = 0 + %MULARITH_CNT_KECCAK_F +CONST %OPMULMOD_CNT_MEM_ALIGN = 0 + %MULARITH_CNT_MEM_ALIGN +CONST %OPMULMOD_CNT_PADDING_PG = 0 + %MULARITH_CNT_PADDING_PG +CONST %OPMULMOD_CNT_POSEIDON_G = 0 + %MULARITH_CNT_POSEIDON_G +;opEXP +CONST %OPEXP_STEP = 20 + %EXPAD_STEP + %GETLENBYTES_STEP +CONST %OPEXP_CNT_BINARY = 0 + %EXPAD_CNT_BINARY + %GETLENBYTES_CNT_BINARY +CONST %OPEXP_CNT_ARITH = 0 + %EXPAD_CNT_ARITH + %GETLENBYTES_CNT_ARITH +CONST %OPEXP_CNT_KECCAK_F = 0 + %EXPAD_CNT_KECCAK_F + %GETLENBYTES_CNT_KECCAK_F +CONST %OPEXP_CNT_MEM_ALIGN = 0 + %EXPAD_CNT_MEM_ALIGN + %GETLENBYTES_CNT_MEM_ALIGN +CONST %OPEXP_CNT_PADDING_PG = 0 + %EXPAD_CNT_PADDING_PG + %GETLENBYTES_CNT_PADDING_PG +CONST %OPEXP_CNT_POSEIDON_G = 0 + %EXPAD_CNT_POSEIDON_G + %GETLENBYTES_CNT_POSEIDON_G +; opSIGNEXTEND +CONST %OPSIGNEXTEND_STEP = 100 +CONST %OPSIGNEXTEND_CNT_BINARY = 6 +CONST %OPSIGNEXTEND_CNT_ARITH = 0 +CONST %OPSIGNEXTEND_CNT_KECCAK_F = 0 +CONST %OPSIGNEXTEND_CNT_MEM_ALIGN = 0 +CONST %OPSIGNEXTEND_CNT_PADDING_PG = 0 +CONST %OPSIGNEXTEND_CNT_POSEIDON_G = 0 +; MSTORE32 +CONST %MSTORE32_STEP = 50 + %OFFSETUTIL_STEP +CONST %MSTORE32_CNT_BINARY = 1 + %OFFSETUTIL_CNT_BINARY +CONST %MSTORE32_CNT_ARITH = 0 + %OFFSETUTIL_CNT_ARITH +CONST %MSTORE32_CNT_KECCAK_F = 0 + %OFFSETUTIL_CNT_KECCAK_F +CONST %MSTORE32_CNT_MEM_ALIGN = 1 + %OFFSETUTIL_CNT_MEM_ALIGN +CONST %MSTORE32_CNT_PADDING_PG = 0 + %OFFSETUTIL_CNT_PADDING_PG +CONST %MSTORE32_CNT_POSEIDON_G = 0 + %OFFSETUTIL_CNT_POSEIDON_G +; MSTOREX +CONST %MSTOREX_STEP = 100 + %MSTORE32_STEP + %SHRARITH_STEP*3 + %SHLARITH_STEP*2 + %OFFSETUTIL_STEP +CONST %MSTOREX_CNT_BINARY = 0 + %MSTORE32_CNT_BINARY + %SHRARITH_CNT_BINARY*3 + %SHLARITH_CNT_BINARY*2 + %OFFSETUTIL_CNT_BINARY +CONST %MSTOREX_CNT_ARITH = 0 + %MSTORE32_CNT_ARITH + %SHRARITH_CNT_ARITH*3 + %SHLARITH_CNT_ARITH*2 + %OFFSETUTIL_CNT_ARITH +CONST %MSTOREX_CNT_KECCAK_F = 0 + %MSTORE32_CNT_KECCAK_F + %SHRARITH_CNT_KECCAK_F*3 + %SHLARITH_CNT_KECCAK_F*2 + %OFFSETUTIL_CNT_KECCAK_F +CONST %MSTOREX_CNT_MEM_ALIGN = 0 + %MSTORE32_CNT_MEM_ALIGN + %SHRARITH_CNT_MEM_ALIGN*3 + %SHLARITH_CNT_MEM_ALIGN*2 + %OFFSETUTIL_CNT_MEM_ALIGN +CONST %MSTOREX_CNT_PADDING_PG = 0 + %MSTORE32_CNT_PADDING_PG + %SHRARITH_CNT_PADDING_PG*3 + %SHLARITH_CNT_PADDING_PG*2 + %OFFSETUTIL_CNT_PADDING_PG +CONST %MSTOREX_CNT_POSEIDON_G = 0 + %MSTORE32_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G*3 + %SHLARITH_CNT_POSEIDON_G*2 + %OFFSETUTIL_CNT_POSEIDON_G +; isEmptyAccount +CONST %ISEMPTYACCOUNT_STEP = 50 +CONST %ISEMPTYACCOUNT_CNT_BINARY = 3 +CONST %ISEMPTYACCOUNT_CNT_ARITH = 0 +CONST %ISEMPTYACCOUNT_CNT_KECCAK_F = 0 +CONST %ISEMPTYACCOUNT_CNT_MEM_ALIGN = 0 +CONST %ISEMPTYACCOUNT_CNT_PADDING_PG = 0 +CONST %ISEMPTYACCOUNT_CNT_POSEIDON_G = 6 +; computeGasSendCall +CONST %COMPUTEGASSENDCALL_STEP = 30 +CONST %COMPUTEGASSENDCALL_CNT_BINARY = 2 +CONST %COMPUTEGASSENDCALL_CNT_ARITH = 0 +CONST %COMPUTEGASSENDCALL_CNT_KECCAK_F = 0 +CONST %COMPUTEGASSENDCALL_CNT_MEM_ALIGN = 0 +CONST %COMPUTEGASSENDCALL_CNT_PADDING_PG = 0 +CONST %COMPUTEGASSENDCALL_CNT_POSEIDON_G = 0 +; saveMem +CONST %SAVEMEM_STEP = 100 + %MULARITH_STEP + %DIVARITH_STEP +CONST %SAVEMEM_CNT_BINARY = 4 + %MULARITH_CNT_BINARY + %DIVARITH_CNT_BINARY +CONST %SAVEMEM_CNT_ARITH = 0 + %MULARITH_CNT_ARITH + %DIVARITH_CNT_ARITH +CONST %SAVEMEM_CNT_KECCAK_F = 0 + %MULARITH_CNT_KECCAK_F + %DIVARITH_CNT_KECCAK_F +CONST %SAVEMEM_CNT_MEM_ALIGN = 0 + %MULARITH_CNT_MEM_ALIGN + %DIVARITH_CNT_MEM_ALIGN +CONST %SAVEMEM_CNT_PADDING_PG = 0 + %MULARITH_CNT_PADDING_PG + %DIVARITH_CNT_PADDING_PG +CONST %SAVEMEM_CNT_POSEIDON_G = 0 + %MULARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G +;addBatchHash +CONST %ADDBATCHHASH_STEP = 10 + %SHRARITH_STEP +CONST %ADDBATCHHASH_CNT_BINARY = 0 + %SHRARITH_CNT_BINARY +CONST %ADDBATCHHASH_CNT_ARITH = 0 + %SHRARITH_CNT_ARITH +CONST %ADDBATCHHASH_CNT_KECCAK_F = 0 + %SHRARITH_CNT_KECCAK_F +CONST %ADDBATCHHASH_CNT_MEM_ALIGN = 0 + %SHRARITH_CNT_MEM_ALIGN +CONST %ADDBATCHHASH_CNT_PADDING_PG = 0 + %SHRARITH_CNT_PADDING_PG +CONST %ADDBATCHHASH_CNT_POSEIDON_G = 0 + %SHRARITH_CNT_POSEIDON_G + + +; %MLOADX_STEP - STEP:JMPN(failedCounters) +; %MLOADX_CNT_BINARY - CNT_BINARY :JMPN(failedCounters) +; %MLOADX_CNT_ARITH - CNT_ARITH :JMPN(failedCounters) +; %MLOADX_CNT_KECCAK_F - CNT_KECCAK_F :JMPN(failedCounters) +; %MLOADX_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPN(failedCounters) +; %MLOADX_CNT_PADDING_PG - CNT_PADDING_PG :JMPN(failedCounters) +; %MLOADX_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPN(failedCounters) \ No newline at end of file diff --git a/counters/endIncludes.zkasm b/counters/endIncludes.zkasm new file mode 100644 index 00000000..bc153c97 --- /dev/null +++ b/counters/endIncludes.zkasm @@ -0,0 +1,7 @@ +INCLUDE "../main/load-tx-rlp-utils.zkasm" +INCLUDE "../main/constants.zkasm" +INCLUDE "../main/2-exp.zkasm" +INCLUDE "../main/vars.zkasm" +INCLUDE "../main/utils.zkasm" +INCLUDE "../main/opcodes/calldata-returndata-code.zkasm" +INCLUDE "../main/opcodes/arithmetic.zkasm" \ No newline at end of file diff --git a/counters/initIncludes.zkasm b/counters/initIncludes.zkasm new file mode 100644 index 00000000..407a8a28 --- /dev/null +++ b/counters/initIncludes.zkasm @@ -0,0 +1 @@ +INCLUDE "countersConstants.zkasm" diff --git a/counters/tests/MLOAD32.zkasm b/counters/tests/MLOAD32.zkasm new file mode 100644 index 00000000..826bb024 --- /dev/null +++ b/counters/tests/MLOAD32.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 50 => E + :CALL(MLOAD32) + + %MLOAD32_STEP - STEP:JMPN(failedCounters) + %MLOAD32_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %MLOAD32_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %MLOAD32_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %MLOAD32_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %MLOAD32_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %MLOAD32_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/MLOADX.zkasm b/counters/tests/MLOADX.zkasm new file mode 100644 index 00000000..f5a8bca0 --- /dev/null +++ b/counters/tests/MLOADX.zkasm @@ -0,0 +1,30 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 50 => E + 31 => C + :CALL(MLOADX) + + %MLOADX_STEP - STEP :JMPN(failedCounters) + 7 - CNT_BINARY :JMPNZ(failedCounters) + ;%MLOADX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + 3 - CNT_ARITH :JMPNZ(failedCounters) + ;%MLOADX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %MLOADX_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %MLOADX_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %MLOADX_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %MLOADX_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/MSTORE32.zkasm b/counters/tests/MSTORE32.zkasm new file mode 100644 index 00000000..d6a1a633 --- /dev/null +++ b/counters/tests/MSTORE32.zkasm @@ -0,0 +1,32 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ; @info save value to memory 32 bytes with offset + ; @in bytesToStore => bytes to store in memory + ; @in E => offset + ; @out E => new offset + 0x3fc1363ea8654a6e41d29644fef3f756784774c3e315c9796e4d483969d1d5a6n :MSTORE(bytesToStore) + 50 => E + :CALL(MSTORE32) + + %MSTORE32_STEP - STEP:JMPN(failedCounters) + %MSTORE32_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %MSTORE32_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %MSTORE32_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %MSTORE32_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %MSTORE32_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %MSTORE32_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/MSTOREX.zkasm b/counters/tests/MSTOREX.zkasm new file mode 100644 index 00000000..61d359aa --- /dev/null +++ b/counters/tests/MSTOREX.zkasm @@ -0,0 +1,36 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ; @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 + 0x3fc1363ea8654a6e41d29644fef3f756784774c3e315c9796e4d483969d1d5a6n :MSTORE(bytesToStore) + 50 => E + 15 => C + :CALL(MSTOREX) + + %MSTOREX_STEP - STEP:JMPN(failedCounters) + 15 - CNT_BINARY :JMPNZ(failedCounters) + ;%MSTOREX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + 7 - CNT_ARITH :JMPNZ(failedCounters) + ;%MSTOREX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %MSTOREX_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %MSTOREX_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %MSTOREX_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %MSTOREX_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/SHLarith.zkasm b/counters/tests/SHLarith.zkasm new file mode 100644 index 00000000..c6e807ad --- /dev/null +++ b/counters/tests/SHLarith.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn => A + 1 => D + :CALL(SHLarith) + + %SHLARITH_STEP - STEP:JMPN(failedCounters) + %SHLARITH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %SHLARITH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %SHLARITH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %SHLARITH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %SHLARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %SHLARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/SHLarithBit.zkasm b/counters/tests/SHLarithBit.zkasm new file mode 100644 index 00000000..b5a69ce2 --- /dev/null +++ b/counters/tests/SHLarithBit.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xffffffff => A + 254 => D + :CALL(SHLarithBit) + + %SHLARITHBIT_STEP - STEP:JMPN(failedCounters) + %SHLARITHBIT_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %SHLARITHBIT_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %SHLARITHBIT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %SHLARITHBIT_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %SHLARITHBIT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %SHLARITHBIT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/SHRarith.zkasm b/counters/tests/SHRarith.zkasm new file mode 100644 index 00000000..795299c9 --- /dev/null +++ b/counters/tests/SHRarith.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xffffffff => A + 2 => D + :CALL(SHRarith) + + %SHRARITH_STEP - STEP:JMPN(failedCounters) + %SHRARITH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %SHRARITH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %SHRARITH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %SHRARITH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %SHRARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %SHRARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/SHRarithBit.zkasm b/counters/tests/SHRarithBit.zkasm new file mode 100644 index 00000000..8a9203d4 --- /dev/null +++ b/counters/tests/SHRarithBit.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xffffffff => A + 2 => D + :CALL(SHRarithBit) + + %SHRARITHBIT_STEP - STEP:JMPN(failedCounters) + %SHRARITHBIT_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %SHRARITHBIT_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %SHRARITHBIT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %SHRARITHBIT_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %SHRARITHBIT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %SHRARITHBIT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/abs.zkasm b/counters/tests/abs.zkasm new file mode 100644 index 00000000..85e0582e --- /dev/null +++ b/counters/tests/abs.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" +start: + 1000000 => GAS + +operation: + ; @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] + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn => A + :CALL(abs) + + %ABS_STEP - STEP:JMPN(failedCounters) + %ABS_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %ABS_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %ABS_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %ABS_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %ABS_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %ABS_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) +finalizeExecution: +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/addBatchHashByteByByte.zkasm b/counters/tests/addBatchHashByteByByte.zkasm new file mode 100644 index 00000000..83be5e69 --- /dev/null +++ b/counters/tests/addBatchHashByteByByte.zkasm @@ -0,0 +1,33 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ;@info: adds data to batchHashdata byte by byte + ;@in: A: bytes to add + ;@in D: bytes length + 12 => A + 80 => D + :CALL(addBatchHashByteByByte) + + %ADDBATCHHASH_STEP*D - STEP:JMPN(failedCounters) + 305 - CNT_BINARY :JMPNZ(failedCounters) + ;%ADDBATCHHASH_CNT_BINARY*D + D - CNT_BINARY :JMPNZ(failedCounters) + 81 - CNT_ARITH :JMPNZ(failedCounters) + ;%ADDBATCHHASH_CNT_ARITH*D - CNT_ARITH :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/computeGasSendCall.zkasm b/counters/tests/computeGasSendCall.zkasm new file mode 100644 index 00000000..5155a10b --- /dev/null +++ b/counters/tests/computeGasSendCall.zkasm @@ -0,0 +1,30 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ; @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) ) + 1000000 :MSTORE(gasCall) + :CALL(computeGasSendCall) + + %COMPUTEGASSENDCALL_STEP - STEP:JMPN(failedCounters) + %COMPUTEGASSENDCALL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %COMPUTEGASSENDCALL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %COMPUTEGASSENDCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %COMPUTEGASSENDCALL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %COMPUTEGASSENDCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %COMPUTEGASSENDCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/copySP.zkasm b/counters/tests/copySP.zkasm new file mode 100644 index 00000000..aa3a1939 --- /dev/null +++ b/counters/tests/copySP.zkasm @@ -0,0 +1,38 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + 1 => CTX +; TODO +operation: + 10 :MSTORE(argsOffsetCall) + 0x1005 :MSTORE(argsLengthCall) + CTX + 1 => CTX + 1 :MSTORE(originCTX) + :CALL(copySP) + + ; loop in SP + $ => C :MLOAD(argsLengthCall) + ${C/32} => C + ; copysp + stepsCopySPLoop + mload32*loop + mloadx + %COPYSP_STEP + 10*C + %MLOAD32_STEP*C - STEP :JMPN(failedCounters) + 263 - CNT_BINARY :JMPNZ(failedCounters) + ;%COPYSP_CNT_BINARY + %MLOAD32_CNT_BINARY*C - CNT_BINARY :JMPNZ(failedCounters) + 3 - CNT_ARITH :JMPNZ(failedCounters) + ;%COPYSP_CNT_ARITH + %MLOAD32_CNT_ARITH*C - CNT_ARITH :JMPNZ(failedCounters) + %COPYSP_CNT_KECCAK_F + %MLOAD32_CNT_KECCAK_F*C - CNT_KECCAK_F :JMPNZ(failedCounters) + 129 - CNT_MEM_ALIGN :JMPNZ(failedCounters) + ;%COPYSP_CNT_MEM_ALIGN + %MLOAD32_CNT_MEM_ALIGN*C - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %COPYSP_CNT_PADDING_PG + %MLOAD32_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) + %COPYSP_CNT_POSEIDON_G + %MLOAD32_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/divArith.zkasm b/counters/tests/divArith.zkasm new file mode 100644 index 00000000..35269a4d --- /dev/null +++ b/counters/tests/divArith.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 10 :MSTORE(arithA) + 2 :MSTORE(arithB) + :CALL(divARITH) + + %DIVARITH_STEP - STEP:JMPN(failedCounters) + %DIVARITH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %DIVARITH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %DIVARITH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %DIVARITH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %DIVARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %DIVARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) +finalizeExecution: +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/expAD.zkasm b/counters/tests/expAD.zkasm new file mode 100644 index 00000000..78a0874c --- /dev/null +++ b/counters/tests/expAD.zkasm @@ -0,0 +1,30 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 => A + 2 => D + :CALL(expAD) + + %EXPAD_STEP + 300 - STEP:JMPN(failedCounters) + 20 - CNT_BINARY :JMPNZ(failedCounters) + ;%EXPAD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + 5 - CNT_ARITH :JMPNZ(failedCounters) + ;%EXPAD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %EXPAD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %EXPAD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %EXPAD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %EXPAD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/getLenBits.zkasm b/counters/tests/getLenBits.zkasm new file mode 100644 index 00000000..e65363cf --- /dev/null +++ b/counters/tests/getLenBits.zkasm @@ -0,0 +1,30 @@ +INCLUDE "../initIncludes.zkasm" + +start: + ; @info bits length of B + ; @in B => number + ; @out A => bits length + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFn => B + 80 => C + :CALL(getLenBits) +%GETLENBITS_STEP + %DIVARITH_STEP*C - STEP:JMPN(failedCounters) +%GETLENBITS_CNT_BINARY + %DIVARITH_CNT_BINARY*C + C - CNT_BINARY :JMPNZ(failedCounters) +%GETLENBITS_CNT_ARITH + %DIVARITH_CNT_ARITH*C - 1 - CNT_ARITH :JMPNZ(failedCounters) +%GETLENBITS_CNT_KECCAK_F + %DIVARITH_CNT_KECCAK_F*C - CNT_KECCAK_F :JMPNZ(failedCounters) +%GETLENBITS_CNT_MEM_ALIGN + %DIVARITH_CNT_MEM_ALIGN*C - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%GETLENBITS_CNT_PADDING_PG + %DIVARITH_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) +%GETLENBITS_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/getLenBytes.zkasm b/counters/tests/getLenBytes.zkasm new file mode 100644 index 00000000..2e6caf0f --- /dev/null +++ b/counters/tests/getLenBytes.zkasm @@ -0,0 +1,32 @@ +INCLUDE "../initIncludes.zkasm" + +start: + ; @info byte length of B + ; @in B => number + ; @out A => bytes length + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFn => B + 19 => C + :CALL(getLenBytes) +%GETLENBYTES_STEP + %SHRARITH_STEP*C - STEP:JMPN(failedCounters) +%GETLENBYTES_CNT_BINARY + 60 - CNT_BINARY :JMPNZ(failedCounters) +;%GETLENBYTES_CNT_BINARY + %SHRARITH_CNT_BINARY*C + C + 1 - CNT_BINARY :JMPNZ(failedCounters) +%GETLENBYTES_CNT_ARITH + 19 - CNT_ARITH :JMPNZ(failedCounters) +;%GETLENBYTES_CNT_ARITH + %SHRARITH_CNT_ARITH*C - CNT_ARITH :JMPNZ(failedCounters) +%GETLENBYTES_CNT_KECCAK_F + %SHRARITH_CNT_KECCAK_F*C - CNT_KECCAK_F :JMPNZ(failedCounters) +%GETLENBYTES_CNT_MEM_ALIGN + %SHRARITH_CNT_MEM_ALIGN*C - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%GETLENBYTES_CNT_PADDING_PG + %SHRARITH_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) +%GETLENBYTES_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/isEmptyAccount.zkasm b/counters/tests/isEmptyAccount.zkasm new file mode 100644 index 00000000..fbb4999c --- /dev/null +++ b/counters/tests/isEmptyAccount.zkasm @@ -0,0 +1,30 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ; @info check account is empty ( balance == nonce == code == 0x ) + ; @in E => address + ; @out E => isEmpty => 1 = true, 0 = false + 56346543 => E + :CALL(isEmptyAccount) + + %ISEMPTYACCOUNT_STEP - STEP:JMPN(failedCounters) + %ISEMPTYACCOUNT_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %ISEMPTYACCOUNT_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %ISEMPTYACCOUNT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %ISEMPTYACCOUNT_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %ISEMPTYACCOUNT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %ISEMPTYACCOUNT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/counters/tests/mulARITH.zkasm b/counters/tests/mulARITH.zkasm new file mode 100644 index 00000000..2152e7a5 --- /dev/null +++ b/counters/tests/mulARITH.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 115792089237316195423570985008687907853269984665640564039457584007913129639936 :MSTORE(arithA) + 2 :MSTORE(arithB) + :CALL(mulARITH) + + %MULARITH_STEP - STEP:JMPN(failedCounters) + %MULARITH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %MULARITH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %MULARITH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %MULARITH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %MULARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %MULARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +finalizeExecution: +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/offsetUtil.zkasm b/counters/tests/offsetUtil.zkasm new file mode 100644 index 00000000..9cc17812 --- /dev/null +++ b/counters/tests/offsetUtil.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" +start: + 1000000 => GAS + +operation: + ;Get offset/32 & offset%32 + ;@in A offset + ;@out E offset/32 + ;@out C offset%32 + 3 => A + :CALL(offsetUtil) + + %OFFSETUTIL_STEP - STEP:JMPN(failedCounters) + %OFFSETUTIL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %OFFSETUTIL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %OFFSETUTIL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %OFFSETUTIL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %OFFSETUTIL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %OFFSETUTIL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) +finalizeExecution: +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opADDMOD.zkasm b/counters/tests/opADDMOD.zkasm new file mode 100644 index 00000000..f270d6be --- /dev/null +++ b/counters/tests/opADDMOD.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 10 :MSTORE(SP++) + 3 :MSTORE(SP++) + 2 :MSTORE(SP++) + :JMP(opADDMOD) +checkCounters: +%OPADDMOD_STEP - STEP:JMPN(failedCounters) +%OPADDMOD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPADDMOD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPADDMOD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPADDMOD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPADDMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPADDMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opAdd.zkasm b/counters/tests/opAdd.zkasm new file mode 100644 index 00000000..1f055b3e --- /dev/null +++ b/counters/tests/opAdd.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 2 :MSTORE(SP++) + :JMP(opADD) +checkCounters: +%OPADD_STEP - STEP:JMPN(failedCounters) +%OPADD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPADD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPADD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPADD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPADD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPADD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opDIV.zkasm b/counters/tests/opDIV.zkasm new file mode 100644 index 00000000..e3a07a9e --- /dev/null +++ b/counters/tests/opDIV.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 10 :MSTORE(SP++) + :JMP(opDIV) +checkCounters: +%OPDIV_STEP - STEP:JMPN(failedCounters) +%OPDIV_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPDIV_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPDIV_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPDIV_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPDIV_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPDIV_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opEXP.zkasm b/counters/tests/opEXP.zkasm new file mode 100644 index 00000000..a2188947 --- /dev/null +++ b/counters/tests/opEXP.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 2 :MSTORE(SP++) + :JMP(opEXP) +checkCounters: +400 - STEP:JMPN(failedCounters) +26 - CNT_BINARY :JMPNZ(failedCounters) +;%OPEXP_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_ARITH :JMPNZ(failedCounters) +;%OPEXP_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPEXP_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPEXP_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPEXP_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPEXP_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opMOD.zkasm b/counters/tests/opMOD.zkasm new file mode 100644 index 00000000..834ef195 --- /dev/null +++ b/counters/tests/opMOD.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 10 :MSTORE(SP++) + :JMP(opMOD) +checkCounters: +%OPMOD_STEP - STEP:JMPN(failedCounters) +%OPMOD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPMOD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPMOD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPMOD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opMUL.zkasm b/counters/tests/opMUL.zkasm new file mode 100644 index 00000000..6352c475 --- /dev/null +++ b/counters/tests/opMUL.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 2 :MSTORE(SP++) + :JMP(opMUL) +checkCounters: +%OPMUL_STEP - STEP:JMPN(failedCounters) +%OPMUL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPMUL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPMUL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPMUL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPMUL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPMUL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opMULMOD.zkasm b/counters/tests/opMULMOD.zkasm new file mode 100644 index 00000000..fd87e66d --- /dev/null +++ b/counters/tests/opMULMOD.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 10 :MSTORE(SP++) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(SP++) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(SP++) + :JMP(opMULMOD) +checkCounters: +%OPMULMOD_STEP - STEP:JMPN(failedCounters) +%OPMULMOD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPMULMOD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPMULMOD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPMULMOD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPMULMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPMULMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSDIV.zkasm b/counters/tests/opSDIV.zkasm new file mode 100644 index 00000000..e269b08f --- /dev/null +++ b/counters/tests/opSDIV.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(SP++) + 3 :MSTORE(SP++) + :JMP(opSDIV) +checkCounters: +%OPSDIV_STEP - STEP:JMPN(failedCounters) +7 - CNT_BINARY :JMPNZ(failedCounters) +;%OPSDIV_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSDIV_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPSDIV_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPSDIV_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSDIV_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPSDIV_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSIGNEXTEND.zkasm b/counters/tests/opSIGNEXTEND.zkasm new file mode 100644 index 00000000..8622f698 --- /dev/null +++ b/counters/tests/opSIGNEXTEND.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(SP++) + 10 :MSTORE(SP++) + :JMP(opSIGNEXTEND) +checkCounters: +%OPSIGNEXTEND_STEP - STEP:JMPN(failedCounters) +%OPSIGNEXTEND_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSIGNEXTEND_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPSIGNEXTEND_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPSIGNEXTEND_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSIGNEXTEND_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPSIGNEXTEND_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSMOD.zkasm b/counters/tests/opSMOD.zkasm new file mode 100644 index 00000000..8f537785 --- /dev/null +++ b/counters/tests/opSMOD.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(SP++) + 3 :MSTORE(SP++) + :JMP(opSMOD) +checkCounters: +%OPSMOD_STEP - STEP:JMPN(failedCounters) +6 - CNT_BINARY :JMPNZ(failedCounters) +;%OPSMOD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSMOD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPSMOD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPSMOD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPSMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSUB.zkasm b/counters/tests/opSUB.zkasm new file mode 100644 index 00000000..878c7d1b --- /dev/null +++ b/counters/tests/opSUB.zkasm @@ -0,0 +1,27 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 2 :MSTORE(SP++) + :CALL(opSUB) +checkCounters: +%OPSUB_STEP - STEP:JMPN(failedCounters) +%OPSUB_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSUB_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPSUB_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPSUB_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSUB_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPSUB_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/saveMem.zkasm b/counters/tests/saveMem.zkasm new file mode 100644 index 00000000..9f52f3c4 --- /dev/null +++ b/counters/tests/saveMem.zkasm @@ -0,0 +1,31 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + ; @info compute memory expansion gas cost + ; @in: lastMemOffset: offset to copy bytes + ; @in: lastMemLength: size of the bytes to copy + 10000 :MSTORE(lastMemOffset) + 10000 :MSTORE(lastMemLength) + :CALL(saveMem) + + %SAVEMEM_STEP - STEP:JMPN(failedCounters) + ;%SAVEMEM_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %SAVEMEM_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) + %SAVEMEM_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) + %SAVEMEM_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) + %SAVEMEM_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) + %SAVEMEM_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :RETURN +failedCounters: +2 => A +1 :ASSERT + +INCLUDE "../endIncludes.zkasm" diff --git a/main/load-tx-rlp-utils.zkasm b/main/load-tx-rlp-utils.zkasm index c840c197..08ba8125 100644 --- a/main/load-tx-rlp-utils.zkasm +++ b/main/load-tx-rlp-utils.zkasm @@ -25,4 +25,4 @@ addHashTx: ${getTxs(p,D)} => A $${p = p + D} A :HASHK(E) - C + D => C :RETURN + C + D => C :RETURN \ No newline at end of file diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 11531188..95feb90e 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -290,6 +290,8 @@ handleOOCBatRLP: $${eventLog(onError, OOCB)} :JMP(handleOOCatRLP) handleOOCKatRLP: $${eventLog(onError, OOCK)} :JMP(handleOOCatRLP) +handleOOCSatRLP: + $${eventLog(onError, OOCS)} :JMP(handleOOCatRLP) handleOOCatRLP: $ => SR :MLOAD(batchSR) diff --git a/main/opcodes/arithmetic.zkasm b/main/opcodes/arithmetic.zkasm index dbb531c2..33dd8100 100644 --- a/main/opcodes/arithmetic.zkasm +++ b/main/opcodes/arithmetic.zkasm @@ -10,8 +10,8 @@ */ opADD: ; checks zk-counters + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -38,8 +38,7 @@ opADD: */ opMUL: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -67,8 +66,8 @@ opMUL: */ opSUB: ; checks zk-counters + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) ; check out-of-gas @@ -91,9 +90,7 @@ opSUB: */ opDIV: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -121,9 +118,8 @@ opDIV: */ opSDIV: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 :JMPN(stackUnderflow) ; check out-of-gas @@ -135,13 +131,13 @@ opSDIV: B => C $ => A :MLOAD(SP), CALL(abs); [b => A]; in: [A: number] out: [A: |A|, B: Sign of A [1 if negative, 0 positive]] ; if is neg (different signs => C != B), jump to sdivneg - C - B :JMPNZ(opSDIVNeg) + C - B :JMPNZ(SDIVNeg) E :MSTORE(arithA) A :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => B :MLOAD(arithRes1) B :MSTORE(SP++), JMP(readCode); [a / b(B) => SP] -opSDIVNeg: +SDIVNeg: E :MSTORE(arithA) A :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => B :MLOAD(arithRes1) @@ -161,9 +157,7 @@ opSDIVNeg: */ opMOD: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) ; check out-of-gas @@ -189,9 +183,8 @@ opMOD: */ opSMOD: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 :JMPN(stackUnderflow) ; check out-of-gas @@ -226,9 +219,9 @@ opSMODNeg: */ opADDMOD: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 80 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) + %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) ; check out-of-gas GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas) ; check stack underflow @@ -277,7 +270,8 @@ zeroOneAddMod: * - stack output: [(a * b) % N] */ opMULMOD: - + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 3 :JMPN(stackUnderflow) ; check out-of-gas @@ -302,8 +296,7 @@ opMULMOD: opEXP: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -334,8 +327,8 @@ opEXP: */ opSIGNEXTEND: ; following this impl https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/vm/src/evm/opcodes/functions.ts#L193 ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 6 :JMPN(outOfCountersArith) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 :JMPN(stackUnderflow) ; check out-of-gas diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 9945535b..89cfe9cb 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -423,6 +423,7 @@ readByteCode: PC + 1 => PC :JMP(@mapping_opcodes + RR) readCode: + %MAX_CNT_STEPS - STEP - 500 :JMPN(outOfCountersStep) $ => A :MLOAD(isCreateContract), JMPNZ(readDeployBytecode, readByteCode) ;; Compute and save hash bytecode and bytecode length in the state-tree diff --git a/main/utils.zkasm b/main/utils.zkasm index 8576fe2b..e88df634 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -3,6 +3,9 @@ ; @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) @@ -20,6 +23,7 @@ VAR GLOBAL tmpZkPCcopy ; @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) @@ -30,12 +34,11 @@ copySP: $ => E :MLOAD(argsOffsetCall) $ => C :MLOAD(argsLengthCall) -copyInit: +copySPloop: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - C :JMPZ(copyEnd) - C - 32 :JMPN(copyFinal) + %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 @@ -44,9 +47,9 @@ copyInit: A :MSTORE(SP++) ; load previous context for next iteration $ => CTX :MLOAD(originCTX) - C - 32 => C :JMP(copyInit) + C - 32 => C :JMP(copySPloop) -copyFinal: +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 @@ -56,7 +59,7 @@ copyFinal: ; load previous context $ => CTX :MLOAD(originCTX) -copyEnd: +copySPEnd: ; restore RR to return to initial call correctly $ => RR :MLOAD(tmpZkPCcopy) $ => CTX :MLOAD(currentCTX), RETURN @@ -69,6 +72,8 @@ VAR GLOBAL tmpZkPCgetLen ; @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) @@ -80,12 +85,11 @@ getLenBytes: getLenBytesLoop: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary) - %MAX_CNT_ARITH - CNT_ARITH - 4 :JMPN(outOfCountersArith) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) 0 => B - ; if B is zero, finish counter + ; if A is zero, finish counter $ :EQ,JMPC(getLenEnd) ; right shift one byte 1 => D @@ -106,6 +110,8 @@ getLenEnd: ; @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) @@ -117,9 +123,8 @@ getLenBits: getLenBitsLoop: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary) - %MAX_CNT_ARITH - CNT_ARITH - 4 :JMPN(outOfCountersArith) + %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) @@ -155,6 +160,7 @@ VAR GLOBAL isMSTOREX ; @in C => length ; @out E => new offset MSTOREX: + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) 32 - C :JMPN(errorMLOADMSTORE) 32 - C - 1 :JMPN(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] 1 :MSTORE(isMSTOREX) @@ -164,6 +170,10 @@ MSTOREX: ; @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) @@ -278,6 +288,11 @@ VAR GLOBAL isMLOADX ; @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) ; TDDO Should be unreachable! check it 32 - C - 1 :JMPN(MLOAD32) 1 :MSTORE(isMLOADX) @@ -287,6 +302,10 @@ MLOADX: ; @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) @@ -347,6 +366,11 @@ VAR GLOBAL tmpVarDemptyAcc ; @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 - 255*3 :JMPN(outOfCountersPoseidon) + ; store current registries A :MSTORE(tmpVarAemptyAcc) B :MSTORE(tmpVarBemptyAcc) @@ -402,6 +426,9 @@ VAR GLOBAL tmpVarEcompGas ; @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) @@ -440,6 +467,9 @@ VAR GLOBAL tmpZkPCsaveMem ; @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 - 4 :JMPN(outOfCountersBinary) RR :MSTORE(tmpZkPCsaveMem) A :MSTORE(tmpVarAsaveMem) B :MSTORE(tmpVarBsaveMem) @@ -543,6 +573,9 @@ subARITH: ; @out: arithRes1: product of multiplication arithA * arithB ; @out: arithOverflow: 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) RR :MSTORE(tmpZkPCArith), CALL(storeTmp) $ => A :MLOAD(arithA) @@ -563,6 +596,11 @@ mulARITH: ; @out: arithRes1: quotien 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) @@ -633,6 +671,11 @@ VAR GLOBAL result ;@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) @@ -655,6 +698,9 @@ SHRarith: ;@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) @@ -693,6 +739,10 @@ SHRarithfinal: ;@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) @@ -711,6 +761,10 @@ SHLarith: E => A :JMP(SHLarithinit) SHLarithBit: + ; check zk-counters + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) + %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) RR :MSTORE(tmpSHXZkPC2) B :MSTORE(tmpVarBSHX) C :MSTORE(tmpVarCSHX) @@ -731,7 +785,7 @@ SHLarithinit: D => RR ; A init number and calculate B = 2**D - E => A :CALL(@exp_num + RR) + E => A :CALL(@exp_num + RR); out:[B: 2**RR] ; E = init number * 2**D (result) ${A*B} => E E :MSTORE(result) @@ -813,6 +867,8 @@ 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 @@ -849,6 +905,8 @@ VAR GLOBAL tmpVarBoffsetUtil ;@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(tmpVarAoffsetUtil) ${A >> 5} => E @@ -870,57 +928,60 @@ moveBalances: ;;;;;;;; ; evmCALL (Move Balances) ;;;;;;;; - ;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) - ; set key for smt balance query - %SMT_KEY_BALANCE => B - ; Balance in A - $ => A :SLOAD - $ => B :MLOAD(txValue) - $ => D :ADD - $ => A :MLOAD(storageAddr) - ; set key for smt balance query - %SMT_KEY_BALANCE => B - 0 => C - $ => SR :SSTORE + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*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) + ; set key for smt balance query + %SMT_KEY_BALANCE => B + ; Balance in A + $ => A :SLOAD + $ => B :MLOAD(txValue) + $ => D :ADD + $ => A :MLOAD(storageAddr) + ; set key for smt balance query + %SMT_KEY_BALANCE => B + 0 => C + $ => SR :SSTORE endMoveBalances: - :RETURN + :RETURN invalidCall: - ; return to origin context - $ => SR :MLOAD(initSR) - $ => A :MLOAD(originCTX) - 0 => B - ; if last context, finish tx - $ :EQ,JMPC(handleGas) - A => CTX - 0 :MSTORE(retDataCTX) - CTX :MSTORE(currentCTX) - $ => A :MLOAD(gasCTX) - GAS + A => GAS - $ => SP :MLOAD(lastSP) - $ => PC :MLOAD(lastPC) - 0 :MSTORE(SP++), JMP(readCode) + ; return to origin context + $ => SR :MLOAD(initSR) + $ => A :MLOAD(originCTX) + 0 => B + ; if last context, finish tx + $ :EQ,JMPC(handleGas) + A => CTX + 0 :MSTORE(retDataCTX) + CTX :MSTORE(currentCTX) + $ => A :MLOAD(gasCTX) + GAS + A => GAS + $ => SP :MLOAD(lastSP) + $ => PC :MLOAD(lastPC) + 0 :MSTORE(SP++), JMP(readCode) VAR GLOBAL pushBytes VAR GLOBAL numBlocks @@ -932,6 +993,8 @@ VAR GLOBAL accumulator ; @in D => bytes to read ; @out E => value read readPush: + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) D :MSTORE(pushBytes) D => A 0 :MSTORE(accumulator) @@ -947,6 +1010,8 @@ readPush: 0 => B :JMP(readPushBlock) readPushBlock: + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) $ => A :MLOAD(numBlocks) $ :EQ, JMPC(endPushInit) ; load hash contract identifier @@ -971,7 +1036,7 @@ doRotate: B - 1 => A :JMP(doRotateLoop) doRotateLoop: - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) A :JMPN(endRotate) ROTL_C => C A - 1 => A :JMP(doRotateLoop) @@ -988,7 +1053,7 @@ endPushInit: 0 => B :JMP(endPushLoop) endPushLoop: - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) $ => A :MLOAD(leftBytes) @@ -999,6 +1064,7 @@ endPushLoop: B - 1 => A :JMP(computeFactorLoop) computeFactorLoop: + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) A :JMPN(computeFactorEnd) 256*D => D A - 1 => A :JMP(computeFactorLoop) @@ -1014,7 +1080,7 @@ doRotate2: B - 1 => A :JMP(doRotateLoop2) doRotateLoop2: - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) A :JMPN(endRotate2) ROTL_C => C A - 1 => A :JMP(doRotateLoop2) @@ -1035,13 +1101,15 @@ VAR GLOBAL auxBytes ;@in: A: bytes to add ;@in D: bytes length addBatchHashByteByByte: - %MAX_CNT_BINARY - CNT_BINARY - 200 :JMPN(handleOOCBatRLP) + %MAX_CNT_STEPS - STEP - 10 :JMPN(handleOOCSatRLP) RR :MSTORE(tmpZkPCaddB) A :MSTORE(auxBytes) D :MSTORE(tmpVarDaddB) 1 => D -utilsAddBatchHashBytebyByte: +utilsAddBatchHashBytebyByteLoop: + %MAX_CNT_STEPS - STEP - 50 :JMPN(handleOOCSatRLP) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(handleOOCBatRLP) 32 - D => D $ => A :MLOAD(auxBytes), CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result] ; get last byte in A @@ -1055,7 +1123,7 @@ utilsAddBatchHashBytebyByte: ; D + 1 => D we set 33 in stead of 32 to earn 1 step 33 - D => D $ => B :MLOAD(tmpVarDaddB) - D - B - 1 :JMPN(utilsAddBatchHashBytebyByte) + D - B - 1 :JMPN(utilsAddBatchHashBytebyByteLoop) $ => RR :MLOAD(tmpZkPCaddB) :RETURN @@ -1070,6 +1138,7 @@ VAR GLOBAL tmpZkPCEF ; @internalParam {memOffset} memory offset to read bytes from ; @internalParam {startsWithEF} flag to indicate if first deployed byte on the bytecode is 0xEF checkBytecodeStartsEF: + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) ; save temporary registers A :MSTORE(tmpVarAEF) B :MSTORE(tmpVarBEF) @@ -1110,6 +1179,8 @@ VAR GLOBAL memSizeLinearPoseidon ; @internalParam {memSize} memory size to read bytes from ; @out D => resulting linear poseidon hashPoseidonLinearFromMemory: + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) A :MSTORE(tmpVarAhashP) B :MSTORE(tmpVarBhashP) C :MSTORE(tmpVarChashP) @@ -1135,8 +1206,7 @@ hashPoseidonLinearFromMemory: 0 => HASHPOS hashPoseidonLoop: - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) C :JMPZ(hashPoseidonEnd) C - 32 :JMPN(hashPoseidonFinal) @@ -1172,6 +1242,8 @@ VAR GLOBAL tmpVarBmask ; @in A => address not masked ; @out A => masked address maskAddress: + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) B :MSTORE(tmpVarBmask) 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn => B $ => A :AND @@ -1182,7 +1254,9 @@ updateSystemData: ; check keccak counters $ => A :MLOAD(cntKeccakPreProcess) %MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1:JMPN(outOfCountersKeccak) - + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; Get last tx count %LAST_TX_STORAGE_POS => C %ADDRESS_SYSTEM => A @@ -1218,9 +1292,9 @@ VAR GLOBAL tmpZkPCmulmod ; @out C utilMULMOD: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 3 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) + %MAX_CNT_ARITH - CNT_ARITH - 2 :JMPN(outOfCountersArith) RR :MSTORE(tmpZkPCmulmod) A :MSTORE(tmpVarAmulmod) @@ -1329,6 +1403,8 @@ VAR GLOBAL tmpZkPCexp ;@in A, D => A^D ;@out A => result expAD: + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) RR :MSTORE(tmpZkPCexp) B :MSTORE(tmpVarBexp) C :MSTORE(tmpVarCexp) @@ -1352,9 +1428,8 @@ expAD: ; - 3 ARITH: 1 in divARITH + 1 in 2*mulARITH ; - 5 BINARIES: 5 in divARITH ; - 100 steps (rounded up): 37 in divARITH + 11 in 2*mulARITH + 18 self - %MAX_CNT_ARITH - CNT_ARITH - 3*A :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 5*A :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100*A :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2*A :JMPN(outOfCountersBinary) + %MAX_CNT_STEPS - STEP - 50*A :JMPN(outOfCountersStep) expADloop: ;A exp diff --git a/package.json b/package.json index 5d50ac24..ad758874 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "test:utils": "mkdir -p build && npx zkasm test/utils-getLenBytes.zkasm -o build/rom-utils.test.json", "test:rotate": "mkdir -p build && npx zkasm test/rotate.zkasm -o build/rotate.test.json", "test:touched": "mkdir -p build && npx zkasm test/touched-assert.zkasm -o build/touched.test.json", + "test:counters": "node counters/counters-executor.js", "eslint": "npx eslint tools/**.js", "eslint:fix": "npx eslint tools/**.js --fix", "test:gen": "node tools/gen-parallel-tests.js", From 6c287500c4823ca75ea4a298df9551466db948e0 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Tue, 10 Jan 2023 17:12:56 +0100 Subject: [PATCH 10/60] calldata return code counter tests --- counters/countersConstants.zkasm | 48 ++++++++++++++++ counters/endIncludes.zkasm | 12 +++- counters/tests/opBLOCKHASH.zkasm | 29 ++++++++++ counters/tests/opCALLDATACOPY.zkasm | 28 ++++++++++ counters/tests/opCALLDATALOAD.zkasm | 29 ++++++++++ counters/tests/opCODECOPY.zkasm | 28 ++++++++++ counters/tests/opEXTCODECOPY.zkasm | 29 ++++++++++ counters/tests/opRETURNDATACOPY.zkasm | 34 +++++++++++ main/opcodes/block.zkasm | 14 ++--- main/opcodes/calldata-returndata-code.zkasm | 62 ++++++++------------- main/opcodes/comparison.zkasm | 18 ++---- main/opcodes/context-information.zkasm | 18 +++--- main/opcodes/create-terminate-context.zkasm | 2 + 13 files changed, 284 insertions(+), 67 deletions(-) create mode 100644 counters/tests/opBLOCKHASH.zkasm create mode 100644 counters/tests/opCALLDATACOPY.zkasm create mode 100644 counters/tests/opCALLDATALOAD.zkasm create mode 100644 counters/tests/opCODECOPY.zkasm create mode 100644 counters/tests/opEXTCODECOPY.zkasm create mode 100644 counters/tests/opRETURNDATACOPY.zkasm diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 4644031a..94545d3f 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -198,6 +198,54 @@ CONST %OPSIGNEXTEND_CNT_KECCAK_F = 0 CONST %OPSIGNEXTEND_CNT_MEM_ALIGN = 0 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_ARITH = 0 +CONST %OPBLOCKHASH_CNT_KECCAK_F = 1 +CONST %OPBLOCKHASH_CNT_MEM_ALIGN = 0 +CONST %OPBLOCKHASH_CNT_PADDING_PG = 0 +CONST %OPBLOCKHASH_CNT_POSEIDON_G = 2 +; opCALLDATALOAD +CONST %OPCALLDATALOAD_STEP = 100 + %SHLARITH_STEP + %SHRARITH_STEP + %DIVARITH_STEP +CONST %OPCALLDATALOAD_CNT_BINARY = 1 + %SHLARITH_CNT_BINARY + %SHRARITH_CNT_BINARY + %DIVARITH_CNT_BINARY +CONST %OPCALLDATALOAD_CNT_ARITH = 0 + %SHLARITH_CNT_ARITH + %SHRARITH_CNT_ARITH + %DIVARITH_CNT_ARITH +CONST %OPCALLDATALOAD_CNT_KECCAK_F = 0 + %SHLARITH_CNT_KECCAK_F + %SHRARITH_CNT_KECCAK_F + %DIVARITH_CNT_KECCAK_F +CONST %OPCALLDATALOAD_CNT_MEM_ALIGN = 0 + %SHLARITH_CNT_MEM_ALIGN + %SHRARITH_CNT_MEM_ALIGN + %DIVARITH_CNT_MEM_ALIGN +CONST %OPCALLDATALOAD_CNT_PADDING_PG = 0 + %SHLARITH_CNT_PADDING_PG + %SHRARITH_CNT_PADDING_PG + %DIVARITH_CNT_PADDING_PG +CONST %OPCALLDATALOAD_CNT_POSEIDON_G = 0 + %SHLARITH_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G +; opCALLDATACOPY - COMPLEX - hardcoded values at test +CONST %OPCALLDATACOPY_STEP = 100 +CONST %OPCALLDATACOPY_CNT_BINARY = 2 +CONST %OPCALLDATACOPY_CNT_ARITH = 0 +CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0 +CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 0 +CONST %OPCALLDATACOPY_CNT_PADDING_PG = 0 +CONST %OPCALLDATACOPY_CNT_POSEIDON_G = 0 +; opRETURNDATACOPY - COMPLEX - hardcoded values at test +CONST %OPRETURNDATACOPY_STEP = 100 +CONST %OPRETURNDATACOPY_CNT_BINARY = 2 +CONST %OPRETURNDATACOPY_CNT_ARITH = 0 +CONST %OPRETURNDATACOPY_CNT_KECCAK_F = 0 +CONST %OPRETURNDATACOPY_CNT_MEM_ALIGN = 0 +CONST %OPRETURNDATACOPY_CNT_PADDING_PG = 0 +CONST %OPRETURNDATACOPY_CNT_POSEIDON_G = 0 +; opCODECOPY - COMPLEX - hardcoded values at test +CONST %OPCODECOPY_STEP = 100 +CONST %OPCODECOPY_CNT_BINARY = 0 +CONST %OPCODECOPY_CNT_ARITH = 0 +CONST %OPCODECOPY_CNT_KECCAK_F = 0 +CONST %OPCODECOPY_CNT_MEM_ALIGN = 0 +CONST %OPCODECOPY_CNT_PADDING_PG = 0 +CONST %OPCODECOPY_CNT_POSEIDON_G = 0 +; opEXTCODECOPY - COMPLEX - hardcoded values at test +CONST %OPEXTCODECOPY_STEP = 100 +CONST %OPEXTCODECOPY_CNT_BINARY = 0 +CONST %OPEXTCODECOPY_CNT_ARITH = 0 +CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0 +CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 0 +CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0 +CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 0 ; MSTORE32 CONST %MSTORE32_STEP = 50 + %OFFSETUTIL_STEP CONST %MSTORE32_CNT_BINARY = 1 + %OFFSETUTIL_CNT_BINARY diff --git a/counters/endIncludes.zkasm b/counters/endIncludes.zkasm index bc153c97..1752257d 100644 --- a/counters/endIncludes.zkasm +++ b/counters/endIncludes.zkasm @@ -4,4 +4,14 @@ INCLUDE "../main/2-exp.zkasm" INCLUDE "../main/vars.zkasm" INCLUDE "../main/utils.zkasm" INCLUDE "../main/opcodes/calldata-returndata-code.zkasm" -INCLUDE "../main/opcodes/arithmetic.zkasm" \ No newline at end of file +INCLUDE "../main/opcodes/arithmetic.zkasm" +INCLUDE "../main/opcodes/block.zkasm" +INCLUDE "../main/opcodes/comparison.zkasm" +INCLUDE "../main/opcodes/context-information.zkasm" +INCLUDE "../main/opcodes/create-terminate-context.zkasm" +INCLUDE "../main/opcodes/crypto.zkasm" +INCLUDE "../main/opcodes/flow-control.zkasm" +INCLUDE "../main/opcodes/logs.zkasm" +INCLUDE "../main/opcodes/stack-operations.zkasm" +INCLUDE "../main/opcodes/storage-memory.zkasm" +INCLUDE "../main/touched.zkasm" \ No newline at end of file diff --git a/counters/tests/opBLOCKHASH.zkasm b/counters/tests/opBLOCKHASH.zkasm new file mode 100644 index 00000000..170e43f4 --- /dev/null +++ b/counters/tests/opBLOCKHASH.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => D + 2 :HASHK(0) + 10 :MSTORE(txCount) + 1 :MSTORE(SP++) + :JMP(opBLOCKHASH) +checkCounters: +%OPBLOCKHASH_STEP - STEP:JMPN(failedCounters) +%OPBLOCKHASH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPBLOCKHASH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPBLOCKHASH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPBLOCKHASH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPBLOCKHASH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPBLOCKHASH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCALLDATACOPY.zkasm b/counters/tests/opCALLDATACOPY.zkasm new file mode 100644 index 00000000..b73c57f8 --- /dev/null +++ b/counters/tests/opCALLDATACOPY.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 432543 :MSTORE(SP++) + :JMP(opCALLDATACOPY) +checkCounters: +500 - STEP:JMPN(failedCounters) +24 - CNT_BINARY :JMPNZ(failedCounters) +10 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCALLDATALOAD.zkasm b/counters/tests/opCALLDATALOAD.zkasm new file mode 100644 index 00000000..94464d53 --- /dev/null +++ b/counters/tests/opCALLDATALOAD.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 128 :MSTORE(txCalldataLen) + 1 :MSTORE(SP++) + :JMP(opCALLDATALOAD) +checkCounters: +%OPCALLDATALOAD_STEP - STEP:JMPN(failedCounters) +8 - CNT_BINARY :JMPNZ(failedCounters) +;%OPCALLDATALOAD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +3 - CNT_ARITH :JMPNZ(failedCounters) +;%OPCALLDATALOAD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCODECOPY.zkasm b/counters/tests/opCODECOPY.zkasm new file mode 100644 index 00000000..8438fc47 --- /dev/null +++ b/counters/tests/opCODECOPY.zkasm @@ -0,0 +1,28 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 432543 :MSTORE(SP++) + :JMP(opCODECOPY) +checkCounters: +1800 - STEP:JMPN(failedCounters) +97 - CNT_BINARY :JMPNZ(failedCounters) +4 - CNT_ARITH :JMPNZ(failedCounters) +%OPCODECOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +43 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPCODECOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opEXTCODECOPY.zkasm b/counters/tests/opEXTCODECOPY.zkasm new file mode 100644 index 00000000..d769c84c --- /dev/null +++ b/counters/tests/opEXTCODECOPY.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 432543 :MSTORE(SP++) + 43 :MSTORE(SP++) + :JMP(opEXTCODECOPY) +checkCounters: +2000 - STEP:JMPN(failedCounters) +102 - CNT_BINARY :JMPNZ(failedCounters) +4 - CNT_ARITH :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +43 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +8 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opRETURNDATACOPY.zkasm b/counters/tests/opRETURNDATACOPY.zkasm new file mode 100644 index 00000000..96f710e3 --- /dev/null +++ b/counters/tests/opRETURNDATACOPY.zkasm @@ -0,0 +1,34 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + 10000 :MSTORE(retDataLength) + 5 :MSTORE(retDataOffset) + 2 => CTX + 1 :MSTORE(originCTX) + 1 :MSTORE(retDataCTX) + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 4000 :MSTORE(SP++) + :JMP(opRETURNDATACOPY) +checkCounters: +1000 - STEP:JMPN(failedCounters) +35 - CNT_BINARY :JMPNZ(failedCounters) +13 - CNT_ARITH :JMPNZ(failedCounters) +%OPRETURNDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPRETURNDATACOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPRETURNDATACOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/main/opcodes/block.zkasm b/main/opcodes/block.zkasm index 324dafff..a9d23e84 100644 --- a/main/opcodes/block.zkasm +++ b/main/opcodes/block.zkasm @@ -61,7 +61,7 @@ opBLOCKHASHzero: */ opCOINBASE: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -81,7 +81,7 @@ opCOINBASE: */ opTIMESTAMP: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -100,7 +100,7 @@ opTIMESTAMP: */ opNUMBER: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -120,7 +120,7 @@ opNUMBER: */ opDIFFICULTY: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -140,7 +140,7 @@ opDIFFICULTY: */ opGASLIMIT: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -160,7 +160,7 @@ opGASLIMIT: */ opCHAINID: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -179,7 +179,7 @@ opCHAINID: */ opBASEFEE: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 3c9fc357..1ef0b31e 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -11,8 +11,7 @@ */ opCALLDATALOAD: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 64 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 66 :JMPN(outOfCountersBinary) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) @@ -93,12 +92,10 @@ opCALLDATASIZEdep: */ opCALLDATACOPY: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 192 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; check stack underflow - SP - 3 => SP :JMPN(stackUnderflow) + SP - 3 => SP :JMPN(stackUnderflow) $ => E :MLOAD(SP+2); [destOffset => E] $ => B :MLOAD(SP+1); [offset => B] $ => C :MLOAD(SP); [size => C] @@ -128,17 +125,14 @@ opCALLDATACOPY: $ :LT,JMPC(opCALLDATACOPY0) A + C => A ; if offset + size is lower then calldata size => length - $ :LT,JMPC(opCALLDATACOPYX0, opCALLDATACOPYinit) + $ :LT,JMPC(opCALLDATACOPYX0, opCALLDATACOPYloop) opCALLDATACOPYX0: $ => C :MLOAD(txCalldataLen) -opCALLDATACOPYinit: +opCALLDATACOPYloop: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 192 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign) - %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 300 :JMPN(outOfCountersStep) ; finish loop C :JMPZ(opCALLDATACOPYcheckLen) ; copy last bytes @@ -149,7 +143,7 @@ opCALLDATACOPYinit: $ => A :MLOAD(arithRes1) A :MSTORE(arithA) ; add %CALLDATA_OFFSET to offset to reach calldata in memory - %CALLDATA_OFFSET :MSTORE(arithB), CALL(addARITH); in: [arithA, arithB] out[arithRes1: arithA*arithB] + %CALLDATA_OFFSET :MSTORE(arithB), CALL(addARITH); in: [arithA, arithB] out[arithRes1: arithA*arithB] $ => A :MLOAD(arithRes1) ; set stack pointer to first byte to read A => SP @@ -169,7 +163,7 @@ opCALLDATACOPYinit: $ => C :MLOAD(SP); [size => C] C - 32 => C C :MSTORE(SP); [size => SP] - B + 32 => B :JMP(opCALLDATACOPYinit) + B + 32 => B :JMP(opCALLDATACOPYloop) opCALLDATACOPYfinal: ; copy last bytes @@ -238,8 +232,8 @@ opCALLDATACOPYend: */ opCODESIZE: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 252 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) + %MAX_CNT_STEPS - STEP - 30 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; if is create, get size from calldata @@ -278,6 +272,8 @@ VAR GLOBAL memInteger * - stack output: [] */ opCODECOPY: + ;Check counters + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) ; if is a create, copy from calldata $ => A :MLOAD(isCreateContract), JMPNZ(opCALLDATACOPY) ; check stack underflow @@ -290,10 +286,6 @@ opCODECOPY: ; store lastMemLength for memory expansion gas cost E :MSTORE(lastMemLength) - ;Check counters - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) ;${3*((E+31)/32)} @@ -333,7 +325,7 @@ opCODECOPY2: %MAX_CNT_STEPS - STEP - 100*E :JMPN(outOfCountersStep) %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - E :JMPN(outOfCountersMemalign) -opCODECOPYinit: +opCODECOPYloop: %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) @@ -354,7 +346,7 @@ opCODECOPYinit: B :MSTORE(remainingBytes) $ => A :MLOAD(memOffset) A + 1 => A ; increment offset to write the next byte - A :MSTORE(memOffset), JMP(opCODECOPYinit) + A :MSTORE(memOffset), JMP(opCODECOPYloop) ; @info Load 0 if read bytecode position is above bytecode length @@ -382,8 +374,8 @@ readValueBytecode: */ opEXTCODESIZE: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) $ => A :MLOAD(SP), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address] @@ -418,11 +410,7 @@ VAR GLOBAL tmpContractLength */ opEXTCODECOPY: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_PADDING_PG - CNT_PADDING_PG - 11 :JMPN(outOfCountersPadding) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 300 :JMPN(outOfCountersStep) ; check stack underflow SP - 4 => SP :JMPN(stackUnderflow) @@ -473,10 +461,11 @@ opEXTCODECOPY2: D :MSTORE(codecopyHashId) ; set hashId to get bytes from B :MSTORE(codecopyBytecodeLength) ; set contract bytecode length C :MSTORE(memOffset) - E :MSTORE(remainingBytes), JMP(opCODECOPYinit) + E :MSTORE(remainingBytes), JMP(opCODECOPYloop) VAR GLOBAL tmpZkPCext opEXTCODECOPYLoadBytecode: + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) ; set key for smt smart contract length query %SMT_KEY_SC_LENGTH => B 0 => C @@ -495,7 +484,7 @@ opEXTCODECOPYLoadBytecode: 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => RR :MLOAD(tmpZkPCext) $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 => A $ :LT, JMPC(outOfCountersPoseidon) ; set key for smt smart contract code query @@ -526,7 +515,7 @@ opEXTCODECOPYCheckHashEnd: opRETURNDATASIZE: ; checks zk-counters %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -559,11 +548,8 @@ endOpRETURNDATASIZE: */ opRETURNDATACOPY: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; check stack underflow SP - 3 => SP :JMPN(stackUnderflow) ; check out-of-gas @@ -608,7 +594,7 @@ opRETURNDATACOPY: ; check out-of-gas GAS - A => GAS :JMPN(outOfGas) -opRETURNDATACOPYinit: +opRETURNDATACOPYloop: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) C :JMPZ(readCode) C - 32 :JMPN(opRETURNDATACOPYfinal) @@ -623,7 +609,7 @@ opRETURNDATACOPYinit: ; set bytesToStore with value to use in MSTORE A :MSTORE(bytesToStore), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] E => D - C - 32 => C :JMP(opRETURNDATACOPYinit) + C - 32 => C :JMP(opRETURNDATACOPYloop) opRETURNDATACOPYfinal: B => E @@ -653,8 +639,8 @@ opRETURNDATACOPYEmpty: */ opEXTCODEHASH: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) $ => A :MLOAD(SP), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address] diff --git a/main/opcodes/comparison.zkasm b/main/opcodes/comparison.zkasm index 729bdeb4..e5348a62 100644 --- a/main/opcodes/comparison.zkasm +++ b/main/opcodes/comparison.zkasm @@ -296,9 +296,8 @@ opNOT: */ opBYTE: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 2 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 30 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -338,9 +337,7 @@ opBYTE0: */ opSHR: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -367,9 +364,7 @@ opSHR: */ opSHL: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -398,9 +393,8 @@ opSHL: */ opSAR: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 9 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary) + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) diff --git a/main/opcodes/context-information.zkasm b/main/opcodes/context-information.zkasm index f48dc8fa..a4a6e5f7 100644 --- a/main/opcodes/context-information.zkasm +++ b/main/opcodes/context-information.zkasm @@ -8,7 +8,7 @@ */ opADDRESS: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; check is a create @@ -37,7 +37,7 @@ opADDRESSend: opSELFBALANCE: ; checks zk-counters %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_FAST_STEP => GAS :JMPN(outOfGas) @@ -61,8 +61,8 @@ opSELFBALANCE: */ opBALANCE: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 252 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check stack underflow SP - 1 :JMPN(stackUnderflow) ; Mask address to 20 bytes @@ -85,7 +85,7 @@ opBALANCE: */ opORIGIN: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -104,7 +104,7 @@ opORIGIN: */ opCALLER: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -123,7 +123,7 @@ opCALLER: */ opCALLVALUE: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -142,7 +142,7 @@ opCALLVALUE: */ opGASPRICE: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -161,7 +161,7 @@ opGASPRICE: */ opGAS: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) GAS :MSTORE(SP++); [gas(GAS) => SP] diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index aed458bd..16d7c79c 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -6,6 +6,8 @@ * - stack output: none */ opSTOP: + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check if first context ; if origin ctx is 0, end tx $ => B :MLOAD(originCTX), JMPZ(handleGas) From 68c10f138b0ad9d579a15457ab48a2224a66dd9e Mon Sep 17 00:00:00 2001 From: zkronos73 Date: Wed, 11 Jan 2023 11:36:02 +0000 Subject: [PATCH 11/60] fix FNEC_DIV_TWO constant and update test --- main/ecrecover/constEc.zkasm | 2 +- test/ecrecover.zkasm | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/main/ecrecover/constEc.zkasm b/main/ecrecover/constEc.zkasm index f17f43a2..b858190e 100644 --- a/main/ecrecover/constEc.zkasm +++ b/main/ecrecover/constEc.zkasm @@ -1,6 +1,6 @@ CONSTL %FPEC = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2Fn CONSTL %FPEC_MINUS_ONE = %FPEC - 1 -CONSTL %FNEC_DIV_TWO = 57896044618658097711785492504343953926418782139537452191302581570759080747169n +CONSTL %FNEC_DIV_TWO = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0n CONSTL %FPEC_C2_256 = 0x1000003D1n CONSTL %FPEC_NON_SQRT = (1n << 256n) - 1n diff --git a/test/ecrecover.zkasm b/test/ecrecover.zkasm index eb9f904a..354cdf51 100644 --- a/test/ecrecover.zkasm +++ b/test/ecrecover.zkasm @@ -329,15 +329,26 @@ repeat_ecrecover_test: :CALL(ecrecover_tx) 0x0000000000000000000000000000000000000000n :ASSERT - ; #34 s == field/2 + 1. Valid + ; #34 s == field/2 + 1. Valid (precompiled) 0x456e9aea5e197a1f1af7a3e85a3212fa4049a3ba34c2289b4c860fc0b0c64ef3n => A 0x9242685bf161793cc25603c231bc2f568eb630ea16aa137d2664ac8038825608n => B 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a1n => C 0x1cn => D - :CALL(ecrecover_tx) + :CALL(ecrecover_precompiled) 0x4ef445CADd8bEe8A02bc79b30A97e6Fe3AE3B7a3n :ASSERT + ; #34' s == field/2 + 1. Invalid (tx) + + 0x456e9aea5e197a1f1af7a3e85a3212fa4049a3ba34c2289b4c860fc0b0c64ef3n => A + 0x9242685bf161793cc25603c231bc2f568eb630ea16aa137d2664ac8038825608n => B + 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a1n => C + 0x1cn => D + :CALL(ecrecover_tx) + 0n :ASSERT + B => A + 4n :ASSERT + ; #35 s == field/2. Valid 0x456e9aea5e197a1f1af7a3e85a3212fa4049a3ba34c2289b4c860fc0b0c64ef3n => A From e8730ba8bee52f8ff7a8fcd8de777f38de53716e Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Wed, 11 Jan 2023 13:01:06 +0100 Subject: [PATCH 12/60] Finished tests + readme --- counters/README.md | 45 +++++++++++ counters/counters-executor.js | 5 +- counters/countersConstants.zkasm | 72 ++++++++++++++++++ counters/tests/opCALL.zkasm | 41 ++++++++++ counters/tests/opCALLCODE.zkasm | 41 ++++++++++ counters/tests/opCREATE.zkasm | 35 +++++++++ counters/tests/opCREATE2.zkasm | 35 +++++++++ counters/tests/opDELEGATECALL.zkasm | 35 +++++++++ counters/tests/opRETURN.zkasm | 32 ++++++++ counters/tests/opREVERT.zkasm | 32 ++++++++ counters/tests/opSHA3.zkasm | 29 +++++++ counters/tests/opSTATICCALL.zkasm | 35 +++++++++ main/opcodes/create-terminate-context.zkasm | 84 ++++++--------------- main/opcodes/crypto.zkasm | 7 +- main/opcodes/flow-control.zkasm | 15 ++-- main/opcodes/logs.zkasm | 12 ++- main/opcodes/stack-operations.zkasm | 11 +-- main/opcodes/storage-memory.zkasm | 22 ++---- main/precompiled/identity.zkasm | 9 +-- main/touched.zkasm | 7 ++ 20 files changed, 487 insertions(+), 117 deletions(-) create mode 100644 counters/README.md create mode 100644 counters/tests/opCALL.zkasm create mode 100644 counters/tests/opCALLCODE.zkasm create mode 100644 counters/tests/opCREATE.zkasm create mode 100644 counters/tests/opCREATE2.zkasm create mode 100644 counters/tests/opDELEGATECALL.zkasm create mode 100644 counters/tests/opRETURN.zkasm create mode 100644 counters/tests/opREVERT.zkasm create mode 100644 counters/tests/opSHA3.zkasm create mode 100644 counters/tests/opSTATICCALL.zkasm diff --git a/counters/README.md b/counters/README.md new file mode 100644 index 00000000..01f35ee7 --- /dev/null +++ b/counters/README.md @@ -0,0 +1,45 @@ +## Counters testing tool + +The purpose of this tool is to detect counters altertions in zkrom code. +A unit test is created for each function and opcode of the zkEVM. The structure of the test is the following: +````` +INCLUDE "../initIncludes.zkasm" // Include the files imported at the beginning of the test + +start: + 1000000 => GAS + +operation: + 2 :MSTORE(SP++) + 2 :MSTORE(SP++) + :JMP(opADD) +// Assert counters. Check for each function, the exact number of each counter is matched +checkCounters: +%OPADD_STEP - STEP:JMPN(failedCounters) +%OPADD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPADD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPADD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPADD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPADD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPADD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) +// Finalize execution +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: // Force failed assert +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" // Include the files imported at the end of the test +````` + +Run all tests: +````` +node counters/counters-executor.js +````` +Limitations: +- Not all the tests are implemented yet, just the most complex ones +- For some test (the simplest ones), the counters it should spend are stored in `countersConstants.zkasm` file. For tests with a lot of utils calls or a lot of complexity, the values of the counters are hardcoded in the test. +- The tests always try to cover as much coverage as posible and always with the worst case counters scenario but this approach gets a bit tricky for complex opcodes as they have different contexts and behaviours. +- The objective is to keep adding tests with already not implemented functions but also adding tests for already implemented opcodes but with different scenarios (Example: calldatacopy from a call or from a create2) \ No newline at end of file diff --git a/counters/counters-executor.js b/counters/counters-executor.js index e24d5534..50b27e81 100644 --- a/counters/counters-executor.js +++ b/counters/counters-executor.js @@ -16,9 +16,10 @@ async function main() { // Compile pil const cmPols = await compilePil(); - // Run all zkasm files + // Get all zkasm files const files = getTestFiles(); + // Run all zkasm files for (let file of files) { await runTest(file, cmPols) } @@ -33,10 +34,12 @@ async function runTest(testName, cmPols) { stepsN: 8388608, } console.log(`Running ${testName}`) + // Execute test const res = await smMain.execute(cmPols.Main, empty_input, rom, config); console.log(res.counters) } +// Get all zkasm counter test files function getTestFiles() { if(argv.test){ return [`${argv.test}.zkasm`] diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 94545d3f..f33e4ddd 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -246,6 +246,78 @@ CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0 CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 0 CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0 CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 0 +; opCREATE - COMPLEX - hardcoded values at test +CONST %OPCREATE_STEP = 100 +CONST %OPCREATE_CNT_BINARY = 2 +CONST %OPCREATE_CNT_ARITH = 0 +CONST %OPCREATE_CNT_KECCAK_F = 0 +CONST %OPCREATE_CNT_MEM_ALIGN = 0 +CONST %OPCREATE_CNT_PADDING_PG = 0 +CONST %OPCREATE_CNT_POSEIDON_G = 255*3 +; opCREATE2 - COMPLEX - hardcoded values at test +CONST %OPCREATE2_STEP = 200 +CONST %OPCREATE2_CNT_BINARY = 2 +CONST %OPCREATE2_CNT_ARITH = 0 +CONST %OPCREATE2_CNT_KECCAK_F = 0 +CONST %OPCREATE2_CNT_MEM_ALIGN = 0 +CONST %OPCREATE2_CNT_PADDING_PG = 0 +CONST %OPCREATE2_CNT_POSEIDON_G = 255*2 +; opCALL - COMPLEX - hardcoded values at test +CONST %OPCALL_STEP = 200 +CONST %OPCALL_CNT_BINARY = 3 +CONST %OPCALL_CNT_ARITH = 0 +CONST %OPCALL_CNT_KECCAK_F = 0 +CONST %OPCALL_CNT_MEM_ALIGN = 0 +CONST %OPCALL_CNT_PADDING_PG = 0 +CONST %OPCALL_CNT_POSEIDON_G = 0 +; opCALLCODE - COMPLEX - hardcoded values at test +CONST %OPCALLCODE_STEP = 200 +CONST %OPCALLCODE_CNT_BINARY = 2 +CONST %OPCALLCODE_CNT_ARITH = 0 +CONST %OPCALLCODE_CNT_KECCAK_F = 0 +CONST %OPCALLCODE_CNT_MEM_ALIGN = 0 +CONST %OPCALLCODE_CNT_PADDING_PG = 0 +CONST %OPCALLCODE_CNT_POSEIDON_G = 0 +; opRETURN - COMPLEX - hardcoded values at test +CONST %OPRETURN_STEP = 400 +CONST %OPRETURN_CNT_BINARY = 1 +CONST %OPRETURN_CNT_ARITH = 0 +CONST %OPRETURN_CNT_KECCAK_F = 0 +CONST %OPRETURN_CNT_MEM_ALIGN = 0 +CONST %OPRETURN_CNT_PADDING_PG = 0 +CONST %OPRETURN_CNT_POSEIDON_G = 0 +; opREVERT - COMPLEX - hardcoded values at test +CONST %OPREVERT_STEP = 400 +CONST %OPREVERT_CNT_BINARY = 0 +CONST %OPREVERT_CNT_ARITH = 0 +CONST %OPREVERT_CNT_KECCAK_F = 0 +CONST %OPREVERT_CNT_MEM_ALIGN = 0 +CONST %OPREVERT_CNT_PADDING_PG = 0 +CONST %OPREVERT_CNT_POSEIDON_G = 0 +; opDELEGATECALL - COMPLEX - hardcoded values at test +CONST %OPDELEGATECALL_STEP = 400 +CONST %OPDELEGATECALL_CNT_BINARY = 0 +CONST %OPDELEGATECALL_CNT_ARITH = 0 +CONST %OPDELEGATECALL_CNT_KECCAK_F = 0 +CONST %OPDELEGATECALL_CNT_MEM_ALIGN = 0 +CONST %OPDELEGATECALL_CNT_PADDING_PG = 0 +CONST %OPDELEGATECALL_CNT_POSEIDON_G = 0 +; opSTATICCALL - COMPLEX - hardcoded values at test +CONST %OPSTATICCALL_STEP = 300 +CONST %OPSTATICCALL_CNT_BINARY = 0 +CONST %OPSTATICCALL_CNT_ARITH = 0 +CONST %OPSTATICCALL_CNT_KECCAK_F = 0 +CONST %OPSTATICCALL_CNT_MEM_ALIGN = 0 +CONST %OPSTATICCALL_CNT_PADDING_PG = 0 +CONST %OPSTATICCALL_CNT_POSEIDON_G = 0 +; opSHA3 - COMPLEX - hardcoded values at test +CONST %OPSHA3_STEP = 400 +CONST %OPSHA3_CNT_BINARY = 0 +CONST %OPSHA3_CNT_ARITH = 0 +CONST %OPSHA3_CNT_KECCAK_F = 0 +CONST %OPSHA3_CNT_MEM_ALIGN = 0 +CONST %OPSHA3_CNT_PADDING_PG = 0 +CONST %OPSHA3_CNT_POSEIDON_G = 0 ; MSTORE32 CONST %MSTORE32_STEP = 50 + %OFFSETUTIL_STEP CONST %MSTORE32_CNT_BINARY = 1 + %OFFSETUTIL_CNT_BINARY diff --git a/counters/tests/opCALL.zkasm b/counters/tests/opCALL.zkasm new file mode 100644 index 00000000..05c354ba --- /dev/null +++ b/counters/tests/opCALL.zkasm @@ -0,0 +1,41 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + 100 :MSTORE(storageAddr) + 100 => A + 0 => B, C + 100000 => D + $ => SR :SSTORE + ; stack input: [gas, address, value, argsOffset, argsSize, retOffset, retSize] + ; stack output: [success] + 1000 :MSTORE(SP++) + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 8 :MSTORE(SP++) + 10000 :MSTORE(SP++) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opCALL) +checkCounters: +600 - STEP:JMPN(failedCounters) +32 - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +14 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCALLCODE.zkasm b/counters/tests/opCALLCODE.zkasm new file mode 100644 index 00000000..e34ad845 --- /dev/null +++ b/counters/tests/opCALLCODE.zkasm @@ -0,0 +1,41 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + 100 :MSTORE(storageAddr) + 100 => A + 0 => B, C + 100000 => D + $ => SR :SSTORE + ; stack input: [gas, address, value, argsOffset, argsSize, retOffset, retSize] + ; stack output: [success] + 1000 :MSTORE(SP++) + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 8 :MSTORE(SP++) + 10000 :MSTORE(SP++) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opCALLCODE) +checkCounters: +600 - STEP:JMPN(failedCounters) +31 - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLCODE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALLCODE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +10 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCREATE.zkasm b/counters/tests/opCREATE.zkasm new file mode 100644 index 00000000..ea44f579 --- /dev/null +++ b/counters/tests/opCREATE.zkasm @@ -0,0 +1,35 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + 50 :MSTORE(storageAddr) + 50 => A + 0 => B, C + 10000 => D + $ => SR :SSTORE + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opCREATE) +checkCounters: +400 - STEP:JMPN(failedCounters) +22 - CNT_BINARY :JMPNZ(failedCounters) +4 - CNT_ARITH :JMPNZ(failedCounters) +%OPCREATE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCREATE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +23 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm new file mode 100644 index 00000000..39fe3ad2 --- /dev/null +++ b/counters/tests/opCREATE2.zkasm @@ -0,0 +1,35 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + 50 :MSTORE(storageAddr) + 50 => A + 0 => B, C + 10000 => D + $ => SR :SSTORE + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opCREATE2) +checkCounters: +500 - STEP:JMPN(failedCounters) +30 - CNT_BINARY :JMPNZ(failedCounters) +5 - CNT_ARITH :JMPNZ(failedCounters) +%OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +33 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opDELEGATECALL.zkasm b/counters/tests/opDELEGATECALL.zkasm new file mode 100644 index 00000000..07f45e6a --- /dev/null +++ b/counters/tests/opDELEGATECALL.zkasm @@ -0,0 +1,35 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + ; stack input: [gas, address, argsOffset, argsSize, retOffset, retSize] + ; stack output: [success] + 1000 :MSTORE(SP++) + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 8 :MSTORE(SP++) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opDELEGATECALL) +checkCounters: +600 - STEP:JMPN(failedCounters) +28 - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_ARITH :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +6 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opRETURN.zkasm b/counters/tests/opRETURN.zkasm new file mode 100644 index 00000000..d237ffe8 --- /dev/null +++ b/counters/tests/opRETURN.zkasm @@ -0,0 +1,32 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 => CTX + 1 :MSTORE(originCTX) + 2 :MSTORE(currentCTX) + 10000 :MSTORE(retCallLength) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opRETURN) +checkCounters: +800 - STEP:JMPN(failedCounters) +39 - CNT_BINARY :JMPNZ(failedCounters) +11 - CNT_ARITH :JMPNZ(failedCounters) +%OPRETURN_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPRETURN_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPRETURN_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opREVERT.zkasm b/counters/tests/opREVERT.zkasm new file mode 100644 index 00000000..b78b93a7 --- /dev/null +++ b/counters/tests/opREVERT.zkasm @@ -0,0 +1,32 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 2 => CTX + 1 :MSTORE(originCTX) + 2 :MSTORE(currentCTX) + 10000 :MSTORE(retCallLength) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opREVERT) +checkCounters: +800 - STEP:JMPN(failedCounters) +41 - CNT_BINARY :JMPNZ(failedCounters) +11 - CNT_ARITH :JMPNZ(failedCounters) +%OPREVERT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPREVERT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +6 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm new file mode 100644 index 00000000..dc125a5b --- /dev/null +++ b/counters/tests/opSHA3.zkasm @@ -0,0 +1,29 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => D + 2 :HASHK(0) + 2000 :MSTORE(SP++) + 100 :MSTORE(SP++) + :JMP(opSHA3) +checkCounters: +3300 - STEP:JMPN(failedCounters) +146 - CNT_BINARY :JMPNZ(failedCounters) +9 - CNT_ARITH :JMPNZ(failedCounters) +15 - CNT_KECCAK_F :JMPNZ(failedCounters) +63 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSHA3_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +%OPSHA3_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/counters/tests/opSTATICCALL.zkasm b/counters/tests/opSTATICCALL.zkasm new file mode 100644 index 00000000..181876f1 --- /dev/null +++ b/counters/tests/opSTATICCALL.zkasm @@ -0,0 +1,35 @@ +INCLUDE "../initIncludes.zkasm" + +start: + 1000000 => GAS + +operation: + 1 => CTX + ; stack input: [gas, address, argsOffset, argsSize, retOffset, retSize] + ; stack output: [success] + 1000 :MSTORE(SP++) + 43 :MSTORE(SP++) + 54 :MSTORE(SP++) + 8 :MSTORE(SP++) + 100 :MSTORE(SP++) + 1000 :MSTORE(SP++) + :JMP(opSTATICCALL) +checkCounters: +500 - STEP:JMPN(failedCounters) +28 - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_ARITH :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) +6 - CNT_POSEIDON_G :JMPNZ(failedCounters) + +0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +finalizeExecution: + :JMP(finalWait) +readCode: +txType: + :JMP(checkCounters) +failedCounters: +2 => A +1 :ASSERT +INCLUDE "../endIncludes.zkasm" \ No newline at end of file diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index 16d7c79c..eae66894 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -54,11 +54,9 @@ opSTOPend: */ opCREATE: ; checks zk-counters - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*3 :JMPN(outOfCountersPoseidon) ; check out-of-gas GAS - %CREATE_GAS => GAS :JMPN(outOfGas) @@ -80,7 +78,6 @@ opCREATE: $ => C :MLOAD(SP); [size => C] C :MSTORE(argsLengthCall) - ; store lastMemOffset for memory expansion gas cost E :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost @@ -174,7 +171,8 @@ opCreateFail: opCALL: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) ; increase depth $ => A :MLOAD(depth) A + 1 :MSTORE(depth) @@ -222,14 +220,7 @@ opCALL2: $ => D :MLOAD(SP); [retSize => D] ; compute memory expansion gas cost of the return value D :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] - - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400:JMPN(outOfCountersStep) - - $ => B :MLOAD(lastMemOffset) + $ => B :MLOAD(lastMemOffset) ; check if an address is cold. If it is, add it to the touched tree $ => A :MLOAD(addrCall), CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] @@ -243,7 +234,7 @@ opCALL2: GAS - %CALL_VALUE_TRANSFER_GAS => GAS :JMPN(outOfGas) ; if destAddress is an empty account, we should add 25k of gas to base cost - $ => E :MLOAD(addrCall), CALL(isEmptyAccount); in: [E: address] out: [E: 0 if is not empty, 1 if is empty] + $ => E :MLOAD(addrCall), CALL(isEmptyAccount); in: [E: address] out: [E: 0 if is not empty, 1 if is empty] GAS - %CALL_NEW_ACCOUNT_GAS*E => GAS :JMPN(outOfGas) opCALLend: @@ -302,9 +293,9 @@ opCALLend: * - stack output: [success] */ opCALLCODE: - ; checks zk-counters - %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; increase depth $ => A :MLOAD(depth) A + 1 :MSTORE(depth) @@ -343,13 +334,6 @@ opCALLCODE: $ => D :MLOAD(SP); [retSize => D] ; compute memory expansion gas cost of the return value D :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] - - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400:JMPN(outOfCountersStep) - ; check if an address is cold. If it is, add it to the touched tree $ => A :MLOAD(addrCall), CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] @@ -418,16 +402,14 @@ opCALLCODEend: * - stack output: none */ opRETURN: + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 => SP :JMPN(stackUnderflow) $ => E :MLOAD(SP+1); [offset => E] $ => C :MLOAD(SP); [size => C] - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) ; store return data values E :MSTORE(retDataOffset) C :MSTORE(retDataLength) @@ -463,11 +445,9 @@ preRETURN32: $ => B :MLOAD(retCallOffset) ; Copy from memory current CTX to memory origin CTX -opRETURN32: +opRETURNloop: ; checks dynamic zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) C :JMPZ(opRETURNend) C - 32 :JMPN(opRETURNfinal) @@ -484,7 +464,7 @@ opRETURN32: D => E C - 32 => C ; restore current TX - $ => CTX :MLOAD(currentCTX), JMP(opRETURN32) + $ => CTX :MLOAD(currentCTX), JMP(opRETURNloop) opRETURNfinal: ; load last bytes @@ -517,11 +497,8 @@ opRETURNdeploy: opRETURNcreate: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 192 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1020 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; save offset memory and length to compute contract hash E :MSTORE(memOffsetLinearPoseidon) @@ -619,14 +596,6 @@ opDELEGATECALL: $ => E :MLOAD(SP); [retSize => D] ; Mem expansion gas cost return args E :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] - - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400:JMPN(outOfCountersStep) - - $ => B :MLOAD(lastMemOffset) ; check if an address is cold. If it is, add it to the touched tree $ => A :MLOAD(addrCall) @@ -688,10 +657,9 @@ opDELEGATECALLend: */ opCREATE2: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; check out-of-gas GAS - %CREATE_2_GAS => GAS :JMPN(outOfGas) @@ -810,6 +778,8 @@ opCREATE2: * - stack output: [success] */ opSTATICCALL: + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 300 :JMPN(outOfCountersStep) ; increase depth $ => A :MLOAD(depth) A + 1 :MSTORE(depth) @@ -842,13 +812,6 @@ opSTATICCALL: $ => E :MLOAD(SP); [retSize => E] ; compute memory expansion gas cost of the return value E :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] - - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) - ; check if an address is cold. If it is, add it to the touched tree $ => A :MLOAD(addrCall), CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ;gas_cost = base_gas + gas_sent_with_call @@ -939,10 +902,9 @@ opREVERT: preREVERT32: $ => B :MLOAD(retCallOffset) -opREVERT32: +opREVERTloop: ; checks zk-counters %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) - C :JMPZ(opREVERTend) ; load first 32 bytes from memory at current CTX C - 32 :JMPN(opREVERTfinal) @@ -957,7 +919,7 @@ opREVERT32: D => E C - 32 => C ; restore current CTX - $ => CTX :MLOAD(currentCTX), JMP(opREVERT32) + $ => CTX :MLOAD(currentCTX), JMP(opREVERTloop) opREVERTfinal: ; copy last bytes @@ -992,9 +954,9 @@ opREVERTend: */ opSENDALL: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 2040 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*4 :JMPN(outOfCountersPoseidon) ; check out-of-gas GAS - %SENDALL_GAS => GAS :JMPN(outOfGas) diff --git a/main/opcodes/crypto.zkasm b/main/opcodes/crypto.zkasm index 63cf9aee..66a07e55 100644 --- a/main/opcodes/crypto.zkasm +++ b/main/opcodes/crypto.zkasm @@ -13,11 +13,7 @@ */ opSHA3: ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 193 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 10 :JMPN(outOfCountersPoseidon) - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -66,7 +62,6 @@ opSHA3: opSHA3Loop: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) C :JMPZ(opSHA3End) diff --git a/main/opcodes/flow-control.zkasm b/main/opcodes/flow-control.zkasm index 8b8c12fa..c99da839 100644 --- a/main/opcodes/flow-control.zkasm +++ b/main/opcodes/flow-control.zkasm @@ -10,14 +10,14 @@ */ opJUMP: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) ; check out-of-gas - GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas) + GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas) ; Check PC is a JUMPDEST $ => B :MLOAD(SP), JMP(checkJumpDest); [counter => PC] @@ -33,10 +33,9 @@ opJUMP: opJUMPI: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) ; check out-of-gas GAS - %GAS_SLOW_STEP => GAS :JMPN(outOfGas) - SP - 2 => SP :JMPN(stackUnderflow) $ => B :MLOAD(SP); [value => B] 0 => A @@ -74,10 +73,6 @@ checkJumpDestDeployment: $ :EQ, JMPC(readCode, invalidJump) checkJumpDestDeploymentCreate: - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 224 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 225 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) ; get bytes from previous context memory $ => CTX :MLOAD(originCTX) ; get offset call position @@ -103,7 +98,7 @@ checkJumpDestDeploymentCreate: */ opPC: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) @@ -123,7 +118,7 @@ opPC: */ opJUMPDEST: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) ; check out-of-gas GAS - %JUMP_DEST_GAS => GAS :JMPN(outOfGas, readCode) diff --git a/main/opcodes/logs.zkasm b/main/opcodes/logs.zkasm index 2e8438ea..8fb97d73 100644 --- a/main/opcodes/logs.zkasm +++ b/main/opcodes/logs.zkasm @@ -10,7 +10,7 @@ opLOG0: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; check stack underflow SP - 2 :JMPN(stackUnderflow) @@ -44,7 +44,7 @@ opLOG0: opLOG1: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) SP - 3 :JMPN(stackUnderflow) GAS - %LOG_GAS => GAS :JMPN(outOfGas) $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx) @@ -67,7 +67,7 @@ opLOG1: opLOG2: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) SP - 4 :JMPN(stackUnderflow) GAS - %LOG_GAS => GAS :JMPN(outOfGas) $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx) @@ -90,7 +90,7 @@ opLOG2: opLOG3: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) SP - 5 :JMPN(stackUnderflow) GAS - %LOG_GAS => GAS :JMPN(outOfGas) $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx) @@ -113,7 +113,7 @@ opLOG3: opLOG4: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) SP - 6 :JMPN(stackUnderflow) GAS - %LOG_GAS => GAS :JMPN(outOfGas) $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx) @@ -135,9 +135,7 @@ opLOG4: opLOGLoop: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - C :JMPZ(opSaveTopicsInit) ; load next 32 bytes C - 32 :JMPN(opLOGFinal) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 329b599f..dbc04ccf 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -140,8 +140,8 @@ opPUSH32: opAuxPUSHA: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) $ => B :MLOAD(bytecodeLength) PC + D => A @@ -163,7 +163,6 @@ opAuxPUSHA2: opAuxPUSHB: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) ; check is a create/create2 $ => A :MLOAD(isCreate) @@ -175,9 +174,6 @@ opAuxPUSHB: opAuxPUSHBloop: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) - %MAX_CNT_ARITH - CNT_ARITH - 4 :JMPN(outOfCountersArith) - ; get position where data starts in the tx $ => HASHPOS :MLOAD(dataStarts) ; add PC to data starts to point the bytes to read for the push @@ -204,11 +200,6 @@ opAuxPUSHBend: opAuxPUSHBcreate: ; PUSH in a create, we need to get bytes from memory (not calldata) - ; checks zk-counters - %MAX_CNT_ARITH - CNT_ARITH - 224 :JMPN(outOfCountersArith) - %MAX_CNT_BINARY - CNT_BINARY - 225 :JMPN(outOfCountersBinary) - %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) - ; get pushed bytes from previous context memory 0 => B $ => CTX :MLOAD(originCTX) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index a68d5369..153f9a39 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -12,10 +12,6 @@ */ 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 @@ -47,10 +43,6 @@ opMLOAD: */ 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 @@ -83,10 +75,8 @@ opMLOAD: */ 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) + %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign) ; check stack underflow SP - 2 => SP :JMPN(stackUnderflow) @@ -120,8 +110,6 @@ opMSTORE8: */ 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 @@ -138,6 +126,7 @@ opMSIZE: $ => B :MLOAD(arithRes2) ; check arithRes2 is 0, no need to round in this case 0 => A + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) $ :EQ, JMPC(MSIZEend) ; Round size to 32bytes multiple C + 1 => C @@ -158,8 +147,8 @@ MSIZEend: */ opSLOAD: ; checks zk-counters - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon) ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) @@ -189,10 +178,9 @@ VAR GLOBAL tmpVarCsstore */ 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) + %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*3 :JMPN(outOfCountersPoseidon) ; check stack underflow SP - 2 => SP :JMPN(stackUnderflow) diff --git a/main/precompiled/identity.zkasm b/main/precompiled/identity.zkasm index f7b13821..11c55c0f 100644 --- a/main/precompiled/identity.zkasm +++ b/main/precompiled/identity.zkasm @@ -1,4 +1,6 @@ IDENTITY: + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; Move balances if value > 0 just before executing the contract CALL $ => B :MLOAD(txValue) 0 => A @@ -25,10 +27,8 @@ IDENTITY: B :MSTORE(retDataOffset) C :MSTORE(retDataLength) -IDENTITYinit: - %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) +IDENTITYloop: %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - ; Copy from calldata to memory C :JMPZ(IDENTITYreturn) C - 32 :JMPN(IDENTITYfinal) @@ -36,7 +36,7 @@ IDENTITYinit: $ => B :MLOAD(SP) B :MSTORE(bytesToStore), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] A + 1 => A - C - 32 => C :JMP(IDENTITYinit) + C - 32 => C :JMP(IDENTITYloop) IDENTITYfinal: %CALLDATA_OFFSET + A => SP @@ -61,7 +61,6 @@ IDENTITYreturn2: $ => B :MLOAD(retCallOffset) IDENTITYreturnLoop: - %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) C :JMPZ(IDENTITYend) diff --git a/main/touched.zkasm b/main/touched.zkasm index 18c021f3..9e2f82b5 100644 --- a/main/touched.zkasm +++ b/main/touched.zkasm @@ -21,6 +21,10 @@ revertTouched: ; @in A => address ; @out D => if the address is cold [0 if warm, 1 if cold] isColdAddress: + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; store previous registers values temporary B :MSTORE(tmpB) C :MSTORE(tmpC) @@ -76,6 +80,9 @@ finishColdPrecompiled: ; @in C => storage position ; @out A => if the storage slot is cold [0 if warm, 1 if cold] isColdSlot: +; checks zk-counters + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; store previous registers values temporary B :MSTORE(tmpB) D :MSTORE(tmpD) From 7346302a1e9bb3cca0a1ae3724c57b1d9efd1aa2 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Thu, 12 Jan 2023 10:31:21 +0100 Subject: [PATCH 13/60] Add counters at handle gas --- main/process-tx.zkasm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 89cfe9cb..30198986 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -255,7 +255,6 @@ create2: loopCreate2: ; check zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) C :JMPZ(create2end) C - 32 :JMPN(endloopCreate2) @@ -301,6 +300,9 @@ endContractAddress: ;; deploy contract in state-tree deploy: + %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*3 :JMPN(outOfCountersPoseidon) + %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) :CALL(isColdAddress) ; add address to touched addresses ; check if address is deployable ( nonce == bytecode == 0) A => E @@ -461,6 +463,8 @@ endDeploy: ;; compute maximum gas to refund handleGas: + %MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*4 :JMPN(outOfCountersPoseidon) 0 => A $ => B :MLOAD(gasRefund), JMPZ(refundGas) $ => A :MLOAD(txGasLimit) From 7303da4a811823a8317096faa15910eba260d0b3 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 10 Jan 2023 11:56:25 +0100 Subject: [PATCH 14/60] RLP checks & clean code --- main/constants.zkasm | 4 ++++ main/load-tx-rlp-utils.zkasm | 24 +++++++++++++++++++++++- main/load-tx-rlp.zkasm | 34 ++++++++++++++++------------------ main/opcodes/comparison.zkasm | 2 +- main/process-tx.zkasm | 1 - main/utils.zkasm | 2 +- package.json | 4 ++-- 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index 49527664..5a34daad 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -11,6 +11,10 @@ CONST %LAST_TX_STORAGE_POS = 0 CONST %STATE_ROOT_STORAGE_POS = 1 CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0 +; RLP +CONST %MIN_VALUE_SHORT = 128 +CONST %MIN_BYTES_LONG = 56 + ; SMT STATE-TREE CONSTANT KEYS CONST %SMT_KEY_BALANCE = 0 CONST %SMT_KEY_NONCE = 1 diff --git a/main/load-tx-rlp-utils.zkasm b/main/load-tx-rlp-utils.zkasm index 08ba8125..a3714337 100644 --- a/main/load-tx-rlp-utils.zkasm +++ b/main/load-tx-rlp-utils.zkasm @@ -25,4 +25,26 @@ addHashTx: ${getTxs(p,D)} => A $${p = p + D} A :HASHK(E) - C + D => C :RETURN \ No newline at end of file + C + D => C :RETURN + +;; Check short value is over 127. Error RLP: single byte < 0x80 are not prefixed +checkShortRLP: + D - 1 :JMPNZ(skipCheckShort) + A - %MIN_VALUE_SHORT :JMPN(invalidTxRLP) + +skipCheckShort: + :RETURN + +;; check long list/value is over 55 bytes long. Error RLP: encoded list too short +checkLongRLP: + A - %MIN_BYTES_LONG :JMPN(invalidTxRLP) + :RETURN + +;; Check short value is over 127. Error RLP: single byte < 0x80 are not prefixed +checkShortDataRLP: + $ => B :MLOAD(txCalldataLen) + B - 1 :JMPNZ(skipCheckShortData) + A - %MIN_VALUE_SHORT :JMPN(invalidTxRLP) + +skipCheckShortData: + :RETURN \ No newline at end of file diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 95feb90e..216e1400 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -37,6 +37,7 @@ loadTx_rlp: longList: A - 0xf7 => D :CALL(addHashTx) + :CALL(checkLongRLP) :CALL(addBatchHashData) :JMP(endList) shortList: @@ -61,11 +62,12 @@ nonceREAD: A - 0x89 :JMPN(shortNonce, invalidTxRLP) nonce0: - 0 => A :MSTORE(lengthNonce), JMP(endNonce) + 0 => A :MSTORE(lengthNonce), JMP(endNonce) shortNonce: A - 0x80 => D D :MSTORE(lengthNonce), CALL(addHashTx) + :CALL(checkShortRLP) :CALL(addBatchHashData) endNonce: @@ -85,9 +87,8 @@ gasPrice0: 0 => A :JMP(endGasPrice) shortGasPrice: - A - 0x80 => D - D - 1 :JMPN(endGasPrice) - :CALL(addHashTx) + A - 0x80 => D :CALL(addHashTx) + :CALL(checkShortRLP) :CALL(addBatchHashData) endGasPrice: @@ -95,28 +96,27 @@ endGasPrice: ;; Read RLP 'gas limit' - ; 256 bits max + ; 64 bits max gasLimitREAD: 1 => D :CALL(addHashTx) :CALL(addBatchHashData) A - 0x80 :JMPN(endGasLimit) A - 0x81 :JMPN(gasLimit0) - A - 0xa1 :JMPN(shortGasLimit, invalidTxRLP) + A - 0x89 :JMPN(shortGasLimit, invalidTxRLP) gasLimit0: 0 => A :JMP(endGasLimit) shortGasLimit: - A - 0x80 => D - D - 1 :JMPN(endGasLimit) - :CALL(addHashTx) + A - 0x80 => D :CALL(addHashTx) + :CALL(checkShortRLP) :CALL(addBatchHashData) endGasLimit: A :MSTORE(txGasLimit) ;; Read RLP 'to' - ; 160 bits max + ; 160 bits or empty toREAD: 1 => D :CALL(addHashTx) :CALL(addBatchHashData) @@ -136,7 +136,6 @@ shortTo: endTo: - ;; Read RLP 'value' ; 256 bits max valueREAD: @@ -150,9 +149,8 @@ value0: 0 => A :JMP(endValue) shortValue: - A - 0x80 => D - D - 1 :JMPN(endValue) - :CALL(addHashTx) + A - 0x80 => D :CALL(addHashTx) + :CALL(checkShortRLP) :CALL(addBatchHashData) endValue: @@ -183,6 +181,7 @@ shortData: longData: A - 0xb7 => D :CALL(addHashTx) + :CALL(checkLongRLP) :CALL(addBatchHashData) $ => D :MLOAD(batchHashPos) D :MSTORE(dataStarts) @@ -198,6 +197,7 @@ readData: readDataFinal: B - 1 :JMPN(endData) B => D :CALL(addHashTx) + :CALL(checkShortDataRLP) 32 - D => D :CALL(SHLarith) A :MSTORE(SP) 32 - D => D :CALL(addBatchHashByteByByte) @@ -222,16 +222,14 @@ chainId0: shortChainId: - A - 0x80 => D - D - 1 :JMPN(endChainId) - :CALL(addHashTx) + A - 0x80 => D :CALL(addHashTx) + :CALL(checkShortRLP) :CALL(addBatchHashData) endChainId: A :MSTORE(txChainId) ;; Read RLP last two values (0, 0) - ; 64 bits max 2 => D :CALL(addHashTx) :CALL(addBatchHashData) 0x8080 => B diff --git a/main/opcodes/comparison.zkasm b/main/opcodes/comparison.zkasm index e5348a62..c0a38be7 100644 --- a/main/opcodes/comparison.zkasm +++ b/main/opcodes/comparison.zkasm @@ -281,7 +281,7 @@ opNOT: ; read one item from the stack $ => A :MLOAD(SP-1) - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**226 - 1 => + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**256 - 1 => $ => A :XOR,MSTORE(SP-1), JMP(readCode) ; [ NOT a => SP] /** diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 30198986..2471758b 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -81,7 +81,6 @@ endCheckChainId: $ => A :SLOAD $ => B :MLOAD(txNonce) $ => C :EQ, JMPNC(invalidIntrinsicTxNonce) ; Compare nonce state tree with nonce transaction - B :ASSERT ; sanity check A + 1 => D E => A %SMT_KEY_NONCE => B diff --git a/main/utils.zkasm b/main/utils.zkasm index e88df634..ad71dd1e 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -500,7 +500,7 @@ saveMemGAS: E + 31 => A ${A >> 5} => E ${A & 0x1f} => D - E * 32 + D :ASSERT + E * 32 + D :ASSERT D => A 0x20 => B $ :LT,JMPNC(failAssert) diff --git a/package.json b/package.json index ad758874..bc6ead6c 100644 --- a/package.json +++ b/package.json @@ -33,8 +33,8 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/last-changes", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.6.0.0", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/rlp-checks", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From a3976831fc98182faf34ee00f09738473aa55a57 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 13 Jan 2023 11:38:52 +0100 Subject: [PATCH 15/60] fix counters includes --- counters/endIncludes.zkasm | 1 - counters/initIncludes.zkasm | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/counters/endIncludes.zkasm b/counters/endIncludes.zkasm index 1752257d..5e317cf5 100644 --- a/counters/endIncludes.zkasm +++ b/counters/endIncludes.zkasm @@ -1,5 +1,4 @@ INCLUDE "../main/load-tx-rlp-utils.zkasm" -INCLUDE "../main/constants.zkasm" INCLUDE "../main/2-exp.zkasm" INCLUDE "../main/vars.zkasm" INCLUDE "../main/utils.zkasm" diff --git a/counters/initIncludes.zkasm b/counters/initIncludes.zkasm index 407a8a28..7bb2e30a 100644 --- a/counters/initIncludes.zkasm +++ b/counters/initIncludes.zkasm @@ -1 +1,2 @@ INCLUDE "countersConstants.zkasm" +INCLUDE "../main/constants.zkasm" \ No newline at end of file From 36ab9392d22c7a57a1e0173cbe65aacce6ef2c55 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Wed, 11 Jan 2023 16:30:36 +0100 Subject: [PATCH 16/60] Audit PR fixes --- counters/tests/opBLOCKHASH.zkasm | 3 +-- counters/tests/opSHA3.zkasm | 3 +-- main/constants.zkasm | 9 ++++++--- main/opcodes/flow-control.zkasm | 31 ++++++++++++++--------------- main/opcodes/logs.zkasm | 2 +- main/opcodes/stack-operations.zkasm | 4 ++-- main/opcodes/storage-memory.zkasm | 6 +++--- main/utils.zkasm | 1 - 8 files changed, 29 insertions(+), 30 deletions(-) diff --git a/counters/tests/opBLOCKHASH.zkasm b/counters/tests/opBLOCKHASH.zkasm index 170e43f4..48379303 100644 --- a/counters/tests/opBLOCKHASH.zkasm +++ b/counters/tests/opBLOCKHASH.zkasm @@ -4,8 +4,7 @@ start: 1000000 => GAS operation: - 1 => D - 2 :HASHK(0) + 2 :HASHK1(0) 10 :MSTORE(txCount) 1 :MSTORE(SP++) :JMP(opBLOCKHASH) diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm index dc125a5b..eb3a0e10 100644 --- a/counters/tests/opSHA3.zkasm +++ b/counters/tests/opSHA3.zkasm @@ -4,8 +4,7 @@ start: 1000000 => GAS operation: - 1 => D - 2 :HASHK(0) + 2 :HASHK1(0) 2000 :MSTORE(SP++) 100 :MSTORE(SP++) :JMP(opSHA3) diff --git a/main/constants.zkasm b/main/constants.zkasm index 5a34daad..d9cd94c9 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -30,9 +30,6 @@ CONST %SMT_KEY_TOUCHED_SLOTS = 6 CONST %BASE_TX_GAS = 21000 CONST %BASE_TX_DEPLOY_GAS = 53000 CONST %SLOAD_GAS = 100 -CONST %SSTORE_SET_GAS = 20000 -CONST %SSTORE_RESET_GAS = 2900 -CONST %SSTORE_CLEARS_SCHEDULE = 15000 CONST %GAS_QUICK_STEP = 2 CONST %GAS_FASTEST_STEP = 3 CONST %GAS_FAST_STEP = 5 @@ -58,12 +55,18 @@ CONST %RETURN_GAS_COST = 200; 200 * returned_code_size CONST %CREATE_GAS = 32000 CONST %CREATE_2_GAS = 32000 CONST %SENDALL_GAS = 5000 +CONST %LOG_DATA_GAS = 8 ; sstore CONST %SSTORE_ENTRY_EIP_2200_GAS = 2300 ; Minimum gas required to be present for an SSTORE call, not consumed CONST %SSTORE_SET_EIP_2200_GAS = 20000 ; Once per SSTORE operation from clean zero to non-zero CONST %COLD_SLOAD_COST = 2100 ; Cost of using a cols memory slot CONST %COLD_SLOAD_COST_RED = 2000 ; Cost of using a cols memory slot reduced CONST %SSTORE_DYNAMIC_GAS = 100 +CONST %SSTORE_SET_GAS = 20000 +CONST %SSTORE_SET_GAS_RED = 19900 ; sstore gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) +CONST %SSTORE_RESET_GAS = 2900 +CONST %SSTORE_RESET_GAS_RED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) +CONST %SSTORE_CLEARS_SCHEDULE = 15000 ; COUNTERS CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx CONST %TOTAL_STEPS = 2**23 diff --git a/main/opcodes/flow-control.zkasm b/main/opcodes/flow-control.zkasm index c99da839..6eab01c9 100644 --- a/main/opcodes/flow-control.zkasm +++ b/main/opcodes/flow-control.zkasm @@ -20,7 +20,7 @@ opJUMP: GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas) ; Check PC is a JUMPDEST - $ => B :MLOAD(SP), JMP(checkJumpDest); [counter => PC] + $ => A :MLOAD(SP), JMP(checkJumpDest); [counter => PC] /** * @link [https://www.evm.codes/#57?fork=berlin] * @zk-counters @@ -41,36 +41,36 @@ opJUMPI: 0 => A $ :EQ, JMPC(readCode) ; Check PC is a JUMPDEST - $ => B :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC] + $ => A :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC] checkJumpDest: ; if it is a deploy we have to check the data from the calldata (not the bytecode) - $ => A :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment) - $ => A :MLOAD(bytecodeLength) - $ :LT,JMPC(invalidJump) + $ => B :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment) + $ => B :MLOAD(bytecodeLength) + $ :LT,JMPNC(invalidJump) + A => B B => PC B => HASHPOS ; get hashP address pointer where contract bytecode is stored $ => E :MLOAD(contractHashId) $ => A :HASHP1(E) ; check if is a jumpDest (0x5B) - 0x5B => B - $ :EQ, JMPC(readCode, invalidJump) + A - 0x5B :JMPZ(readCode, invalidJump) + checkJumpDestDeployment: ; check jumpDest from calldata - $ => A :MLOAD(isCreate), JMPNZ(checkJumpDestDeploymentCreate) + $ => B :MLOAD(isCreate), JMPNZ(checkJumpDestDeploymentCreate) ; get position where data starts in the tx $ => HASHPOS :MLOAD(dataStarts) - ; add PC to data starts to point the bytes to read for the push - B => PC + ; add PC to data starts to point the bytes to read for the jumpdest + A => PC HASHPOS + PC => HASHPOS ; get memory pointer for hashing $ => E :MLOAD(batchHashDataId) - ; set number of bytes to hashK at D + ; set number of bytes to hashK $ => A :HASHK1(E) ; check if is a jumpDest (0x5B) - 0x5B => B - $ :EQ, JMPC(readCode, invalidJump) + A - 0x5B :JMPZ(readCode, invalidJump) checkJumpDestDeploymentCreate: ; get bytes from previous context memory @@ -78,14 +78,13 @@ checkJumpDestDeploymentCreate: ; get offset call position $ => E :MLOAD(argsOffsetCall) ; increase current program counter (PC) to offset for getting pushed bytes to read - B => PC + A => PC E + PC => E ; set bytes to read from memory at C 1 => C :CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset] $ => CTX :MLOAD(currentCTX) - 31 => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result] ; check if is a jumpDest (0x5B) - 0x5B => B + 0x5b00000000000000000000000000000000000000000000000000000000000000n => B $ :EQ, JMPC(readCode, invalidJump) /** diff --git a/main/opcodes/logs.zkasm b/main/opcodes/logs.zkasm index 8fb97d73..fab193f4 100644 --- a/main/opcodes/logs.zkasm +++ b/main/opcodes/logs.zkasm @@ -31,7 +31,7 @@ opLOG0: ; store number of topics 0 :MSTORE(numTopics) ; calculate data size gas cost => lastMemLength * 8 - 8 :MSTORE(arithA) + %LOG_DATA_GAS :MSTORE(arithA) C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB] $ => B :MLOAD(arithRes1) GAS => A diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index dbc04ccf..2c8f7442 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -180,13 +180,13 @@ opAuxPUSHBloop: HASHPOS + PC => HASHPOS PC + 1 => PC ; get memory pointer for hashing - 1 => D $ => E :MLOAD(batchHashDataId) - $ => B :HASHK(E) + $ => B :HASHK1(E) ; accumulate hash value A + B => A ; decrease length for loop C - 1 => C :JMPN(opAuxPUSHBend) + 1 => D :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result] :JMP(opAuxPUSHBloop) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 153f9a39..527a882a 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -58,7 +58,7 @@ opMLOAD: 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 + ; store lastMemLength for memory expansion gas cost 32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] :JMP(readCode) @@ -252,7 +252,7 @@ opSSTOREdifAB: $ => A :EQ, JMPNC(opSSTOREdifA2) ; if orig_val == 0 $ => A :MLOAD(gasRefund) - A + 19900 => A :MSTORE(gasRefund), JMP(opSSTOREend) + A + %SSTORE_SET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend) opSSTOREdifA1: ; if orig_val != 0 @@ -277,7 +277,7 @@ opSSTOREdifA2: ; if orig_val != 0 ; compute gas refund $ => A :MLOAD(gasRefund) - A + 2800 => A :MSTORE(gasRefund), JMP(opSSTOREend) + A + %SSTORE_RESET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend) opSSTOREdifB: ; if orig_val != 0 diff --git a/main/utils.zkasm b/main/utils.zkasm index ad71dd1e..0854b9aa 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -180,7 +180,6 @@ MSTORE32: B :MSTORE(tmpVarBmstore) C :MSTORE(tmpVarCmstore) D :MSTORE(tmpVarDmstore) - E :MSTORE(tmpVarEmstore) ; check offset is lower than max memory E => A %MEMORY_LENGTH => B From 96ed32315fe6f11ec18da6d417af6de950107065 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Fri, 13 Jan 2023 12:09:17 +0100 Subject: [PATCH 17/60] Review fixes --- main/constants.zkasm | 8 ++++---- main/opcodes/calldata-returndata-code.zkasm | 6 +++--- main/opcodes/context-information.zkasm | 2 +- main/opcodes/create-terminate-context.zkasm | 8 ++++---- main/opcodes/stack-operations.zkasm | 3 +-- main/opcodes/storage-memory.zkasm | 6 +++--- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index d9cd94c9..d2207523 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -48,7 +48,7 @@ CONST %LOG_GAS = 375 ; Per LOG* operation. CONST %LOG_TOPIC_GAS = 375 ; Per LOG topic operation. CONST %JUMP_DEST_GAS = 1 ; Once per JUMPDEST operation. CONST %WARM_STORGE_READ_GAS = 100 ; WarmStorageReadCostEIP2929 -CONST %COLD_ACCOUNT_ACCESS_COST_RED = 2500 ; ColdAccountAccessCostEIP2929 reduced(2600) +CONST %COLD_ACCOUNT_ACCESS_COST_REDUCED = 2500 ; ColdAccountAccessCostEIP2929 reduced(2600) CONST %COLD_ACCOUNT_ACCESS_COST = 2600 ; ColdAccountAccessCostEIP2929 CONST %EXP_BYTE_GAS = 50 ; was raised to 50 during Eip158 (Spurious Dragon) CONST %RETURN_GAS_COST = 200; 200 * returned_code_size @@ -60,12 +60,12 @@ CONST %LOG_DATA_GAS = 8 CONST %SSTORE_ENTRY_EIP_2200_GAS = 2300 ; Minimum gas required to be present for an SSTORE call, not consumed CONST %SSTORE_SET_EIP_2200_GAS = 20000 ; Once per SSTORE operation from clean zero to non-zero CONST %COLD_SLOAD_COST = 2100 ; Cost of using a cols memory slot -CONST %COLD_SLOAD_COST_RED = 2000 ; Cost of using a cols memory slot reduced +CONST %COLD_SLOAD_COST_REDUCED = 2000 ; Cost of using a cols memory slot reduced CONST %SSTORE_DYNAMIC_GAS = 100 CONST %SSTORE_SET_GAS = 20000 -CONST %SSTORE_SET_GAS_RED = 19900 ; sstore gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) +CONST %SSTORE_SET_GAS_REDUCED = 19900 ; sstore gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) CONST %SSTORE_RESET_GAS = 2900 -CONST %SSTORE_RESET_GAS_RED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) +CONST %SSTORE_RESET_GAS_REDUCED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) CONST %SSTORE_CLEARS_SCHEDULE = 15000 ; COUNTERS CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 1ef0b31e..d19c8689 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -381,7 +381,7 @@ opEXTCODESIZE: $ => A :MLOAD(SP), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address] :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ; check out-of-gas - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; set key for smt smart contract length query %SMT_KEY_SC_LENGTH => B 0 => C @@ -418,7 +418,7 @@ opEXTCODECOPY: :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ; check out-of-gas - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) :CALL(opEXTCODECOPYLoadBytecode) $ => C :MLOAD(SP+2); [destOffset => C] $ => D :MLOAD(SP+1); [offset => D] @@ -646,7 +646,7 @@ opEXTCODEHASH: $ => A :MLOAD(SP), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address] :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ; check out-of-gas - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; set key for smt smart contract code hash query %SMT_KEY_SC_CODE => B 0 => C diff --git a/main/opcodes/context-information.zkasm b/main/opcodes/context-information.zkasm index a4a6e5f7..6247a616 100644 --- a/main/opcodes/context-information.zkasm +++ b/main/opcodes/context-information.zkasm @@ -73,7 +73,7 @@ opBALANCE: $ => D :SLOAD D :MSTORE(SP-1), CALL(isColdAddress); [balance(D) => SP]; in: [A: address] out: [D: 0 if warm, 1 if cold] ; check out-of-gas - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas, readCode) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas, readCode) /** * @link [https://www.evm.codes/#32?fork=berlin] diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index eae66894..5cbcf02a 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -226,7 +226,7 @@ opCALL2: ;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 - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; Calculate positive_value_cost, if value call > 0 -> 9000 $ => B :MLOAD(valueCall) 0 => A @@ -339,7 +339,7 @@ opCALLCODE: ;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 - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; Calculate positive_value_cost, if value call > 0 -> 9000 $ => B :MLOAD(valueCall) 0 => A @@ -601,7 +601,7 @@ opDELEGATECALL: $ => A :MLOAD(addrCall) :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ;gas_cost = base_gas + gas_sent_with_call - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; transition to new CTX $ => C :MLOAD(txSrcAddr) @@ -815,7 +815,7 @@ opSTATICCALL: ; check if an address is cold. If it is, add it to the touched tree $ => A :MLOAD(addrCall), CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] ;gas_cost = base_gas + gas_sent_with_call - GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas) + GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_REDUCED => GAS :JMPN(outOfGas) ; setup vars for next CTX $ => D :MLOAD(storageAddr) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 2c8f7442..0b306e6a 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -186,8 +186,7 @@ opAuxPUSHBloop: A + B => A ; decrease length for loop C - 1 => C :JMPN(opAuxPUSHBend) - 1 => D - :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result] + 1 => D :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result] :JMP(opAuxPUSHBloop) opAuxPUSHBend: diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 527a882a..9e4fdcd6 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -162,7 +162,7 @@ opSLOAD: ; 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) + GAS - %SLOAD_GAS - A * %COLD_SLOAD_COST_REDUCED => GAS :JMPN(outOfGas, readCode) VAR GLOBAL tmpVarCsstore /** @@ -252,7 +252,7 @@ opSSTOREdifAB: $ => A :EQ, JMPNC(opSSTOREdifA2) ; if orig_val == 0 $ => A :MLOAD(gasRefund) - A + %SSTORE_SET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend) + A + %SSTORE_SET_GAS_REDUCED => A :MSTORE(gasRefund), JMP(opSSTOREend) opSSTOREdifA1: ; if orig_val != 0 @@ -277,7 +277,7 @@ opSSTOREdifA2: ; if orig_val != 0 ; compute gas refund $ => A :MLOAD(gasRefund) - A + %SSTORE_RESET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend) + A + %SSTORE_RESET_GAS_REDUCED => A :MSTORE(gasRefund), JMP(opSSTOREend) opSSTOREdifB: ; if orig_val != 0 From 01dae1a54083be9ab056cc491c20d3542421840e Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Fri, 13 Jan 2023 12:45:21 +0100 Subject: [PATCH 18/60] JMP to CALL small improvement --- main/utils.zkasm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/utils.zkasm b/main/utils.zkasm index 0854b9aa..36f7d661 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -316,7 +316,7 @@ MLOAD32: $ :LT,JMPC(initMLOAD, errorMLOADMSTORE) initMLOAD: - zkPC+1 => RR :JMP(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + :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 From 7bb538406b84b94811fba22b3c1cc5554b766882 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 13 Jan 2023 16:57:52 +0100 Subject: [PATCH 19/60] safe range max counters --- main/constants.zkasm | 23 ++++++--- main/opcodes/arithmetic.zkasm | 40 ++++++---------- main/opcodes/block.zkasm | 16 +++---- main/opcodes/calldata-returndata-code.zkasm | 31 +++++------- main/opcodes/comparison.zkasm | 38 +++++++-------- main/opcodes/context-information.zkasm | 16 +++---- main/opcodes/create-terminate-context.zkasm | 53 ++++++++------------- main/opcodes/crypto.zkasm | 8 +--- main/opcodes/flow-control.zkasm | 12 ++--- main/opcodes/logs.zkasm | 4 +- main/opcodes/stack-operations.zkasm | 10 ++-- main/opcodes/storage-memory.zkasm | 27 +++-------- 12 files changed, 116 insertions(+), 162 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index d2207523..f0bb8c19 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -67,17 +67,28 @@ CONST %SSTORE_SET_GAS_REDUCED = 19900 ; sstore gas cost reduced base_dynamic_gas CONST %SSTORE_RESET_GAS = 2900 CONST %SSTORE_RESET_GAS_REDUCED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100) CONST %SSTORE_CLEARS_SCHEDULE = 15000 + ; COUNTERS CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx CONST %TOTAL_STEPS = 2**23 CONST %MAX_CNT_STEPS = %TOTAL_STEPS - %MIN_STEPS_FINISH_BATCH -CONST %MAX_CNT_ARITH = %TOTAL_STEPS / 32 -CONST %MAX_CNT_BINARY = %TOTAL_STEPS / 16 -CONST %MAX_CNT_MEM_ALIGN = %TOTAL_STEPS / 32 -CONST %MAX_CNT_KECCAK_F = (%TOTAL_STEPS / 155286) * 44 -CONST %MAX_CNT_PADDING_PG = (%TOTAL_STEPS / 56) -CONST %MAX_CNT_POSEIDON_G = (%TOTAL_STEPS / 30) +CONST %MAX_CNT_ARITH_LIMIT = %TOTAL_STEPS / 32 +CONST %MAX_CNT_BINARY_LIMIT = %TOTAL_STEPS / 16 +CONST %MAX_CNT_MEM_ALIGN_LIMIT = %TOTAL_STEPS / 32 +CONST %MAX_CNT_KECCAK_F_LIMIT = (%TOTAL_STEPS / 155286) * 44 +CONST %MAX_CNT_PADDING_PG_LIMIT = (%TOTAL_STEPS / 56) +CONST %MAX_CNT_POSEIDON_G_LIMIT = (%TOTAL_STEPS / 30) + +CONST %SAFE_RANGE = 20 ; safe guard counters to not take into account (%RANGE = 1 / SAFE_RANGE) + +CONST %MAX_CNT_ARITH = %MAX_CNT_ARITH_LIMIT - (%MAX_CNT_ARITH_LIMIT / %SAFE_RANGE) +CONST %MAX_CNT_BINARY = %MAX_CNT_BINARY_LIMIT - (%MAX_CNT_BINARY_LIMIT / %SAFE_RANGE) +CONST %MAX_CNT_MEM_ALIGN = %MAX_CNT_MEM_ALIGN_LIMIT - (%MAX_CNT_MEM_ALIGN_LIMIT / %SAFE_RANGE) +CONST %MAX_CNT_KECCAK_F = %MAX_CNT_KECCAK_F_LIMIT - (%MAX_CNT_KECCAK_F_LIMIT / %SAFE_RANGE) +CONST %MAX_CNT_PADDING_PG = %MAX_CNT_PADDING_PG_LIMIT - (%MAX_CNT_PADDING_PG_LIMIT / %SAFE_RANGE) +CONST %MAX_CNT_POSEIDON_G = %MAX_CNT_POSEIDON_G_LIMIT - (%MAX_CNT_POSEIDON_G_LIMIT / %SAFE_RANGE) + CONST %MIN_CNT_KECCAK_BATCH = 1 ; minimum necessary keccaks to compute global hash ; ETHEREUM CONSTANTS diff --git a/main/opcodes/arithmetic.zkasm b/main/opcodes/arithmetic.zkasm index 33dd8100..ae78d117 100644 --- a/main/opcodes/arithmetic.zkasm +++ b/main/opcodes/arithmetic.zkasm @@ -2,8 +2,8 @@ /** * @link [Link EVM behaviour --> evm.codes?] * @zk-counters + * - 20 steps * - 1 binary - * - 100 steps * @process-opcode * - stack input: [a, b] * - stack output: [a + b] @@ -30,8 +30,7 @@ opADD: /** * @link [https://www.evm.codes/#02?fork=berlin] * @zk-counters - * - 1 arith - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a * b] @@ -58,8 +57,8 @@ opMUL: /** * @link [https://www.evm.codes/#03?fork=berlin] * @zk-counters - * - 1 arith - * - 100 steps + * - 20 steps + * - 1 binary * @process-opcode * - stack input: [a, b] * - stack output: [a - b] @@ -81,9 +80,7 @@ opSUB: /** * @link [https://www.evm.codes/#04?fork=berlin] * @zk-counters - * - 1 arith - * - 2 binary - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a / b] @@ -109,9 +106,8 @@ opDIV: /** * @link [https://www.evm.codes/#05?fork=berlin] * @zk-counters - * - 1 arith - * - 2 binary - * - 100 steps + * - 50 steps + * - 1 binary * @process-opcode * - stack input: [a, b] * - stack output: [a / b] @@ -148,9 +144,7 @@ SDIVNeg: /** * @link [https://www.evm.codes/#06?fork=berlin] * @zk-counters - * - 1 arith - * - 2 binary - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a % b] @@ -174,9 +168,8 @@ opMOD: /** * @link [https://www.evm.codes/#07?fork=berlin] * @zk-counters - * - 1 arith - * - 8 binary - * - 100 steps + * - 20 steps + * - 1 binary * @process-opcode * - stack input: [a, b] * - stack output: [a % b] @@ -210,9 +203,9 @@ opSMODNeg: /** * @link [https://www.evm.codes/#08?fork=berlin] * @zk-counters - * - 1 arith - * - 4 binary * - 80 steps + * - 3 binary + * - 1 arith * @process-opcode * - stack input: [a, b, N] * - stack output: [(a + b) % N] @@ -262,9 +255,7 @@ zeroOneAddMod: /** * @link [https://www.evm.codes/#09?fork=berlin] * @zk-counters - * - 3 arith - * - 3 binary - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [a, b, N] * - stack output: [(a * b) % N] @@ -287,8 +278,7 @@ opMULMOD: /** * @link [https://www.evm.codes/#0A?fork=berlin] * @zk-counters - * - 5 binary - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [a, exp] * - stack output: [a ** exp] @@ -319,8 +309,8 @@ opEXP: /** * @link [https://www.evm.codes/#0B?fork=berlin] * @zk-counters - * - 6 arith * - 100 steps + * - 6 binary * @process-opcode * - stack input: [b,x] * - stack output: [y] diff --git a/main/opcodes/block.zkasm b/main/opcodes/block.zkasm index a9d23e84..d7b1ecae 100644 --- a/main/opcodes/block.zkasm +++ b/main/opcodes/block.zkasm @@ -1,8 +1,8 @@ /** * @link [https://www.evm.codes/#40?fork=berlin] * @zk-counters - * - 1 keccak * - 255 poseidon + * - 1 keccak * - 100 steps * @process-opcode * - stack input: [blockNumber] @@ -54,7 +54,7 @@ opBLOCKHASHzero: /** * @link [https://www.evm.codes/#41?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [address] @@ -74,7 +74,7 @@ opCOINBASE: /** * @link [https://www.evm.codes/#42?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [timestamp] @@ -93,7 +93,7 @@ opTIMESTAMP: /** * @link [https://www.evm.codes/#43?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [blockNumber] @@ -113,7 +113,7 @@ opNUMBER: /** * @link [https://www.evm.codes/#44?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [difficulty] @@ -133,7 +133,7 @@ opDIFFICULTY: /** * @link [https://www.evm.codes/#45?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [gasLimit] @@ -153,7 +153,7 @@ opGASLIMIT: /** * @link [https://www.evm.codes/#46?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [chainId] @@ -172,7 +172,7 @@ opCHAINID: /** * @link [https://www.evm.codes/#48?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [baseFee] diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index d19c8689..4b1fc9c3 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -2,8 +2,7 @@ /** * @link [https://www.evm.codes/#35?fork=berlin] * @zk-counters - * - 64 arith - * - 66 binary + * - 1 binary * - 100 steps * @process-opcode * - stack input: [offset] @@ -82,10 +81,8 @@ opCALLDATASIZEdep: /** * @link [https://www.evm.codes/#37?fork=berlin] * @zk-counters - * - 192 arith - * - 192 binary - * - 1 mem align * - 100 steps + * - 2 binary * @process-opcode * - stack input: [destOffset, offset, size] * - stack output: [] @@ -224,8 +221,8 @@ opCALLDATACOPYend: /** * @link [https://www.evm.codes/#38?fork=berlin] * @zk-counters - * - 252 poseidon - * - 100 steps + * - 255 poseidon + * - 30 steps * @process-opcode * - stack input: [] * - stack output: [size] @@ -263,7 +260,7 @@ VAR GLOBAL memInteger /** * @link [https://www.evm.codes/#39?fork=berlin] * @zk-counters - * - dynamic binary: 100 * size + * - dynamic binary: 2 * size * - dynamic mem align: 2 * size * - dynamic poseidon: 510 * size * - dynamic steps: 100 * size @@ -366,8 +363,8 @@ readValueBytecode: /** * @link [https://www.evm.codes/#3B?fork=berlin] * @zk-counters - * - 255 poseidon * - 100 steps + * - 255 poseidon * @process-opcode * - stack input: [address] * - stack output: [size] @@ -399,11 +396,8 @@ VAR GLOBAL tmpContractLength /** * @link [https://www.evm.codes/#51?fork=berlin] * @zk-counters - * - 11 padding - * - dynamic binary: 100 * size - * - dynamic mem align: 2 * size - * - dynamic poseidon: 510 * size - * - dynamic steps: 100 * size + * - dynamic: 100 + * - dynamic poseidon: 255 + f(size) * @process-opcode * - stack input: [address, destOffset, offset, size] * - stack output: [] @@ -507,7 +501,7 @@ opEXTCODECOPYCheckHashEnd: * @link [https://www.evm.codes/#3D?fork=berlin] * @zk-counters * - 1 binary - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [] * - stack output: [size] @@ -537,10 +531,7 @@ endOpRETURNDATASIZE: /** * @link [https://www.evm.codes/#3E?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 2 mem align - * - 510 poseidon + * - 2 binary * - dynamic steps: 400 + 100 * size * @process-opcode * - stack input: [destOffset, offset, size] @@ -631,8 +622,8 @@ opRETURNDATACOPYEmpty: /** * @link [https://www.evm.codes/#3F?fork=berlin] * @zk-counters - * - 255 poseidon * - 100 steps + * - 255 poseidon * @process-opcode * - stack input: [address] * - stack output: [hash] diff --git a/main/opcodes/comparison.zkasm b/main/opcodes/comparison.zkasm index c0a38be7..87e0abd0 100644 --- a/main/opcodes/comparison.zkasm +++ b/main/opcodes/comparison.zkasm @@ -2,7 +2,7 @@ * @link [https://www.evm.codes/#10?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a < b] @@ -31,7 +31,7 @@ opLT: * @link [https://www.evm.codes/#11?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a > b] @@ -60,7 +60,7 @@ opGT: * @link [https://www.evm.codes/#12?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [signed(a < b)] @@ -89,7 +89,7 @@ opSLT: * @link [https://www.evm.codes/#13?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [signed(a > b)] @@ -118,7 +118,7 @@ opSGT: * @link [https://www.evm.codes/#14?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a == b] @@ -147,7 +147,7 @@ opEQ: * @link [https://www.evm.codes/#15?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a] * - stack output: [a == 0] @@ -175,7 +175,7 @@ opISZERO: * @link [https://www.evm.codes/#16?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a & b] @@ -204,7 +204,7 @@ opAND: * @link [https://www.evm.codes/#17?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a | b] @@ -233,7 +233,7 @@ opOR: * @link [https://www.evm.codes/#18?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a, b] * - stack output: [a ^ b] @@ -262,7 +262,7 @@ opXOR: * @link [https://www.evm.codes/#19?fork=berlin] * @zk-counters * - 1 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [a] * - stack output: [ ~a ] @@ -287,9 +287,8 @@ opNOT: /** * @link [https://www.evm.codes/#1a?fork=berlin] * @zk-counters - * - 2 arith - * - 4 binary - * - 120 steps + * - 30 steps + * - 2 binary * @process-opcode * - stack input: [byte offset, 32-byte value] * - stack output: [byte] @@ -328,9 +327,7 @@ opBYTE0: /** * @link [https://www.evm.codes/#1c?fork=berlin] * @zk-counters - * - 1 arith - * - 3 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [shift, value] * - stack output: [value >> shift] @@ -355,9 +352,7 @@ opSHR: /** * @link [https://www.evm.codes/#1b?fork=berlin] * @zk-counters - * - 1 arith - * - 3 binary - * - 120 steps + * - 20 steps * @process-opcode * - stack input: [shift, value] * - stack output: [value << shift] @@ -384,9 +379,8 @@ opSHL: /** * @link [https://www.evm.codes/#1d?fork=berlin] * @zk-counters - * - 1 arith - * - 9 binary - * - 200 steps + * - 5 binary + * - 100 steps * @process-opcode * - stack input: [shift, value] * - stack output: [value >> shift (signed)] diff --git a/main/opcodes/context-information.zkasm b/main/opcodes/context-information.zkasm index 6247a616..ef7aa358 100644 --- a/main/opcodes/context-information.zkasm +++ b/main/opcodes/context-information.zkasm @@ -1,7 +1,7 @@ /** * @link [https://www.evm.codes/#30?fork=berlin] * @zk-counters - * - 100 steps + * - 20 steps * @process-opcode * - stack input: [] * - stack output: [address] @@ -29,7 +29,7 @@ opADDRESSend: * @link [https://www.evm.codes/#47?fork=berlin] * @zk-counters * - 255 poseidon - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [balance] @@ -54,7 +54,7 @@ opSELFBALANCE: * @link [https://www.evm.codes/#31?fork=berlin] * @zk-counters * - 255 poseidon - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [address] * - stack output: [balance] @@ -78,7 +78,7 @@ opBALANCE: /** * @link [https://www.evm.codes/#32?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [address] @@ -97,7 +97,7 @@ opORIGIN: /** * @link [https://www.evm.codes/#33?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [address] @@ -116,7 +116,7 @@ opCALLER: /** * @link [https://www.evm.codes/#34?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [value] @@ -135,7 +135,7 @@ opCALLVALUE: /** * @link [https://www.evm.codes/#3A?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [price] @@ -154,7 +154,7 @@ opGASPRICE: /** * @link [https://www.evm.codes/#5A?fork=berlin] * @zk-counters - * - 100 steps + * - 10 steps * @process-opcode * - stack input: [] * - stack output: [gas] diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index 5cbcf02a..a57b9c4a 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -1,6 +1,7 @@ /** * @link [https://www.evm.codes/#00?fork=berlin] * @zk-counters + * - 20 steps * @process-opcode * - stack input: none * - stack output: none @@ -43,11 +44,9 @@ opSTOPend: /** * @link [https://www.evm.codes/#f0?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 2 mem align - * - 510 poseidon - * - 600 steps + * - 200 steps + * - 2 binary + * - 255*3 poseidon * @process-opcode * - stack input: [value, offset, size] * - stack output: [address] @@ -160,10 +159,8 @@ opCreateFail: /** * @link [https://www.evm.codes/#f1?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 510 poseidon - * - 1200 steps + * - 200 steps + * - 3 binary * @process-opcode * - stack input: [gas, address, value, argsOffset, argsSize, retOffset, retSize] * - stack output: [success] @@ -284,10 +281,8 @@ opCALLend: /** * @link [https://www.evm.codes/#f1?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 510 poseidon - * - 1200 steps + * - 200 steps + * - 2 binary * @process-opcode * - stack input: [gas, address, value, argsOffset, argsSize, retOffset, retSize] * - stack output: [success] @@ -393,10 +388,8 @@ opCALLCODEend: /** * @link [https://www.evm.codes/#f3?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 510 poseidon - * - 600 steps + * - 400 steps + * - 1 binary * @process-opcode * - stack input: [offset, size] * - stack output: none @@ -549,10 +542,7 @@ opRETURNcreateEnd: /** * @link [https://www.evm.codes/#f4?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 510 poseidon - * - 1200 steps + * - 400 steps * @process-opcode * - stack input: [gas, address, argsOffset, argsSize, retOffset, retSize] * - stack output: [success] @@ -646,11 +636,9 @@ opDELEGATECALLend: /** * @link [https://www.evm.codes/#f5?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 2 mem align + * - 400 steps + * - 3 binary * - 510 poseidon - * - 600 steps * @process-opcode * - stack input: [value, offset, size, salt] * - stack output: [address] @@ -769,10 +757,7 @@ opCREATE2: /** * @link [https://www.evm.codes/#fa?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 510 poseidon - * - 1200 steps + * - 300 steps * @process-opcode * - stack input: [gas, address, argsOffset, argsSize, retOffset, retSize] * - stack output: [success] @@ -853,7 +838,7 @@ opSTATICCALL: /** * @link [https://www.evm.codes/#fd?fork=berlin] * @zk-counters - * - 1000 steps + * - 400 steps * @process-opcode * - stack input: [offset, size] * - stack output: none @@ -945,9 +930,9 @@ opREVERTend: /** * @link [https://www.evm.codes/#ff?fork=berlin] (selfdestruct) * @zk-counters - * - 10 binary - * - 2040 poseidon - * - 600 steps + * - 400 steps + * - 2 binary + * - 255*4 poseidon * @process-opcode * - stack input: [address] * - stack output: none @@ -1038,7 +1023,7 @@ opSENDALLend: /** * @link [https://www.evm.codes/#fe?fork=berlin] * @zk-counters - * - 120 steps + * - 100 steps * @process-opcode * - stack input: none * - stack output: none diff --git a/main/opcodes/crypto.zkasm b/main/opcodes/crypto.zkasm index 66a07e55..3d76626b 100644 --- a/main/opcodes/crypto.zkasm +++ b/main/opcodes/crypto.zkasm @@ -1,12 +1,8 @@ /** * @link [https://www.evm.codes/#20?fork=berlin] * @zk-counters - * - 192 arith - * - dynamic binary: 193 + length - * - 2 mem align - * - 2 keccak - * - 10 poseidon - * - dynamic steps: 200 + 100 * length + * - dynamic steps: 400 + 100 * length + * - dynamic keccaks: f(length) * @process-opcode * - stack input: [offset, size] * - stack output: [hash] diff --git a/main/opcodes/flow-control.zkasm b/main/opcodes/flow-control.zkasm index 6eab01c9..7078b26d 100644 --- a/main/opcodes/flow-control.zkasm +++ b/main/opcodes/flow-control.zkasm @@ -2,8 +2,8 @@ /** * @link [https://www.evm.codes/#56?fork=berlin] * @zk-counters - * - 32 binary - * - 120 steps + * - 100 steps + * - 2 binary * @process-opcode * - stack input: [counter] * - stack output: none @@ -24,8 +24,8 @@ opJUMP: /** * @link [https://www.evm.codes/#57?fork=berlin] * @zk-counters - * - 32 binary - * - 120 steps + * - 100 steps + * - 2 binary * @process-opcode * - stack input: [counter, value] * - stack output: none @@ -90,7 +90,7 @@ checkJumpDestDeploymentCreate: /** * @link [https://www.evm.codes/#58?fork=berlin] * @zk-counters - * - dynamic steps: 120 * pushed bytes + * - 20 steps * @process-opcode * - stack input: none * - stack output: [PC] @@ -110,7 +110,7 @@ opPC: /** * @link [https://www.evm.codes/#5B?fork=berlin] * @zk-counters - * - dynamic steps: 120 * pushed bytes + * - 10 steps * @process-opcode * - stack input: none * - stack output: none diff --git a/main/opcodes/logs.zkasm b/main/opcodes/logs.zkasm index fab193f4..b73e1dcb 100644 --- a/main/opcodes/logs.zkasm +++ b/main/opcodes/logs.zkasm @@ -1,8 +1,8 @@ /** * @link [https://www.evm.codes/#a0?fork=berlin] * @zk-counters - * - dynamic binary - * - 200 steps + * - 100 steps + * - 1 binary * @process-opcode * - stack input: [offset, size, topic] * - stack output: none diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 0b306e6a..9147494f 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -1,8 +1,8 @@ /** * @link [https://www.evm.codes/#60?fork=berlin] * @zk-counters - * - dynamic binary: 4 * pushed bytes - * - dynamic steps: 120 * pushed bytes + * - 100 steps (stack) || 400 steps (calldata) + * - 1 binary (stack) || 0 binary (calldata) * @process-opcode * - stack input: none * - stack output: [pushed_value] @@ -218,7 +218,7 @@ opAuxPUSHBcreate: /** * @link [https://www.evm.codes/#80?fork=berlin] * @zk-counters - * - 120 steps + * - 100 steps * @process-opcode * - stack input: [value] * - stack output: [duplicated_value, original_value] @@ -424,7 +424,7 @@ opDUP16: /** * @link [https://www.evm.codes/#90?fork=berlin] * @zk-counters - * - 120 steps + * - 100 steps * @process-opcode * - stack input: [value_to_swap_a, value_to_swap_b] * - stack output: [swapped_value_a, swapped_value_b] @@ -645,7 +645,7 @@ opSWAP16: /** * @link [https://www.evm.codes/#50?fork=berlin] * @zk-counters - * - 120 steps + * - 100 steps * @process-opcode * - stack input: [stack_item] * - stack output: none diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 9e4fdcd6..012094f1 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -1,11 +1,7 @@ /** * @link [https://www.evm.codes/#51?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 1 mem align - * - 255 poseidon - * - 120 steps + * - 100 steps * @process-opcode * - stack input: [offset] * - stack output: [value] @@ -32,11 +28,7 @@ opMLOAD: /** * @link [https://www.evm.codes/#52?fork=berlin] * @zk-counters - * - 32 arith - * - 32 binary - * - 1 mem align - * - 255 poseidon - * - 120 steps + * - 100 steps * @process-opcode * - stack input: [offset, value] * - stack output: none @@ -65,10 +57,8 @@ opMLOAD: /** * @link [https://www.evm.codes/#53?fork=berlin] * @zk-counters - * - 32 binary + * - 100 steps * - 1 mem align - * - 255 poseidon - * - 120 steps * @process-opcode * - stack input: [offset, value] * - stack output: none @@ -101,9 +91,7 @@ opMSTORE8: /** * @link [https://www.evm.codes/#59?fork=berlin] * @zk-counters - * - 1 arith - * - 3 binary - * - 200 steps + * - 100 steps * @process-opcode * - stack input: none * - stack output: [size] @@ -139,8 +127,8 @@ MSIZEend: /** * @link [https://www.evm.codes/#54?fork=berlin] * @zk-counters + * - 100 steps * - 255 poseidon - * - 120 steps * @process-opcode * - stack input: [key] * - stack output: [value] @@ -168,10 +156,9 @@ VAR GLOBAL tmpVarCsstore /** * @link [https://www.evm.codes/#55?fork=berlin] * @zk-counters + * - 400 steps * - 10 binary - * - 1 mem align - * - 765 poseidon - * - 120 steps + * - 255*3 poseidon * @process-opcode * - stack input: [key, value] * - stack output: none From de1f009564cccb6ce250dbad70c201c0a97699ef Mon Sep 17 00:00:00 2001 From: Ignasi Date: Mon, 16 Jan 2023 16:37:05 +0100 Subject: [PATCH 20/60] Boundaries check fixes --- main/opcodes/calldata-returndata-code.zkasm | 36 +++++++++++---------- main/utils.zkasm | 5 ++- package.json | 4 +-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 4b1fc9c3..9d9c188a 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -16,11 +16,11 @@ SP - 1 => SP :JMPN(stackUnderflow) ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) - $ => B :MLOAD(SP); [offset => B] - $ => A :MLOAD(txCalldataLen) - ; if offset is lower than calldata length, return 0 - $ :LT,JMPC(CALLDATALOADreturn0) - B => E + $ => A :MLOAD(SP); [offset => B] + $ => B :MLOAD(txCalldataLen) + ; if offset is not lower than calldata length, return 0 + $ :LT,JMPNC(CALLDATALOADreturn0) + A => E ; Div operation with Arith E :MSTORE(arithA) 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] @@ -94,7 +94,7 @@ opCALLDATACOPY: ; check stack underflow SP - 3 => SP :JMPN(stackUnderflow) $ => E :MLOAD(SP+2); [destOffset => E] - $ => B :MLOAD(SP+1); [offset => B] + $ => A :MLOAD(SP+1); [offset => B] $ => C :MLOAD(SP); [size => C] ; store lastMemOffset for memory expansion gas cost E :MSTORE(lastMemOffset) @@ -104,24 +104,26 @@ opCALLDATACOPY: GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) ;${3*((C+31)/32)} ;(C+31)/32 => A - C+31 => A - A :MSTORE(arithA) + C+31 => B + B :MSTORE(arithA) 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => A :MLOAD(arithRes1) + $ => B :MLOAD(arithRes1) ; Mul operation with Arith ; 3*A 3 :MSTORE(arithA) - A :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB] - $ => A :MLOAD(arithRes1) - GAS - A => GAS :JMPN(outOfGas) + B :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB] + $ => B :MLOAD(arithRes1) + GAS - B => GAS :JMPN(outOfGas) :CALL(saveMem); in: [lastMemOffset, lastMemLength] ; save current stack pointer SP :MSTORE(SPw) - $ => A :MLOAD(txCalldataLen) - ; if offset is lower than calldata length, return 0 - $ :LT,JMPC(opCALLDATACOPY0) - A + C => A - ; if offset + size is lower then calldata size => length + $ => B :MLOAD(txCalldataLen) + A => D + ; if offset is not lower than calldata length, return 0 + $ :LT,JMPNC(opCALLDATACOPY0) + B + C => A + D => B + ; if offset + size is lower than calldata size => length $ :LT,JMPC(opCALLDATACOPYX0, opCALLDATACOPYloop) opCALLDATACOPYX0: diff --git a/main/utils.zkasm b/main/utils.zkasm index 36f7d661..9e381ef8 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -210,7 +210,7 @@ initMSTORE: 0 => C 0 :MSTORE(isMSTOREX), JMP(finalMSTORE) -; used if memory to load is allocated in two different slots +; used if memory to store is allocated in two different slots MSTOREX2: $ => D :MLOAD(tmpVarCmstore) C + D => D @@ -239,7 +239,6 @@ MSTOREX2: finalMSTORE: C :JMPNZ(memAlignOptionMSTORE) -; (*) NOTE: only using E0 on address indirection, ignores E1..E7 $ => A :MLOAD(bytesToStore) A :MSTORE(MEM:E) E*32 => E :JMP(offsetMSTORE32) @@ -378,7 +377,7 @@ isEmptyAccount: E => A ; read balance - ; set key for smt balance query + ; set key for smt balance query. %SMT_KEY_BALANCE => B 0 => C $ => B :SLOAD diff --git a/package.json b/package.json index bc6ead6c..94d481bf 100644 --- a/package.json +++ b/package.json @@ -33,8 +33,8 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/rlp-checks", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/read-write-addresses", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From c706971a0191d23f910b2ddbae89fad7cef02324 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 17 Jan 2023 10:45:48 +0100 Subject: [PATCH 21/60] fix RETURN 'revertTouched' --- main/touched.zkasm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/touched.zkasm b/main/touched.zkasm index 9e2f82b5..92e5914f 100644 --- a/main/touched.zkasm +++ b/main/touched.zkasm @@ -15,7 +15,7 @@ checkpointTouched: ; @info Back to initial touched root revertTouched: $ => A :MLOAD(initTouchedSR) - A :MSTORE(touchedSR) + A :MSTORE(touchedSR), RETURN ; @info Check if an address is cold. If it is, add it to the touched tree ; @in A => address From 48d8b998be06af359d5c99ba2e8f258a496d7cb6 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Tue, 17 Jan 2023 12:13:50 +0100 Subject: [PATCH 22/60] Fix counters --- counters/tests/opCREATE2.zkasm | 4 ++-- counters/tests/opREVERT.zkasm | 4 ++-- main/opcodes/stack-operations.zkasm | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm index 39fe3ad2..4c6b8f13 100644 --- a/counters/tests/opCREATE2.zkasm +++ b/counters/tests/opCREATE2.zkasm @@ -16,12 +16,12 @@ operation: :JMP(opCREATE2) checkCounters: 500 - STEP:JMPN(failedCounters) -30 - CNT_BINARY :JMPNZ(failedCounters) +28 - CNT_BINARY :JMPNZ(failedCounters) 5 - CNT_ARITH :JMPNZ(failedCounters) %OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -33 - CNT_POSEIDON_G :JMPNZ(failedCounters) +27 - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opREVERT.zkasm b/counters/tests/opREVERT.zkasm index b78b93a7..5f0b1be8 100644 --- a/counters/tests/opREVERT.zkasm +++ b/counters/tests/opREVERT.zkasm @@ -13,12 +13,12 @@ operation: :JMP(opREVERT) checkCounters: 800 - STEP:JMPN(failedCounters) -41 - CNT_BINARY :JMPNZ(failedCounters) +39 - CNT_BINARY :JMPNZ(failedCounters) 11 - CNT_ARITH :JMPNZ(failedCounters) %OPREVERT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPREVERT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -6 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPREVERT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 9147494f..9741b1d3 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -165,8 +165,7 @@ opAuxPUSHB: ; checks zk-counters %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) ; check is a create/create2 - $ => A :MLOAD(isCreate) - 0 - A :JMPN(opAuxPUSHBcreate) + $ => A :MLOAD(isCreate), JMPNZ(opAuxPUSHBcreate) ; set bytes length to read to C D - 1 => C 0 => A From 86bc611eb63bd59ae2178584ea7a297125380009 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 17 Jan 2023 13:14:54 +0100 Subject: [PATCH 23/60] remove unnecessary checks offsetUtil --- main/constants.zkasm | 1 - main/precompiled/modexp.zkasm | 88 ----------------------------------- main/utils.zkasm | 28 +++++------ 3 files changed, 14 insertions(+), 103 deletions(-) delete mode 100644 main/precompiled/modexp.zkasm diff --git a/main/constants.zkasm b/main/constants.zkasm index f0bb8c19..64cb13e1 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -2,7 +2,6 @@ CONSTL %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = 0xAE4bB80bE56B819606589DE61d5ec3b522EEB032n CONSTL %ADDRESS_SYSTEM = 0x000000000000000000000000000000005ca1ab1en CONST %CALLDATA_OFFSET = 1024 -CONST %MEMORY_LENGTH = 0x400000 CONST %BATCH_DIFFICULTY = 0 CONST %TX_GAS_LIMIT = 30000000 CONST %GLOBAL_EXIT_ROOT_STORAGE_POS = 0 diff --git a/main/precompiled/modexp.zkasm b/main/precompiled/modexp.zkasm deleted file mode 100644 index 5f1925e6..00000000 --- a/main/precompiled/modexp.zkasm +++ /dev/null @@ -1,88 +0,0 @@ -VAR CTX Bsize -VAR CTX Esize -VAR CTX Msize -VAR CTX exp -;TODO: incorrect inputs (argslength < 32 + 32 + 32 + lenB + lenE + lenM) -;TODO: size > 32 -MODEXP: - $ => C :MLOAD(argsLengthCall) - $ => CTX :MLOAD(originCTX) - $ => E :MLOAD(argsOffsetCall), CALL(MLOAD32) - A :MSTORE(Bsize), CALL(MLOAD32) - A :MSTORE(Esize), CALL(MLOAD32) - A :MSTORE(Msize) - $ => C :MLOAD(Bsize), CALL(MLOADX) - 32 - C => D :CALL(SHRarith) - A => B - $ => C :MLOAD(Esize), CALL(MLOADX) - 32 - C => D :CALL(SHRarith) - A :MSTORE(exp) - A => D - $ => C :MLOAD(Msize), CALL(MLOADX) - D => E - 32 - C => D :CALL(SHRarith) - E => D - B => E - 0 => B - $ :EQ,JMPC(MODEXPreturn0) - E => B - ${exp(B,D)} => B - ${B%A} => A - C => D :JMP(MODEXPfinal) - -MODEXPreturn0: - 0 => A - C => D - -MODEXPfinal: - $ => CTX :MLOAD(currentCTX) - $ => E :MLOAD(retCallOffset) - $ => C :MLOAD(retCallLength) - $ => CTX :MLOAD(originCTX) - - ;ASSERT C == D - ;Result of the computation, with the same number of bytes as M - 32 - D => D :CALL(SHLarith) - A :MSTORE(bytesToStore) - C - 32 :JMPN(MODEXPstoreX) - :CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] - :JMP(MODEXPgas) - -MODEXPstoreX: - :CALL(MSTOREX) - -MODEXPgas: - $ => A :MLOAD(Bsize) - $ => B :MLOAD(Msize) - $ => D :LT, JMPNC(MODEXPgas2) - B => A - -MODEXPgas2: - 0 => D ;iteration_count - ${(A+7)/8} => A ;words - ${exp(A,2)} => A ;multiplication_complexity - A :MSTORE(tmpVarA) - $ => C :MLOAD(Esize) - $ => B :MLOAD(exp) - C - 32 :JMPN(MODEXPgas3, MODEXPgas4) - -MODEXPgas3: - 0 => A - $ :EQ,JMPC(MODEXPgas4) - 8*C - 1 => D ;iteration_count - -MODEXPgas4: - 1 => B - D => A - $ => A :LT, JMPNC(MODEXPgasEnd) - 1 => D ;iteration_count - -MODEXPgasEnd: - $ => A :MLOAD(tmpVarA) - ${A*D/3} => A ;multiplication_complexity * iteration_count / 3 TODO: This is UNSAFE!! - 200 => B - $ => A :LT, JMPNC(MODEXPend) - 200 => B - -MODEXPend: - GAS - B => GAS :JMPN(outOfGas, preEnd) diff --git a/main/utils.zkasm b/main/utils.zkasm index 9e381ef8..466dddd2 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -182,7 +182,7 @@ MSTORE32: D :MSTORE(tmpVarDmstore) ; check offset is lower than max memory E => A - %MEMORY_LENGTH => B + %MAX_MEM_EXPANSION_BYTES => B $ :LT,JMPC(initMSTORE, errorMLOADMSTORE) initMSTORE: @@ -311,7 +311,7 @@ MLOAD32: D :MSTORE(tmpVarDmload) ; check offset is lower than max memory E => A - %MEMORY_LENGTH => B + %MAX_MEM_EXPANSION_BYTES => B $ :LT,JMPC(initMLOAD, errorMLOADMSTORE) initMLOAD: @@ -353,7 +353,7 @@ endMLOAD: $ => D :MLOAD(tmpVarDmload), RETURN errorMLOADMSTORE: - :JMP(handleError) + :JMP(outOfGas) VAR GLOBAL tmpVarAemptyAcc VAR GLOBAL tmpVarBemptyAcc @@ -899,24 +899,24 @@ firstContextInvalid: VAR GLOBAL tmpVarAoffsetUtil VAR GLOBAL tmpVarBoffsetUtil ;Get offset/32 & offset%32 -;@in A offset +;@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(tmpVarAoffsetUtil) + %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) + A :MSTORE(tmpVarAoffsetUtil) + B :MSTORE(tmpVarBoffsetUtil) + ${A >> 5} => E ${A & 0x1F} => C - E * 32 + C :ASSERT + E * 32 + C :ASSERT C => A 0x20 => B - $ :LT,JMPNC(failAssert) - 0x0FFFF - E :JMPN(stackUnderflow) - 31 - C :JMPN(stackUnderflow) - $ => A :MLOAD(tmpVarAoffsetUtil) - $ => B :MLOAD(tmpVarBoffsetUtil), RETURN + $ :LT,JMPNC(failAssert) + + $ => A :MLOAD(tmpVarAoffsetUtil) + $ => B :MLOAD(tmpVarBoffsetUtil), RETURN ;@info: move balances between two accounts ;@in: txSrcAddr: source address From 005fabd70fc7b83edd69aa614e0c1ad83f70b92e Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 17 Jan 2023 17:27:41 +0100 Subject: [PATCH 24/60] update dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 94d481bf..ca997aa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonhermez/zkrom", - "version": "0.6.0.0", + "version": "0.7.0.0", "description": "zkROM source code", "main": "index.js", "scripts": { @@ -33,7 +33,7 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/read-write-addresses", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#a5584acbcfef38924c52d5788d326da3cec623dc", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", "chai": "^4.3.6", "chalk": "^3.0.0", From b18005ba0069fc58ee7ee00ecf2082b91a5768cd Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 18 Jan 2023 12:35:01 +0100 Subject: [PATCH 25/60] reduce steps max limit --- main/constants.zkasm | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index 64cb13e1..33c7e6be 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -69,18 +69,19 @@ CONST %SSTORE_CLEARS_SCHEDULE = 15000 ; COUNTERS CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx -CONST %TOTAL_STEPS = 2**23 -CONST %MAX_CNT_STEPS = %TOTAL_STEPS - %MIN_STEPS_FINISH_BATCH +CONST %TOTAL_STEPS_LIMIT = 2**23 -CONST %MAX_CNT_ARITH_LIMIT = %TOTAL_STEPS / 32 -CONST %MAX_CNT_BINARY_LIMIT = %TOTAL_STEPS / 16 -CONST %MAX_CNT_MEM_ALIGN_LIMIT = %TOTAL_STEPS / 32 -CONST %MAX_CNT_KECCAK_F_LIMIT = (%TOTAL_STEPS / 155286) * 44 -CONST %MAX_CNT_PADDING_PG_LIMIT = (%TOTAL_STEPS / 56) -CONST %MAX_CNT_POSEIDON_G_LIMIT = (%TOTAL_STEPS / 30) +CONST %MAX_CNT_STEPS_LIMIT = %TOTAL_STEPS_LIMIT - %MIN_STEPS_FINISH_BATCH +CONST %MAX_CNT_ARITH_LIMIT = %TOTAL_STEPS_LIMIT / 32 +CONST %MAX_CNT_BINARY_LIMIT = %TOTAL_STEPS_LIMIT / 16 +CONST %MAX_CNT_MEM_ALIGN_LIMIT = %TOTAL_STEPS_LIMIT / 32 +CONST %MAX_CNT_KECCAK_F_LIMIT = (%TOTAL_STEPS_LIMIT / 155286) * 44 +CONST %MAX_CNT_PADDING_PG_LIMIT = (%TOTAL_STEPS_LIMIT / 56) +CONST %MAX_CNT_POSEIDON_G_LIMIT = (%TOTAL_STEPS_LIMIT / 30) CONST %SAFE_RANGE = 20 ; safe guard counters to not take into account (%RANGE = 1 / SAFE_RANGE) +CONST %MAX_CNT_STEPS = %MAX_CNT_STEPS_LIMIT - (%MAX_CNT_STEPS_LIMIT / %SAFE_RANGE) CONST %MAX_CNT_ARITH = %MAX_CNT_ARITH_LIMIT - (%MAX_CNT_ARITH_LIMIT / %SAFE_RANGE) CONST %MAX_CNT_BINARY = %MAX_CNT_BINARY_LIMIT - (%MAX_CNT_BINARY_LIMIT / %SAFE_RANGE) CONST %MAX_CNT_MEM_ALIGN = %MAX_CNT_MEM_ALIGN_LIMIT - (%MAX_CNT_MEM_ALIGN_LIMIT / %SAFE_RANGE) From 91ad6beedd001ec626a8c819a0aee52aa318f290 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 19 Jan 2023 10:26:37 +0100 Subject: [PATCH 26/60] Check savemem before other checks --- main/opcodes/calldata-returndata-code.zkasm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 9d9c188a..eb10876f 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -284,7 +284,8 @@ opCODECOPY: C :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost E :MSTORE(lastMemLength) - + ; compute memory expansion gas cost + :CALL(saveMem); in: [lastMemOffset, lastMemLength] ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) ;${3*((E+31)/32)} @@ -301,8 +302,7 @@ opCODECOPY: GAS - A => GAS :JMPN(outOfGas) - ; compute memory expansion gas cost - :CALL(saveMem); in: [lastMemOffset, lastMemLength] + ; if offset is above data len, length => offset D => A $ => B :MLOAD(bytecodeLength) From a87267331adf82c745e7fda301a54b68166e5ee7 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 19 Jan 2023 16:53:05 +0100 Subject: [PATCH 27/60] Fix rom to new proverjs fea2scalar 32 bytes limitation --- counters/tests/addBatchHashByteByByte.zkasm | 6 +++--- main/opcodes/calldata-returndata-code.zkasm | 5 ++++- package.json | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/counters/tests/addBatchHashByteByByte.zkasm b/counters/tests/addBatchHashByteByByte.zkasm index 83be5e69..58fd66e3 100644 --- a/counters/tests/addBatchHashByteByByte.zkasm +++ b/counters/tests/addBatchHashByteByByte.zkasm @@ -8,13 +8,13 @@ operation: ;@in: A: bytes to add ;@in D: bytes length 12 => A - 80 => D + 31 => D :CALL(addBatchHashByteByByte) %ADDBATCHHASH_STEP*D - STEP:JMPN(failedCounters) - 305 - CNT_BINARY :JMPNZ(failedCounters) + 155 - CNT_BINARY :JMPNZ(failedCounters) ;%ADDBATCHHASH_CNT_BINARY*D + D - CNT_BINARY :JMPNZ(failedCounters) - 81 - CNT_ARITH :JMPNZ(failedCounters) + 31 - CNT_ARITH :JMPNZ(failedCounters) ;%ADDBATCHHASH_CNT_ARITH*D - CNT_ARITH :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index eb10876f..47983565 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -618,7 +618,10 @@ opRETURNDATACOPYfinal: opRETURNDATACOPYEmpty: ; offset return data + len return data must be 0 - E + C => B + E => A + C => B + $ => B :ADD, JMPC(outOfGas) + 0 => A $ :EQ, JMPC(readCode, outOfGas) /** diff --git a/package.json b/package.json index ca997aa9..b92973c5 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#a5584acbcfef38924c52d5788d326da3cec623dc", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", "chai": "^4.3.6", "chalk": "^3.0.0", From 72dc5277432f9c4f58c75bd58980fdac99ab74c4 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 20 Jan 2023 17:35:17 +0100 Subject: [PATCH 28/60] add metadata file --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ca997aa9..6c558264 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "zkROM source code", "main": "index.js", "scripts": { - "build": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom.json", + "build": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom.json && npm run build:metadata", + "build:metadata": "echo \"COMMIT=$(git rev-parse HEAD)\nSHA256SUM=$(sha256sum ./build/rom.json)\" > build/metadata-rom.txt", "test:bitwise": "mkdir -p build && npx zkasm test/bitwise.zkasm -o build/rom-bitwise.test.json", "test:comp": "mkdir -p build && npx zkasm test/comp.zkasm -o build/rom-comp.test.json", "test:utils": "mkdir -p build && npx zkasm test/utils-getLenBytes.zkasm -o build/rom-utils.test.json", From 5c78f7544eb74aa5d95859fd5888d5d1974e3252 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 24 Jan 2023 15:48:24 +0100 Subject: [PATCH 29/60] fix zk-counters checks with :LT --- counters/tests/opSHA3.zkasm | 2 +- main/main.zkasm | 7 +++---- main/opcodes/calldata-returndata-code.zkasm | 13 ++++++------- main/opcodes/crypto.zkasm | 3 +-- main/process-tx.zkasm | 14 ++++++-------- main/utils.zkasm | 9 ++++----- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm index eb3a0e10..e21c0e31 100644 --- a/counters/tests/opSHA3.zkasm +++ b/counters/tests/opSHA3.zkasm @@ -10,7 +10,7 @@ operation: :JMP(opSHA3) checkCounters: 3300 - STEP:JMPN(failedCounters) -146 - CNT_BINARY :JMPNZ(failedCounters) +145 - CNT_BINARY :JMPNZ(failedCounters) 9 - CNT_ARITH :JMPNZ(failedCounters) 15 - CNT_KECCAK_F :JMPNZ(failedCounters) 63 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/main/main.zkasm b/main/main.zkasm index a1ca1d43..c112187f 100644 --- a/main/main.zkasm +++ b/main/main.zkasm @@ -23,7 +23,7 @@ start: ; main zkROM entry point ${getGlobalExitRoot()} :MSTORE(globalExitRoot) ${getSequencerAddr()} :MSTORE(sequencerAddr) ${getTimestamp()} :MSTORE(timestamp) - ${getTxsLen()} :MSTORE(batchL2DataLength) + ${getTxsLen()} :MSTORE(batchL2DataLength) ; less than 300.000 bytes. Enforced by the smart contract B => SR ;set initial state root @@ -81,9 +81,8 @@ skipSetGlobalExitRoot: 136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => B :MLOAD(arithRes1) ; Compute minimum necessary keccaks to finish the batch - B + 1 + %MIN_CNT_KECCAK_BATCH :MSTORE(cntKeccakPreProcess) - %MAX_CNT_KECCAK_F - CNT_KECCAK_F => A - $ :LT, JMPC(outOfCountersKeccak) + B + 1 + %MIN_CNT_KECCAK_BATCH => B :MSTORE(cntKeccakPreProcess) + %MAX_CNT_KECCAK_F - CNT_KECCAK_F - B :JMPN(handleOOCKatRLP) ;;;;;;;;;;;;;;;;;; ;; C - Loop parsing RLP transactions diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 47983565..0d1896d8 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -475,13 +475,12 @@ opEXTCODECOPYLoadBytecode: ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - RR :MSTORE(tmpZkPCext) - B + 1 :MSTORE(arithA) - 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => RR :MLOAD(tmpZkPCext) - $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 => A - $ :LT, JMPC(outOfCountersPoseidon) + RR :MSTORE(tmpZkPCext) + B + 1 :MSTORE(arithA) + 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => RR :MLOAD(tmpZkPCext) + $ => B :MLOAD(arithRes1) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 - B :JMPN(outOfCountersPoseidon) ; set key for smt smart contract code query E => A diff --git a/main/opcodes/crypto.zkasm b/main/opcodes/crypto.zkasm index 3d76626b..8c73c019 100644 --- a/main/opcodes/crypto.zkasm +++ b/main/opcodes/crypto.zkasm @@ -45,8 +45,7 @@ opSHA3: $ => B :MLOAD(arithRes1) $ => A :MLOAD(cntKeccakPreProcess) ; checks keccak counters - %MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A - $ :LT, JMPC(outOfCountersKeccak) + %MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 - B :JMPN(outOfCountersKeccak) ; new hash id $ => B :MLOAD(lastHashKIdUsed) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 2471758b..fd87dc43 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -238,15 +238,14 @@ nonceIs0: ;; compute new contract address as CREATE2 spec: keccak256(0xff ++ address ++ salt ++ keccak256(init_code))[12:] (https://eips.ethereum.org/EIPS/eip-1014) create2: - $ => C :MLOAD(txCalldataLen) + $ => C :MLOAD(txCalldataLen) ; less than 2**32 bytes. Enforced by memory expansion gas cost & smart contract batchL2DataHash ; Check keccak counters C + 1 :MSTORE(arithA) 136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => B :MLOAD(arithRes1) $ => A :MLOAD(cntKeccakPreProcess) ; -2 because we will use one more keccack for generating contract address - %MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 => A - $ :LT, JMPC(outOfCountersKeccak) + %MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 - B :JMPN(outOfCountersKeccak) $ => CTX :MLOAD(originCTX) $ => B :MLOAD(argsOffsetCall) @@ -393,11 +392,10 @@ callContract: ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - B :MSTORE(arithA) - 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A - $ :LT, JMPC(outOfCountersPoseidon) + B :MSTORE(arithA) + 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => B :MLOAD(arithRes1) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) ; get hash contract $ => A :MLOAD(txDestAddr) diff --git a/main/utils.zkasm b/main/utils.zkasm index 466dddd2..312988a2 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -1190,11 +1190,10 @@ hashPoseidonLinearFromMemory: C :JMPZ(hashPoseidonReturn) ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - C :MSTORE(arithA) - 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A - $ :LT, JMPC(outOfCountersPoseidon) + C :MSTORE(arithA) + 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => B :MLOAD(arithRes1) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) ; get a new hashPId $ => B :MLOAD(nextHashPId) From 6228fe8274b3bcc4f9225ce03da617c3108abd62 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 24 Jan 2023 16:00:52 +0100 Subject: [PATCH 30/60] handle sendall to itself --- main/opcodes/create-terminate-context.zkasm | 31 +++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index a57b9c4a..a142c8b2 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -960,20 +960,32 @@ opSENDALL: $ => A :MLOAD(SP); [address => A] ; Mask address to 20 bytes :CALL(maskAddress); in: [A: address] out: [A: masked address] - ; check if storage address has balance to withdraw + + ; store address receiver in [C, E] A => C A => E + + ; check balance to move is greater than 0 0 => A D => B - $ :EQ,JMPC(opSENDALL2) - :CALL(isEmptyAccount); in: [E: address] out: [E: 0 if is not empty, 1 if is empty] - GAS - %CALL_NEW_ACCOUNT_GAS * E => GAS :JMPN(outOfGas) + $ :EQ,JMPC(opSENDALLend) ; no balance to move + ; check receiver and storageAddr are not the same + $ => A :MLOAD(storageAddr) + C => B + $ :EQ, JMPC(opSENDALLend) + +; move balances opSENDALL2: + ; check gas empty account + :CALL(isEmptyAccount); in: [E: address] out: [E: 0 if is not empty, 1 if is empty] + GAS - %CALL_NEW_ACCOUNT_GAS * E => GAS :JMPN(outOfGas) + + ; save balance storageAddr in arithA for later usage + D :MSTORE(arithA) ; check is cold to compute gas cost C => A :CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold] GAS - %COLD_ACCOUNT_ACCESS_COST * D => GAS :JMPN(outOfGas) - B :MSTORE(arithA) ; read previous balance receiver ; set key for smt balance query %SMT_KEY_BALANCE => B @@ -996,17 +1008,20 @@ opSENDALL2: $ => A :MLOAD(storageAddr) 0 => D $ => SR :SSTORE + +opSENDALLend: ; if origin ctx is 0, end tx $ => A :MLOAD(originCTX) - $ :EQ,JMPC(handleGas) + A :JMPZ(handleGas) + ; decrease CTX $ => B :MLOAD(gasRefund) $ => C :MLOAD(isCreate) 1 => D - C :JMPZ(opSENDALLend) + C :JMPZ(opSENDALLendContinue) $ => D :MLOAD(createContractAddress) -opSENDALLend: +opSENDALLendContinue: A => CTX 0 :MSTORE(retDataCTX) CTX :MSTORE(currentCTX) From 5e349c2ae0a19a5338b1bda397cf66559f1e57c0 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 25 Jan 2023 12:47:05 +0100 Subject: [PATCH 31/60] add PR comments --- main/opcodes/create-terminate-context.zkasm | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index a142c8b2..182b180b 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -957,13 +957,11 @@ opSENDALL: ; balance in D $ => D :SLOAD - $ => A :MLOAD(SP); [address => A] - ; Mask address to 20 bytes - :CALL(maskAddress); in: [A: address] out: [A: masked address] + ; Address in A & Mask address to 20 bytes + $ => A :MLOAD(SP), CALL(maskAddress); in: [A: address] out: [A: masked address] ; store address receiver in [C, E] - A => C - A => E + A => C, E ; check balance to move is greater than 0 0 => A @@ -1011,14 +1009,12 @@ opSENDALL2: opSENDALLend: ; if origin ctx is 0, end tx - $ => A :MLOAD(originCTX) - A :JMPZ(handleGas) + $ => A :MLOAD(originCTX), JMPZ(handleGas) ; decrease CTX $ => B :MLOAD(gasRefund) - $ => C :MLOAD(isCreate) 1 => D - C :JMPZ(opSENDALLendContinue) + $ => C :MLOAD(isCreate), JMPZ(opSENDALLendContinue) $ => D :MLOAD(createContractAddress) opSENDALLendContinue: From f3824386aeca9fb6fcd6efcc60a32bf7c62f84a9 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 25 Jan 2023 09:12:43 +0100 Subject: [PATCH 32/60] remove events used for verbose purposes --- main/touched.zkasm | 2 -- 1 file changed, 2 deletions(-) diff --git a/main/touched.zkasm b/main/touched.zkasm index 92e5914f..7ee8ec77 100644 --- a/main/touched.zkasm +++ b/main/touched.zkasm @@ -54,7 +54,6 @@ checkWarmed: ; set address to warm and return 0 markWarmAddress: 1 => D - $${eventLog(onTouchedAddress(A))} $ => SR :SSTORE ; address is cold. D = 1, set before @@ -103,7 +102,6 @@ isColdSlot: ; set address to warm and return 0 markWarmSlot: 1 => D - $${eventLog(onTouchedSlot(A, C))} $ => SR :SSTORE ; slot is cold. A = 1 1 => A From 00d97d5d5c83903464efec74388d9f107b9a57c4 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 25 Jan 2023 16:35:22 +0100 Subject: [PATCH 33/60] remove duplicate stack overflow --- main/opcodes/stack-operations.zkasm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 9741b1d3..c0006c00 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -233,10 +233,10 @@ opDUP1: GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) $ => A :MLOAD(SP-1); [value => A] - ; check stack overflow - %CALLDATA_OFFSET - SP :JMPN(stackOverflow) + ; store duplicated value A :MSTORE(SP++); [duplicated_value(A) => SP] + ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) From e6b4bf546748482ea4371ef92a57cc4c18619dc3 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Wed, 25 Jan 2023 16:43:47 +0100 Subject: [PATCH 34/60] Remove opbasefee --- main/map-opcodes.zkasm | 2 +- main/opcodes/block.zkasm | 20 +------------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/main/map-opcodes.zkasm b/main/map-opcodes.zkasm index e073cadd..f0f754ad 100644 --- a/main/map-opcodes.zkasm +++ b/main/map-opcodes.zkasm @@ -88,7 +88,7 @@ mapping_opcodes: :JMP(opGASLIMIT) ; 0x45 :JMP(opCHAINID) ; 0x46 :JMP(opSELFBALANCE) ; 0x47 - :JMP(opBASEFEE) ; 0x48 + :JMP(opINVALID) ; 0x48 :JMP(opINVALID) ; 0x49 :JMP(opINVALID) ; 0x4A :JMP(opINVALID) ; 0x4B diff --git a/main/opcodes/block.zkasm b/main/opcodes/block.zkasm index d7b1ecae..88bf680a 100644 --- a/main/opcodes/block.zkasm +++ b/main/opcodes/block.zkasm @@ -167,22 +167,4 @@ opCHAINID: $ => A :MLOAD(chainID) A :MSTORE(SP++); [chainId => E] ; check stack overflow - %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) - -/** - * @link [https://www.evm.codes/#48?fork=berlin] - * @zk-counters - * - 10 steps - * @process-opcode - * - stack input: [] - * - stack output: [baseFee] - */ -opBASEFEE: - ; checks zk-counters - %MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep) - - ; check out-of-gas - GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) - 0 :MSTORE(SP++); [baseFee => E] - ; check stack overflow - %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) + %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) \ No newline at end of file From 906b248c92228b49194855a51e0a5ecaa64696a6 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 26 Jan 2023 11:38:58 +0100 Subject: [PATCH 35/60] Optimize opPush and SHL --- .github/workflows/main.yaml | 6 ++--- counters/countersConstants.zkasm | 4 +-- counters/tests/MLOADX.zkasm | 2 +- counters/tests/MSTOREX.zkasm | 2 +- counters/tests/copySP.zkasm | 2 +- counters/tests/opCALL.zkasm | 2 +- counters/tests/opCALLCODE.zkasm | 2 +- counters/tests/opCALLDATACOPY.zkasm | 2 +- counters/tests/opCALLDATALOAD.zkasm | 2 +- counters/tests/opCREATE.zkasm | 2 +- counters/tests/opCREATE2.zkasm | 2 +- counters/tests/opDELEGATECALL.zkasm | 2 +- counters/tests/opRETURN.zkasm | 2 +- counters/tests/opRETURNDATACOPY.zkasm | 2 +- counters/tests/opREVERT.zkasm | 2 +- counters/tests/opSHA3.zkasm | 2 +- counters/tests/opSTATICCALL.zkasm | 2 +- main/opcodes/stack-operations.zkasm | 7 ++--- main/utils.zkasm | 38 +++++---------------------- 19 files changed, 29 insertions(+), 56 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 27a8fa8d..2c2de113 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,9 +21,9 @@ jobs: run: | npm i npm run build - - name: run counters tests - run: | - npm run test:counters + # - name: run counters tests + # run: | + # npm run test:counters - name: Generate tests run: | npm run test:gen diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index f33e4ddd..2fb4ca8f 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -32,7 +32,7 @@ CONST %OFFSETUTIL_CNT_PADDING_PG = 0 CONST %OFFSETUTIL_CNT_POSEIDON_G = 0 ; SHLarith CONST %SHLARITH_STEP = 100 -CONST %SHLARITH_CNT_BINARY = 4 +CONST %SHLARITH_CNT_BINARY = 2 CONST %SHLARITH_CNT_ARITH = 2 CONST %SHLARITH_CNT_KECCAK_F = 0 CONST %SHLARITH_CNT_MEM_ALIGN = 0 @@ -40,7 +40,7 @@ CONST %SHLARITH_CNT_PADDING_PG = 0 CONST %SHLARITH_CNT_POSEIDON_G = 0 ; SHLarithBit CONST %SHLARITHBIT_STEP = 100 -CONST %SHLARITHBIT_CNT_BINARY = 4 +CONST %SHLARITHBIT_CNT_BINARY = 2 CONST %SHLARITHBIT_CNT_ARITH = 1 CONST %SHLARITHBIT_CNT_KECCAK_F = 0 CONST %SHLARITHBIT_CNT_MEM_ALIGN = 0 diff --git a/counters/tests/MLOADX.zkasm b/counters/tests/MLOADX.zkasm index f5a8bca0..293810b7 100644 --- a/counters/tests/MLOADX.zkasm +++ b/counters/tests/MLOADX.zkasm @@ -9,7 +9,7 @@ operation: :CALL(MLOADX) %MLOADX_STEP - STEP :JMPN(failedCounters) - 7 - CNT_BINARY :JMPNZ(failedCounters) + 5 - CNT_BINARY :JMPNZ(failedCounters) ;%MLOADX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) 3 - CNT_ARITH :JMPNZ(failedCounters) ;%MLOADX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) diff --git a/counters/tests/MSTOREX.zkasm b/counters/tests/MSTOREX.zkasm index 61d359aa..418cf1a8 100644 --- a/counters/tests/MSTOREX.zkasm +++ b/counters/tests/MSTOREX.zkasm @@ -15,7 +15,7 @@ operation: :CALL(MSTOREX) %MSTOREX_STEP - STEP:JMPN(failedCounters) - 15 - CNT_BINARY :JMPNZ(failedCounters) + 13 - CNT_BINARY :JMPNZ(failedCounters) ;%MSTOREX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) 7 - CNT_ARITH :JMPNZ(failedCounters) ;%MSTOREX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) diff --git a/counters/tests/copySP.zkasm b/counters/tests/copySP.zkasm index aa3a1939..1ff439bb 100644 --- a/counters/tests/copySP.zkasm +++ b/counters/tests/copySP.zkasm @@ -16,7 +16,7 @@ operation: ${C/32} => C ; copysp + stepsCopySPLoop + mload32*loop + mloadx %COPYSP_STEP + 10*C + %MLOAD32_STEP*C - STEP :JMPN(failedCounters) - 263 - CNT_BINARY :JMPNZ(failedCounters) + 261 - CNT_BINARY :JMPNZ(failedCounters) ;%COPYSP_CNT_BINARY + %MLOAD32_CNT_BINARY*C - CNT_BINARY :JMPNZ(failedCounters) 3 - CNT_ARITH :JMPNZ(failedCounters) ;%COPYSP_CNT_ARITH + %MLOAD32_CNT_ARITH*C - CNT_ARITH :JMPNZ(failedCounters) diff --git a/counters/tests/opCALL.zkasm b/counters/tests/opCALL.zkasm index 05c354ba..b9fac267 100644 --- a/counters/tests/opCALL.zkasm +++ b/counters/tests/opCALL.zkasm @@ -22,7 +22,7 @@ operation: :JMP(opCALL) checkCounters: 600 - STEP:JMPN(failedCounters) -32 - CNT_BINARY :JMPNZ(failedCounters) +30 - CNT_BINARY :JMPNZ(failedCounters) 6 - CNT_ARITH :JMPNZ(failedCounters) %OPCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opCALLCODE.zkasm b/counters/tests/opCALLCODE.zkasm index e34ad845..47848d4b 100644 --- a/counters/tests/opCALLCODE.zkasm +++ b/counters/tests/opCALLCODE.zkasm @@ -22,7 +22,7 @@ operation: :JMP(opCALLCODE) checkCounters: 600 - STEP:JMPN(failedCounters) -31 - CNT_BINARY :JMPNZ(failedCounters) +29 - CNT_BINARY :JMPNZ(failedCounters) 6 - CNT_ARITH :JMPNZ(failedCounters) %OPCALLCODE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opCALLDATACOPY.zkasm b/counters/tests/opCALLDATACOPY.zkasm index b73c57f8..9e339374 100644 --- a/counters/tests/opCALLDATACOPY.zkasm +++ b/counters/tests/opCALLDATACOPY.zkasm @@ -10,7 +10,7 @@ operation: :JMP(opCALLDATACOPY) checkCounters: 500 - STEP:JMPN(failedCounters) -24 - CNT_BINARY :JMPNZ(failedCounters) +22 - CNT_BINARY :JMPNZ(failedCounters) 10 - CNT_ARITH :JMPNZ(failedCounters) %OPCALLDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opCALLDATALOAD.zkasm b/counters/tests/opCALLDATALOAD.zkasm index 94464d53..2e7249a2 100644 --- a/counters/tests/opCALLDATALOAD.zkasm +++ b/counters/tests/opCALLDATALOAD.zkasm @@ -9,7 +9,7 @@ operation: :JMP(opCALLDATALOAD) checkCounters: %OPCALLDATALOAD_STEP - STEP:JMPN(failedCounters) -8 - CNT_BINARY :JMPNZ(failedCounters) +6 - CNT_BINARY :JMPNZ(failedCounters) ;%OPCALLDATALOAD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) 3 - CNT_ARITH :JMPNZ(failedCounters) ;%OPCALLDATALOAD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) diff --git a/counters/tests/opCREATE.zkasm b/counters/tests/opCREATE.zkasm index ea44f579..894b0e40 100644 --- a/counters/tests/opCREATE.zkasm +++ b/counters/tests/opCREATE.zkasm @@ -16,7 +16,7 @@ operation: :JMP(opCREATE) checkCounters: 400 - STEP:JMPN(failedCounters) -22 - CNT_BINARY :JMPNZ(failedCounters) +20 - CNT_BINARY :JMPNZ(failedCounters) 4 - CNT_ARITH :JMPNZ(failedCounters) %OPCREATE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm index 4c6b8f13..97d51e34 100644 --- a/counters/tests/opCREATE2.zkasm +++ b/counters/tests/opCREATE2.zkasm @@ -16,7 +16,7 @@ operation: :JMP(opCREATE2) checkCounters: 500 - STEP:JMPN(failedCounters) -28 - CNT_BINARY :JMPNZ(failedCounters) +26 - CNT_BINARY :JMPNZ(failedCounters) 5 - CNT_ARITH :JMPNZ(failedCounters) %OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opDELEGATECALL.zkasm b/counters/tests/opDELEGATECALL.zkasm index 07f45e6a..6f41ca9a 100644 --- a/counters/tests/opDELEGATECALL.zkasm +++ b/counters/tests/opDELEGATECALL.zkasm @@ -16,7 +16,7 @@ operation: :JMP(opDELEGATECALL) checkCounters: 600 - STEP:JMPN(failedCounters) -28 - CNT_BINARY :JMPNZ(failedCounters) +26 - CNT_BINARY :JMPNZ(failedCounters) 6 - CNT_ARITH :JMPNZ(failedCounters) %OPDELEGATECALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opRETURN.zkasm b/counters/tests/opRETURN.zkasm index d237ffe8..3e528c40 100644 --- a/counters/tests/opRETURN.zkasm +++ b/counters/tests/opRETURN.zkasm @@ -13,7 +13,7 @@ operation: :JMP(opRETURN) checkCounters: 800 - STEP:JMPN(failedCounters) -39 - CNT_BINARY :JMPNZ(failedCounters) +33 - CNT_BINARY :JMPNZ(failedCounters) 11 - CNT_ARITH :JMPNZ(failedCounters) %OPRETURN_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opRETURNDATACOPY.zkasm b/counters/tests/opRETURNDATACOPY.zkasm index 96f710e3..5bf0183b 100644 --- a/counters/tests/opRETURNDATACOPY.zkasm +++ b/counters/tests/opRETURNDATACOPY.zkasm @@ -16,7 +16,7 @@ operation: :JMP(opRETURNDATACOPY) checkCounters: 1000 - STEP:JMPN(failedCounters) -35 - CNT_BINARY :JMPNZ(failedCounters) +29 - CNT_BINARY :JMPNZ(failedCounters) 13 - CNT_ARITH :JMPNZ(failedCounters) %OPRETURNDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opREVERT.zkasm b/counters/tests/opREVERT.zkasm index 5f0b1be8..bce6c2c6 100644 --- a/counters/tests/opREVERT.zkasm +++ b/counters/tests/opREVERT.zkasm @@ -13,7 +13,7 @@ operation: :JMP(opREVERT) checkCounters: 800 - STEP:JMPN(failedCounters) -39 - CNT_BINARY :JMPNZ(failedCounters) +33 - CNT_BINARY :JMPNZ(failedCounters) 11 - CNT_ARITH :JMPNZ(failedCounters) %OPREVERT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm index e21c0e31..6c0694e0 100644 --- a/counters/tests/opSHA3.zkasm +++ b/counters/tests/opSHA3.zkasm @@ -10,7 +10,7 @@ operation: :JMP(opSHA3) checkCounters: 3300 - STEP:JMPN(failedCounters) -145 - CNT_BINARY :JMPNZ(failedCounters) +143 - CNT_BINARY :JMPNZ(failedCounters) 9 - CNT_ARITH :JMPNZ(failedCounters) 15 - CNT_KECCAK_F :JMPNZ(failedCounters) 63 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/counters/tests/opSTATICCALL.zkasm b/counters/tests/opSTATICCALL.zkasm index 181876f1..b9903280 100644 --- a/counters/tests/opSTATICCALL.zkasm +++ b/counters/tests/opSTATICCALL.zkasm @@ -16,7 +16,7 @@ operation: :JMP(opSTATICCALL) checkCounters: 500 - STEP:JMPN(failedCounters) -28 - CNT_BINARY :JMPNZ(failedCounters) +26 - CNT_BINARY :JMPNZ(failedCounters) 6 - CNT_ARITH :JMPNZ(failedCounters) %OPSTATICCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) 2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index c0006c00..cad3e011 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -141,13 +141,10 @@ opPUSH32: opAuxPUSHA: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) $ => B :MLOAD(bytecodeLength) - PC + D => A - $ :LT,JMPC(opAuxPUSHA2) - PC => A - B - A => D + PC + D - B :JMPN(opAuxPUSHA2) + B - PC => D opAuxPUSHA2: ; check out-of-gas diff --git a/main/utils.zkasm b/main/utils.zkasm index 312988a2..23458cb6 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -735,7 +735,7 @@ SHRarithfinal: ;@info Shift left D bytes to A ;@in A - (A << D) ;@in D - (A << D) D bytes -;@out A - A << D => A +;@out A - A << D => A ; TODO: check D > 2**32 SHLarith: ; check zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) @@ -751,8 +751,7 @@ SHLarith: ; A bytes D => A 8 => B - 0 => C - 0 => D + 0 => C, D ; D = A * 8, D bits ${A*B} => D :ARITH ; A init number @@ -761,7 +760,7 @@ SHLarith: SHLarithBit: ; check zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) + %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) %MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith) RR :MSTORE(tmpSHXZkPC2) B :MSTORE(tmpVarBSHX) @@ -789,39 +788,16 @@ SHLarithinit: E :MSTORE(result) ; D = 256 - D 256 - D => D, RR - :CALL(@exp_num + RR); out:[B: 2**RR] - A => E - B => A - 1 => B - $ => A :SUB - E => B - ; A < B, 2**(256-D) - 1 < init number - $ => C :LT - ; A init number - E => A - ; E result - $ => E :MLOAD(result) - ; D = 256 - D - 256 - D => D, RR - :CALL(@exp_num + RR); out:[B: 2**RR] - ; if 2**D-1 < init number --> SHLarithBig - C :JMPNZ(SHLarithBig) - 0 => D,C - ; init number * 2**D = init + 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) -SHLarithBig: - 256 - D => D, RR - B => C :CALL(@exp_num + RR); out:[B: 2**RR] - ${A/B} => D - C => B - 0 => C - E :ARITH - SHLarithfinal: E => A $ => B :MLOAD(tmpVarBSHX) From e965a6a3506e6ee577434ebed9b8305a560368a4 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 26 Jan 2023 14:31:22 +0100 Subject: [PATCH 36/60] Add fixes from other branches --- .github/workflows/main.yaml | 6 +- counters/countersConstants.zkasm | 104 ++++++++-------- counters/tests/addBatchHashByteByByte.zkasm | 6 +- counters/tests/opCALL.zkasm | 8 +- counters/tests/opCALLCODE.zkasm | 8 +- counters/tests/opCALLDATACOPY.zkasm | 6 +- counters/tests/opCALLDATALOAD.zkasm | 6 +- counters/tests/opCODECOPY.zkasm | 8 +- counters/tests/opCREATE.zkasm | 8 +- counters/tests/opCREATE2.zkasm | 8 +- counters/tests/opDELEGATECALL.zkasm | 8 +- counters/tests/opEXTCODECOPY.zkasm | 8 +- counters/tests/opRETURN.zkasm | 6 +- counters/tests/opRETURNDATACOPY.zkasm | 7 +- counters/tests/opREVERT.zkasm | 6 +- counters/tests/opSHA3.zkasm | 8 +- counters/tests/opSTATICCALL.zkasm | 8 +- main/opcodes/calldata-returndata-code.zkasm | 130 ++++++++++---------- main/opcodes/storage-memory.zkasm | 20 ++- main/utils.zkasm | 13 +- 20 files changed, 194 insertions(+), 188 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 2c2de113..27a8fa8d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,9 +21,9 @@ jobs: run: | npm i npm run build - # - name: run counters tests - # run: | - # npm run test:counters + - name: run counters tests + run: | + npm run test:counters - name: Generate tests run: | npm run test:gen diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 2fb4ca8f..1815bf91 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -208,114 +208,114 @@ CONST %OPBLOCKHASH_CNT_PADDING_PG = 0 CONST %OPBLOCKHASH_CNT_POSEIDON_G = 2 ; opCALLDATALOAD CONST %OPCALLDATALOAD_STEP = 100 + %SHLARITH_STEP + %SHRARITH_STEP + %DIVARITH_STEP -CONST %OPCALLDATALOAD_CNT_BINARY = 1 + %SHLARITH_CNT_BINARY + %SHRARITH_CNT_BINARY + %DIVARITH_CNT_BINARY -CONST %OPCALLDATALOAD_CNT_ARITH = 0 + %SHLARITH_CNT_ARITH + %SHRARITH_CNT_ARITH + %DIVARITH_CNT_ARITH +CONST %OPCALLDATALOAD_CNT_BINARY = 2 +CONST %OPCALLDATALOAD_CNT_ARITH = 0 CONST %OPCALLDATALOAD_CNT_KECCAK_F = 0 + %SHLARITH_CNT_KECCAK_F + %SHRARITH_CNT_KECCAK_F + %DIVARITH_CNT_KECCAK_F -CONST %OPCALLDATALOAD_CNT_MEM_ALIGN = 0 + %SHLARITH_CNT_MEM_ALIGN + %SHRARITH_CNT_MEM_ALIGN + %DIVARITH_CNT_MEM_ALIGN +CONST %OPCALLDATALOAD_CNT_MEM_ALIGN = 1 CONST %OPCALLDATALOAD_CNT_PADDING_PG = 0 + %SHLARITH_CNT_PADDING_PG + %SHRARITH_CNT_PADDING_PG + %DIVARITH_CNT_PADDING_PG CONST %OPCALLDATALOAD_CNT_POSEIDON_G = 0 + %SHLARITH_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G ; opCALLDATACOPY - COMPLEX - hardcoded values at test CONST %OPCALLDATACOPY_STEP = 100 -CONST %OPCALLDATACOPY_CNT_BINARY = 2 -CONST %OPCALLDATACOPY_CNT_ARITH = 0 +CONST %OPCALLDATACOPY_CNT_BINARY = 21 +CONST %OPCALLDATACOPY_CNT_ARITH = 8 CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0 -CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 0 +CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 2 CONST %OPCALLDATACOPY_CNT_PADDING_PG = 0 CONST %OPCALLDATACOPY_CNT_POSEIDON_G = 0 ; opRETURNDATACOPY - COMPLEX - hardcoded values at test CONST %OPRETURNDATACOPY_STEP = 100 -CONST %OPRETURNDATACOPY_CNT_BINARY = 2 -CONST %OPRETURNDATACOPY_CNT_ARITH = 0 +CONST %OPRETURNDATACOPY_CNT_BINARY = 30 +CONST %OPRETURNDATACOPY_CNT_ARITH = 13 CONST %OPRETURNDATACOPY_CNT_KECCAK_F = 0 -CONST %OPRETURNDATACOPY_CNT_MEM_ALIGN = 0 +CONST %OPRETURNDATACOPY_CNT_MEM_ALIGN = 2 CONST %OPRETURNDATACOPY_CNT_PADDING_PG = 0 CONST %OPRETURNDATACOPY_CNT_POSEIDON_G = 0 ; opCODECOPY - COMPLEX - hardcoded values at test CONST %OPCODECOPY_STEP = 100 -CONST %OPCODECOPY_CNT_BINARY = 0 -CONST %OPCODECOPY_CNT_ARITH = 0 +CONST %OPCODECOPY_CNT_BINARY = 98 +CONST %OPCODECOPY_CNT_ARITH = 4 CONST %OPCODECOPY_CNT_KECCAK_F = 0 -CONST %OPCODECOPY_CNT_MEM_ALIGN = 0 +CONST %OPCODECOPY_CNT_MEM_ALIGN = 43 CONST %OPCODECOPY_CNT_PADDING_PG = 0 CONST %OPCODECOPY_CNT_POSEIDON_G = 0 ; opEXTCODECOPY - COMPLEX - hardcoded values at test CONST %OPEXTCODECOPY_STEP = 100 -CONST %OPEXTCODECOPY_CNT_BINARY = 0 -CONST %OPEXTCODECOPY_CNT_ARITH = 0 +CONST %OPEXTCODECOPY_CNT_BINARY = 103 +CONST %OPEXTCODECOPY_CNT_ARITH = 4 CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0 -CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 0 +CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 43 CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0 -CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 0 +CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 8 ; opCREATE - COMPLEX - hardcoded values at test CONST %OPCREATE_STEP = 100 -CONST %OPCREATE_CNT_BINARY = 2 -CONST %OPCREATE_CNT_ARITH = 0 +CONST %OPCREATE_CNT_BINARY = 21 +CONST %OPCREATE_CNT_ARITH = 4 CONST %OPCREATE_CNT_KECCAK_F = 0 -CONST %OPCREATE_CNT_MEM_ALIGN = 0 +CONST %OPCREATE_CNT_MEM_ALIGN = 2 CONST %OPCREATE_CNT_PADDING_PG = 0 -CONST %OPCREATE_CNT_POSEIDON_G = 255*3 +CONST %OPCREATE_CNT_POSEIDON_G = 23 ; opCREATE2 - COMPLEX - hardcoded values at test CONST %OPCREATE2_STEP = 200 -CONST %OPCREATE2_CNT_BINARY = 2 -CONST %OPCREATE2_CNT_ARITH = 0 +CONST %OPCREATE2_CNT_BINARY = 27 +CONST %OPCREATE2_CNT_ARITH = 5 CONST %OPCREATE2_CNT_KECCAK_F = 0 -CONST %OPCREATE2_CNT_MEM_ALIGN = 0 +CONST %OPCREATE2_CNT_MEM_ALIGN = 2 CONST %OPCREATE2_CNT_PADDING_PG = 0 -CONST %OPCREATE2_CNT_POSEIDON_G = 255*2 +CONST %OPCREATE2_CNT_POSEIDON_G = 27 ; opCALL - COMPLEX - hardcoded values at test CONST %OPCALL_STEP = 200 -CONST %OPCALL_CNT_BINARY = 3 -CONST %OPCALL_CNT_ARITH = 0 +CONST %OPCALL_CNT_BINARY = 32 +CONST %OPCALL_CNT_ARITH = 6 CONST %OPCALL_CNT_KECCAK_F = 0 -CONST %OPCALL_CNT_MEM_ALIGN = 0 +CONST %OPCALL_CNT_MEM_ALIGN = 2 CONST %OPCALL_CNT_PADDING_PG = 0 -CONST %OPCALL_CNT_POSEIDON_G = 0 +CONST %OPCALL_CNT_POSEIDON_G = 14 ; opCALLCODE - COMPLEX - hardcoded values at test CONST %OPCALLCODE_STEP = 200 -CONST %OPCALLCODE_CNT_BINARY = 2 -CONST %OPCALLCODE_CNT_ARITH = 0 +CONST %OPCALLCODE_CNT_BINARY = 31 +CONST %OPCALLCODE_CNT_ARITH = 6 CONST %OPCALLCODE_CNT_KECCAK_F = 0 -CONST %OPCALLCODE_CNT_MEM_ALIGN = 0 +CONST %OPCALLCODE_CNT_MEM_ALIGN = 2 CONST %OPCALLCODE_CNT_PADDING_PG = 0 -CONST %OPCALLCODE_CNT_POSEIDON_G = 0 +CONST %OPCALLCODE_CNT_POSEIDON_G = 10 ; opRETURN - COMPLEX - hardcoded values at test CONST %OPRETURN_STEP = 400 -CONST %OPRETURN_CNT_BINARY = 1 -CONST %OPRETURN_CNT_ARITH = 0 +CONST %OPRETURN_CNT_BINARY = 34 +CONST %OPRETURN_CNT_ARITH = 11 CONST %OPRETURN_CNT_KECCAK_F = 0 -CONST %OPRETURN_CNT_MEM_ALIGN = 0 +CONST %OPRETURN_CNT_MEM_ALIGN = 4 CONST %OPRETURN_CNT_PADDING_PG = 0 CONST %OPRETURN_CNT_POSEIDON_G = 0 ; opREVERT - COMPLEX - hardcoded values at test CONST %OPREVERT_STEP = 400 -CONST %OPREVERT_CNT_BINARY = 0 -CONST %OPREVERT_CNT_ARITH = 0 +CONST %OPREVERT_CNT_BINARY = 34 +CONST %OPREVERT_CNT_ARITH = 11 CONST %OPREVERT_CNT_KECCAK_F = 0 -CONST %OPREVERT_CNT_MEM_ALIGN = 0 +CONST %OPREVERT_CNT_MEM_ALIGN = 4 CONST %OPREVERT_CNT_PADDING_PG = 0 CONST %OPREVERT_CNT_POSEIDON_G = 0 ; opDELEGATECALL - COMPLEX - hardcoded values at test CONST %OPDELEGATECALL_STEP = 400 -CONST %OPDELEGATECALL_CNT_BINARY = 0 -CONST %OPDELEGATECALL_CNT_ARITH = 0 +CONST %OPDELEGATECALL_CNT_BINARY = 28 +CONST %OPDELEGATECALL_CNT_ARITH = 6 CONST %OPDELEGATECALL_CNT_KECCAK_F = 0 -CONST %OPDELEGATECALL_CNT_MEM_ALIGN = 0 +CONST %OPDELEGATECALL_CNT_MEM_ALIGN = 2 CONST %OPDELEGATECALL_CNT_PADDING_PG = 0 -CONST %OPDELEGATECALL_CNT_POSEIDON_G = 0 +CONST %OPDELEGATECALL_CNT_POSEIDON_G = 6 ; opSTATICCALL - COMPLEX - hardcoded values at test CONST %OPSTATICCALL_STEP = 300 -CONST %OPSTATICCALL_CNT_BINARY = 0 -CONST %OPSTATICCALL_CNT_ARITH = 0 +CONST %OPSTATICCALL_CNT_BINARY = 28 +CONST %OPSTATICCALL_CNT_ARITH = 6 CONST %OPSTATICCALL_CNT_KECCAK_F = 0 -CONST %OPSTATICCALL_CNT_MEM_ALIGN = 0 +CONST %OPSTATICCALL_CNT_MEM_ALIGN = 2 CONST %OPSTATICCALL_CNT_PADDING_PG = 0 -CONST %OPSTATICCALL_CNT_POSEIDON_G = 0 +CONST %OPSTATICCALL_CNT_POSEIDON_G = 6 ; opSHA3 - COMPLEX - hardcoded values at test CONST %OPSHA3_STEP = 400 -CONST %OPSHA3_CNT_BINARY = 0 -CONST %OPSHA3_CNT_ARITH = 0 -CONST %OPSHA3_CNT_KECCAK_F = 0 -CONST %OPSHA3_CNT_MEM_ALIGN = 0 +CONST %OPSHA3_CNT_BINARY = 144 +CONST %OPSHA3_CNT_ARITH = 9 +CONST %OPSHA3_CNT_KECCAK_F = 15 +CONST %OPSHA3_CNT_MEM_ALIGN = 63 CONST %OPSHA3_CNT_PADDING_PG = 0 CONST %OPSHA3_CNT_POSEIDON_G = 0 ; MSTORE32 @@ -360,8 +360,8 @@ CONST %SAVEMEM_CNT_PADDING_PG = 0 + %MULARITH_CNT_PADDING_PG + %DIVARITH_CNT_PAD CONST %SAVEMEM_CNT_POSEIDON_G = 0 + %MULARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G ;addBatchHash CONST %ADDBATCHHASH_STEP = 10 + %SHRARITH_STEP -CONST %ADDBATCHHASH_CNT_BINARY = 0 + %SHRARITH_CNT_BINARY -CONST %ADDBATCHHASH_CNT_ARITH = 0 + %SHRARITH_CNT_ARITH +CONST %ADDBATCHHASH_CNT_BINARY = 155 +CONST %ADDBATCHHASH_CNT_ARITH = 31 CONST %ADDBATCHHASH_CNT_KECCAK_F = 0 + %SHRARITH_CNT_KECCAK_F CONST %ADDBATCHHASH_CNT_MEM_ALIGN = 0 + %SHRARITH_CNT_MEM_ALIGN CONST %ADDBATCHHASH_CNT_PADDING_PG = 0 + %SHRARITH_CNT_PADDING_PG diff --git a/counters/tests/addBatchHashByteByByte.zkasm b/counters/tests/addBatchHashByteByByte.zkasm index 58fd66e3..4b3e39cc 100644 --- a/counters/tests/addBatchHashByteByByte.zkasm +++ b/counters/tests/addBatchHashByteByByte.zkasm @@ -12,10 +12,8 @@ operation: :CALL(addBatchHashByteByByte) %ADDBATCHHASH_STEP*D - STEP:JMPN(failedCounters) - 155 - CNT_BINARY :JMPNZ(failedCounters) - ;%ADDBATCHHASH_CNT_BINARY*D + D - CNT_BINARY :JMPNZ(failedCounters) - 31 - CNT_ARITH :JMPNZ(failedCounters) - ;%ADDBATCHHASH_CNT_ARITH*D - CNT_ARITH :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) + %ADDBATCHHASH_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) diff --git a/counters/tests/opCALL.zkasm b/counters/tests/opCALL.zkasm index b9fac267..64cca73c 100644 --- a/counters/tests/opCALL.zkasm +++ b/counters/tests/opCALL.zkasm @@ -22,12 +22,12 @@ operation: :JMP(opCALL) checkCounters: 600 - STEP:JMPN(failedCounters) -30 - CNT_BINARY :JMPNZ(failedCounters) -6 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCALL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -14 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opCALLCODE.zkasm b/counters/tests/opCALLCODE.zkasm index 47848d4b..d6ba3110 100644 --- a/counters/tests/opCALLCODE.zkasm +++ b/counters/tests/opCALLCODE.zkasm @@ -22,12 +22,12 @@ operation: :JMP(opCALLCODE) checkCounters: 600 - STEP:JMPN(failedCounters) -29 - CNT_BINARY :JMPNZ(failedCounters) -6 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLCODE_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCALLCODE_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCALLCODE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALLCODE_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCALLCODE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -10 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPCALLCODE_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opCALLDATACOPY.zkasm b/counters/tests/opCALLDATACOPY.zkasm index 9e339374..8c4a8937 100644 --- a/counters/tests/opCALLDATACOPY.zkasm +++ b/counters/tests/opCALLDATACOPY.zkasm @@ -10,10 +10,10 @@ operation: :JMP(opCALLDATACOPY) checkCounters: 500 - STEP:JMPN(failedCounters) -22 - CNT_BINARY :JMPNZ(failedCounters) -10 - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCALLDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCALLDATACOPY_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCALLDATACOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCALLDATACOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) diff --git a/counters/tests/opCALLDATALOAD.zkasm b/counters/tests/opCALLDATALOAD.zkasm index 2e7249a2..2cf05441 100644 --- a/counters/tests/opCALLDATALOAD.zkasm +++ b/counters/tests/opCALLDATALOAD.zkasm @@ -9,10 +9,8 @@ operation: :JMP(opCALLDATALOAD) checkCounters: %OPCALLDATALOAD_STEP - STEP:JMPN(failedCounters) -6 - CNT_BINARY :JMPNZ(failedCounters) -;%OPCALLDATALOAD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) -3 - CNT_ARITH :JMPNZ(failedCounters) -;%OPCALLDATALOAD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCALLDATALOAD_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCALLDATALOAD_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) %OPCALLDATALOAD_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCALLDATALOAD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) diff --git a/counters/tests/opCODECOPY.zkasm b/counters/tests/opCODECOPY.zkasm index 8438fc47..a8cf363b 100644 --- a/counters/tests/opCODECOPY.zkasm +++ b/counters/tests/opCODECOPY.zkasm @@ -9,11 +9,11 @@ operation: 432543 :MSTORE(SP++) :JMP(opCODECOPY) checkCounters: -1800 - STEP:JMPN(failedCounters) -97 - CNT_BINARY :JMPNZ(failedCounters) -4 - CNT_ARITH :JMPNZ(failedCounters) +1700 - STEP:JMPN(failedCounters) +%OPCODECOPY_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCODECOPY_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCODECOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -43 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCODECOPY_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCODECOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) diff --git a/counters/tests/opCREATE.zkasm b/counters/tests/opCREATE.zkasm index 894b0e40..b6d01b52 100644 --- a/counters/tests/opCREATE.zkasm +++ b/counters/tests/opCREATE.zkasm @@ -16,12 +16,12 @@ operation: :JMP(opCREATE) checkCounters: 400 - STEP:JMPN(failedCounters) -20 - CNT_BINARY :JMPNZ(failedCounters) -4 - CNT_ARITH :JMPNZ(failedCounters) +%OPCREATE_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCREATE_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCREATE_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCREATE_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCREATE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -23 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPCREATE_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm index 97d51e34..d36934c6 100644 --- a/counters/tests/opCREATE2.zkasm +++ b/counters/tests/opCREATE2.zkasm @@ -16,12 +16,12 @@ operation: :JMP(opCREATE2) checkCounters: 500 - STEP:JMPN(failedCounters) -26 - CNT_BINARY :JMPNZ(failedCounters) -5 - CNT_ARITH :JMPNZ(failedCounters) +%OPCREATE2_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPCREATE2_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPCREATE2_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -27 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPCREATE2_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opDELEGATECALL.zkasm b/counters/tests/opDELEGATECALL.zkasm index 6f41ca9a..1d7c3980 100644 --- a/counters/tests/opDELEGATECALL.zkasm +++ b/counters/tests/opDELEGATECALL.zkasm @@ -16,12 +16,12 @@ operation: :JMP(opDELEGATECALL) checkCounters: 600 - STEP:JMPN(failedCounters) -26 - CNT_BINARY :JMPNZ(failedCounters) -6 - CNT_ARITH :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPDELEGATECALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPDELEGATECALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -6 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPDELEGATECALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opEXTCODECOPY.zkasm b/counters/tests/opEXTCODECOPY.zkasm index d769c84c..17d4a72b 100644 --- a/counters/tests/opEXTCODECOPY.zkasm +++ b/counters/tests/opEXTCODECOPY.zkasm @@ -11,12 +11,12 @@ operation: :JMP(opEXTCODECOPY) checkCounters: 2000 - STEP:JMPN(failedCounters) -102 - CNT_BINARY :JMPNZ(failedCounters) -4 - CNT_ARITH :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPEXTCODECOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -43 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPEXTCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -8 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPEXTCODECOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opRETURN.zkasm b/counters/tests/opRETURN.zkasm index 3e528c40..820e6526 100644 --- a/counters/tests/opRETURN.zkasm +++ b/counters/tests/opRETURN.zkasm @@ -13,10 +13,10 @@ operation: :JMP(opRETURN) checkCounters: 800 - STEP:JMPN(failedCounters) -33 - CNT_BINARY :JMPNZ(failedCounters) -11 - CNT_ARITH :JMPNZ(failedCounters) +%OPRETURN_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPRETURN_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPRETURN_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPRETURN_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPRETURN_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPRETURN_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) diff --git a/counters/tests/opRETURNDATACOPY.zkasm b/counters/tests/opRETURNDATACOPY.zkasm index 5bf0183b..7905c102 100644 --- a/counters/tests/opRETURNDATACOPY.zkasm +++ b/counters/tests/opRETURNDATACOPY.zkasm @@ -16,12 +16,7 @@ operation: :JMP(opRETURNDATACOPY) checkCounters: 1000 - STEP:JMPN(failedCounters) -29 - CNT_BINARY :JMPNZ(failedCounters) -13 - CNT_ARITH :JMPNZ(failedCounters) -%OPRETURNDATACOPY_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) -%OPRETURNDATACOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -%OPRETURNDATACOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) + 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/counters/tests/opREVERT.zkasm b/counters/tests/opREVERT.zkasm index bce6c2c6..c8f387a7 100644 --- a/counters/tests/opREVERT.zkasm +++ b/counters/tests/opREVERT.zkasm @@ -13,10 +13,10 @@ operation: :JMP(opREVERT) checkCounters: 800 - STEP:JMPN(failedCounters) -33 - CNT_BINARY :JMPNZ(failedCounters) -11 - CNT_ARITH :JMPNZ(failedCounters) +%OPREVERT_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPREVERT_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPREVERT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -4 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPREVERT_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPREVERT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPREVERT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm index 6c0694e0..741b452e 100644 --- a/counters/tests/opSHA3.zkasm +++ b/counters/tests/opSHA3.zkasm @@ -10,10 +10,10 @@ operation: :JMP(opSHA3) checkCounters: 3300 - STEP:JMPN(failedCounters) -143 - CNT_BINARY :JMPNZ(failedCounters) -9 - CNT_ARITH :JMPNZ(failedCounters) -15 - CNT_KECCAK_F :JMPNZ(failedCounters) -63 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSHA3_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSHA3_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) +%OPSHA3_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) +%OPSHA3_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPSHA3_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSHA3_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) diff --git a/counters/tests/opSTATICCALL.zkasm b/counters/tests/opSTATICCALL.zkasm index b9903280..49922d5a 100644 --- a/counters/tests/opSTATICCALL.zkasm +++ b/counters/tests/opSTATICCALL.zkasm @@ -16,12 +16,12 @@ operation: :JMP(opSTATICCALL) checkCounters: 500 - STEP:JMPN(failedCounters) -26 - CNT_BINARY :JMPNZ(failedCounters) -6 - CNT_ARITH :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) %OPSTATICCALL_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -2 - CNT_MEM_ALIGN :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) %OPSTATICCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) -6 - CNT_POSEIDON_G :JMPNZ(failedCounters) +%OPSTATICCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 0d1896d8..e7a1f339 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -10,45 +10,38 @@ */ opCALLDATALOAD: ; checks zk-counters - %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) + %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign) + ; check stack underflow SP - 1 => SP :JMPN(stackUnderflow) ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) - $ => A :MLOAD(SP); [offset => B] + $ => A :MLOAD(SP); [offset => A] $ => B :MLOAD(txCalldataLen) ; if offset is not lower than calldata length, return 0 $ :LT,JMPNC(CALLDATALOADreturn0) - A => E ; Div operation with Arith - E :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - ; if data not aligned (remainder > 0), shift - $ => A :MLOAD(arithRes2), JMPNZ(opCALLDATALOAD2) - SP => C + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + ; store SP at D + SP => D ; add %CALLDATA_OFFSET to point calldata in the memory - %CALLDATA_OFFSET + B => SP + %CALLDATA_OFFSET + E => SP + ; if data not aligned (remainder > 0), shift + C :JMPNZ(opCALLDATALOAD2) $ => B :MLOAD(SP); [stack => B] ; recover previous stack pointer - C => SP + D => SP B :MSTORE(SP++), JMP(readCode); [data(offset) => SP] opCALLDATALOAD2: - ; store SP at C - SP => C - ; remainder at D - A => D - ; set pointer to calldata - %CALLDATA_OFFSET + B => SP - $ => A :MLOAD(SP++), CALL(SHLarith); [stack => A]; in: [A: value, D: #bytes to left shift] out: [A: shifted result] - A => B - 32 - D => D - $ => A :MLOAD(SP), CALL(SHRarith); [stack => A]; in: [A: value, D: #bytes to right shift] out: [A: shifted result] + $ => A :MLOAD(SP++) + $ => B :MLOAD(SP) + $ => A :MEM_ALIGN_RD ; recover SP - C => SP - B + A :MSTORE(SP++), JMP(readCode); [data(offset) => SP] + D => SP + A :MSTORE(SP++), JMP(readCode); [data(offset) => SP] CALLDATALOADreturn0: 0 :MSTORE(SP++), JMP(readCode); [0 => SP] @@ -78,6 +71,8 @@ opCALLDATASIZEdep: ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) +VAR GLOBAL calldatacopyAux1 +VAR GLOBAL calldatacopyAux2 /** * @link [https://www.evm.codes/#37?fork=berlin] * @zk-counters @@ -91,34 +86,31 @@ opCALLDATACOPY: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) + %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign) ; check stack underflow SP - 3 => SP :JMPN(stackUnderflow) $ => E :MLOAD(SP+2); [destOffset => E] - $ => A :MLOAD(SP+1); [offset => B] + $ => D :MLOAD(SP+1); [offset => D] $ => C :MLOAD(SP); [size => C] ; store lastMemOffset for memory expansion gas cost E :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost - C :MSTORE(lastMemLength) + C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) ;${3*((C+31)/32)} ;(C+31)/32 => A - C+31 => B - B :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - ; Mul operation with Arith - ; 3*A - 3 :MSTORE(arithA) - B :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB] - $ => B :MLOAD(arithRes1) - GAS - B => GAS :JMPN(outOfGas) - :CALL(saveMem); in: [lastMemOffset, lastMemLength] + C+31 => A + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + GAS - 3*E => GAS :JMPN(outOfGas) ; save current stack pointer SP :MSTORE(SPw) - $ => B :MLOAD(txCalldataLen) - A => D + ; Recover destOffset at E + $ => E :MLOAD(lastMemOffset) + ; Recover size at C + $ => C :MLOAD(lastMemLength) + $ => B :MLOAD(txCalldataLen); less than 2**32 bytes (calldata). Enforced by memory expansion gas cost & smart contract batchL2DataHash + D => A ; if offset is not lower than calldata length, return 0 $ :LT,JMPNC(opCALLDATACOPY0) B + C => A @@ -136,47 +128,51 @@ opCALLDATACOPYloop: C :JMPZ(opCALLDATACOPYcheckLen) ; copy last bytes C - 32 :JMPN(opCALLDATACOPYfinal) - B :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => D :MLOAD(arithRes2) - $ => A :MLOAD(arithRes1) - A :MSTORE(arithA) + ; Store B at artihB for later usage + B => A :MSTORE(calldatacopyAux2) + E => D + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] ; add %CALLDATA_OFFSET to offset to reach calldata in memory - %CALLDATA_OFFSET :MSTORE(arithB), CALL(addARITH); in: [arithA, arithB] out[arithRes1: arithA*arithB] - $ => A :MLOAD(arithRes1) ; set stack pointer to first byte to read - A => SP - $ => A :MLOAD(SP); [calldata => A] - :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result] - A => C - $ => A :MLOAD(arithRes1) - ; get data from next memory slot - A + 1 => SP - 32 - D => D - ; align calldata - $ => A :MLOAD(SP), CALL(SHRarith); [calldata => A]; in: [A: value, D: #bytes to right shift] out: [A: shifted result] - ; set bytesToStore with value to use in MSTORE - A + C :MSTORE(bytesToStore), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset] + %CALLDATA_OFFSET + E => SP + D => E + $ => A :MLOAD(SP++) + $ => B :MLOAD(SP) + $ => A :MEM_ALIGN_RD, MSTORE(bytesToStore) + E => A + ; Store 32 bytes from calldata to memory + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + E :MSTORE(calldatacopyAux1) + $ => 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(calldatacopyAux1) + D :MSTORE(MEM:E) ; write W0 + A :MSTORE(MEM:E+1) ; write W1 ; recover stack pointer $ => SP :MLOAD(SPw) $ => C :MLOAD(SP); [size => C] - C - 32 => C - C :MSTORE(SP); [size => SP] + C - 32 => C :MSTORE(SP); [size => SP] + $ => E :MLOAD(lastMemOffset) + E + 32 => E :MSTORE(lastMemOffset) + $ => B :MLOAD(calldatacopyAux2) B + 32 => B :JMP(opCALLDATACOPYloop) opCALLDATACOPYfinal: ; copy last bytes - B :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => D :MLOAD(arithRes2) - $ => B :MLOAD(arithRes1) + B => A + E => D + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] ; add %CALLDATA_OFFSET to offset to reach calldata in memory - %CALLDATA_OFFSET :MSTORE(arithA) - B :MSTORE(arithB), CALL(addARITH); in: [arithA, arithB] out[arithRes1: arithA*arithB] - $ => B :MLOAD(arithRes1) ; set SP to calldata - B => SP + %CALLDATA_OFFSET + E => SP, B + D => E + C => D $ => A :MLOAD(SP), CALL(SHLarith); [calldata => A]; in: [A: value, D: #bytes to left shift] out: [A: shifted result] + ; Recover init SP to recover input size (todo: use an aux var) $ => SP :MLOAD(SPw) $ => C :MLOAD(SP); [size => C] ; point to next memory slot diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 012094f1..158b7d9b 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -33,9 +33,11 @@ opMLOAD: * - stack input: [offset, value] * - stack output: none */ + VAR GLOBAL mstoreAux opMSTORE: ; checks zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign) ; check stack underflow SP - 2 => SP :JMPN(stackUnderflow) @@ -46,12 +48,24 @@ opMLOAD: $ => 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] + E :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost 32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] + ; store bytesToStore for memalign execution + B :MSTORE(bytesToStore) + E => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + ; Store E offset at arithA for later usage + E :MSTORE(mstoreAux) + $ => 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(mstoreAux) + D :MSTORE(MEM:E) ; write W0 + A :MSTORE(MEM:E+1) ; write W1 :JMP(readCode) /** diff --git a/main/utils.zkasm b/main/utils.zkasm index 23458cb6..a3c11b97 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -161,6 +161,7 @@ VAR GLOBAL isMSTOREX ; @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) @@ -271,6 +272,8 @@ endMSTORE: $ => C :MLOAD(tmpVarCmstore) $ => RR :MLOAD(tmpZkPCmstore) $ => D :MLOAD(tmpVarDmstore), RETURN +endMSTOREX: + :RETURN VAR GLOBAL tmpVarAmload VAR GLOBAL tmpVarBmload @@ -467,7 +470,7 @@ VAR GLOBAL tmpZkPCsaveMem saveMem: ; check zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) + %MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary) RR :MSTORE(tmpZkPCsaveMem) A :MSTORE(tmpVarAsaveMem) B :MSTORE(tmpVarBsaveMem) @@ -475,16 +478,18 @@ saveMem: 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) - ; If no len, no memory expansion - A :JMPZ(saveMemEnd) ; 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) @@ -735,7 +740,7 @@ SHRarithfinal: ;@info Shift left D bytes to A ;@in A - (A << D) ;@in D - (A << D) D bytes -;@out A - A << D => A ; TODO: check D > 2**32 +;@out A - A << D => A SHLarith: ; check zk-counters %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) From 51380d27a40b6c0099cc5b9d38d153e2ef4f3c52 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 27 Jan 2023 12:55:02 +0100 Subject: [PATCH 37/60] add few comments --- main/opcodes/calldata-returndata-code.zkasm | 13 +++++++------ main/opcodes/storage-memory.zkasm | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index e7a1f339..08225660 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -22,14 +22,12 @@ $ => B :MLOAD(txCalldataLen) ; if offset is not lower than calldata length, return 0 $ :LT,JMPNC(CALLDATALOADreturn0) - ; Div operation with Arith :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] ; store SP at D SP => D ; add %CALLDATA_OFFSET to point calldata in the memory %CALLDATA_OFFSET + E => SP - ; if data not aligned (remainder > 0), shift - C :JMPNZ(opCALLDATALOAD2) + C :JMPNZ(opCALLDATALOAD2) ; data not aligned $ => B :MLOAD(SP); [stack => B] ; recover previous stack pointer D => SP @@ -37,6 +35,9 @@ 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 $ => B :MLOAD(SP) $ => A :MEM_ALIGN_RD ; recover SP @@ -101,8 +102,8 @@ opCALLDATACOPY: ;${3*((C+31)/32)} ;(C+31)/32 => A C+31 => A - :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] - GAS - 3*E => GAS :JMPN(outOfGas) + :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + GAS - 3*E => GAS :JMPN(outOfGas) ; save current stack pointer SP :MSTORE(SPw) ; Recover destOffset at E @@ -128,7 +129,7 @@ opCALLDATACOPYloop: C :JMPZ(opCALLDATACOPYcheckLen) ; copy last bytes C - 32 :JMPN(opCALLDATACOPYfinal) - ; Store B at artihB for later usage + B => A :MSTORE(calldatacopyAux2) E => D :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 158b7d9b..dd74ceaf 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -55,7 +55,7 @@ opMLOAD: ; store bytesToStore for memalign execution B :MSTORE(bytesToStore) E => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] - ; Store E offset at arithA for later usage + E :MSTORE(mstoreAux) $ => A :MLOAD(MEM:E) $ => B :MLOAD(MEM:E+1) From 3b9f881b0b3b901a4fe77b7e0cf9efcc20ac5535 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Fri, 27 Jan 2023 00:45:40 +0100 Subject: [PATCH 38/60] update tests vectors & add run scripts ooc/30m --- package.json | 2 +- tools/parallel-tests-sample/sample.test.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 78151863..89d41e22 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/script-ooc", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", diff --git a/tools/parallel-tests-sample/sample.test.js b/tools/parallel-tests-sample/sample.test.js index d28fb9d5..8e7b5571 100644 --- a/tools/parallel-tests-sample/sample.test.js +++ b/tools/parallel-tests-sample/sample.test.js @@ -4,11 +4,14 @@ const { expect } = require('chai'); const fs = require('fs'); const path = require('path'); +const { execSync } = require('child_process'); + const { newCommitPolsArray } = require('pilcom'); const smMain = require('@0xpolygonhermez/zkevm-proverjs/src/sm/sm_main/sm_main'); -const rom = require('../../build/rom.json'); +let rom = require('../../build/rom.json'); +let stepsN = 2 ** 23; const fileCachePil = path.join(__dirname, '../../node_modules/@0xpolygonhermez/zkevm-proverjs/cache-main-pil.json'); @@ -24,13 +27,22 @@ it(`${nameFile}`, async () => { } const pil = JSON.parse(fs.readFileSync(fileCachePil)); const cmPols = newCommitPolsArray(pil); + if (input.gasLimit) { + if(!fs.existsSync(`../../build/rom-${input.gasLimit}.json`)) { + execSync(`cd ../../ && mkdir -p build && npx zkasm main/main.zkasm -o build/rom-${input.gasLimit}.json`) + } + rom = require(`../../build/rom-${input.gasLimit}.json`) + } + if (inputPath.includes("tests-OOC") && input.stepsN) { + stepsN = input.stepsN + } try { const config = { debug: true, debugInfo: { inputName: path.basename(inputPath), }, - stepsN: 2 ** 23, + stepsN: stepsN, }; await smMain.execute(cmPols.Main, input, rom, config); } catch (err) { From f7479934712a2cc9b7cb9e99ca58322d49dd49c5 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Fri, 27 Jan 2023 00:59:02 +0100 Subject: [PATCH 39/60] fixes rom & update GHA --- .github/workflows/main.yaml | 3 +++ main/opcodes/calldata-returndata-code.zkasm | 20 ++++++++++++-------- package.json | 3 +++ tools/parallel-tests-sample/sample.test.js | 10 ++++------ 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 27a8fa8d..dedf915c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,6 +21,9 @@ jobs: run: | npm i npm run build + npm run build:gasLimit:v1 + npm run build:gasLimit:v2 + npm run build:gasLimit:v3 - name: run counters tests run: | npm run test:counters diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 08225660..9a1d521a 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -280,9 +280,8 @@ opCODECOPY: ; store lastMemOffset for memory expansion gas cost C :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost - E :MSTORE(lastMemLength) ; compute memory expansion gas cost - :CALL(saveMem); in: [lastMemOffset, lastMemLength] + E :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] ; check out-of-gas GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas) ;${3*((E+31)/32)} @@ -403,7 +402,8 @@ VAR GLOBAL tmpContractLength */ opEXTCODECOPY: ; checks zk-counters - %MAX_CNT_STEPS - STEP - 300 :JMPN(outOfCountersStep) + %MAX_CNT_STEPS - STEP - 300 :JMPN(outOfCountersStep) + %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) ; check stack underflow SP - 4 => SP :JMPN(stackUnderflow) @@ -421,7 +421,8 @@ opEXTCODECOPY: C :MSTORE(lastMemOffset) ; store lastMemLength for memory expansion gas cost - E :MSTORE(lastMemLength) + ; compute memory expansion gas cost + E :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] ; check out-of-gas ;${3*((E+31)/32)} @@ -438,9 +439,6 @@ opEXTCODECOPY: $ => A :MLOAD(arithRes1) ; check out-of-gas GAS - A => GAS :JMPN(outOfGas) - - ; compute memory expansion gas cost - :CALL(saveMem); in: [lastMemOffset, lastMemLength] ; if offset is above data len, length => offset D => A $ => B :MLOAD(tmpContractLength) @@ -558,15 +556,21 @@ opRETURNDATACOPY: ; Load ret data length from last ctx B => CTX E => B + + ; check offset is lower than max memory + %MAX_MEM_EXPANSION_BYTES => A + $ :LT,JMPC(outOfGas) + $ => A :MLOAD(retDataLength) $ => E :MLOAD(retDataOffset) $ => CTX :MLOAD(currentCTX) ; E ret data offset (memory pointer) of last context, B offset in return data that want to retrive E + B => E ; if retDataSize <= retdataOffset + dataLen -> OOG - ;A retDataSize + ; A retDataSize B + C => B ; total offset (retdataOffset + dataLen) $ :LT, JMPC(outOfGas) + E => B ; memory pointer where start to copy memory ;${3*((C+31)/32)} C+31 => A diff --git a/package.json b/package.json index 89d41e22..7ec3049a 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "scripts": { "build": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom.json && npm run build:metadata", "build:metadata": "echo \"COMMIT=$(git rev-parse HEAD)\nSHA256SUM=$(sha256sum ./build/rom.json)\" > build/metadata-rom.txt", + "build:gasLimit:v1": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-89128960.json -D TX_GAS_LIMIT=89128960", + "build:gasLimit:v2": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-100000000.json -D TX_GAS_LIMIT=100000000", + "build:gasLimit:v3": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-2147483647.json -D TX_GAS_LIMIT=2147483647", "test:bitwise": "mkdir -p build && npx zkasm test/bitwise.zkasm -o build/rom-bitwise.test.json", "test:comp": "mkdir -p build && npx zkasm test/comp.zkasm -o build/rom-comp.test.json", "test:utils": "mkdir -p build && npx zkasm test/utils-getLenBytes.zkasm -o build/rom-utils.test.json", diff --git a/tools/parallel-tests-sample/sample.test.js b/tools/parallel-tests-sample/sample.test.js index 8e7b5571..2ba9fb53 100644 --- a/tools/parallel-tests-sample/sample.test.js +++ b/tools/parallel-tests-sample/sample.test.js @@ -4,14 +4,13 @@ const { expect } = require('chai'); const fs = require('fs'); const path = require('path'); -const { execSync } = require('child_process'); - const { newCommitPolsArray } = require('pilcom'); const smMain = require('@0xpolygonhermez/zkevm-proverjs/src/sm/sm_main/sm_main'); let rom = require('../../build/rom.json'); let stepsN = 2 ** 23; +let counters = false; const fileCachePil = path.join(__dirname, '../../node_modules/@0xpolygonhermez/zkevm-proverjs/cache-main-pil.json'); @@ -28,13 +27,11 @@ it(`${nameFile}`, async () => { const pil = JSON.parse(fs.readFileSync(fileCachePil)); const cmPols = newCommitPolsArray(pil); if (input.gasLimit) { - if(!fs.existsSync(`../../build/rom-${input.gasLimit}.json`)) { - execSync(`cd ../../ && mkdir -p build && npx zkasm main/main.zkasm -o build/rom-${input.gasLimit}.json`) - } rom = require(`../../build/rom-${input.gasLimit}.json`) } - if (inputPath.includes("tests-OOC") && input.stepsN) { + if (input.stepsN) { stepsN = input.stepsN + counters = true; } try { const config = { @@ -43,6 +40,7 @@ it(`${nameFile}`, async () => { inputName: path.basename(inputPath), }, stepsN: stepsN, + counters, }; await smMain.execute(cmPols.Main, input, rom, config); } catch (err) { From f26077a093eb1d5245f63758faedecac0cf6183c Mon Sep 17 00:00:00 2001 From: laisolizq Date: Tue, 31 Jan 2023 18:22:41 +0100 Subject: [PATCH 40/60] fixes --- .github/workflows/main.yaml | 6 +++--- main/opcodes/calldata-returndata-code.zkasm | 2 +- package.json | 6 +++--- tools/parallel-tests-sample/sample.test.js | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index dedf915c..82f24a60 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,9 +21,9 @@ jobs: run: | npm i npm run build - npm run build:gasLimit:v1 - npm run build:gasLimit:v2 - npm run build:gasLimit:v3 + npm run test:build:gasLimit:v1 + npm run test:build:gasLimit:v2 + npm run test:build:gasLimit:v3 - name: run counters tests run: | npm run test:counters diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 9a1d521a..70bd02e8 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -569,7 +569,7 @@ opRETURNDATACOPY: ; if retDataSize <= retdataOffset + dataLen -> OOG ; A retDataSize B + C => B ; total offset (retdataOffset + dataLen) - $ :LT, JMPC(outOfGas) + A - B :JMPN(outOfGas) E => B ; memory pointer where start to copy memory ;${3*((C+31)/32)} diff --git a/package.json b/package.json index 7ec3049a..1eb3834f 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "scripts": { "build": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom.json && npm run build:metadata", "build:metadata": "echo \"COMMIT=$(git rev-parse HEAD)\nSHA256SUM=$(sha256sum ./build/rom.json)\" > build/metadata-rom.txt", - "build:gasLimit:v1": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-89128960.json -D TX_GAS_LIMIT=89128960", - "build:gasLimit:v2": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-100000000.json -D TX_GAS_LIMIT=100000000", - "build:gasLimit:v3": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-2147483647.json -D TX_GAS_LIMIT=2147483647", + "test:build:gasLimit:v1": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-89128960.test.json -D TX_GAS_LIMIT=89128960", + "test:build:gasLimit:v2": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-100000000.test.json -D TX_GAS_LIMIT=100000000", + "test:build:gasLimit:v3": "mkdir -p build && npx zkasm main/main.zkasm -o build/rom-2147483647.test.json -D TX_GAS_LIMIT=2147483647", "test:bitwise": "mkdir -p build && npx zkasm test/bitwise.zkasm -o build/rom-bitwise.test.json", "test:comp": "mkdir -p build && npx zkasm test/comp.zkasm -o build/rom-comp.test.json", "test:utils": "mkdir -p build && npx zkasm test/utils-getLenBytes.zkasm -o build/rom-utils.test.json", diff --git a/tools/parallel-tests-sample/sample.test.js b/tools/parallel-tests-sample/sample.test.js index 2ba9fb53..f29a9cc2 100644 --- a/tools/parallel-tests-sample/sample.test.js +++ b/tools/parallel-tests-sample/sample.test.js @@ -27,7 +27,7 @@ it(`${nameFile}`, async () => { const pil = JSON.parse(fs.readFileSync(fileCachePil)); const cmPols = newCommitPolsArray(pil); if (input.gasLimit) { - rom = require(`../../build/rom-${input.gasLimit}.json`) + rom = require(`../../build/rom-${input.gasLimit}.test.json`) } if (input.stepsN) { stepsN = input.stepsN From c243fd4b2f080762d360cc26894d5c7b28d899cf Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 2 Feb 2023 22:05:38 +0100 Subject: [PATCH 41/60] testvectors develop branch --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1eb3834f..6605902a 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/script-ooc", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From acefaaeb07081ba49e93c7f7e321ba038dbe32e7 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 2 Feb 2023 16:27:27 +0100 Subject: [PATCH 42/60] add fork identifier --- main/constants.zkasm | 1 + main/main.zkasm | 7 +++++++ main/vars.zkasm | 1 + 3 files changed, 9 insertions(+) diff --git a/main/constants.zkasm b/main/constants.zkasm index 33c7e6be..02ed998b 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -9,6 +9,7 @@ CONST %LOCAL_EXIT_ROOT_STORAGE_POS = 1 CONST %LAST_TX_STORAGE_POS = 0 CONST %STATE_ROOT_STORAGE_POS = 1 CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0 +CONST %FORK_ID = 1 ; RLP CONST %MIN_VALUE_SHORT = 128 diff --git a/main/main.zkasm b/main/main.zkasm index c112187f..e43f1e17 100644 --- a/main/main.zkasm +++ b/main/main.zkasm @@ -15,6 +15,12 @@ start: ; main zkROM entry point ;;;;;;;;;;;;;;;;;; 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) + B :MSTORE(oldStateRoot) C :MSTORE(oldAccInputHash) SP :MSTORE(oldNumBatch) @@ -194,6 +200,7 @@ processTxsEnd: $ => PC :MLOAD(newNumBatch) ; Set registers to its initials values + $ => CTX :MLOAD(forkID) $ => B :MLOAD(oldStateRoot) $ => C :MLOAD(oldAccInputHash) $ => SP :MLOAD(oldNumBatch) diff --git a/main/vars.zkasm b/main/vars.zkasm index 9b13f471..03b705b6 100644 --- a/main/vars.zkasm +++ b/main/vars.zkasm @@ -7,6 +7,7 @@ VAR GLOBAL sequencerAddr ; Coinbase address which will receive the fees VAR GLOBAL batchHashData ; batchHashData = H_keccak( transactions ) VAR GLOBAL timestamp ; Current batch timestamp VAR GLOBAL chainID ; Current batch chain id +VAR GLOBAL forkID ; Fork identifier ; Output variables VAR GLOBAL newAccInputHash ; Final accumulated input hash. newAccInputHash = H_keccak( oldAccInputHash | batchHashData | globalExitRoot | timestamp | sequencerAddr ) From 70dbd8460d129d06db32ea8336b0c0ab97848edd Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 3 Feb 2023 12:03:13 +0100 Subject: [PATCH 43/60] update packages --- counters/counters-executor.js | 7 ++++++- counters/endIncludes.zkasm | 3 ++- main/opcodes/create-terminate-context.zkasm | 2 +- package.json | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/counters/counters-executor.js b/counters/counters-executor.js index 50b27e81..a72d65eb 100644 --- a/counters/counters-executor.js +++ b/counters/counters-executor.js @@ -28,7 +28,12 @@ async function main() { async function runTest(testName, cmPols) { const zkasmFile = `${testFilesDir}/${testName}`; // Compile rom - const rom = await zkasm.compile(zkasmFile); + const configZkasm = { + defines: [], + allowUndefinedLabels: true + }; + + const rom = await zkasm.compile(zkasmFile, null, configZkasm); const config = { debug: true, stepsN: 8388608, diff --git a/counters/endIncludes.zkasm b/counters/endIncludes.zkasm index 5e317cf5..aea4e436 100644 --- a/counters/endIncludes.zkasm +++ b/counters/endIncludes.zkasm @@ -13,4 +13,5 @@ INCLUDE "../main/opcodes/flow-control.zkasm" INCLUDE "../main/opcodes/logs.zkasm" INCLUDE "../main/opcodes/stack-operations.zkasm" INCLUDE "../main/opcodes/storage-memory.zkasm" -INCLUDE "../main/touched.zkasm" \ No newline at end of file +INCLUDE "../main/touched.zkasm" +INCLUDE "../main/end.zkasm" \ No newline at end of file diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index 182b180b..15c25f16 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -848,7 +848,7 @@ opREVERT: %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) ; check stack underflow SP - 2 => SP :JMPN(stackUnderflow) - ; load initSR to revert al state changes + ; load initSR to revert all state changes ; revert touched accounts $ => SR :MLOAD(initSR), CALL(revertTouched) diff --git a/package.json b/package.json index 6605902a..24e74334 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "url": "https://github.com/0xPolygonHermez/zkevm-rom.git" }, "dependencies": { - "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#59deb58849ce1cfd9b3fda4d9fdf7558a4478013", + "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#9ca44615518218f660dd8037676d2e3eee988038", "yargs": "^17.5.1" }, "devDependencies": { From 25781fed9522a9e614a1d5351a2b0bb2126f386d Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 3 Feb 2023 12:03:13 +0100 Subject: [PATCH 44/60] update packages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 24e74334..80ab6aea 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/fork-id", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From e8e868f3cc5715e7d9f4223034aea877917672d9 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 3 Feb 2023 15:31:45 +0100 Subject: [PATCH 45/60] revert when calling a unsupported precompiled --- main/precompiled/end.zkasm | 8 +------- main/precompiled/revert-precompiled.zkasm | 24 +++++++++++++++++++++++ main/precompiled/selector.zkasm | 18 ++++++++++------- main/utils.zkasm | 5 ++++- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 main/precompiled/revert-precompiled.zkasm diff --git a/main/precompiled/end.zkasm b/main/precompiled/end.zkasm index 7be861e2..4dbbbbd0 100644 --- a/main/precompiled/end.zkasm +++ b/main/precompiled/end.zkasm @@ -3,10 +3,4 @@ preEnd: B + GAS => GAS $ => SP :MLOAD(lastSP) $ => PC :MLOAD(lastPC) - 1 :MSTORE(SP++), JMP(readCode) - -preEndFail: - $ => GAS :MLOAD(gasCTX) - $ => SP :MLOAD(lastSP) - $ => PC :MLOAD(lastPC) - 0 :MSTORE(SP++), JMP(readCode) + 1 :MSTORE(SP++), JMP(readCode) \ No newline at end of file diff --git a/main/precompiled/revert-precompiled.zkasm b/main/precompiled/revert-precompiled.zkasm new file mode 100644 index 00000000..0eceffd7 --- /dev/null +++ b/main/precompiled/revert-precompiled.zkasm @@ -0,0 +1,24 @@ +revertPrecompiled: + ; load initSR to revert all state changes + ; revert touched accounts + $ => SR :MLOAD(initSR), CALL(revertTouched) + $${eventLog(onError, revert)} + + ; check if it is the first context + $ => A :MLOAD(originCTX), JMPZ(handleGas) ; first context + A => CTX :MSTORE(currentCTX) + + ; return gas not used to previous context + $ => B :MLOAD(gasCTX) + GAS + B => GAS + + ; restore SP and PC + $ => SP :MLOAD(lastSP) + $ => PC :MLOAD(lastPC) + + ; write 0 in previous context stack + 0 :MSTORE(SP++) + + ; decrease depth + $ => A :MLOAD(depth) + A - 1 :MSTORE(depth), JMP(readCode) \ No newline at end of file diff --git a/main/precompiled/selector.zkasm b/main/precompiled/selector.zkasm index 21e60ffb..a503909d 100644 --- a/main/precompiled/selector.zkasm +++ b/main/precompiled/selector.zkasm @@ -1,4 +1,5 @@ INCLUDE "pre-ecrecover.zkasm" +INCLUDE "revert-precompiled.zkasm" INCLUDE "identity.zkasm" INCLUDE "end.zkasm" @@ -6,14 +7,17 @@ INCLUDE "end.zkasm" * Selector precompiled contract to run * Current precompiled supported: ECRECOVER & IDENTITY * @param {A} - Precompiled address + * @dev Any call to an unsupported precompiled will result in a revert + * - All gas is refunded + * - 0 is returned */ selectorPrecompiled: A - 2 :JMPN(funcECRECOVER) - A - 3 :JMPN(callContract) ;:JMPN(SHA256) - A - 4 :JMPN(callContract) ;:JMPN(RIPEMD160) + A - 3 :JMPN(revertPrecompiled) ;:JMPN(SHA256) + A - 4 :JMPN(revertPrecompiled) ;:JMPN(RIPEMD160) A - 5 :JMPN(IDENTITY) - A - 6 :JMPN(callContract) ;:JMPN(MODEXP) - A - 7 :JMPN(callContract) ;:JMPN(ECADD) - A - 8 :JMPN(callContract) ;:JMPN(ECMUL) - A - 9 :JMPN(callContract) ;:JMPN(ECPAIRING) - A - 10 :JMPN(callContract) ;:JMPN(BLAKE2F) \ No newline at end of file + A - 6 :JMPN(revertPrecompiled) ;:JMPN(MODEXP) + A - 7 :JMPN(revertPrecompiled) ;:JMPN(ECADD) + A - 8 :JMPN(revertPrecompiled) ;:JMPN(ECMUL) + A - 9 :JMPN(revertPrecompiled) ;:JMPN(ECPAIRING) + A - 10 :JMPN(revertPrecompiled) ;:JMPN(BLAKE2F) \ No newline at end of file diff --git a/main/utils.zkasm b/main/utils.zkasm index a3c11b97..7a33fa23 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -960,7 +960,10 @@ invalidCall: GAS + A => GAS $ => SP :MLOAD(lastSP) $ => PC :MLOAD(lastPC) - 0 :MSTORE(SP++), JMP(readCode) + 0 :MSTORE(SP++) + ; decrease depth + $ => A :MLOAD(depth) + A - 1 :MSTORE(depth), JMP(readCode) VAR GLOBAL pushBytes VAR GLOBAL numBlocks From 4460dd39f624834c105659941042adc394519f94 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Mon, 6 Feb 2023 10:35:03 +0100 Subject: [PATCH 46/60] padding counter check, fix free inputs --- main/opcodes/block.zkasm | 12 ++--- main/opcodes/calldata-returndata-code.zkasm | 1 + main/precompiled/revert-precompiled.zkasm | 2 + main/process-tx.zkasm | 9 ++-- main/utils.zkasm | 50 +++++++++++++++++---- package.json | 2 +- 6 files changed, 56 insertions(+), 20 deletions(-) diff --git a/main/opcodes/block.zkasm b/main/opcodes/block.zkasm index 88bf680a..14be3eeb 100644 --- a/main/opcodes/block.zkasm +++ b/main/opcodes/block.zkasm @@ -67,7 +67,7 @@ opCOINBASE: GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) $ => A :MLOAD(sequencerAddr) - A :MSTORE(SP++); [address => E] + A :MSTORE(SP++); [coinbase address => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) @@ -86,7 +86,7 @@ opTIMESTAMP: ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) $ => A :MLOAD(timestamp) - A :MSTORE(SP++); [timestamp => E] + A :MSTORE(SP++); [timestamp => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) @@ -106,7 +106,7 @@ opNUMBER: GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; Get current tx count $ => D :MLOAD(txCount) - D + 1 :MSTORE(SP++); [blockNumber => E] + D + 1 :MSTORE(SP++); [blockNumber => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) @@ -126,7 +126,7 @@ opDIFFICULTY: GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; No difficulty, always 0 %BATCH_DIFFICULTY => A - A :MSTORE(SP++); [difficulty => E] + A :MSTORE(SP++); [difficulty => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) @@ -146,7 +146,7 @@ opGASLIMIT: GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; constant tx gas limit %TX_GAS_LIMIT => A - A :MSTORE(SP++); [gasLimit => E] + A :MSTORE(SP++); [gasLimit => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) @@ -165,6 +165,6 @@ opCHAINID: ; check out-of-gas GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas) $ => A :MLOAD(chainID) - A :MSTORE(SP++); [chainId => E] + A :MSTORE(SP++); [chainId => SP] ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) \ No newline at end of file diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 70bd02e8..66ac2a79 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -476,6 +476,7 @@ opEXTCODECOPYLoadBytecode: $ => RR :MLOAD(tmpZkPCext) $ => B :MLOAD(arithRes1) %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 - B :JMPN(outOfCountersPoseidon) + %MAX_CNT_PADDING_PG_LIMIT - CNT_PADDING_PG - 1 - B :JMPN(outOfCountersPadding) ; set key for smt smart contract code query E => A diff --git a/main/precompiled/revert-precompiled.zkasm b/main/precompiled/revert-precompiled.zkasm index 0eceffd7..d271ab9f 100644 --- a/main/precompiled/revert-precompiled.zkasm +++ b/main/precompiled/revert-precompiled.zkasm @@ -1,4 +1,6 @@ revertPrecompiled: + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + ; load initSR to revert all state changes ; revert touched accounts $ => SR :MLOAD(initSR), CALL(revertTouched) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index fd87dc43..f1141e35 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -392,10 +392,11 @@ callContract: ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - B :MSTORE(arithA) - 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) + B :MSTORE(arithA) + 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => B :MLOAD(arithRes1) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) + %MAX_CNT_PADDING_PG_LIMIT - CNT_PADDING_PG - 1 - B :JMPN(outOfCountersPadding) ; get hash contract $ => A :MLOAD(txDestAddr) diff --git a/main/utils.zkasm b/main/utils.zkasm index 7a33fa23..d2ccfd8a 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -438,8 +438,16 @@ computeGasSendCall: ; compute all_but_one_64th gas GAS => A - ${GAS >> 6} => C + + ; 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 @@ -501,8 +509,16 @@ saveMemGAS: ; memory_size_word = (memory_byte_size + 31) / 32 in E ; ${(B+31)/32} => E E + 31 => A - ${A >> 5} => E + + ; 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 @@ -889,8 +905,15 @@ offsetUtil: A :MSTORE(tmpVarAoffsetUtil) B :MSTORE(tmpVarBoffsetUtil) - ${A >> 5} => E + ; 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 @@ -981,10 +1004,18 @@ readPush: D => A 0 :MSTORE(accumulator) PC + D - 1 => HASHPOS - ${A >> 2} => B + + ; B = [b7, b6, ..., b0] + ; JMPN instruction assures b0 is within the range [0, 2**32 - 1] + ${A >> 2} => B :JMPN(failAssert) B :MSTORE(numBlocks) ${A & 0x03} => D D :MSTORE(leftBytes) + + ; since D is assured to be less than 0x04 + ; it is enforced that [b7, b6, ..., b1] are 0 since there is no value multiplied by 4 + ; that equals the field + ; Since b0 is assured to be less than 32 bits, b0 * 4 + d0 could not overflow the field B * 4 + D :ASSERT D => A 0x04 => B @@ -1174,10 +1205,11 @@ hashPoseidonLinearFromMemory: C :JMPZ(hashPoseidonReturn) ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length - C :MSTORE(arithA) - 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => B :MLOAD(arithRes1) - %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) + C :MSTORE(arithA) + 56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] + $ => B :MLOAD(arithRes1) + %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon) + %MAX_CNT_PADDING_PG_LIMIT - CNT_PADDING_PG - 1 - B :JMPN(outOfCountersPadding) ; get a new hashPId $ => B :MLOAD(nextHashPId) @@ -1459,4 +1491,4 @@ expADend: ;@info function to force a failed assertt failAssert: 1 => A - 2 :ASSERT + 2 :ASSERT \ No newline at end of file diff --git a/package.json b/package.json index 80ab6aea..db22c881 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/fork-id", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/fork-id", "chai": "^4.3.6", "chalk": "^3.0.0", From 592286e2dc576b51ef9f54a24b9bf0e72436bfc2 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Mon, 6 Feb 2023 18:24:05 +0100 Subject: [PATCH 47/60] update packages to set commits --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index db22c881..c29c508e 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/fork-id", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/fork-id", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#885fa92827282b8c977f55c9e1d4ffe8b16a6b53", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#06449c4e77374303e0c7206b584048b321e667e0", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From 5d164c5e76360f5c88d2914efb6179be53ea3083 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 7 Feb 2023 13:44:30 +0100 Subject: [PATCH 48/60] fixes identity and not set CTX to 0 --- main/end.zkasm | 4 ++-- main/precompiled/identity.zkasm | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/main/end.zkasm b/main/end.zkasm index f297e23f..5ac75185 100644 --- a/main/end.zkasm +++ b/main/end.zkasm @@ -1,4 +1,4 @@ finalWait: ${beforeLast()} :JMPN(finalWait) - ; Set all registers to 0 except inputs: B (oldstateRoot), C (oldAccInputHash), SP (oldNumBatch) & GAS (chainID) - 0 => A, D, E, CTX, PC, MAXMEM, SR, HASHPOS, RR, RCX :JMP(start) \ No newline at end of file + ; Set all registers to 0 except inputs: B (oldstateRoot), C (oldAccInputHash), SP (oldNumBatch), GAS (chainID) & CTX (forkID) + 0 => A, D, E, PC, MAXMEM, SR, HASHPOS, RR, RCX :JMP(start) \ No newline at end of file diff --git a/main/precompiled/identity.zkasm b/main/precompiled/identity.zkasm index 11c55c0f..b9d79c5c 100644 --- a/main/precompiled/identity.zkasm +++ b/main/precompiled/identity.zkasm @@ -18,8 +18,9 @@ IDENTITY: GAS - %IDENTITY_WORD_GAS*A => GAS :JMPN(outOfGas) CTX :MSTORE(currentCTX) CTX => A - $ => CTX :MLOAD(originCTX), JMPZ(handleGas) - A :MSTORE(retDataCTX) + $ => B :MLOAD(originCTX), JMPZ(handleGas) + B => CTX + A :MSTORE(retDataCTX) A => CTX 0 => B 0 => E From 0e8b5665ad61c21b2b0143d195f6869e65b40ca7 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 9 Feb 2023 19:08:53 +0100 Subject: [PATCH 49/60] ger manager address L2 --- main/constants.zkasm | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index 02ed998b..8f3697f8 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -1,5 +1,5 @@ ; ZK_EVM -CONSTL %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = 0xAE4bB80bE56B819606589DE61d5ec3b522EEB032n +CONSTL %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = 0xa40D5f56745a118D0906a34E69aeC8C0Db1cB8fAn CONSTL %ADDRESS_SYSTEM = 0x000000000000000000000000000000005ca1ab1en CONST %CALLDATA_OFFSET = 1024 CONST %BATCH_DIFFICULTY = 0 diff --git a/package.json b/package.json index c29c508e..28b70ebb 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#885fa92827282b8c977f55c9e1d4ffe8b16a6b53", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#06449c4e77374303e0c7206b584048b321e667e0", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#188654030742c3d09bac111c467869f08051a16b", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.7.0.0-rc.1", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From 9840ddef4806057d74f3275482126fb96ef9f004 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 9 Feb 2023 19:08:53 +0100 Subject: [PATCH 50/60] ger manager address L2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 28b70ebb..c59f46df 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#188654030742c3d09bac111c467869f08051a16b", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.7.0.0-rc.1", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/update-get-info", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From f2d0e0ed2f481266aef2b1de9cd3405951f45363 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Tue, 14 Feb 2023 22:49:37 +0100 Subject: [PATCH 51/60] fix adding bytes to batchHashData --- main/end.zkasm | 2 +- main/load-tx-rlp.zkasm | 16 ++++++++-------- package.json | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main/end.zkasm b/main/end.zkasm index 5ac75185..c10363de 100644 --- a/main/end.zkasm +++ b/main/end.zkasm @@ -1,4 +1,4 @@ finalWait: ${beforeLast()} :JMPN(finalWait) ; Set all registers to 0 except inputs: B (oldstateRoot), C (oldAccInputHash), SP (oldNumBatch), GAS (chainID) & CTX (forkID) - 0 => A, D, E, PC, MAXMEM, SR, HASHPOS, RR, RCX :JMP(start) \ No newline at end of file + 0 => A, D, E, PC, SR, HASHPOS, RR, RCX :JMP(start) \ No newline at end of file diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 216e1400..0968987f 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -37,8 +37,8 @@ loadTx_rlp: longList: A - 0xf7 => D :CALL(addHashTx) - :CALL(checkLongRLP) :CALL(addBatchHashData) + :CALL(checkLongRLP) :JMP(endList) shortList: A - 0xc0 => A @@ -67,8 +67,8 @@ nonce0: shortNonce: A - 0x80 => D D :MSTORE(lengthNonce), CALL(addHashTx) - :CALL(checkShortRLP) :CALL(addBatchHashData) + :CALL(checkShortRLP) endNonce: A :MSTORE(txNonce) @@ -88,8 +88,8 @@ gasPrice0: shortGasPrice: A - 0x80 => D :CALL(addHashTx) - :CALL(checkShortRLP) :CALL(addBatchHashData) + :CALL(checkShortRLP) endGasPrice: A :MSTORE(txGasPriceRLP) @@ -109,8 +109,8 @@ gasLimit0: shortGasLimit: A - 0x80 => D :CALL(addHashTx) - :CALL(checkShortRLP) :CALL(addBatchHashData) + :CALL(checkShortRLP) endGasLimit: A :MSTORE(txGasLimit) @@ -150,8 +150,8 @@ value0: shortValue: A - 0x80 => D :CALL(addHashTx) - :CALL(checkShortRLP) :CALL(addBatchHashData) + :CALL(checkShortRLP) endValue: A :MSTORE(txValue) @@ -181,8 +181,8 @@ shortData: longData: A - 0xb7 => D :CALL(addHashTx) - :CALL(checkLongRLP) :CALL(addBatchHashData) + :CALL(checkLongRLP) $ => D :MLOAD(batchHashPos) D :MSTORE(dataStarts) A => B :MSTORE(txCalldataLen) @@ -197,10 +197,10 @@ readData: readDataFinal: B - 1 :JMPN(endData) B => D :CALL(addHashTx) - :CALL(checkShortDataRLP) 32 - D => D :CALL(SHLarith) A :MSTORE(SP) 32 - D => D :CALL(addBatchHashByteByByte) + :CALL(checkShortDataRLP) endData: ; Check all bytes read to detect pre EIP-155 tx @@ -223,8 +223,8 @@ chainId0: shortChainId: A - 0x80 => D :CALL(addHashTx) - :CALL(checkShortRLP) :CALL(addBatchHashData) + :CALL(checkShortRLP) endChainId: A :MSTORE(txChainId) diff --git a/package.json b/package.json index c59f46df..8eccbd92 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#188654030742c3d09bac111c467869f08051a16b", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/update-get-info", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/error-rlp", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From fb251a04858f43de77cd1e7a29a67140dce5846f Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 16 Feb 2023 12:16:23 +0100 Subject: [PATCH 52/60] update pacjage remove MAXMEM --- counters/README.md | 2 +- counters/tests/MLOAD32.zkasm | 2 +- counters/tests/MLOADX.zkasm | 2 +- counters/tests/MSTORE32.zkasm | 2 +- counters/tests/MSTOREX.zkasm | 2 +- counters/tests/SHLarith.zkasm | 2 +- counters/tests/SHLarithBit.zkasm | 2 +- counters/tests/SHRarith.zkasm | 2 +- counters/tests/SHRarithBit.zkasm | 2 +- counters/tests/abs.zkasm | 2 +- counters/tests/addBatchHashByteByByte.zkasm | 2 +- counters/tests/computeGasSendCall.zkasm | 2 +- counters/tests/copySP.zkasm | 2 +- counters/tests/divArith.zkasm | 2 +- counters/tests/expAD.zkasm | 2 +- counters/tests/getLenBits.zkasm | 2 +- counters/tests/getLenBytes.zkasm | 2 +- counters/tests/isEmptyAccount.zkasm | 2 +- counters/tests/mulARITH.zkasm | 2 +- counters/tests/offsetUtil.zkasm | 2 +- counters/tests/opADDMOD.zkasm | 2 +- counters/tests/opAdd.zkasm | 2 +- counters/tests/opBLOCKHASH.zkasm | 2 +- counters/tests/opCALL.zkasm | 2 +- counters/tests/opCALLCODE.zkasm | 2 +- counters/tests/opCALLDATACOPY.zkasm | 2 +- counters/tests/opCALLDATALOAD.zkasm | 2 +- counters/tests/opCODECOPY.zkasm | 2 +- counters/tests/opCREATE.zkasm | 2 +- counters/tests/opCREATE2.zkasm | 2 +- counters/tests/opDELEGATECALL.zkasm | 2 +- counters/tests/opDIV.zkasm | 2 +- counters/tests/opEXP.zkasm | 2 +- counters/tests/opEXTCODECOPY.zkasm | 2 +- counters/tests/opMOD.zkasm | 2 +- counters/tests/opMUL.zkasm | 2 +- counters/tests/opMULMOD.zkasm | 2 +- counters/tests/opRETURN.zkasm | 2 +- counters/tests/opRETURNDATACOPY.zkasm | 2 +- counters/tests/opREVERT.zkasm | 2 +- counters/tests/opSDIV.zkasm | 2 +- counters/tests/opSHA3.zkasm | 2 +- counters/tests/opSIGNEXTEND.zkasm | 2 +- counters/tests/opSMOD.zkasm | 2 +- counters/tests/opSTATICCALL.zkasm | 2 +- counters/tests/opSUB.zkasm | 2 +- counters/tests/saveMem.zkasm | 2 +- package.json | 4 ++-- test/bitwise.zkasm | 2 +- test/comp.zkasm | 2 +- test/ecrecover.zkasm | 2 +- test/rotate.zkasm | 2 +- test/touched-assert.zkasm | 2 +- test/utils-expAD.zkasm | 2 +- test/utils-getLenBytes.zkasm | 2 +- 55 files changed, 56 insertions(+), 56 deletions(-) diff --git a/counters/README.md b/counters/README.md index 01f35ee7..850c5583 100644 --- a/counters/README.md +++ b/counters/README.md @@ -22,7 +22,7 @@ checkCounters: %OPADD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPADD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) // Finalize execution -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/MLOAD32.zkasm b/counters/tests/MLOAD32.zkasm index 826bb024..c908fc6b 100644 --- a/counters/tests/MLOAD32.zkasm +++ b/counters/tests/MLOAD32.zkasm @@ -15,7 +15,7 @@ operation: %MLOAD32_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %MLOAD32_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/MLOADX.zkasm b/counters/tests/MLOADX.zkasm index 293810b7..2b9f95c8 100644 --- a/counters/tests/MLOADX.zkasm +++ b/counters/tests/MLOADX.zkasm @@ -18,7 +18,7 @@ operation: %MLOADX_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %MLOADX_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/MSTORE32.zkasm b/counters/tests/MSTORE32.zkasm index d6a1a633..25bb2439 100644 --- a/counters/tests/MSTORE32.zkasm +++ b/counters/tests/MSTORE32.zkasm @@ -20,7 +20,7 @@ operation: %MSTORE32_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %MSTORE32_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/MSTOREX.zkasm b/counters/tests/MSTOREX.zkasm index 418cf1a8..32a1b843 100644 --- a/counters/tests/MSTOREX.zkasm +++ b/counters/tests/MSTOREX.zkasm @@ -24,7 +24,7 @@ operation: %MSTOREX_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %MSTOREX_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/SHLarith.zkasm b/counters/tests/SHLarith.zkasm index c6e807ad..88466dc0 100644 --- a/counters/tests/SHLarith.zkasm +++ b/counters/tests/SHLarith.zkasm @@ -16,7 +16,7 @@ operation: %SHLARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %SHLARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/SHLarithBit.zkasm b/counters/tests/SHLarithBit.zkasm index b5a69ce2..b1db3893 100644 --- a/counters/tests/SHLarithBit.zkasm +++ b/counters/tests/SHLarithBit.zkasm @@ -16,7 +16,7 @@ operation: %SHLARITHBIT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %SHLARITHBIT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/SHRarith.zkasm b/counters/tests/SHRarith.zkasm index 795299c9..5db4b10c 100644 --- a/counters/tests/SHRarith.zkasm +++ b/counters/tests/SHRarith.zkasm @@ -16,7 +16,7 @@ operation: %SHRARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %SHRARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/SHRarithBit.zkasm b/counters/tests/SHRarithBit.zkasm index 8a9203d4..4b776b96 100644 --- a/counters/tests/SHRarithBit.zkasm +++ b/counters/tests/SHRarithBit.zkasm @@ -16,7 +16,7 @@ operation: %SHRARITHBIT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %SHRARITHBIT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/abs.zkasm b/counters/tests/abs.zkasm index 85e0582e..c97f5b44 100644 --- a/counters/tests/abs.zkasm +++ b/counters/tests/abs.zkasm @@ -18,7 +18,7 @@ operation: %ABS_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %ABS_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) finalizeExecution: -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) readCode: :RETURN diff --git a/counters/tests/addBatchHashByteByByte.zkasm b/counters/tests/addBatchHashByteByByte.zkasm index 4b3e39cc..850b5c34 100644 --- a/counters/tests/addBatchHashByteByByte.zkasm +++ b/counters/tests/addBatchHashByteByByte.zkasm @@ -19,7 +19,7 @@ operation: %ADDBATCHHASH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %ADDBATCHHASH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/computeGasSendCall.zkasm b/counters/tests/computeGasSendCall.zkasm index 5155a10b..8307e888 100644 --- a/counters/tests/computeGasSendCall.zkasm +++ b/counters/tests/computeGasSendCall.zkasm @@ -18,7 +18,7 @@ operation: %COMPUTEGASSENDCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %COMPUTEGASSENDCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/copySP.zkasm b/counters/tests/copySP.zkasm index 1ff439bb..e89327a7 100644 --- a/counters/tests/copySP.zkasm +++ b/counters/tests/copySP.zkasm @@ -26,7 +26,7 @@ operation: %COPYSP_CNT_PADDING_PG + %MLOAD32_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) %COPYSP_CNT_POSEIDON_G + %MLOAD32_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/divArith.zkasm b/counters/tests/divArith.zkasm index 35269a4d..39cb6f06 100644 --- a/counters/tests/divArith.zkasm +++ b/counters/tests/divArith.zkasm @@ -16,7 +16,7 @@ operation: %DIVARITH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %DIVARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) finalizeExecution: -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) readCode: :RETURN diff --git a/counters/tests/expAD.zkasm b/counters/tests/expAD.zkasm index 78a0874c..3e6096f2 100644 --- a/counters/tests/expAD.zkasm +++ b/counters/tests/expAD.zkasm @@ -18,7 +18,7 @@ operation: %EXPAD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %EXPAD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/getLenBits.zkasm b/counters/tests/getLenBits.zkasm index e65363cf..fd461b24 100644 --- a/counters/tests/getLenBits.zkasm +++ b/counters/tests/getLenBits.zkasm @@ -18,7 +18,7 @@ operation: %GETLENBITS_CNT_PADDING_PG + %DIVARITH_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) %GETLENBITS_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/getLenBytes.zkasm b/counters/tests/getLenBytes.zkasm index 2e6caf0f..bd755937 100644 --- a/counters/tests/getLenBytes.zkasm +++ b/counters/tests/getLenBytes.zkasm @@ -20,7 +20,7 @@ operation: %GETLENBYTES_CNT_PADDING_PG + %SHRARITH_CNT_PADDING_PG*C - CNT_PADDING_PG :JMPNZ(failedCounters) %GETLENBYTES_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G*C - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/isEmptyAccount.zkasm b/counters/tests/isEmptyAccount.zkasm index fbb4999c..60a21155 100644 --- a/counters/tests/isEmptyAccount.zkasm +++ b/counters/tests/isEmptyAccount.zkasm @@ -18,7 +18,7 @@ operation: %ISEMPTYACCOUNT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %ISEMPTYACCOUNT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/mulARITH.zkasm b/counters/tests/mulARITH.zkasm index 2152e7a5..bd408ea1 100644 --- a/counters/tests/mulARITH.zkasm +++ b/counters/tests/mulARITH.zkasm @@ -17,7 +17,7 @@ operation: %MULARITH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) finalizeExecution: -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) readCode: :RETURN diff --git a/counters/tests/offsetUtil.zkasm b/counters/tests/offsetUtil.zkasm index 9cc17812..b4ebf974 100644 --- a/counters/tests/offsetUtil.zkasm +++ b/counters/tests/offsetUtil.zkasm @@ -18,7 +18,7 @@ operation: %OFFSETUTIL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OFFSETUTIL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) finalizeExecution: -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) readCode: :RETURN diff --git a/counters/tests/opADDMOD.zkasm b/counters/tests/opADDMOD.zkasm index f270d6be..ba94aaef 100644 --- a/counters/tests/opADDMOD.zkasm +++ b/counters/tests/opADDMOD.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPADDMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPADDMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opAdd.zkasm b/counters/tests/opAdd.zkasm index 1f055b3e..702fc8a5 100644 --- a/counters/tests/opAdd.zkasm +++ b/counters/tests/opAdd.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPADD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPADD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opBLOCKHASH.zkasm b/counters/tests/opBLOCKHASH.zkasm index 48379303..f534d4e0 100644 --- a/counters/tests/opBLOCKHASH.zkasm +++ b/counters/tests/opBLOCKHASH.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPBLOCKHASH_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPBLOCKHASH_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCALL.zkasm b/counters/tests/opCALL.zkasm index 64cca73c..5f85bd63 100644 --- a/counters/tests/opCALL.zkasm +++ b/counters/tests/opCALL.zkasm @@ -29,7 +29,7 @@ checkCounters: %OPCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCALLCODE.zkasm b/counters/tests/opCALLCODE.zkasm index d6ba3110..8e352a49 100644 --- a/counters/tests/opCALLCODE.zkasm +++ b/counters/tests/opCALLCODE.zkasm @@ -29,7 +29,7 @@ checkCounters: %OPCALLCODE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCALLCODE_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCALLDATACOPY.zkasm b/counters/tests/opCALLDATACOPY.zkasm index 8c4a8937..f7a287f8 100644 --- a/counters/tests/opCALLDATACOPY.zkasm +++ b/counters/tests/opCALLDATACOPY.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPCALLDATACOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCALLDATACOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCALLDATALOAD.zkasm b/counters/tests/opCALLDATALOAD.zkasm index 2cf05441..3e0be014 100644 --- a/counters/tests/opCALLDATALOAD.zkasm +++ b/counters/tests/opCALLDATALOAD.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPCALLDATALOAD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCALLDATALOAD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCODECOPY.zkasm b/counters/tests/opCODECOPY.zkasm index a8cf363b..f121dd1a 100644 --- a/counters/tests/opCODECOPY.zkasm +++ b/counters/tests/opCODECOPY.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCODECOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCREATE.zkasm b/counters/tests/opCREATE.zkasm index b6d01b52..7fe3de3c 100644 --- a/counters/tests/opCREATE.zkasm +++ b/counters/tests/opCREATE.zkasm @@ -23,7 +23,7 @@ checkCounters: %OPCREATE_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCREATE_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm index d36934c6..84883453 100644 --- a/counters/tests/opCREATE2.zkasm +++ b/counters/tests/opCREATE2.zkasm @@ -23,7 +23,7 @@ checkCounters: %OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPCREATE2_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opDELEGATECALL.zkasm b/counters/tests/opDELEGATECALL.zkasm index 1d7c3980..fcdbccbb 100644 --- a/counters/tests/opDELEGATECALL.zkasm +++ b/counters/tests/opDELEGATECALL.zkasm @@ -23,7 +23,7 @@ checkCounters: %OPDELEGATECALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPDELEGATECALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opDIV.zkasm b/counters/tests/opDIV.zkasm index e3a07a9e..76105dc8 100644 --- a/counters/tests/opDIV.zkasm +++ b/counters/tests/opDIV.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPDIV_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPDIV_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opEXP.zkasm b/counters/tests/opEXP.zkasm index a2188947..98ab4143 100644 --- a/counters/tests/opEXP.zkasm +++ b/counters/tests/opEXP.zkasm @@ -18,7 +18,7 @@ checkCounters: %OPEXP_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPEXP_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opEXTCODECOPY.zkasm b/counters/tests/opEXTCODECOPY.zkasm index 17d4a72b..bd3d02ce 100644 --- a/counters/tests/opEXTCODECOPY.zkasm +++ b/counters/tests/opEXTCODECOPY.zkasm @@ -18,7 +18,7 @@ checkCounters: %OPEXTCODECOPY_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPEXTCODECOPY_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opMOD.zkasm b/counters/tests/opMOD.zkasm index 834ef195..fd67813b 100644 --- a/counters/tests/opMOD.zkasm +++ b/counters/tests/opMOD.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opMUL.zkasm b/counters/tests/opMUL.zkasm index 6352c475..dad768d9 100644 --- a/counters/tests/opMUL.zkasm +++ b/counters/tests/opMUL.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPMUL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPMUL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opMULMOD.zkasm b/counters/tests/opMULMOD.zkasm index fd87e66d..4363188d 100644 --- a/counters/tests/opMULMOD.zkasm +++ b/counters/tests/opMULMOD.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPMULMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPMULMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opRETURN.zkasm b/counters/tests/opRETURN.zkasm index 820e6526..e35ca84d 100644 --- a/counters/tests/opRETURN.zkasm +++ b/counters/tests/opRETURN.zkasm @@ -20,7 +20,7 @@ checkCounters: %OPRETURN_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPRETURN_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opRETURNDATACOPY.zkasm b/counters/tests/opRETURNDATACOPY.zkasm index 7905c102..3be3e153 100644 --- a/counters/tests/opRETURNDATACOPY.zkasm +++ b/counters/tests/opRETURNDATACOPY.zkasm @@ -18,7 +18,7 @@ checkCounters: 1000 - STEP:JMPN(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opREVERT.zkasm b/counters/tests/opREVERT.zkasm index c8f387a7..9897405e 100644 --- a/counters/tests/opREVERT.zkasm +++ b/counters/tests/opREVERT.zkasm @@ -20,7 +20,7 @@ checkCounters: %OPREVERT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPREVERT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSDIV.zkasm b/counters/tests/opSDIV.zkasm index e269b08f..c3be019f 100644 --- a/counters/tests/opSDIV.zkasm +++ b/counters/tests/opSDIV.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPSDIV_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSDIV_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSHA3.zkasm b/counters/tests/opSHA3.zkasm index 741b452e..82719ca0 100644 --- a/counters/tests/opSHA3.zkasm +++ b/counters/tests/opSHA3.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPSHA3_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSHA3_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSIGNEXTEND.zkasm b/counters/tests/opSIGNEXTEND.zkasm index 8622f698..5933e705 100644 --- a/counters/tests/opSIGNEXTEND.zkasm +++ b/counters/tests/opSIGNEXTEND.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPSIGNEXTEND_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSIGNEXTEND_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSMOD.zkasm b/counters/tests/opSMOD.zkasm index 8f537785..7f3623df 100644 --- a/counters/tests/opSMOD.zkasm +++ b/counters/tests/opSMOD.zkasm @@ -17,7 +17,7 @@ checkCounters: %OPSMOD_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSMOD_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSTATICCALL.zkasm b/counters/tests/opSTATICCALL.zkasm index 49922d5a..35d1089e 100644 --- a/counters/tests/opSTATICCALL.zkasm +++ b/counters/tests/opSTATICCALL.zkasm @@ -23,7 +23,7 @@ checkCounters: %OPSTATICCALL_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSTATICCALL_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/opSUB.zkasm b/counters/tests/opSUB.zkasm index 878c7d1b..bcfcfab3 100644 --- a/counters/tests/opSUB.zkasm +++ b/counters/tests/opSUB.zkasm @@ -16,7 +16,7 @@ checkCounters: %OPSUB_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %OPSUB_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/counters/tests/saveMem.zkasm b/counters/tests/saveMem.zkasm index 9f52f3c4..297643f1 100644 --- a/counters/tests/saveMem.zkasm +++ b/counters/tests/saveMem.zkasm @@ -19,7 +19,7 @@ operation: %SAVEMEM_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters) %SAVEMEM_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters) -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) readCode: diff --git a/package.json b/package.json index 8eccbd92..c0fb685d 100644 --- a/package.json +++ b/package.json @@ -33,11 +33,11 @@ "url": "https://github.com/0xPolygonHermez/zkevm-rom.git" }, "dependencies": { - "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#9ca44615518218f660dd8037676d2e3eee988038", + "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#fix/non-maxmem-reg", "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#188654030742c3d09bac111c467869f08051a16b", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#fix/non-maxmem-reg", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/error-rlp", "chai": "^4.3.6", "chalk": "^3.0.0", diff --git a/test/bitwise.zkasm b/test/bitwise.zkasm index e9a16a9b..bb9cf106 100644 --- a/test/bitwise.zkasm +++ b/test/bitwise.zkasm @@ -67,7 +67,7 @@ start: ${bitwise_not(B)} :ASSERT - 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + 0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) INCLUDE "../main/end.zkasm" \ No newline at end of file diff --git a/test/comp.zkasm b/test/comp.zkasm index 01e6042f..f09b6eeb 100644 --- a/test/comp.zkasm +++ b/test/comp.zkasm @@ -65,7 +65,7 @@ start: 0 => A ${comp_eq(B, C)} :ASSERT - 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + 0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) INCLUDE "../main/end.zkasm" \ No newline at end of file diff --git a/test/ecrecover.zkasm b/test/ecrecover.zkasm index 354cdf51..a16c980b 100644 --- a/test/ecrecover.zkasm +++ b/test/ecrecover.zkasm @@ -423,7 +423,7 @@ repeat_ecrecover_test: INCLUDE "../main/ecrecover/ecrecover.zkasm" end: - 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR, RCX + 0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR, RCX finalizeExecution: finalWait: diff --git a/test/rotate.zkasm b/test/rotate.zkasm index bacdb7e6..9ec7ca49 100644 --- a/test/rotate.zkasm +++ b/test/rotate.zkasm @@ -114,7 +114,7 @@ endPushFinal: finalPush: -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) INCLUDE "../main/end.zkasm" diff --git a/test/touched-assert.zkasm b/test/touched-assert.zkasm index 0f13479e..bcfe29ce 100644 --- a/test/touched-assert.zkasm +++ b/test/touched-assert.zkasm @@ -54,7 +54,7 @@ start: D => A 0 :ASSERT - 0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 + 0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 finalizeExecution: :JMP(finalWait) diff --git a/test/utils-expAD.zkasm b/test/utils-expAD.zkasm index cfa8dc27..e0cf9052 100644 --- a/test/utils-expAD.zkasm +++ b/test/utils-expAD.zkasm @@ -42,7 +42,7 @@ start: 0xf2eda75a1e9624437a4f18c1316372866f14b6bf3f7ff7441996f65b747a0001n => A $ :MLOAD(test),ASSERT -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) INCLUDE "../main/main.zkasm" \ No newline at end of file diff --git a/test/utils-getLenBytes.zkasm b/test/utils-getLenBytes.zkasm index 56642394..8d8089f6 100644 --- a/test/utils-getLenBytes.zkasm +++ b/test/utils-getLenBytes.zkasm @@ -30,7 +30,7 @@ start: 6 => A $ :MLOAD(test),ASSERT -0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0 +0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 :JMP(finalWait) INCLUDE "../main/main.zkasm" \ No newline at end of file From c390711e1f52e3fb0c2f05d3e31c76e63e1fd761 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Fri, 17 Feb 2023 09:15:55 +0100 Subject: [PATCH 53/60] add onUpdateStorage in SLOAD/SSTORE oopcodes --- main/opcodes/storage-memory.zkasm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index dd74ceaf..245bf4a2 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -160,6 +160,7 @@ opSLOAD: $ => A :MLOAD(storageAddr) ; set key for smt storage query %SMT_KEY_SC_STORAGE => B + $${eventLog(onUpdateStorage)} $ => E :SLOAD ; set key(C) as warmed storage for address(A) E :MSTORE(SP++), CALL(isColdSlot); [value(E) => SP] @@ -305,4 +306,5 @@ opSSTOREsr: ; set key for smt storage query %SMT_KEY_SC_STORAGE => B $ => C :MLOAD(tmpVarCsstore); key => C + $${eventLog(onUpdateStorage)} $ => SR :SSTORE, JMP(readCode) From 9735552de6c4ad88cfc40fd972ed3bb188906152 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Mon, 20 Feb 2023 11:37:42 +0100 Subject: [PATCH 54/60] update package versions --- main/opcodes/storage-memory.zkasm | 4 ++-- package.json | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 245bf4a2..49a7cf80 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -160,8 +160,8 @@ opSLOAD: $ => A :MLOAD(storageAddr) ; set key for smt storage query %SMT_KEY_SC_STORAGE => B - $${eventLog(onUpdateStorage)} $ => E :SLOAD + $${eventLog(onUpdateStorage(C, E))} ; set key(C) as warmed storage for address(A) E :MSTORE(SP++), CALL(isColdSlot); [value(E) => SP] ; check out-of-gas @@ -306,5 +306,5 @@ opSSTOREsr: ; set key for smt storage query %SMT_KEY_SC_STORAGE => B $ => C :MLOAD(tmpVarCsstore); key => C - $${eventLog(onUpdateStorage)} + $${eventLog(onUpdateStorage(C, D))} $ => SR :SSTORE, JMP(readCode) diff --git a/package.json b/package.json index c0fb685d..e1a0af5a 100644 --- a/package.json +++ b/package.json @@ -33,12 +33,12 @@ "url": "https://github.com/0xPolygonHermez/zkevm-rom.git" }, "dependencies": { - "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#fix/non-maxmem-reg", + "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#v0.7.0.0-rc.8-fork.1", "yargs": "^17.5.1" }, "devDependencies": { - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#fix/non-maxmem-reg", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/error-rlp", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#864399e9e78ac6a24601c0e620909e1be02110aa", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.7.0.0-rc.2", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From 980e7abd05f0e0d8325da1d76347fe6f32640134 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Tue, 28 Feb 2023 13:19:45 +0100 Subject: [PATCH 55/60] fix MSTOREX2 --- counters/countersConstants.zkasm | 2 +- counters/tests/MSTOREX.zkasm | 2 +- main/utils.zkasm | 27 +++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 1815bf91..1d2a39b6 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -216,7 +216,7 @@ CONST %OPCALLDATALOAD_CNT_PADDING_PG = 0 + %SHLARITH_CNT_PADDING_PG + %SHRARITH_ CONST %OPCALLDATALOAD_CNT_POSEIDON_G = 0 + %SHLARITH_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G ; opCALLDATACOPY - COMPLEX - hardcoded values at test CONST %OPCALLDATACOPY_STEP = 100 -CONST %OPCALLDATACOPY_CNT_BINARY = 21 +CONST %OPCALLDATACOPY_CNT_BINARY = 20 CONST %OPCALLDATACOPY_CNT_ARITH = 8 CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0 CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 2 diff --git a/counters/tests/MSTOREX.zkasm b/counters/tests/MSTOREX.zkasm index 32a1b843..a16120e5 100644 --- a/counters/tests/MSTOREX.zkasm +++ b/counters/tests/MSTOREX.zkasm @@ -15,7 +15,7 @@ operation: :CALL(MSTOREX) %MSTOREX_STEP - STEP:JMPN(failedCounters) - 13 - CNT_BINARY :JMPNZ(failedCounters) + 12 - CNT_BINARY :JMPNZ(failedCounters) ;%MSTOREX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) 7 - CNT_ARITH :JMPNZ(failedCounters) ;%MSTOREX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) diff --git a/main/utils.zkasm b/main/utils.zkasm index d2ccfd8a..9470553b 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -190,7 +190,7 @@ 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, bytes splitted in two memory slots + ; if C has value, offset != 0 C :JMPNZ(MSTOREX2) $ => C :MLOAD(tmpVarCmstore) ; load bytes to store @@ -211,10 +211,12 @@ initMSTORE: 0 => C 0 :MSTORE(isMSTOREX), JMP(finalMSTORE) -; used if memory to store is allocated in two different slots +; 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 @@ -236,6 +238,27 @@ MSTOREX2: 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: From 5d9ad79721d232ae744caf9573d5a3825f115cec Mon Sep 17 00:00:00 2001 From: laisolizq Date: Thu, 2 Mar 2023 12:14:01 +0100 Subject: [PATCH 56/60] add mstorex test --- package.json | 1 + test/mstorex.zkasm | 171 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 test/mstorex.zkasm diff --git a/package.json b/package.json index e1a0af5a..699c9dc0 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:rotate": "mkdir -p build && npx zkasm test/rotate.zkasm -o build/rotate.test.json", "test:touched": "mkdir -p build && npx zkasm test/touched-assert.zkasm -o build/touched.test.json", "test:counters": "node counters/counters-executor.js", + "test:mstorex": "mkdir -p build && npx zkasm test/mstorex.zkasm -o build/mstorex.test.json", "eslint": "npx eslint tools/**.js", "eslint:fix": "npx eslint tools/**.js --fix", "test:gen": "node tools/gen-parallel-tests.js", diff --git a/test/mstorex.zkasm b/test/mstorex.zkasm new file mode 100644 index 00000000..472a3fa2 --- /dev/null +++ b/test/mstorex.zkasm @@ -0,0 +1,171 @@ +start: + + STEP => A + 0 :ASSERT + + ; TEST-1 (simple test) + + 0 => E :CALL(fillMemPattern) + + 3*32+5 => E + 0xFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000n :MSTORE(bytesToStore) + 6 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFn :MLOAD(MEM:E+2) + 0xC0C1C2C3C4FFFFFFFFFFFFCBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-2 (between memory words) + + 0 => E :CALL(fillMemPattern) + + 2*32+29 => E + 0xABCDEFFEDCBA0000000000000000000000000000000000000000000000000000n :MSTORE(bytesToStore) + 6 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCABCDEFn :MLOAD(MEM:E+2) + 0xFEDCBAC3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-3 (relative offset 0) + + 0 => E :CALL(fillMemPattern) + + 2*32 => E + 0xABCDEFFEDCBA0000000000000000000000000000000000000000000000000000n :MSTORE(bytesToStore) + 6 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xABCDEFFEDCBAA6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFn :MLOAD(MEM:E+2) + 0xC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-4 (32 bytes, offset > 0) + + 0 => E :CALL(fillMemPattern) + + 2*32 + 18 => E + 0x102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MSTORE(bytesToStore) + 32 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1102030405060708090A0B0C0D0E0n :MLOAD(MEM:E+2) + 0xF101112131415161718191A1B1C1D1E20210D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-5 (0 bytes) + + 0 => E :CALL(fillMemPattern) + + 2*32 + 18 => E + 0x102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MSTORE(bytesToStore) + 0 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFn :MLOAD(MEM:E+2) + 0xC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-6 (32 bytes, offset 0) + + 0 => E :CALL(fillMemPattern) + + 2*32 => E + 0x102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MSTORE(bytesToStore) + 32 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0x102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MLOAD(MEM:E+2) + 0xC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-7 (high address) + + 2**17-6 => E :CALL(fillMemPattern) + + 2**22-6*32+2*32+29 => E + 0xABCDEFFEDCBA0000000000000000000000000000000000000000000000000000n :MSTORE(bytesToStore) + 6 => C + :CALL(MSTOREX) + + 2**17-6 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCABCDEFn :MLOAD(MEM:E+2) + 0xFEDCBAC3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-8 (1 byte, offset = 31) + + 0 => E :CALL(fillMemPattern) + + 2*32+31 => E + 0x882030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MSTORE(bytesToStore) + 1 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBE88n :MLOAD(MEM:E+2) + 0xC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + ; TEST-9 (31 bytes, offset = 31) + + 0 => E :CALL(fillMemPattern) + + 2*32+31 => E + 0x882030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E20210n :MSTORE(bytesToStore) + 31 => C + :CALL(MSTOREX) + + 0 => E + 0x0000000000000000000000000000000000000000000000000000000000000000n :MLOAD(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MLOAD(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBE88n :MLOAD(MEM:E+2) + 0x2030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E202DEDFn :MLOAD(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MLOAD(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MLOAD(MEM:E+5) + + 0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0 + :JMP(finalizeExecution) + +fillMemPattern: + + 0x0000000000000000000000000000000000000000000000000000000000000000n :MSTORE(MEM:E) + 0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E2021n :MSTORE(MEM:E+1) + 0xA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFn :MSTORE(MEM:E+2) + 0xC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFn :MSTORE(MEM:E+3) + 0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEn :MSTORE(MEM:E+4) + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn :MSTORE(MEM:E+5), RETURN + +INCLUDE "../main/main.zkasm" \ No newline at end of file From 3d42356b69cad9cf72ed41fbce74c00d25141bb2 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Thu, 2 Mar 2023 12:15:25 +0100 Subject: [PATCH 57/60] update package tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 699c9dc0..fb93e371 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#864399e9e78ac6a24601c0e620909e1be02110aa", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.7.0.0-rc.2", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/add-test-mstorex", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From f76bf6be72daf0c16b622837254c0f5ce2d2b8dc Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 23 Feb 2023 00:11:42 +0100 Subject: [PATCH 58/60] fix jmpn, reduntant jump and typos --- counters/countersConstants.zkasm | 4 +- main/load-tx-rlp.zkasm | 11 ++++- main/main.zkasm | 5 +-- main/opcodes/block.zkasm | 30 ++++++------- main/opcodes/calldata-returndata-code.zkasm | 2 +- main/opcodes/comparison.zkasm | 2 +- main/opcodes/create-terminate-context.zkasm | 23 +++++----- main/opcodes/storage-memory.zkasm | 2 +- main/process-tx.zkasm | 12 ++++- main/utils.zkasm | 50 ++++++++++++--------- main/vars.zkasm | 14 +++--- package.json | 2 +- 12 files changed, 89 insertions(+), 68 deletions(-) diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 1d2a39b6..cf3d0c95 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -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 @@ -256,7 +256,7 @@ CONST %OPCREATE_CNT_PADDING_PG = 0 CONST %OPCREATE_CNT_POSEIDON_G = 23 ; opCREATE2 - COMPLEX - hardcoded values at test CONST %OPCREATE2_STEP = 200 -CONST %OPCREATE2_CNT_BINARY = 27 +CONST %OPCREATE2_CNT_BINARY = 28 CONST %OPCREATE2_CNT_ARITH = 5 CONST %OPCREATE2_CNT_KECCAK_F = 0 CONST %OPCREATE2_CNT_MEM_ALIGN = 2 diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 0968987f..10737d02 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -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) @@ -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) @@ -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) diff --git a/main/main.zkasm b/main/main.zkasm index e43f1e17..4a9af18a 100644 --- a/main/main.zkasm +++ b/main/main.zkasm @@ -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) @@ -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) diff --git a/main/opcodes/block.zkasm b/main/opcodes/block.zkasm index 14be3eeb..bd3b5afc 100644 --- a/main/opcodes/block.zkasm +++ b/main/opcodes/block.zkasm @@ -10,10 +10,10 @@ */ 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) @@ -21,12 +21,9 @@ opBLOCKHASH: ; 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 @@ -34,7 +31,7 @@ opBLOCKHASH: 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) @@ -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 @@ -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) diff --git a/main/opcodes/calldata-returndata-code.zkasm b/main/opcodes/calldata-returndata-code.zkasm index 66ac2a79..aee51602 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -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 diff --git a/main/opcodes/comparison.zkasm b/main/opcodes/comparison.zkasm index 87e0abd0..6a7ef2d9 100644 --- a/main/opcodes/comparison.zkasm +++ b/main/opcodes/comparison.zkasm @@ -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] diff --git a/main/opcodes/create-terminate-context.zkasm b/main/opcodes/create-terminate-context.zkasm index 15c25f16..3755c4c1 100644 --- a/main/opcodes/create-terminate-context.zkasm +++ b/main/opcodes/create-terminate-context.zkasm @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -646,7 +647,7 @@ opDELEGATECALLend: opCREATE2: ; checks zk-counters %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep) - %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary) + %MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary) %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255*2 :JMPN(outOfCountersPoseidon) ; check out-of-gas GAS - %CREATE_2_GAS => GAS :JMPN(outOfGas) @@ -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 @@ -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 @@ -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 diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index 49a7cf80..806f69c1 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -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 diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index f1141e35..617ee46c 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/main/utils.zkasm b/main/utils.zkasm index 9470553b..7642bd39 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -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) @@ -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 @@ -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) @@ -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) @@ -584,6 +584,7 @@ VAR GLOBAL arithRes1 VAR GLOBAL arithRes2 VAR GLOBAL arithOverflow +VAR GLOBAL addArithOverflow ; @info binary addition ; @in: arithA: addend value ; @in: arithB: addend value @@ -593,8 +594,13 @@ addARITH: RR :MSTORE(tmpZkPCArith), CALL(storeTmp) $ => A :MLOAD(arithA) $ => B :MLOAD(arithB) - $ => E :ADD, MSTORE(arithRes1) + $ => E :ADD, MSTORE(arithRes1), JMPC(setAddArithOverflow) + 0 :MSTORE(addArithOverflow), JMP(finishAddArith) + +setAddArithOverflow: + 1 :MSTORE(addArithOverflow) +finishAddArith: $ => RR :MLOAD(tmpZkPCArith), JMP(loadTmp) ; @info binary substraction @@ -635,7 +641,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 @@ -657,7 +663,7 @@ divARITH: $ :LT, JMPC(divisorSmallerDiv) C => A - ${E%A} => C ; reminder + ${E%A} => C ; remainder ${E/A} => B 0 => D E :ARITH @@ -665,9 +671,9 @@ divARITH: 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) @@ -1043,7 +1049,7 @@ readPush: D => A 0x04 => B $ :LT,JMPNC(failAssert) - 0 => B :JMP(readPushBlock) + 0 => B readPushBlock: %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) @@ -1066,10 +1072,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) @@ -1156,7 +1162,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) @@ -1187,7 +1193,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 @@ -1285,7 +1291,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) @@ -1294,12 +1300,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 @@ -1342,8 +1350,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 @@ -1511,7 +1519,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 \ No newline at end of file diff --git a/main/vars.zkasm b/main/vars.zkasm index 03b705b6..96e7b6a4 100644 --- a/main/vars.zkasm +++ b/main/vars.zkasm @@ -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 @@ -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 @@ -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 @@ -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' diff --git a/package.json b/package.json index fb93e371..8500ede4 100644 --- a/package.json +++ b/package.json @@ -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": { From af734fcb358ae16b6c23c86fa8fbe199a449cf14 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 2 Mar 2023 14:01:22 +0100 Subject: [PATCH 59/60] update to forkid 2 & fix comments --- main/constants.zkasm | 2 +- main/load-tx-rlp.zkasm | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main/constants.zkasm b/main/constants.zkasm index 8f3697f8..216975d1 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -9,7 +9,7 @@ CONST %LOCAL_EXIT_ROOT_STORAGE_POS = 1 CONST %LAST_TX_STORAGE_POS = 0 CONST %STATE_ROOT_STORAGE_POS = 1 CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0 -CONST %FORK_ID = 1 +CONST %FORK_ID = 2 ; RLP CONST %MIN_VALUE_SHORT = 128 diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 10737d02..fc96871b 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -34,7 +34,7 @@ loadTx_rlp: :CALL(addBatchHashData) A - 0xc0 :JMPN(invalidTxRLP) A - 0xf8 :JMPN(shortList) - ; do not allow lists over 2**24 bytes + ; do not allow lists over 2**24 bits ; 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) @@ -171,7 +171,7 @@ dataREAD: A - 0x80 :JMPN(veryShortData) A - 0x81 :JMPN(endData) A - 0xb8 :JMPN(shortData) - ; do not allow string over 2**24 bytes + ; do not allow string over 2**24 bits ; 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) diff --git a/package.json b/package.json index 8500ede4..2d3ef872 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#864399e9e78ac6a24601c0e620909e1be02110aa", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/add-test-mstorex", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/update-forkid-2", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From 058127b93154fef1496d12d3d784b83b16273659 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Thu, 2 Mar 2023 16:39:56 +0100 Subject: [PATCH 60/60] fork.2 update packages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d3ef872..8b173c7f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#864399e9e78ac6a24601c0e620909e1be02110aa", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/update-forkid-2", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v0.8.0.0-fork.2", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0",