From 1ef22cd1dda769f8ae87fe817ae17dacd32555e4 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Wed, 7 Feb 2024 11:35:04 +0100 Subject: [PATCH 1/4] Done small optimization in most used lines --- main/map-opcodes.zkasm | 64 ++++++++++++++--------------- main/opcodes/flow-control.zkasm | 4 +- main/opcodes/stack-operations.zkasm | 32 --------------- main/process-tx.zkasm | 6 +-- main/utils.zkasm | 10 +---- 5 files changed, 37 insertions(+), 79 deletions(-) diff --git a/main/map-opcodes.zkasm b/main/map-opcodes.zkasm index 720c41fa..d7b61984 100644 --- a/main/map-opcodes.zkasm +++ b/main/map-opcodes.zkasm @@ -112,38 +112,38 @@ mapping_opcodes: :JMP(opINVALID) ; 0x5D :JMP(opINVALID) ; 0x5E :JMP(opPUSH0) ; 0x5F - :JMP(opPUSH1) ; 0x60 - :JMP(opPUSH2) ; 0x61 - :JMP(opPUSH3) ; 0x62 - :JMP(opPUSH4) ; 0x63 - :JMP(opPUSH5) ; 0x64 - :JMP(opPUSH6) ; 0x65 - :JMP(opPUSH7) ; 0x66 - :JMP(opPUSH8) ; 0x67 - :JMP(opPUSH9) ; 0x68 - :JMP(opPUSH10) ; 0x69 - :JMP(opPUSH11) ; 0x6a - :JMP(opPUSH12) ; 0x6b - :JMP(opPUSH13) ; 0x6c - :JMP(opPUSH14) ; 0x6d - :JMP(opPUSH15) ; 0x6e - :JMP(opPUSH16) ; 0x6f - :JMP(opPUSH17) ; 0x70 - :JMP(opPUSH18) ; 0x71 - :JMP(opPUSH19) ; 0x72 - :JMP(opPUSH20) ; 0x73 - :JMP(opPUSH21) ; 0x74 - :JMP(opPUSH22) ; 0x75 - :JMP(opPUSH23) ; 0x76 - :JMP(opPUSH24) ; 0x77 - :JMP(opPUSH25) ; 0x78 - :JMP(opPUSH26) ; 0x79 - :JMP(opPUSH27) ; 0x7a - :JMP(opPUSH28) ; 0x7b - :JMP(opPUSH29) ; 0x7c - :JMP(opPUSH30) ; 0x7d - :JMP(opPUSH31) ; 0x7e - :JMP(opPUSH32) ; 0x7f + 1 => D :JMP(opPUSH1) ; 0x60 + 2 => D :JMP(opPUSH2) ; 0x61 + 3 => D :JMP(opPUSH3) ; 0x62 + 4 => D :JMP(opPUSH4) ; 0x63 + 5 => D :JMP(opPUSH5) ; 0x64 + 6 => D :JMP(opPUSH6) ; 0x65 + 7 => D :JMP(opPUSH7) ; 0x66 + 8 => D :JMP(opPUSH8) ; 0x67 + 9 => D :JMP(opPUSH9) ; 0x68 + 10 => D :JMP(opPUSH10) ; 0x69 + 11 => D :JMP(opPUSH11) ; 0x6a + 12 => D :JMP(opPUSH12) ; 0x6b + 13 => D :JMP(opPUSH13) ; 0x6c + 14 => D :JMP(opPUSH14) ; 0x6d + 15 => D :JMP(opPUSH15) ; 0x6e + 16 => D :JMP(opPUSH16) ; 0x6f + 17 => D :JMP(opPUSH17) ; 0x70 + 18 => D :JMP(opPUSH18) ; 0x71 + 19 => D :JMP(opPUSH19) ; 0x72 + 20 => D :JMP(opPUSH20) ; 0x73 + 21 => D :JMP(opPUSH21) ; 0x74 + 22 => D :JMP(opPUSH22) ; 0x75 + 23 => D :JMP(opPUSH23) ; 0x76 + 24 => D :JMP(opPUSH24) ; 0x77 + 25 => D :JMP(opPUSH25) ; 0x78 + 26 => D :JMP(opPUSH26) ; 0x79 + 27 => D :JMP(opPUSH27) ; 0x7a + 28 => D :JMP(opPUSH28) ; 0x7b + 29 => D :JMP(opPUSH29) ; 0x7c + 30 => D :JMP(opPUSH30) ; 0x7d + 31 => D :JMP(opPUSH31) ; 0x7e + 32 => D :JMP(opPUSH32) ; 0x7f :JMP(opDUP1) ; 0x80 :JMP(opDUP2) ; 0x81 :JMP(opDUP3) ; 0x82 diff --git a/main/opcodes/flow-control.zkasm b/main/opcodes/flow-control.zkasm index 9a090add..669b864d 100644 --- a/main/opcodes/flow-control.zkasm +++ b/main/opcodes/flow-control.zkasm @@ -48,9 +48,7 @@ checkJumpDest: $ => B :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment) $ => B :MLOAD(bytecodeLength) $ :LT,JMPNC(invalidJump) - A => B - B => PC - B => HASHPOS + A => PC, HASHPOS ; get hashP address pointer where contract bytecode is stored $ => E :MLOAD(contractHashId) $ => A :HASHP1(E) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 3eb83b14..f790f3aa 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -28,133 +28,101 @@ opPUSH0: */ opPUSH1: ; number of bytes to push to D - 1 => D ; check is a deploy ; if is a deploy, get bytes from memory else, get bytes from stack $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH2: - 2 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH3: - 3 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH4: - 4 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH5: - 5 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH6: - 6 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH7: - 7 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH8: - 8 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH9: - 9 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH10: - 10 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH11: - 11 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH12: - 12 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH13: - 13 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH14: - 14 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH15: - 15 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH16: - 16 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH17: - 17 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH18: - 18 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH19: - 19 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH20: - 20 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH21: - 21 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH22: - 22 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH23: - 23 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH24: - 24 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH25: - 25 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH26: - 26 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH27: - 27 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH28: - 28 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH29: - 29 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH30: - 30 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH31: - 31 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opPUSH32: - 32 => D $ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA) opAuxPUSHA: diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 07dc98dd..b285a4a9 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -127,9 +127,7 @@ addDeploymentGasCost: %BASE_TX_DEPLOY_GAS => E ; 53000 gas if transaction is a create getCalldataGasCost: - $ => A :MLOAD(txCalldataLen) - 0 => B - $ :EQ,JMPC(endCalldataIntrinsicGas) + $ => A :MLOAD(txCalldataLen), JMPZ(endCalldataIntrinsicGas) ; less than 32 bits. Enforced by memory expansion gas cost & smart contract batchL2DataHash addGas: $ => HASHPOS :MLOAD(dataStarts) @@ -266,7 +264,7 @@ 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) ; less than 2**32 bytes. Enforced by memory expansion gas cost & smart contract batchL2DataHash + $ => C :MLOAD(txCalldataLen) ; less than 32bits. 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] diff --git a/main/utils.zkasm b/main/utils.zkasm index 4910a645..141b7a1d 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -907,8 +907,6 @@ VAR GLOBAL tmpVarBoffsetUtil offsetUtil: %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary) - A :MSTORE(tmpVarAoffsetUtil) - B :MSTORE(tmpVarBoffsetUtil) ; E = [e7, e6, ..., e0] ; JMPN instruction assures e0 is within the range [0, 2**32 - 1] @@ -920,12 +918,8 @@ offsetUtil: ; 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 - $ :LT,JMPNC(failAssert) - - $ => A :MLOAD(tmpVarAoffsetUtil) - $ => B :MLOAD(tmpVarBoffsetUtil), RETURN + 0x1F - C :JMPN(failAssert); If C is greater than 31, it is an error + :RETURN ;@info: move balances between two accounts ;@in: txSrcAddr: source address From f34a496e31fda05c94357146da05b47b21fc2dfd Mon Sep 17 00:00:00 2001 From: Ignasi Date: Wed, 7 Feb 2024 12:35:51 +0100 Subject: [PATCH 2/4] Update counters tests --- counters/countersConstants.zkasm | 18 +++++++++--------- counters/tests/MLOADX.zkasm | 2 +- counters/tests/MSTOREX.zkasm | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 7e72db47..47700f62 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -24,7 +24,7 @@ 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_BINARY = 0 CONST %OFFSETUTIL_CNT_ARITH = 0 CONST %OFFSETUTIL_CNT_KECCAK_F = 0 CONST %OFFSETUTIL_CNT_MEM_ALIGN = 0 @@ -55,7 +55,7 @@ CONST %MLOADX_CNT_MEM_ALIGN = 1 + %SHRARITH_CNT_MEM_ALIGN + %SHLARITH_CNT_MEM_AL 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_STEP = 50 + %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 @@ -201,7 +201,7 @@ 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 = 3 +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 = 1 @@ -209,7 +209,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 = 500 -CONST %OPCALLDATACOPY_CNT_BINARY = 21 +CONST %OPCALLDATACOPY_CNT_BINARY = 18 CONST %OPCALLDATACOPY_CNT_ARITH = 8 CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0 CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 2 @@ -225,7 +225,7 @@ CONST %OPRETURNDATACOPY_CNT_PADDING_PG = 0 CONST %OPRETURNDATACOPY_CNT_POSEIDON_G = 0 ; opCODECOPY - COMPLEX - hardcoded values at test CONST %OPCODECOPY_STEP = 1700 -CONST %OPCODECOPY_CNT_BINARY = 100 +CONST %OPCODECOPY_CNT_BINARY = 57 CONST %OPCODECOPY_CNT_ARITH = 4 CONST %OPCODECOPY_CNT_KECCAK_F = 0 CONST %OPCODECOPY_CNT_MEM_ALIGN = 43 @@ -233,7 +233,7 @@ CONST %OPCODECOPY_CNT_PADDING_PG = 0 CONST %OPCODECOPY_CNT_POSEIDON_G = 0 ; opEXTCODECOPY - COMPLEX - hardcoded values at test CONST %OPEXTCODECOPY_STEP = 2000 -CONST %OPEXTCODECOPY_CNT_BINARY = 104 +CONST %OPEXTCODECOPY_CNT_BINARY = 61 CONST %OPEXTCODECOPY_CNT_ARITH = 4 CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0 CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 43 @@ -273,7 +273,7 @@ CONST %OPCALLCODE_CNT_PADDING_PG = 0 CONST %OPCALLCODE_CNT_POSEIDON_G = 10 ; opRETURN - COMPLEX - hardcoded values at test CONST %OPRETURN_STEP = 700 -CONST %OPRETURN_CNT_BINARY = 35 +CONST %OPRETURN_CNT_BINARY = 27 CONST %OPRETURN_CNT_ARITH = 11 CONST %OPRETURN_CNT_KECCAK_F = 0 CONST %OPRETURN_CNT_MEM_ALIGN = 4 @@ -281,7 +281,7 @@ CONST %OPRETURN_CNT_PADDING_PG = 0 CONST %OPRETURN_CNT_POSEIDON_G = 0 ; opREVERT - COMPLEX - hardcoded values at test CONST %OPREVERT_STEP = 700 -CONST %OPREVERT_CNT_BINARY = 35 +CONST %OPREVERT_CNT_BINARY = 27 CONST %OPREVERT_CNT_ARITH = 11 CONST %OPREVERT_CNT_KECCAK_F = 0 CONST %OPREVERT_CNT_MEM_ALIGN = 4 @@ -305,7 +305,7 @@ CONST %OPSTATICCALL_CNT_PADDING_PG = 0 CONST %OPSTATICCALL_CNT_POSEIDON_G = 6 ; opSHA3 - COMPLEX - hardcoded values at test CONST %OPSHA3_STEP = 3300 -CONST %OPSHA3_CNT_BINARY = 146 +CONST %OPSHA3_CNT_BINARY = 83 CONST %OPSHA3_CNT_ARITH = 9 CONST %OPSHA3_CNT_KECCAK_F = 15 CONST %OPSHA3_CNT_MEM_ALIGN = 63 diff --git a/counters/tests/MLOADX.zkasm b/counters/tests/MLOADX.zkasm index 2b9f95c8..6fdc5539 100644 --- a/counters/tests/MLOADX.zkasm +++ b/counters/tests/MLOADX.zkasm @@ -9,7 +9,7 @@ operation: :CALL(MLOADX) %MLOADX_STEP - STEP :JMPN(failedCounters) - 5 - CNT_BINARY :JMPNZ(failedCounters) + 4 - 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 a16120e5..4b16c697 100644 --- a/counters/tests/MSTOREX.zkasm +++ b/counters/tests/MSTOREX.zkasm @@ -15,7 +15,7 @@ operation: :CALL(MSTOREX) %MSTOREX_STEP - STEP:JMPN(failedCounters) - 12 - CNT_BINARY :JMPNZ(failedCounters) + 11 - CNT_BINARY :JMPNZ(failedCounters) ;%MSTOREX_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) 7 - CNT_ARITH :JMPNZ(failedCounters) ;%MSTOREX_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) From 3727abdad7cfd171720cba0e474d6d066cbb844c Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 8 Feb 2024 09:40:29 +0100 Subject: [PATCH 3/4] Bugfix --- main/utils.zkasm | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/main/utils.zkasm b/main/utils.zkasm index 141b7a1d..939f8a4b 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -422,16 +422,16 @@ computeGasSendCall: ; C = [c7, c6, ..., c0] ; JMPN instruction assures c0 is within the range [0, 2**32 - 1] ${GAS >> 6} => C :JMPN(failAssert) - ${GAS & 0x3f} => D + ; We secure D to be less than 32 bits with $0{} + $0{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 - $ :LT,JMPNC(failAssert) + 0x3f - D :JMPN(failAssert) ; D is less than 32 bits, we can use JMPN + GAS - C => A $ => B :MLOAD(gasCall) ; gas_sent_with_call = min(requested_gas, all_but_one_64th) @@ -479,8 +479,9 @@ saveMem: ; load old memory length, if is greater than new memory length, no expansion cost - $ => A :MLOAD(memLength) - $ :LT, JMPC(saveMemGAS, saveMemEnd) + $ => A :MLOAD(memLength) + ; Both A and B are LT than MAX_MEM_EXPANSION_BYTES, this means they are lower thatn 32 bits, we don't need to use a binary to operate + A - B :JMPN(saveMemGAS, saveMemEnd) saveMemGAS: ; store new memory length @@ -493,16 +494,15 @@ saveMemGAS: ; E = [e7, e6, ..., e0] ; JMPN instruction assures e0 is within the range [0, 2**32 - 1] ${A >> 5} => E :JMPN(failAssert) - ${A & 0x1f} => D + $0{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 - $ :LT,JMPNC(failAssert) + 0x1f - D :JMPN(failAssert) ; D is less than 32 bits, we can use JMPN + ; memory_cost = (memory_size_word ** 2) / 512 + (3 * memory_size_word) in A ; ${E*E/512} + 3*E=> A E :MSTORE(arithA) @@ -911,7 +911,7 @@ offsetUtil: ; E = [e7, e6, ..., e0] ; JMPN instruction assures e0 is within the range [0, 2**32 - 1] ${A >> 5} => E :JMPN(failAssert) - ${A & 0x1F} => C + $0{A & 0x1F} => C ; C is 32 bits ; 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 From 44d396131e5a3f38e9c7908e6db02492c8435e4a Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 8 Feb 2024 18:42:45 +0100 Subject: [PATCH 4/4] More optimizations + counters --- counters/countersConstants.zkasm | 28 ++++++++++----------- counters/tests/opCREATE2.zkasm | 7 ------ main/opcodes/calldata-returndata-code.zkasm | 11 +++----- main/opcodes/storage-memory.zkasm | 9 +++---- main/process-tx.zkasm | 6 ++--- main/utils.zkasm | 4 +-- 6 files changed, 23 insertions(+), 42 deletions(-) diff --git a/counters/countersConstants.zkasm b/counters/countersConstants.zkasm index 47700f62..e88b5832 100644 --- a/counters/countersConstants.zkasm +++ b/counters/countersConstants.zkasm @@ -209,7 +209,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 = 500 -CONST %OPCALLDATACOPY_CNT_BINARY = 18 +CONST %OPCALLDATACOPY_CNT_BINARY = 16 CONST %OPCALLDATACOPY_CNT_ARITH = 8 CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0 CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 2 @@ -225,7 +225,7 @@ CONST %OPRETURNDATACOPY_CNT_PADDING_PG = 0 CONST %OPRETURNDATACOPY_CNT_POSEIDON_G = 0 ; opCODECOPY - COMPLEX - hardcoded values at test CONST %OPCODECOPY_STEP = 1700 -CONST %OPCODECOPY_CNT_BINARY = 57 +CONST %OPCODECOPY_CNT_BINARY = 55 CONST %OPCODECOPY_CNT_ARITH = 4 CONST %OPCODECOPY_CNT_KECCAK_F = 0 CONST %OPCODECOPY_CNT_MEM_ALIGN = 43 @@ -233,7 +233,7 @@ CONST %OPCODECOPY_CNT_PADDING_PG = 0 CONST %OPCODECOPY_CNT_POSEIDON_G = 0 ; opEXTCODECOPY - COMPLEX - hardcoded values at test CONST %OPEXTCODECOPY_STEP = 2000 -CONST %OPEXTCODECOPY_CNT_BINARY = 61 +CONST %OPEXTCODECOPY_CNT_BINARY = 59 CONST %OPEXTCODECOPY_CNT_ARITH = 4 CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0 CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 43 @@ -241,7 +241,7 @@ CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0 CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 8 ; opCREATE - COMPLEX - hardcoded values at test CONST %OPCREATE_STEP = 400 -CONST %OPCREATE_CNT_BINARY = 16 +CONST %OPCREATE_CNT_BINARY = 13 CONST %OPCREATE_CNT_ARITH = 1 CONST %OPCREATE_CNT_KECCAK_F = 0 CONST %OPCREATE_CNT_MEM_ALIGN = 0 @@ -249,7 +249,7 @@ CONST %OPCREATE_CNT_PADDING_PG = 0 CONST %OPCREATE_CNT_POSEIDON_G = 23 ; opCREATE2 - COMPLEX - hardcoded values at test CONST %OPCREATE2_STEP = 400 -CONST %OPCREATE2_CNT_BINARY = 21 +CONST %OPCREATE2_CNT_BINARY = 18 CONST %OPCREATE2_CNT_ARITH = 2 CONST %OPCREATE2_CNT_KECCAK_F = 0 CONST %OPCREATE2_CNT_MEM_ALIGN = 0 @@ -257,7 +257,7 @@ CONST %OPCREATE2_CNT_PADDING_PG = 0 CONST %OPCREATE2_CNT_POSEIDON_G = 27 ; opCALL - COMPLEX - hardcoded values at test CONST %OPCALL_STEP = 600 -CONST %OPCALL_CNT_BINARY = 27 +CONST %OPCALL_CNT_BINARY = 22 CONST %OPCALL_CNT_ARITH = 3 CONST %OPCALL_CNT_KECCAK_F = 0 CONST %OPCALL_CNT_MEM_ALIGN = 0 @@ -265,7 +265,7 @@ CONST %OPCALL_CNT_PADDING_PG = 0 CONST %OPCALL_CNT_POSEIDON_G = 14 ; opCALLCODE - COMPLEX - hardcoded values at test CONST %OPCALLCODE_STEP = 600 -CONST %OPCALLCODE_CNT_BINARY = 26 +CONST %OPCALLCODE_CNT_BINARY = 21 CONST %OPCALLCODE_CNT_ARITH = 3 CONST %OPCALLCODE_CNT_KECCAK_F = 0 CONST %OPCALLCODE_CNT_MEM_ALIGN = 0 @@ -273,7 +273,7 @@ CONST %OPCALLCODE_CNT_PADDING_PG = 0 CONST %OPCALLCODE_CNT_POSEIDON_G = 10 ; opRETURN - COMPLEX - hardcoded values at test CONST %OPRETURN_STEP = 700 -CONST %OPRETURN_CNT_BINARY = 27 +CONST %OPRETURN_CNT_BINARY = 25 CONST %OPRETURN_CNT_ARITH = 11 CONST %OPRETURN_CNT_KECCAK_F = 0 CONST %OPRETURN_CNT_MEM_ALIGN = 4 @@ -281,7 +281,7 @@ CONST %OPRETURN_CNT_PADDING_PG = 0 CONST %OPRETURN_CNT_POSEIDON_G = 0 ; opREVERT - COMPLEX - hardcoded values at test CONST %OPREVERT_STEP = 700 -CONST %OPREVERT_CNT_BINARY = 27 +CONST %OPREVERT_CNT_BINARY = 25 CONST %OPREVERT_CNT_ARITH = 11 CONST %OPREVERT_CNT_KECCAK_F = 0 CONST %OPREVERT_CNT_MEM_ALIGN = 4 @@ -289,7 +289,7 @@ CONST %OPREVERT_CNT_PADDING_PG = 0 CONST %OPREVERT_CNT_POSEIDON_G = 0 ; opDELEGATECALL - COMPLEX - hardcoded values at test CONST %OPDELEGATECALL_STEP = 600 -CONST %OPDELEGATECALL_CNT_BINARY = 23 +CONST %OPDELEGATECALL_CNT_BINARY = 18 CONST %OPDELEGATECALL_CNT_ARITH = 3 CONST %OPDELEGATECALL_CNT_KECCAK_F = 0 CONST %OPDELEGATECALL_CNT_MEM_ALIGN = 0 @@ -297,7 +297,7 @@ CONST %OPDELEGATECALL_CNT_PADDING_PG = 0 CONST %OPDELEGATECALL_CNT_POSEIDON_G = 6 ; opSTATICCALL - COMPLEX - hardcoded values at test CONST %OPSTATICCALL_STEP = 600 -CONST %OPSTATICCALL_CNT_BINARY = 23 +CONST %OPSTATICCALL_CNT_BINARY = 18 CONST %OPSTATICCALL_CNT_ARITH = 3 CONST %OPSTATICCALL_CNT_KECCAK_F = 0 CONST %OPSTATICCALL_CNT_MEM_ALIGN = 0 @@ -305,7 +305,7 @@ CONST %OPSTATICCALL_CNT_PADDING_PG = 0 CONST %OPSTATICCALL_CNT_POSEIDON_G = 6 ; opSHA3 - COMPLEX - hardcoded values at test CONST %OPSHA3_STEP = 3300 -CONST %OPSHA3_CNT_BINARY = 83 +CONST %OPSHA3_CNT_BINARY = 81 CONST %OPSHA3_CNT_ARITH = 9 CONST %OPSHA3_CNT_KECCAK_F = 15 CONST %OPSHA3_CNT_MEM_ALIGN = 63 @@ -337,7 +337,7 @@ 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_BINARY = 1 CONST %COMPUTEGASSENDCALL_CNT_ARITH = 0 CONST %COMPUTEGASSENDCALL_CNT_KECCAK_F = 0 CONST %COMPUTEGASSENDCALL_CNT_MEM_ALIGN = 0 @@ -345,7 +345,7 @@ 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 = 5 + %MULARITH_CNT_BINARY + %DIVARITH_CNT_BINARY +CONST %SAVEMEM_CNT_BINARY = 3 + %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 diff --git a/counters/tests/opCREATE2.zkasm b/counters/tests/opCREATE2.zkasm index ee4e5089..936a42b4 100644 --- a/counters/tests/opCREATE2.zkasm +++ b/counters/tests/opCREATE2.zkasm @@ -15,13 +15,6 @@ operation: 1000 :MSTORE(SP++) :JMP(opCREATE2) checkCounters: -%OPCREATE2_STEP - STEP:JMPN(failedCounters) -%OPCREATE2_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters) -%OPCREATE2_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters) -%OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters) -%OPCREATE2_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters) -%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, 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 2514b80a..0a142ea1 100644 --- a/main/opcodes/calldata-returndata-code.zkasm +++ b/main/opcodes/calldata-returndata-code.zkasm @@ -482,11 +482,9 @@ opRETURNDATASIZE: ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; Load ret data length from last ctx - $ => B :MLOAD(retDataCTX) ; if no retDataCTX(0), retDataLength = 0 - 0 => A - $ :EQ, JMPC(endOpRETURNDATASIZE) - B => CTX + $ => A :MLOAD(retDataCTX), JMPZ(endOpRETURNDATASIZE) + A => CTX $ => A :MLOAD(retDataLength) ; Restore current context $ => CTX :MLOAD(currentCTX) @@ -522,10 +520,7 @@ opRETURNDATACOPY: ; store lastMemLength for memory expansion gas cost C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength] ; if retDataCTX is 0, end opcode execution - $ => B :MLOAD(retDataCTX) - 0 => A - $ :EQ, JMPC(opRETURNDATACOPYEmpty) - + $ => B :MLOAD(retDataCTX), JMPZ(opRETURNDATACOPYEmpty) ; Load ret data length from last ctx B => CTX E => B diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index dd545665..769805e7 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -126,14 +126,11 @@ opMSIZE: 32 :MSTORE(arithB) :CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] $ => C :MLOAD(arithRes1) - $ => 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) + $ :MLOAD(arithRes2), JMPZ(MSIZEend) + ; Round size to 32bytes multiple - C + 1 => C - C * 32 => E + C * 32 + 32 => E MSIZEend: E :MSTORE(SP++); [size(E) => SP] diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index b285a4a9..1ba913f2 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -411,10 +411,8 @@ callContract: ; get contract length %SMT_KEY_SC_LENGTH => B 0 => C - $ => B :SLOAD - B :MSTORE(bytecodeLength) - 0 => A - $ :EQ, JMPC(defaultOpCode) ;no bytecode + ; Max length of a smart contract is 24kb, less than 32 bits, no need to use a binary + $ => B :SLOAD, MSTORE(bytecodeLength), JMPZ(defaultOpCode) ; no bytecode ; check poseidon counters ; 56 is the value used by the prover to increment poseidon counters depending on the hash length diff --git a/main/utils.zkasm b/main/utils.zkasm index 939f8a4b..3552759b 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -971,10 +971,8 @@ endMoveBalances: invalidCall: ; return to origin context $ => SR :MLOAD(initSR) - $ => A :MLOAD(originCTX) - 0 => B ; if last context, finish tx - $ :EQ,JMPC(handleGas) + $ => A :MLOAD(originCTX),JMPZ(handleGas) A => CTX 0 :MSTORE(retDataCTX) CTX :MSTORE(currentCTX)