From bc0ecc8ca44736177cf8abffc475f07e39418e04 Mon Sep 17 00:00:00 2001 From: Ignasirv Date: Wed, 11 Jan 2023 16:30:36 +0100 Subject: [PATCH] Audit PR fixes --- counters/tests/opBLOCKHASH.zkasm | 3 +-- counters/tests/opSHA3.zkasm | 3 +-- main/constants.zkasm | 9 ++++++--- main/opcodes/comparison.zkasm | 2 +- 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 - 9 files changed, 30 insertions(+), 31 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 49527664..ab664c30 100644 --- a/main/constants.zkasm +++ b/main/constants.zkasm @@ -26,9 +26,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 @@ -54,12 +51,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/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/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 e88df634..a5facf31 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