From 42dd1a4e74e504f560c55b5ae2ec982046189d1a Mon Sep 17 00:00:00 2001 From: Ignasi Date: Wed, 24 May 2023 12:15:29 +0200 Subject: [PATCH 1/5] Effective gas implementation --- main/load-tx-rlp.zkasm | 5 +++++ main/process-tx.zkasm | 29 +++++++++++++++++++++++++++-- main/vars.zkasm | 1 + package.json | 4 ++-- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/main/load-tx-rlp.zkasm b/main/load-tx-rlp.zkasm index 74e1d7bd..650aaf7f 100644 --- a/main/load-tx-rlp.zkasm +++ b/main/load-tx-rlp.zkasm @@ -285,6 +285,11 @@ vREADTx: A :MSTORE(txV) C + D => C :CALL(addBatchHashData) +;; read effective percentage +effectivePercentageTx: + 1 => D :CALL(getTxBytes) + A :MSTORE(effectivePercentageRLP) + C + D => C :CALL(addBatchHashData) ;;;;;;;;; ;; D - Finish RLP parsing ;;;;;;;;; diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 68ea07de..f0d2515a 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -68,9 +68,34 @@ endCheckChainId: :CALL(isColdAddress) ; add tx.origin to touched addresses 0 :MSTORE(depth) ; Initial depth is 0 -;; Set gasPrice global var +;; Set gasPrice global var depending on effectivePercentage [0-255] -> txGasPrice = Floor((gasPrice * (effectivePercentage + 1)) / 256) + ; _effGasPriceShifted = gasPrice * (effectivePercentage + 1) + ; A => gasPrice $ => A :MLOAD(txGasPriceRLP) - A :MSTORE(txGasPrice) + ; B => effectivePercentage + $ => B :MLOAD(effectivePercentageRLP) + ; B => [0, 256] + B + 1 => B + ; A*B + C = D * 2**256 + op(E) + 0 => C + ; _effGasPriceShifted = gasPrice * (effectivePercentage + 1) + $${var _effGasPriceShifted = A * B} + ; get value above 256 bits + ${_effGasPriceShifted >> 256} => D + ; compute ARITH + ${_effGasPriceShifted} => E :ARITH + + ; txGasPrice = _effGasPriceShifted / 256 + 256 => B + ; (_effGasPriceShifted / 256)(A) * 256(B) + (_effGasPriceShifted % 256)(C) = D * 2**256 + op(E) + ${_effGasPriceShifted / 256} => A :MSTORE(txGasPrice) + ${_effGasPriceShifted % 256} => C + ; compute ARITH + E :ARITH + ; check divisor > remainder + C => A + 1 :LT + ;;;;;;;;;;;;;;;;;; ;; D - Verify and increase nonce ;;;;;;;;;;;;;;;;;; diff --git a/main/vars.zkasm b/main/vars.zkasm index aae52f65..22d6c0d1 100644 --- a/main/vars.zkasm +++ b/main/vars.zkasm @@ -56,6 +56,7 @@ VAR CTX storageAddr ; address which the storage will be modified VAR CTX txValue ; transaction parameter: 'value' VAR CTX txNonce ; transaction parameter: nonce VAR CTX txGasPriceRLP ; transaction parameter: 'gasPrice' decoded from the RLP +VAR CTX effectivePercentageRLP ; transaction parameter: 'effectivePercentage' decoded from the RLP VAR CTX txChainId ; transaction parameter: 'chainId' VAR CTX txS ; transaction parameter: ecdsa signature S VAR CTX txR ; transaction parameter: ecdsa signature R diff --git a/package.json b/package.json index 4017ac1c..921740aa 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,8 @@ }, "devDependencies": { "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#3bc0368dd67caf92d38e3cbebe7b030e78c54e35", - "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v1.2.0-rc.1-fork.5", - "@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#v1.0.1-rc.3", + "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/effective-gas-price", + "@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#feature/effective-gas-price", "chai": "^4.3.6", "chalk": "^3.0.0", "eslint": "^8.25.0", From fbf5a82113fcfc196124381f543baaf9ed38d790 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Fri, 26 May 2023 11:17:31 +0200 Subject: [PATCH 2/5] PR review --- main/process-tx.zkasm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index f0d2515a..86b22c55 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -74,7 +74,7 @@ endCheckChainId: $ => A :MLOAD(txGasPriceRLP) ; B => effectivePercentage $ => B :MLOAD(effectivePercentageRLP) - ; B => [0, 256] + ; B => [0, 255] B + 1 => B ; A*B + C = D * 2**256 + op(E) 0 => C From d09f1c51268cb81a06e3cbbc2cb4950d0718c395 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Mon, 29 May 2023 13:09:21 +0200 Subject: [PATCH 3/5] Add arith checker at beginning of processTx --- main/process-tx.zkasm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 86b22c55..fb91d45b 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -32,6 +32,8 @@ processTx: ; Minimum of 100000 steps left to process a tx %MAX_CNT_STEPS - STEP - 100000 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 100 :JMPN(outOfCountersBinary) + %MAX_CNT_ARITH - CNT_ARITH - 2 :JMPN(outOfCountersArith) + $ => A :MLOAD(txHash) ; Check the signature $ => B :MLOAD(txR) From 7d966f870279a2b6e6689906c4ad3176288b7931 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Mon, 29 May 2023 13:32:45 +0200 Subject: [PATCH 4/5] review changes --- main/opcodes/stack-operations.zkasm | 20 +++++++++++++++----- main/utils.zkasm | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/main/opcodes/stack-operations.zkasm b/main/opcodes/stack-operations.zkasm index 12669f2b..ddc01d1f 100644 --- a/main/opcodes/stack-operations.zkasm +++ b/main/opcodes/stack-operations.zkasm @@ -1,14 +1,15 @@ /** - * @link [https://www.evm.codes/#60?fork=berlin] + * @link [https://www.evm.codes/#5f?fork=shanghai] * @zk-counters - * - 100 steps (stack) || 400 steps (calldata) - * - 1 binary (stack) || 0 binary (calldata) + * - 100 steps * @process-opcode * - stack input: none - * - stack output: [pushed_value] + * - stack output: [0] */ - opPUSH0: + ; checks zk-counters + %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep) + ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) ; store stack output @@ -16,6 +17,15 @@ opPUSH0: ; check stack overflow %CALLDATA_OFFSET - SP :JMPN(stackOverflow, readCode) +/** + * @link [https://www.evm.codes/#60?fork=berlin] + * @zk-counters + * - 100 steps (stack) || 400 steps (calldata) + * - 1 binary (stack) || 0 binary (calldata) + * @process-opcode + * - stack input: none + * - stack output: [pushed_value] + */ opPUSH1: ; number of bytes to push to D 1 => D diff --git a/main/utils.zkasm b/main/utils.zkasm index d9fe5d7e..2a5dd088 100644 --- a/main/utils.zkasm +++ b/main/utils.zkasm @@ -928,6 +928,8 @@ handleError: handleBatchError: ; restore init state root and finish batch $ => SR :MLOAD(batchSR) + ; if batch error is triggered while parsing the RLP, it jumps to 'invalidTxRLP' + ; to fill the missing bytes to complete 'batchDataHash' $ :MLOAD(isLoadingRLP),JMPNZ(invalidTxRLP) $${eventLog(onFinishTx)} $${eventLog(onFinishBatch)} :JMP(processTxsEnd) From ac512151d218c231aeb4f315c0bf9fa46e0980ce Mon Sep 17 00:00:00 2001 From: Ignasi Date: Mon, 29 May 2023 13:09:21 +0200 Subject: [PATCH 5/5] Add arith checker at beginning of processTx --- main/process-tx.zkasm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 86b22c55..845d3338 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -32,6 +32,8 @@ processTx: ; Minimum of 100000 steps left to process a tx %MAX_CNT_STEPS - STEP - 100000 :JMPN(outOfCountersStep) %MAX_CNT_BINARY - CNT_BINARY - 100 :JMPN(outOfCountersBinary) + %MAX_CNT_ARITH - CNT_ARITH - 2 :JMPN(outOfCountersArith) + $ => A :MLOAD(txHash) ; Check the signature $ => B :MLOAD(txR) @@ -69,12 +71,11 @@ endCheckChainId: 0 :MSTORE(depth) ; Initial depth is 0 ;; Set gasPrice global var depending on effectivePercentage [0-255] -> txGasPrice = Floor((gasPrice * (effectivePercentage + 1)) / 256) - ; _effGasPriceShifted = gasPrice * (effectivePercentage + 1) - ; A => gasPrice + ; gasPrice => A $ => A :MLOAD(txGasPriceRLP) - ; B => effectivePercentage + ; effectivePercentage => B $ => B :MLOAD(effectivePercentageRLP) - ; B => [0, 255] + ; B -> [1, 256] B + 1 => B ; A*B + C = D * 2**256 + op(E) 0 => C