diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 27a8fa8d..82f24a60 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,6 +21,9 @@ jobs: run: | npm i npm run build + 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 08225660..70bd02e8 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) + A - B :JMPN(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 78151863..6605902a 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", + "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 d28fb9d5..f29a9cc2 100644 --- a/tools/parallel-tests-sample/sample.test.js +++ b/tools/parallel-tests-sample/sample.test.js @@ -8,7 +8,9 @@ const path = require('path'); 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; +let counters = false; const fileCachePil = path.join(__dirname, '../../node_modules/@0xpolygonhermez/zkevm-proverjs/cache-main-pil.json'); @@ -24,13 +26,21 @@ it(`${nameFile}`, async () => { } const pil = JSON.parse(fs.readFileSync(fileCachePil)); const cmPols = newCommitPolsArray(pil); + if (input.gasLimit) { + rom = require(`../../build/rom-${input.gasLimit}.test.json`) + } + if (input.stepsN) { + stepsN = input.stepsN + counters = true; + } try { const config = { debug: true, debugInfo: { inputName: path.basename(inputPath), }, - stepsN: 2 ** 23, + stepsN: stepsN, + counters, }; await smMain.execute(cmPols.Main, input, rom, config); } catch (err) {