zkevm-rom 0.0.1-security → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of zkevm-rom might be problematic. Click here for more details.
- package/.eslintrc.js +33 -0
- package/.github/CODEOWNERS +14 -0
- package/.github/ISSUE_TEMPLATE/bug.yml +38 -0
- package/.github/ISSUE_TEMPLATE/feature.yml +26 -0
- package/.github/ISSUE_TEMPLATE/question.yml +26 -0
- package/.github/workflows/main.yaml +40 -0
- package/LICENSE +636 -0
- package/README.md +23 -5
- package/audits/Hexens_Polygon_zkEVM_PUBLIC_27.02.23.pdf +0 -0
- package/audits/Polygon-zkEVM-Public-v1.1-verichains-19-03-2024.pdf +0 -0
- package/audits/zkEVM-ROM-upgrade-1-Spearbit-30-May.pdf +0 -0
- package/audits/zkEVM-ROM-upgrade-2-Spearbit-21-August.pdf +0 -0
- package/audits/zkEVM-engagement-1-Spearbit-27-March.pdf +0 -0
- package/audits/zkEVM-engagement-2-Spearbit-27-March.pdf +0 -0
- package/audits/zkEVM-engagement-3-Spearbit-6-April.pdf +0 -0
- package/counters/README.md +45 -0
- package/counters/counters-executor.js +80 -0
- package/counters/countersConstants.zkasm +370 -0
- package/counters/endIncludes.zkasm +18 -0
- package/counters/initIncludes.zkasm +2 -0
- package/counters/tests/MLOAD32.zkasm +27 -0
- package/counters/tests/MLOADX.zkasm +30 -0
- package/counters/tests/MSTORE32.zkasm +32 -0
- package/counters/tests/MSTOREX.zkasm +36 -0
- package/counters/tests/SHLarith.zkasm +28 -0
- package/counters/tests/SHLarithBit.zkasm +28 -0
- package/counters/tests/SHRarith.zkasm +28 -0
- package/counters/tests/SHRarithBit.zkasm +28 -0
- package/counters/tests/abs.zkasm +29 -0
- package/counters/tests/addBatchHashByteByByte.zkasm +31 -0
- package/counters/tests/computeGasSendCall.zkasm +30 -0
- package/counters/tests/divArith.zkasm +27 -0
- package/counters/tests/expAD.zkasm +30 -0
- package/counters/tests/getLenBits.zkasm +30 -0
- package/counters/tests/getLenBytes.zkasm +32 -0
- package/counters/tests/isEmptyAccount.zkasm +30 -0
- package/counters/tests/mulARITH.zkasm +28 -0
- package/counters/tests/offsetUtil.zkasm +29 -0
- package/counters/tests/opADDMOD.zkasm +28 -0
- package/counters/tests/opAdd.zkasm +27 -0
- package/counters/tests/opBLOCKHASH.zkasm +28 -0
- package/counters/tests/opCALL.zkasm +41 -0
- package/counters/tests/opCALLCODE.zkasm +41 -0
- package/counters/tests/opCALLDATACOPY.zkasm +28 -0
- package/counters/tests/opCALLDATALOAD.zkasm +27 -0
- package/counters/tests/opCODECOPY.zkasm +28 -0
- package/counters/tests/opCREATE.zkasm +35 -0
- package/counters/tests/opCREATE2.zkasm +35 -0
- package/counters/tests/opDELEGATECALL.zkasm +35 -0
- package/counters/tests/opDIV.zkasm +27 -0
- package/counters/tests/opEXP.zkasm +29 -0
- package/counters/tests/opEXTCODECOPY.zkasm +29 -0
- package/counters/tests/opMOD.zkasm +27 -0
- package/counters/tests/opMUL.zkasm +27 -0
- package/counters/tests/opMULMOD.zkasm +28 -0
- package/counters/tests/opRETURN.zkasm +32 -0
- package/counters/tests/opRETURNDATACOPY.zkasm +29 -0
- package/counters/tests/opREVERT.zkasm +32 -0
- package/counters/tests/opSDIV.zkasm +28 -0
- package/counters/tests/opSHA3.zkasm +28 -0
- package/counters/tests/opSIGNEXTEND.zkasm +27 -0
- package/counters/tests/opSMOD.zkasm +28 -0
- package/counters/tests/opSTATICCALL.zkasm +35 -0
- package/counters/tests/opSUB.zkasm +27 -0
- package/counters/tests/saveMem.zkasm +31 -0
- package/docs/opcode-cost-zk-counters.md +315 -0
- package/docs/usage-ecrecover.md +51 -0
- package/index.js +43 -0
- package/main/block-info.zkasm +204 -0
- package/main/constants.zkasm +145 -0
- package/main/ecrecover/addFpEc.zkasm +31 -0
- package/main/ecrecover/checkSqrtFpEc.zkasm +1558 -0
- package/main/ecrecover/constEc.zkasm +13 -0
- package/main/ecrecover/ecrecover.zkasm +280 -0
- package/main/ecrecover/invFnEc.zkasm +44 -0
- package/main/ecrecover/invFpEc.zkasm +45 -0
- package/main/ecrecover/mulFnEc.zkasm +36 -0
- package/main/ecrecover/mulFpEc.zkasm +36 -0
- package/main/ecrecover/mulPointEc.zkasm +311 -0
- package/main/ecrecover/sqFpEc.zkasm +38 -0
- package/main/ecrecover/sqrtFpEc.zkasm +70 -0
- package/main/end.zkasm +4 -0
- package/main/l2-tx-hash.zkasm +159 -0
- package/main/load-change-l2-block-utils.zkasm +11 -0
- package/main/load-change-l2-block.zkasm +28 -0
- package/main/load-tx-rlp-utils.zkasm +72 -0
- package/main/load-tx-rlp.zkasm +431 -0
- package/main/main.zkasm +237 -0
- package/main/map-opcodes.zkasm +274 -0
- package/main/modexp/array_lib/array_add_AGTB.zkasm +123 -0
- package/main/modexp/array_lib/array_add_short.zkasm +85 -0
- package/main/modexp/array_lib/array_div.zkasm +215 -0
- package/main/modexp/array_lib/array_div_long.zkasm +284 -0
- package/main/modexp/array_lib/array_div_short.zkasm +222 -0
- package/main/modexp/array_lib/array_mul.zkasm +97 -0
- package/main/modexp/array_lib/array_mul_long.zkasm +156 -0
- package/main/modexp/array_lib/array_mul_short.zkasm +127 -0
- package/main/modexp/array_lib/array_square.zkasm +246 -0
- package/main/modexp/array_lib/unused/array_add.zkasm +100 -0
- package/main/modexp/array_lib/unused/array_is_odd.zkasm +23 -0
- package/main/modexp/array_lib/unused/array_is_one.zkasm +33 -0
- package/main/modexp/array_lib/unused/array_is_zero.zkasm +34 -0
- package/main/modexp/array_lib/unused/array_sub_AGTB.zkasm +111 -0
- package/main/modexp/array_lib/unused/array_unshift.zkasm +37 -0
- package/main/modexp/array_lib/utils/array_compare.zkasm +82 -0
- package/main/modexp/array_lib/utils/array_trim.zkasm +49 -0
- package/main/modexp/constants.zkasm +5 -0
- package/main/modexp/modexp.zkasm +296 -0
- package/main/modexp/modexp_utils.zkasm +230 -0
- package/main/opcodes/arithmetic.zkasm +357 -0
- package/main/opcodes/block.zkasm +163 -0
- package/main/opcodes/calldata-returndata-code.zkasm +619 -0
- package/main/opcodes/comparison.zkasm +446 -0
- package/main/opcodes/context-information.zkasm +169 -0
- package/main/opcodes/create-terminate-context.zkasm +1011 -0
- package/main/opcodes/crypto.zkasm +96 -0
- package/main/opcodes/flow-control.zkasm +126 -0
- package/main/opcodes/logs.zkasm +193 -0
- package/main/opcodes/stack-operations.zkasm +658 -0
- package/main/opcodes/storage-memory.zkasm +313 -0
- package/main/pairings/BN254/addPointBN254.zkasm +245 -0
- package/main/pairings/BN254/ecAdd.zkasm +312 -0
- package/main/pairings/BN254/ecMul.zkasm +159 -0
- package/main/pairings/BN254/escalarMulBN254.zkasm +155 -0
- package/main/pairings/BN254/lineDiffPointsBN254.zkasm +83 -0
- package/main/pairings/BN254/lineSamePointsBN254.zkasm +96 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/compressFp12BN254.zkasm +49 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/decompressFp12BN254.zkasm +236 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/expByXCompCycloFp12BN254.zkasm +444 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/squareCompCycloFp12BN254.zkasm +212 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/squareCycloFp12BN254.zkasm +228 -0
- package/main/pairings/FP12BN254/CYCLOFP12BN254/xBinDecompBN254.zkasm +64 -0
- package/main/pairings/FP12BN254/frob2Fp12BN254.zkasm +80 -0
- package/main/pairings/FP12BN254/frob3Fp12BN254.zkasm +96 -0
- package/main/pairings/FP12BN254/frobFp12BN254.zkasm +96 -0
- package/main/pairings/FP12BN254/inverseFp12BN254.zkasm +289 -0
- package/main/pairings/FP12BN254/mulFp12BN254.zkasm +408 -0
- package/main/pairings/FP12BN254/sparseMulAFp12BN254.zkasm +296 -0
- package/main/pairings/FP12BN254/sparseMulBFp12BN254.zkasm +291 -0
- package/main/pairings/FP12BN254/squareFp12BN254.zkasm +376 -0
- package/main/pairings/FP2BN254/addFp2BN254.zkasm +19 -0
- package/main/pairings/FP2BN254/escalarMulFp2BN254.zkasm +20 -0
- package/main/pairings/FP2BN254/invFp2BN254.zkasm +66 -0
- package/main/pairings/FP2BN254/mulFp2BN254.zkasm +19 -0
- package/main/pairings/FP2BN254/squareFp2BN254.zkasm +21 -0
- package/main/pairings/FP2BN254/subFp2BN254.zkasm +19 -0
- package/main/pairings/FP4BN254/squareFp4BN254.zkasm +76 -0
- package/main/pairings/FP6BN254/addFp6BN254.zkasm +59 -0
- package/main/pairings/FP6BN254/escalarMulFp6BN254.zkasm +51 -0
- package/main/pairings/FP6BN254/inverseFp6BN254.zkasm +208 -0
- package/main/pairings/FP6BN254/mulFp6BN254.zkasm +201 -0
- package/main/pairings/FP6BN254/sparseMulAFp6BN254.zkasm +65 -0
- package/main/pairings/FP6BN254/sparseMulBFp6BN254.zkasm +134 -0
- package/main/pairings/FP6BN254/sparseMulCFp6BN254.zkasm +128 -0
- package/main/pairings/FP6BN254/squareFp6BN254.zkasm +147 -0
- package/main/pairings/FP6BN254/subFp6BN254.zkasm +59 -0
- package/main/pairings/FPBN254/addFpBN254.zkasm +29 -0
- package/main/pairings/FPBN254/invFpBN254.zkasm +55 -0
- package/main/pairings/FPBN254/mulFpBN254.zkasm +29 -0
- package/main/pairings/FPBN254/reduceFpBN254.zkasm +25 -0
- package/main/pairings/FPBN254/squareFpBN254.zkasm +31 -0
- package/main/pairings/FPBN254/subFpBN254.zkasm +36 -0
- package/main/pairings/FRBN254/reduceFrBN254.zkasm +25 -0
- package/main/pairings/constants.zkasm +62 -0
- package/main/pairings/ecPairing.zkasm +244 -0
- package/main/pairings/finalExpBN254.zkasm +2095 -0
- package/main/pairings/halfPairingBN254.zkasm +428 -0
- package/main/pairings/loopLengthBN254.zkasm +75 -0
- package/main/pairings/millerLoopBN254.zkasm +741 -0
- package/main/pairings/pairingBN254.zkasm +481 -0
- package/main/pairings/unused/addFp12BN254.zkasm +130 -0
- package/main/pairings/unused/expByXCycloFp12BN254.zkasm +411 -0
- package/main/pairings/unused/expFp12BN254.zkasm +333 -0
- package/main/pairings/unused/subFp12BN254.zkasm +130 -0
- package/main/pairings/unused/xPseudoBinDecompBN254.zkasm +68 -0
- package/main/pairings/utilsTests/expCycloFp12BN254.zkasm +334 -0
- package/main/precompiled/end.zkasm +42 -0
- package/main/precompiled/identity.zkasm +99 -0
- package/main/precompiled/pre-ecAdd.zkasm +84 -0
- package/main/precompiled/pre-ecMul.zkasm +82 -0
- package/main/precompiled/pre-ecPairing.zkasm +72 -0
- package/main/precompiled/pre-ecrecover.zkasm +71 -0
- package/main/precompiled/pre-modexp.zkasm +367 -0
- package/main/precompiled/pre-sha2-256.zkasm +125 -0
- package/main/precompiled/revert-precompiled.zkasm +25 -0
- package/main/precompiled/selector.zkasm +77 -0
- package/main/process-change-l2-block.zkasm +147 -0
- package/main/process-tx.zkasm +587 -0
- package/main/tables/2-exp.zkasm +260 -0
- package/main/touched.zkasm +118 -0
- package/main/utils.zkasm +2335 -0
- package/main/vars.zkasm +117 -0
- package/package.json +62 -3
- package/test/bytes-length.zkasm +39 -0
- package/test/ecrecover.zkasm +538 -0
- package/test/lt4-test.zkasm +38 -0
- package/test/mstorex.zkasm +191 -0
- package/test/opcalldatacopy.ignore.zkasm +331 -0
- package/test/performance/read-push.zkasm +71 -0
- package/test/read-push.zkasm +304 -0
- package/test/testArrayArith.zkasm +1099 -0
- package/test/testArrayUtils.zkasm +335 -0
- package/test/testCycloFp12ArithBN254.zkasm +548 -0
- package/test/testEcAdd.zkasm +252 -0
- package/test/testEcMul.zkasm +231 -0
- package/test/testEcPairing.zkasm +436 -0
- package/test/testFinalExpBn254.zkasm +139 -0
- package/test/testFp12ArithBN254.zkasm +692 -0
- package/test/testFp2ArithBN254.zkasm +185 -0
- package/test/testFp4ArithBN254.zkasm +128 -0
- package/test/testFp6ArithBN254.zkasm +260 -0
- package/test/testFpArithBN254.zkasm +159 -0
- package/test/testFrArithBN254.zkasm +113 -0
- package/test/testHalfPairingBN254.zkasm +285 -0
- package/test/testModExp.zkasm +586 -0
- package/test/testModExpReturn.zkasm +81 -0
- package/test/testPairingBN254.zkasm +463 -0
- package/test/testPointArithBN254.zkasm +270 -0
- package/test/testSHA256.zkasm +27 -0
- package/test/touched-assert.zkasm +59 -0
- package/test/utils-expAD.zkasm +48 -0
- package/test/utils-getLenBytes.zkasm +36 -0
- package/tools/audit-tools/registry-op-checker.js +71 -0
- package/tools/get-not-used-labels.js +31 -0
- package/tools/helpers/helpers.js +47 -0
- package/tools/modexp-utils/README.md +5 -0
- package/tools/modexp-utils/modexp-test-gen.js +168 -0
- package/tools/modexp-utils/modexp-test-int.sage +37 -0
- package/tools/parallel-testing/checker.sh +6 -0
- package/tools/parallel-testing/gen-parallel-tests.js +78 -0
- package/tools/parallel-testing/parallel-tests-sample/sample.test.js +136 -0
- package/tools/run-tests-zkasm.js +83 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
/**
|
2
|
+
* @link [https://www.evm.codes/#20?fork=berlin]
|
3
|
+
* @zk-counters
|
4
|
+
* - dynamic steps: 400 + 100 * length
|
5
|
+
* - dynamic keccaks: f(length)
|
6
|
+
* @process-opcode
|
7
|
+
* - stack input: [offset, size]
|
8
|
+
* - stack output: [hash]
|
9
|
+
*/
|
10
|
+
opSHA3:
|
11
|
+
; checks zk-counters
|
12
|
+
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
|
13
|
+
|
14
|
+
; check stack underflow
|
15
|
+
SP - 2 :JMPN(stackUnderflow)
|
16
|
+
|
17
|
+
; check out-of-gas
|
18
|
+
GAS - %KECCAK_GAS => GAS :JMPN(outOfGas)
|
19
|
+
SP - 1 => SP
|
20
|
+
$ => E :MLOAD(SP--); [offset => E]
|
21
|
+
$ => C :MLOAD(SP) ; [size => C]
|
22
|
+
|
23
|
+
; store lastMemOffset for memory expansion gas cost
|
24
|
+
E :MSTORE(lastMemOffset)
|
25
|
+
; store lastMemLength for memory expansion gas cost
|
26
|
+
; compute memory expansion gas cost
|
27
|
+
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
|
28
|
+
; check out-of-gas, dynamic
|
29
|
+
;${6*((C+31)/32)}
|
30
|
+
C+31 => A
|
31
|
+
;(C+31)/32
|
32
|
+
A :MSTORE(arithA)
|
33
|
+
32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
|
34
|
+
$ => A :MLOAD(arithRes1)
|
35
|
+
; Mul operation with Arith
|
36
|
+
; 6*((C+31)/32)
|
37
|
+
6 :MSTORE(arithA)
|
38
|
+
A :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
|
39
|
+
$ => A :MLOAD(arithRes1)
|
40
|
+
GAS - A => GAS :JMPN(outOfGas) ; dynamic_gas = 6 * minimum_word_size + memory_expansion_cost
|
41
|
+
|
42
|
+
; check keccak counters
|
43
|
+
C + 1 :MSTORE(arithA)
|
44
|
+
136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
|
45
|
+
$ => B :MLOAD(arithRes1)
|
46
|
+
$ => A :MLOAD(cntKeccakPreProcess)
|
47
|
+
; checks keccak counters
|
48
|
+
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 - B :JMPN(outOfCountersKeccak)
|
49
|
+
|
50
|
+
; new hash id
|
51
|
+
$ => B :MLOAD(lastHashKIdUsed)
|
52
|
+
B + 1 => B :MSTORE(lastHashKIdUsed)
|
53
|
+
; set bytes to hash at D
|
54
|
+
32 => D
|
55
|
+
; A new hash with position 0 is started
|
56
|
+
0 => HASHPOS
|
57
|
+
|
58
|
+
opSHA3Loop:
|
59
|
+
; checks zk-counters
|
60
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
61
|
+
|
62
|
+
C :JMPZ(opSHA3End)
|
63
|
+
C - 32 :JMPN(opSHA3Final)
|
64
|
+
; load next 32 bytes from memory
|
65
|
+
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
|
66
|
+
; save new offset at B
|
67
|
+
E => B
|
68
|
+
; get current hash pointer
|
69
|
+
$ => E :MLOAD(lastHashKIdUsed)
|
70
|
+
; append A to hash pointer E
|
71
|
+
A :HASHK(E)
|
72
|
+
; restore new offset at E
|
73
|
+
B => E
|
74
|
+
C - 32 => C :JMP(opSHA3Loop)
|
75
|
+
|
76
|
+
opSHA3Final:
|
77
|
+
; load next C bytes from memory
|
78
|
+
:CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
|
79
|
+
; set #bytes to right shift
|
80
|
+
32 - C => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
|
81
|
+
; get current hash pointer
|
82
|
+
$ => E :MLOAD(lastHashKIdUsed)
|
83
|
+
; set remaining bytes length to hash at D
|
84
|
+
C => D
|
85
|
+
; append A to hash pointer E
|
86
|
+
A :HASHK(E)
|
87
|
+
|
88
|
+
opSHA3End:
|
89
|
+
; get current hash pointer
|
90
|
+
$ => E :MLOAD(lastHashKIdUsed)
|
91
|
+
; append A to hash pointer E
|
92
|
+
HASHPOS :HASHKLEN(E)
|
93
|
+
; compute hash
|
94
|
+
$ => A :HASHKDIGEST(E)
|
95
|
+
; store hash
|
96
|
+
A :MSTORE(SP++), JMP(readCode); [hash(A) => SP]
|
@@ -0,0 +1,126 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* @link [https://www.evm.codes/#56?fork=berlin]
|
4
|
+
* @zk-counters
|
5
|
+
* - 100 steps
|
6
|
+
* - 2 binary
|
7
|
+
* @process-opcode
|
8
|
+
* - stack input: [counter]
|
9
|
+
* - stack output: none
|
10
|
+
*/
|
11
|
+
opJUMP:
|
12
|
+
; checks zk-counters
|
13
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
14
|
+
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
|
15
|
+
|
16
|
+
; check stack underflow
|
17
|
+
SP - 1 => SP :JMPN(stackUnderflow)
|
18
|
+
|
19
|
+
; check out-of-gas
|
20
|
+
GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas)
|
21
|
+
|
22
|
+
; Check PC is a JUMPDEST
|
23
|
+
$ => A :MLOAD(SP), JMP(checkJumpDest); [counter => PC]
|
24
|
+
/**
|
25
|
+
* @link [https://www.evm.codes/#57?fork=berlin]
|
26
|
+
* @zk-counters
|
27
|
+
* - 100 steps
|
28
|
+
* - 2 binary
|
29
|
+
* @process-opcode
|
30
|
+
* - stack input: [counter, value]
|
31
|
+
* - stack output: none
|
32
|
+
*/
|
33
|
+
opJUMPI:
|
34
|
+
; checks zk-counters
|
35
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
36
|
+
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
|
37
|
+
; check out-of-gas
|
38
|
+
GAS - %GAS_SLOW_STEP => GAS :JMPN(outOfGas)
|
39
|
+
SP - 2 => SP :JMPN(stackUnderflow)
|
40
|
+
$ => B :MLOAD(SP); [value => B]
|
41
|
+
0 => A
|
42
|
+
$ :EQ, JMPC(readCode)
|
43
|
+
; Check PC is a JUMPDEST
|
44
|
+
$ => A :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC]
|
45
|
+
|
46
|
+
checkJumpDest:
|
47
|
+
; if it is a deploy we have to check the data from the calldata (not the bytecode)
|
48
|
+
$ => B :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment)
|
49
|
+
$ => B :MLOAD(bytecodeLength)
|
50
|
+
$ :LT,JMPNC(invalidJump)
|
51
|
+
A => B
|
52
|
+
B => PC
|
53
|
+
B => HASHPOS
|
54
|
+
; get hashP address pointer where contract bytecode is stored
|
55
|
+
$ => E :MLOAD(contractHashId)
|
56
|
+
$ => A :HASHP1(E)
|
57
|
+
; check if is a jumpDest (0x5B)
|
58
|
+
A - 0x5B :JMPZ(readCode, invalidJump)
|
59
|
+
|
60
|
+
checkJumpDestDeployment:
|
61
|
+
; check enough bytes to read in calldata
|
62
|
+
$ => B :MLOAD(txCalldataLen)
|
63
|
+
$ :LT,JMPNC(invalidJump)
|
64
|
+
; check jumpDest from calldata
|
65
|
+
$ => B :MLOAD(isCreate), JMPNZ(checkJumpDestDeploymentCreate)
|
66
|
+
; get position where data starts in the tx
|
67
|
+
$ => HASHPOS :MLOAD(dataStarts)
|
68
|
+
; add PC to data starts to point the bytes to read for the jumpdest
|
69
|
+
A => PC
|
70
|
+
HASHPOS + PC => HASHPOS
|
71
|
+
; get memory pointer for hashing
|
72
|
+
$ => E :MLOAD(batchHashDataId)
|
73
|
+
; set number of bytes to hashK
|
74
|
+
$ => A :HASHK1(E)
|
75
|
+
; check if is a jumpDest (0x5B)
|
76
|
+
A - 0x5B :JMPZ(readCode, invalidJump)
|
77
|
+
|
78
|
+
checkJumpDestDeploymentCreate:
|
79
|
+
; get bytes from previous context memory
|
80
|
+
$ => CTX :MLOAD(originCTX)
|
81
|
+
; get offset call position
|
82
|
+
$ => E :MLOAD(argsOffsetCall)
|
83
|
+
; increase current program counter (PC) to offset for getting pushed bytes to read
|
84
|
+
A => PC
|
85
|
+
E + PC => E
|
86
|
+
; set bytes to read from memory at C
|
87
|
+
1 => C :CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
|
88
|
+
$ => CTX :MLOAD(currentCTX)
|
89
|
+
; check if is a jumpDest (0x5B)
|
90
|
+
0x5b00000000000000000000000000000000000000000000000000000000000000n => B
|
91
|
+
$ :EQ, JMPC(readCode, invalidJump)
|
92
|
+
|
93
|
+
/**
|
94
|
+
* @link [https://www.evm.codes/#58?fork=berlin]
|
95
|
+
* @zk-counters
|
96
|
+
* - 20 steps
|
97
|
+
* @process-opcode
|
98
|
+
* - stack input: none
|
99
|
+
* - stack output: [PC]
|
100
|
+
*/
|
101
|
+
opPC:
|
102
|
+
; checks zk-counters
|
103
|
+
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
|
104
|
+
|
105
|
+
; check out-of-gas
|
106
|
+
GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
|
107
|
+
; store current PC
|
108
|
+
PC - 1 :MSTORE(SP++); [PC => SP]
|
109
|
+
; check stack overflow
|
110
|
+
%MAX_STACK_SIZE - SP :JMPN(stackOverflow, readCode)
|
111
|
+
|
112
|
+
|
113
|
+
/**
|
114
|
+
* @link [https://www.evm.codes/#5B?fork=berlin]
|
115
|
+
* @zk-counters
|
116
|
+
* - 10 steps
|
117
|
+
* @process-opcode
|
118
|
+
* - stack input: none
|
119
|
+
* - stack output: none
|
120
|
+
*/
|
121
|
+
opJUMPDEST:
|
122
|
+
; checks zk-counters
|
123
|
+
%MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep)
|
124
|
+
|
125
|
+
; check out-of-gas
|
126
|
+
GAS - %JUMP_DEST_GAS => GAS :JMPN(outOfGas, readCode)
|
@@ -0,0 +1,193 @@
|
|
1
|
+
/**
|
2
|
+
* @link [https://www.evm.codes/#a0?fork=berlin]
|
3
|
+
* @zk-counters
|
4
|
+
* - 100 steps
|
5
|
+
* - 1 binary
|
6
|
+
* @process-opcode
|
7
|
+
* - stack input: [offset, size, topic]
|
8
|
+
* - stack output: none
|
9
|
+
*/
|
10
|
+
VAR GLOBAL opLogAux
|
11
|
+
VAR GLOBAL logNextHashPIdAux
|
12
|
+
opLOG0:
|
13
|
+
; checks zk-counters
|
14
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
15
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
16
|
+
; check stack underflow
|
17
|
+
SP - 2 :JMPN(stackUnderflow)
|
18
|
+
|
19
|
+
; check out-of-gas
|
20
|
+
; gas_cost = %LOG_GAS + %LOG_GAS * num_topics + 8 * data_size + mem_expansion_cost
|
21
|
+
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
|
22
|
+
|
23
|
+
; check is static
|
24
|
+
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
|
25
|
+
|
26
|
+
SP - 1 => SP
|
27
|
+
$ => E :MLOAD(SP--) ; [offset => E]
|
28
|
+
$ => C :MLOAD(SP) ; [size => C]
|
29
|
+
; store lastMemOffset for memory expansion gas cost
|
30
|
+
E :MSTORE(lastMemOffset)
|
31
|
+
; store lastMemLength for memory expansion gas cost
|
32
|
+
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
|
33
|
+
; store number of topics
|
34
|
+
0 :MSTORE(numTopics)
|
35
|
+
; calculate data size gas cost => lastMemLength * 8
|
36
|
+
%LOG_DATA_GAS :MSTORE(arithA)
|
37
|
+
C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
|
38
|
+
$ => B :MLOAD(arithRes1)
|
39
|
+
GAS => A
|
40
|
+
; check out-of-gas
|
41
|
+
$ :LT,JMPC(outOfGas)
|
42
|
+
GAS - B => GAS :JMP(initLogLoop)
|
43
|
+
opLOG1:
|
44
|
+
|
45
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
46
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
47
|
+
SP - 3 :JMPN(stackUnderflow)
|
48
|
+
GAS - %LOG_GAS - %LOG_TOPIC_GAS => GAS :JMPN(outOfGas)
|
49
|
+
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
|
50
|
+
SP - 1 => SP
|
51
|
+
$ => E :MLOAD(SP--)
|
52
|
+
$ => C :MLOAD(SP)
|
53
|
+
E :MSTORE(lastMemOffset)
|
54
|
+
C :MSTORE(lastMemLength), CALL(saveMem)
|
55
|
+
1 :MSTORE(numTopics)
|
56
|
+
%LOG_DATA_GAS :MSTORE(arithA)
|
57
|
+
C :MSTORE(arithB), CALL(mulARITH)
|
58
|
+
$ => B :MLOAD(arithRes1)
|
59
|
+
GAS => A
|
60
|
+
; check out-of-gas
|
61
|
+
$ :LT,JMPC(outOfGas)
|
62
|
+
GAS - B => GAS :JMP(initLogLoop)
|
63
|
+
|
64
|
+
opLOG2:
|
65
|
+
|
66
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
67
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
68
|
+
SP - 4 :JMPN(stackUnderflow)
|
69
|
+
GAS - %LOG_GAS - %LOG_TOPIC_GAS*2 => GAS :JMPN(outOfGas)
|
70
|
+
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
|
71
|
+
SP - 1 => SP
|
72
|
+
$ => E :MLOAD(SP--)
|
73
|
+
$ => C :MLOAD(SP)
|
74
|
+
E :MSTORE(lastMemOffset)
|
75
|
+
C :MSTORE(lastMemLength), CALL(saveMem)
|
76
|
+
2 :MSTORE(numTopics)
|
77
|
+
%LOG_DATA_GAS :MSTORE(arithA)
|
78
|
+
C :MSTORE(arithB), CALL(mulARITH)
|
79
|
+
$ => B :MLOAD(arithRes1)
|
80
|
+
GAS => A
|
81
|
+
; check out-of-gas
|
82
|
+
$ :LT,JMPC(outOfGas)
|
83
|
+
GAS - B => GAS :JMP(initLogLoop)
|
84
|
+
|
85
|
+
opLOG3:
|
86
|
+
|
87
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
88
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
89
|
+
SP - 5 :JMPN(stackUnderflow)
|
90
|
+
GAS - %LOG_GAS - %LOG_TOPIC_GAS*3 => GAS :JMPN(outOfGas)
|
91
|
+
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
|
92
|
+
SP - 1 => SP
|
93
|
+
$ => E :MLOAD(SP--)
|
94
|
+
$ => C :MLOAD(SP)
|
95
|
+
E :MSTORE(lastMemOffset)
|
96
|
+
C :MSTORE(lastMemLength), CALL(saveMem)
|
97
|
+
3 :MSTORE(numTopics)
|
98
|
+
%LOG_DATA_GAS :MSTORE(arithA)
|
99
|
+
C :MSTORE(arithB), CALL(mulARITH)
|
100
|
+
$ => B :MLOAD(arithRes1)
|
101
|
+
GAS => A
|
102
|
+
; check out-of-gas
|
103
|
+
$ :LT,JMPC(outOfGas)
|
104
|
+
GAS - B => GAS :JMP(initLogLoop)
|
105
|
+
|
106
|
+
opLOG4:
|
107
|
+
|
108
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
109
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
110
|
+
SP - 6 :JMPN(stackUnderflow)
|
111
|
+
GAS - %LOG_GAS - %LOG_TOPIC_GAS*4 => GAS :JMPN(outOfGas)
|
112
|
+
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
|
113
|
+
SP - 1 => SP
|
114
|
+
$ => E :MLOAD(SP--)
|
115
|
+
$ => C :MLOAD(SP)
|
116
|
+
E :MSTORE(lastMemOffset)
|
117
|
+
C :MSTORE(lastMemLength), CALL(saveMem)
|
118
|
+
4 :MSTORE(numTopics)
|
119
|
+
%LOG_DATA_GAS :MSTORE(arithA)
|
120
|
+
C :MSTORE(arithB), CALL(mulARITH)
|
121
|
+
$ => B :MLOAD(arithRes1)
|
122
|
+
GAS => A
|
123
|
+
; check out-of-gas
|
124
|
+
$ :LT,JMPC(outOfGas)
|
125
|
+
GAS - B => GAS :JMP(initLogLoop)
|
126
|
+
|
127
|
+
initLogLoop:
|
128
|
+
; check poseidon counters
|
129
|
+
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
|
130
|
+
C :MSTORE(arithA)
|
131
|
+
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
|
132
|
+
$ => B :MLOAD(arithRes1)
|
133
|
+
; We count B and also the number of topics, as max is 4 topics of 32 bytes each 32*4/56 = 2.2 -> 3
|
134
|
+
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 4 - B :JMPN(outOfCountersPoseidon)
|
135
|
+
%MAX_CNT_PADDING_PG - CNT_PADDING_PG - 4 - B :JMPN(outOfCountersPadding)
|
136
|
+
0 => HASHPOS
|
137
|
+
32 => D
|
138
|
+
$ => B :MLOAD(currentLogIndex)
|
139
|
+
$ => A :MLOAD(nextHashPId)
|
140
|
+
; Update nextHashPId
|
141
|
+
A + 1 :MSTORE(nextHashPId)
|
142
|
+
; store nextHashPId to aux variable
|
143
|
+
A :MSTORE(logNextHashPIdAux)
|
144
|
+
|
145
|
+
logLoop:
|
146
|
+
; checks zk-counters
|
147
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
148
|
+
C :JMPZ(opSaveTopicsInit)
|
149
|
+
; load next 32 bytes
|
150
|
+
C - 32 :JMPN(opLogFinal)
|
151
|
+
:CALL(MLOAD32); in: [E: offset] out: [A: value]
|
152
|
+
E :MSTORE(opLogAux)
|
153
|
+
$ => E :MLOAD(logNextHashPIdAux)
|
154
|
+
A :HASHP(E)
|
155
|
+
$ => E :MLOAD(opLogAux)
|
156
|
+
$${storeLog(B, 0, A)} ; storeLog(indexLog, isTopic, bytesToStore)
|
157
|
+
C - 32 => C :JMP(logLoop)
|
158
|
+
|
159
|
+
opLogFinal:
|
160
|
+
; load last C bytes
|
161
|
+
:CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
|
162
|
+
$${storeLog(B, 0, A)}; storeLog(indexLog, isTopic, bytesToStore)
|
163
|
+
32 - C => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
|
164
|
+
C => D
|
165
|
+
$ => E :MLOAD(logNextHashPIdAux)
|
166
|
+
A :HASHP(E)
|
167
|
+
:JMP(opSaveTopicsInit) ; instruction added to allow executing $$ function
|
168
|
+
|
169
|
+
opSaveTopicsInit:
|
170
|
+
; save topics
|
171
|
+
$ => A :MLOAD(numTopics)
|
172
|
+
$ => E :MLOAD(logNextHashPIdAux)
|
173
|
+
32 => D
|
174
|
+
|
175
|
+
opSaveTopicsLoop:
|
176
|
+
; checks zk-counters
|
177
|
+
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
|
178
|
+
|
179
|
+
A :JMPZ(finishSaveTopics)
|
180
|
+
; check stack underflow
|
181
|
+
SP - 1 => SP
|
182
|
+
; check out-of-gas
|
183
|
+
$ => C :MLOAD(SP) ; [topic => C]
|
184
|
+
C :HASHP(E)
|
185
|
+
$${storeLog(B, 1, C)} ; storeLog(indexLog, isTopic, bytesToStore)
|
186
|
+
A - 1 => A :JMP(opSaveTopicsLoop)
|
187
|
+
|
188
|
+
finishSaveTopics:
|
189
|
+
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
|
190
|
+
; Compute hash of the log
|
191
|
+
HASHPOS :HASHPLEN(E)
|
192
|
+
$ => D :HASHPDIGEST(E), CALL(fillBlockInfoTreeWithLog); in: [D: linearPoseidon(log_data + log_topics)]
|
193
|
+
:JMP(readCode)
|