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.

Files changed (232) hide show
  1. package/.eslintrc.js +33 -0
  2. package/.github/CODEOWNERS +14 -0
  3. package/.github/ISSUE_TEMPLATE/bug.yml +38 -0
  4. package/.github/ISSUE_TEMPLATE/feature.yml +26 -0
  5. package/.github/ISSUE_TEMPLATE/question.yml +26 -0
  6. package/.github/workflows/main.yaml +40 -0
  7. package/LICENSE +636 -0
  8. package/README.md +23 -5
  9. package/audits/Hexens_Polygon_zkEVM_PUBLIC_27.02.23.pdf +0 -0
  10. package/audits/Polygon-zkEVM-Public-v1.1-verichains-19-03-2024.pdf +0 -0
  11. package/audits/zkEVM-ROM-upgrade-1-Spearbit-30-May.pdf +0 -0
  12. package/audits/zkEVM-ROM-upgrade-2-Spearbit-21-August.pdf +0 -0
  13. package/audits/zkEVM-engagement-1-Spearbit-27-March.pdf +0 -0
  14. package/audits/zkEVM-engagement-2-Spearbit-27-March.pdf +0 -0
  15. package/audits/zkEVM-engagement-3-Spearbit-6-April.pdf +0 -0
  16. package/counters/README.md +45 -0
  17. package/counters/counters-executor.js +80 -0
  18. package/counters/countersConstants.zkasm +370 -0
  19. package/counters/endIncludes.zkasm +18 -0
  20. package/counters/initIncludes.zkasm +2 -0
  21. package/counters/tests/MLOAD32.zkasm +27 -0
  22. package/counters/tests/MLOADX.zkasm +30 -0
  23. package/counters/tests/MSTORE32.zkasm +32 -0
  24. package/counters/tests/MSTOREX.zkasm +36 -0
  25. package/counters/tests/SHLarith.zkasm +28 -0
  26. package/counters/tests/SHLarithBit.zkasm +28 -0
  27. package/counters/tests/SHRarith.zkasm +28 -0
  28. package/counters/tests/SHRarithBit.zkasm +28 -0
  29. package/counters/tests/abs.zkasm +29 -0
  30. package/counters/tests/addBatchHashByteByByte.zkasm +31 -0
  31. package/counters/tests/computeGasSendCall.zkasm +30 -0
  32. package/counters/tests/divArith.zkasm +27 -0
  33. package/counters/tests/expAD.zkasm +30 -0
  34. package/counters/tests/getLenBits.zkasm +30 -0
  35. package/counters/tests/getLenBytes.zkasm +32 -0
  36. package/counters/tests/isEmptyAccount.zkasm +30 -0
  37. package/counters/tests/mulARITH.zkasm +28 -0
  38. package/counters/tests/offsetUtil.zkasm +29 -0
  39. package/counters/tests/opADDMOD.zkasm +28 -0
  40. package/counters/tests/opAdd.zkasm +27 -0
  41. package/counters/tests/opBLOCKHASH.zkasm +28 -0
  42. package/counters/tests/opCALL.zkasm +41 -0
  43. package/counters/tests/opCALLCODE.zkasm +41 -0
  44. package/counters/tests/opCALLDATACOPY.zkasm +28 -0
  45. package/counters/tests/opCALLDATALOAD.zkasm +27 -0
  46. package/counters/tests/opCODECOPY.zkasm +28 -0
  47. package/counters/tests/opCREATE.zkasm +35 -0
  48. package/counters/tests/opCREATE2.zkasm +35 -0
  49. package/counters/tests/opDELEGATECALL.zkasm +35 -0
  50. package/counters/tests/opDIV.zkasm +27 -0
  51. package/counters/tests/opEXP.zkasm +29 -0
  52. package/counters/tests/opEXTCODECOPY.zkasm +29 -0
  53. package/counters/tests/opMOD.zkasm +27 -0
  54. package/counters/tests/opMUL.zkasm +27 -0
  55. package/counters/tests/opMULMOD.zkasm +28 -0
  56. package/counters/tests/opRETURN.zkasm +32 -0
  57. package/counters/tests/opRETURNDATACOPY.zkasm +29 -0
  58. package/counters/tests/opREVERT.zkasm +32 -0
  59. package/counters/tests/opSDIV.zkasm +28 -0
  60. package/counters/tests/opSHA3.zkasm +28 -0
  61. package/counters/tests/opSIGNEXTEND.zkasm +27 -0
  62. package/counters/tests/opSMOD.zkasm +28 -0
  63. package/counters/tests/opSTATICCALL.zkasm +35 -0
  64. package/counters/tests/opSUB.zkasm +27 -0
  65. package/counters/tests/saveMem.zkasm +31 -0
  66. package/docs/opcode-cost-zk-counters.md +315 -0
  67. package/docs/usage-ecrecover.md +51 -0
  68. package/index.js +43 -0
  69. package/main/block-info.zkasm +204 -0
  70. package/main/constants.zkasm +145 -0
  71. package/main/ecrecover/addFpEc.zkasm +31 -0
  72. package/main/ecrecover/checkSqrtFpEc.zkasm +1558 -0
  73. package/main/ecrecover/constEc.zkasm +13 -0
  74. package/main/ecrecover/ecrecover.zkasm +280 -0
  75. package/main/ecrecover/invFnEc.zkasm +44 -0
  76. package/main/ecrecover/invFpEc.zkasm +45 -0
  77. package/main/ecrecover/mulFnEc.zkasm +36 -0
  78. package/main/ecrecover/mulFpEc.zkasm +36 -0
  79. package/main/ecrecover/mulPointEc.zkasm +311 -0
  80. package/main/ecrecover/sqFpEc.zkasm +38 -0
  81. package/main/ecrecover/sqrtFpEc.zkasm +70 -0
  82. package/main/end.zkasm +4 -0
  83. package/main/l2-tx-hash.zkasm +159 -0
  84. package/main/load-change-l2-block-utils.zkasm +11 -0
  85. package/main/load-change-l2-block.zkasm +28 -0
  86. package/main/load-tx-rlp-utils.zkasm +72 -0
  87. package/main/load-tx-rlp.zkasm +431 -0
  88. package/main/main.zkasm +237 -0
  89. package/main/map-opcodes.zkasm +274 -0
  90. package/main/modexp/array_lib/array_add_AGTB.zkasm +123 -0
  91. package/main/modexp/array_lib/array_add_short.zkasm +85 -0
  92. package/main/modexp/array_lib/array_div.zkasm +215 -0
  93. package/main/modexp/array_lib/array_div_long.zkasm +284 -0
  94. package/main/modexp/array_lib/array_div_short.zkasm +222 -0
  95. package/main/modexp/array_lib/array_mul.zkasm +97 -0
  96. package/main/modexp/array_lib/array_mul_long.zkasm +156 -0
  97. package/main/modexp/array_lib/array_mul_short.zkasm +127 -0
  98. package/main/modexp/array_lib/array_square.zkasm +246 -0
  99. package/main/modexp/array_lib/unused/array_add.zkasm +100 -0
  100. package/main/modexp/array_lib/unused/array_is_odd.zkasm +23 -0
  101. package/main/modexp/array_lib/unused/array_is_one.zkasm +33 -0
  102. package/main/modexp/array_lib/unused/array_is_zero.zkasm +34 -0
  103. package/main/modexp/array_lib/unused/array_sub_AGTB.zkasm +111 -0
  104. package/main/modexp/array_lib/unused/array_unshift.zkasm +37 -0
  105. package/main/modexp/array_lib/utils/array_compare.zkasm +82 -0
  106. package/main/modexp/array_lib/utils/array_trim.zkasm +49 -0
  107. package/main/modexp/constants.zkasm +5 -0
  108. package/main/modexp/modexp.zkasm +296 -0
  109. package/main/modexp/modexp_utils.zkasm +230 -0
  110. package/main/opcodes/arithmetic.zkasm +357 -0
  111. package/main/opcodes/block.zkasm +163 -0
  112. package/main/opcodes/calldata-returndata-code.zkasm +619 -0
  113. package/main/opcodes/comparison.zkasm +446 -0
  114. package/main/opcodes/context-information.zkasm +169 -0
  115. package/main/opcodes/create-terminate-context.zkasm +1011 -0
  116. package/main/opcodes/crypto.zkasm +96 -0
  117. package/main/opcodes/flow-control.zkasm +126 -0
  118. package/main/opcodes/logs.zkasm +193 -0
  119. package/main/opcodes/stack-operations.zkasm +658 -0
  120. package/main/opcodes/storage-memory.zkasm +313 -0
  121. package/main/pairings/BN254/addPointBN254.zkasm +245 -0
  122. package/main/pairings/BN254/ecAdd.zkasm +312 -0
  123. package/main/pairings/BN254/ecMul.zkasm +159 -0
  124. package/main/pairings/BN254/escalarMulBN254.zkasm +155 -0
  125. package/main/pairings/BN254/lineDiffPointsBN254.zkasm +83 -0
  126. package/main/pairings/BN254/lineSamePointsBN254.zkasm +96 -0
  127. package/main/pairings/FP12BN254/CYCLOFP12BN254/compressFp12BN254.zkasm +49 -0
  128. package/main/pairings/FP12BN254/CYCLOFP12BN254/decompressFp12BN254.zkasm +236 -0
  129. package/main/pairings/FP12BN254/CYCLOFP12BN254/expByXCompCycloFp12BN254.zkasm +444 -0
  130. package/main/pairings/FP12BN254/CYCLOFP12BN254/squareCompCycloFp12BN254.zkasm +212 -0
  131. package/main/pairings/FP12BN254/CYCLOFP12BN254/squareCycloFp12BN254.zkasm +228 -0
  132. package/main/pairings/FP12BN254/CYCLOFP12BN254/xBinDecompBN254.zkasm +64 -0
  133. package/main/pairings/FP12BN254/frob2Fp12BN254.zkasm +80 -0
  134. package/main/pairings/FP12BN254/frob3Fp12BN254.zkasm +96 -0
  135. package/main/pairings/FP12BN254/frobFp12BN254.zkasm +96 -0
  136. package/main/pairings/FP12BN254/inverseFp12BN254.zkasm +289 -0
  137. package/main/pairings/FP12BN254/mulFp12BN254.zkasm +408 -0
  138. package/main/pairings/FP12BN254/sparseMulAFp12BN254.zkasm +296 -0
  139. package/main/pairings/FP12BN254/sparseMulBFp12BN254.zkasm +291 -0
  140. package/main/pairings/FP12BN254/squareFp12BN254.zkasm +376 -0
  141. package/main/pairings/FP2BN254/addFp2BN254.zkasm +19 -0
  142. package/main/pairings/FP2BN254/escalarMulFp2BN254.zkasm +20 -0
  143. package/main/pairings/FP2BN254/invFp2BN254.zkasm +66 -0
  144. package/main/pairings/FP2BN254/mulFp2BN254.zkasm +19 -0
  145. package/main/pairings/FP2BN254/squareFp2BN254.zkasm +21 -0
  146. package/main/pairings/FP2BN254/subFp2BN254.zkasm +19 -0
  147. package/main/pairings/FP4BN254/squareFp4BN254.zkasm +76 -0
  148. package/main/pairings/FP6BN254/addFp6BN254.zkasm +59 -0
  149. package/main/pairings/FP6BN254/escalarMulFp6BN254.zkasm +51 -0
  150. package/main/pairings/FP6BN254/inverseFp6BN254.zkasm +208 -0
  151. package/main/pairings/FP6BN254/mulFp6BN254.zkasm +201 -0
  152. package/main/pairings/FP6BN254/sparseMulAFp6BN254.zkasm +65 -0
  153. package/main/pairings/FP6BN254/sparseMulBFp6BN254.zkasm +134 -0
  154. package/main/pairings/FP6BN254/sparseMulCFp6BN254.zkasm +128 -0
  155. package/main/pairings/FP6BN254/squareFp6BN254.zkasm +147 -0
  156. package/main/pairings/FP6BN254/subFp6BN254.zkasm +59 -0
  157. package/main/pairings/FPBN254/addFpBN254.zkasm +29 -0
  158. package/main/pairings/FPBN254/invFpBN254.zkasm +55 -0
  159. package/main/pairings/FPBN254/mulFpBN254.zkasm +29 -0
  160. package/main/pairings/FPBN254/reduceFpBN254.zkasm +25 -0
  161. package/main/pairings/FPBN254/squareFpBN254.zkasm +31 -0
  162. package/main/pairings/FPBN254/subFpBN254.zkasm +36 -0
  163. package/main/pairings/FRBN254/reduceFrBN254.zkasm +25 -0
  164. package/main/pairings/constants.zkasm +62 -0
  165. package/main/pairings/ecPairing.zkasm +244 -0
  166. package/main/pairings/finalExpBN254.zkasm +2095 -0
  167. package/main/pairings/halfPairingBN254.zkasm +428 -0
  168. package/main/pairings/loopLengthBN254.zkasm +75 -0
  169. package/main/pairings/millerLoopBN254.zkasm +741 -0
  170. package/main/pairings/pairingBN254.zkasm +481 -0
  171. package/main/pairings/unused/addFp12BN254.zkasm +130 -0
  172. package/main/pairings/unused/expByXCycloFp12BN254.zkasm +411 -0
  173. package/main/pairings/unused/expFp12BN254.zkasm +333 -0
  174. package/main/pairings/unused/subFp12BN254.zkasm +130 -0
  175. package/main/pairings/unused/xPseudoBinDecompBN254.zkasm +68 -0
  176. package/main/pairings/utilsTests/expCycloFp12BN254.zkasm +334 -0
  177. package/main/precompiled/end.zkasm +42 -0
  178. package/main/precompiled/identity.zkasm +99 -0
  179. package/main/precompiled/pre-ecAdd.zkasm +84 -0
  180. package/main/precompiled/pre-ecMul.zkasm +82 -0
  181. package/main/precompiled/pre-ecPairing.zkasm +72 -0
  182. package/main/precompiled/pre-ecrecover.zkasm +71 -0
  183. package/main/precompiled/pre-modexp.zkasm +367 -0
  184. package/main/precompiled/pre-sha2-256.zkasm +125 -0
  185. package/main/precompiled/revert-precompiled.zkasm +25 -0
  186. package/main/precompiled/selector.zkasm +77 -0
  187. package/main/process-change-l2-block.zkasm +147 -0
  188. package/main/process-tx.zkasm +587 -0
  189. package/main/tables/2-exp.zkasm +260 -0
  190. package/main/touched.zkasm +118 -0
  191. package/main/utils.zkasm +2335 -0
  192. package/main/vars.zkasm +117 -0
  193. package/package.json +62 -3
  194. package/test/bytes-length.zkasm +39 -0
  195. package/test/ecrecover.zkasm +538 -0
  196. package/test/lt4-test.zkasm +38 -0
  197. package/test/mstorex.zkasm +191 -0
  198. package/test/opcalldatacopy.ignore.zkasm +331 -0
  199. package/test/performance/read-push.zkasm +71 -0
  200. package/test/read-push.zkasm +304 -0
  201. package/test/testArrayArith.zkasm +1099 -0
  202. package/test/testArrayUtils.zkasm +335 -0
  203. package/test/testCycloFp12ArithBN254.zkasm +548 -0
  204. package/test/testEcAdd.zkasm +252 -0
  205. package/test/testEcMul.zkasm +231 -0
  206. package/test/testEcPairing.zkasm +436 -0
  207. package/test/testFinalExpBn254.zkasm +139 -0
  208. package/test/testFp12ArithBN254.zkasm +692 -0
  209. package/test/testFp2ArithBN254.zkasm +185 -0
  210. package/test/testFp4ArithBN254.zkasm +128 -0
  211. package/test/testFp6ArithBN254.zkasm +260 -0
  212. package/test/testFpArithBN254.zkasm +159 -0
  213. package/test/testFrArithBN254.zkasm +113 -0
  214. package/test/testHalfPairingBN254.zkasm +285 -0
  215. package/test/testModExp.zkasm +586 -0
  216. package/test/testModExpReturn.zkasm +81 -0
  217. package/test/testPairingBN254.zkasm +463 -0
  218. package/test/testPointArithBN254.zkasm +270 -0
  219. package/test/testSHA256.zkasm +27 -0
  220. package/test/touched-assert.zkasm +59 -0
  221. package/test/utils-expAD.zkasm +48 -0
  222. package/test/utils-getLenBytes.zkasm +36 -0
  223. package/tools/audit-tools/registry-op-checker.js +71 -0
  224. package/tools/get-not-used-labels.js +31 -0
  225. package/tools/helpers/helpers.js +47 -0
  226. package/tools/modexp-utils/README.md +5 -0
  227. package/tools/modexp-utils/modexp-test-gen.js +168 -0
  228. package/tools/modexp-utils/modexp-test-int.sage +37 -0
  229. package/tools/parallel-testing/checker.sh +6 -0
  230. package/tools/parallel-testing/gen-parallel-tests.js +78 -0
  231. package/tools/parallel-testing/parallel-tests-sample/sample.test.js +136 -0
  232. package/tools/run-tests-zkasm.js +83 -0
@@ -0,0 +1,313 @@
1
+ /**
2
+ * @link [https://www.evm.codes/#51?fork=berlin]
3
+ * @zk-counters
4
+ * - 100 steps
5
+ * @process-opcode
6
+ * - stack input: [offset]
7
+ * - stack output: [value]
8
+ */
9
+ opMLOAD:
10
+ ; checks zk-counters
11
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
12
+
13
+ ; check stack underflow
14
+ SP - 1 => SP :JMPN(stackUnderflow)
15
+
16
+ ; check out-of-gas
17
+ GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
18
+
19
+ $ => E :MLOAD(SP); [offset => E]
20
+ ; store lastMemOffset for memory expansion gas cost
21
+ E :MSTORE(lastMemOffset)
22
+ ; store lastMemLength for memory expansion gas cost
23
+ ; compute memory expansion gas cost
24
+ 32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
25
+ ; read and store stack output
26
+ :CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
27
+ A :MSTORE(SP++), JMP(readCode); [value(A) => SP]
28
+
29
+ /**
30
+ * @link [https://www.evm.codes/#52?fork=berlin]
31
+ * @zk-counters
32
+ * - 100 steps
33
+ * @process-opcode
34
+ * - stack input: [offset, value]
35
+ * - stack output: none
36
+ */
37
+ VAR GLOBAL mstoreAux
38
+ opMSTORE:
39
+ ; checks zk-counters
40
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
41
+ %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
42
+
43
+ ; check stack underflow
44
+ SP - 2 => SP :JMPN(stackUnderflow)
45
+
46
+ ; check out-of-gas
47
+ GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
48
+
49
+ $ => E :MLOAD(SP+1); [offset => E]
50
+ $ => B :MLOAD(SP); [value => B]
51
+
52
+ ; store lastMemOffset for memory expansion gas cost
53
+ E :MSTORE(lastMemOffset)
54
+ ; store lastMemLength for memory expansion gas cost
55
+ 32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
56
+ ; store bytesToStore for memalign execution
57
+ B :MSTORE(bytesToStore)
58
+ E => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
59
+
60
+ E :MSTORE(mstoreAux)
61
+ $ => A :MLOAD(MEM:E)
62
+ $ => B :MLOAD(MEM:E+1)
63
+ ${memAlignWR_W0(A,mem.bytesToStore,C)} => D ; no trust calculate W0
64
+ ${memAlignWR_W1(B,mem.bytesToStore,C)} => E ; no trust calculate W1
65
+ $ :MEM_ALIGN_WR,MLOAD(bytesToStore)
66
+ E => A
67
+ $ => E :MLOAD(mstoreAux)
68
+ D :MSTORE(MEM:E) ; write W0
69
+ A :MSTORE(MEM:E+1) ; write W1
70
+ :JMP(readCode)
71
+
72
+ /**
73
+ * @link [https://www.evm.codes/#53?fork=berlin]
74
+ * @zk-counters
75
+ * - 100 steps
76
+ * - 1 mem align
77
+ * @process-opcode
78
+ * - stack input: [offset, value]
79
+ * - stack output: none
80
+ */
81
+ opMSTORE8:
82
+ ; checks zk-counters
83
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
84
+ %MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
85
+
86
+ ; check stack underflow
87
+ SP - 2 => SP :JMPN(stackUnderflow)
88
+
89
+ ; check out-of-gas
90
+ GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
91
+
92
+ $ => B :MLOAD(SP+1); [offset => B]
93
+ ; store lastMemOffset for memory expansion gas cost
94
+ B :MSTORE(lastMemOffset)
95
+ ; store lastMemLength for memory expansion gas cost. In case of MSTORE8, always 1 byte
96
+ 1 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
97
+ B => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
98
+ $ => B :MLOAD(SP); [value => B]
99
+ ; read from memory position E
100
+ $ => A :MLOAD(MEM:E)
101
+ ${memAlignWR8_W0(A,B,C)} => D ; no trust calculate W0
102
+ B :MEM_ALIGN_WR8 ; only use LSB of B, rest of bytes could be non zero.
103
+ ; write at memory position E
104
+ D :MSTORE(MEM:E), JMP(readCode)
105
+
106
+ /**
107
+ * @link [https://www.evm.codes/#59?fork=berlin]
108
+ * @zk-counters
109
+ * - 100 steps
110
+ * @process-opcode
111
+ * - stack input: none
112
+ * - stack output: [size]
113
+ */
114
+ opMSIZE:
115
+ ; checks zk-counters
116
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
117
+
118
+ ; check out-of-gas
119
+ GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
120
+
121
+ ; load current memory length
122
+ $ => E :MLOAD(memLength)
123
+ ; MSIZE should be multiple of a word (32 bytes)
124
+ ; Div operation with Arith
125
+ E :MSTORE(arithA)
126
+ 32 :MSTORE(arithB)
127
+ :CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
128
+ $ => C :MLOAD(arithRes1)
129
+ $ => B :MLOAD(arithRes2)
130
+ ; check arithRes2 is 0, no need to round in this case
131
+ 0 => A
132
+ %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
133
+ $ :EQ, JMPC(MSIZEend)
134
+ ; Round size to 32bytes multiple
135
+ C + 1 => C
136
+ C * 32 => E
137
+
138
+ MSIZEend:
139
+ E :MSTORE(SP++); [size(E) => SP]
140
+ ; check stack overflow
141
+ %MAX_STACK_SIZE - SP :JMPN(stackOverflow, readCode)
142
+ /**
143
+ * @link [https://www.evm.codes/#54?fork=berlin]
144
+ * @zk-counters
145
+ * - 100 steps
146
+ * - %MAX_CNT_POSEIDON_SLOAD_SSTORE poseidon
147
+ * @process-opcode
148
+ * - stack input: [key]
149
+ * - stack output: [value]
150
+ */
151
+ opSLOAD:
152
+ ; checks zk-counters
153
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
154
+ %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE :JMPN(outOfCountersPoseidon)
155
+
156
+ ; check stack underflow
157
+ SP - 1 => SP :JMPN(stackUnderflow)
158
+
159
+ $ => C :MLOAD(SP); [key => C]
160
+ ; get current storage address
161
+ $ => A :MLOAD(storageAddr)
162
+ ; set key for smt storage query
163
+ %SMT_KEY_SC_STORAGE => B
164
+ $ => E :SLOAD
165
+ $${eventLog(onUpdateStorage(C, E))}
166
+ ; set key(C) as warmed storage for address(A)
167
+ E :MSTORE(SP++), CALL(isColdSlot); [value(E) => SP]
168
+ ; check out-of-gas
169
+ GAS - %SLOAD_GAS - A * %COLD_SLOAD_COST_REDUCED => GAS :JMPN(outOfGas, readCode)
170
+
171
+ VAR GLOBAL tmpVarCsstore
172
+ /**
173
+ * @link [https://www.evm.codes/#55?fork=berlin]
174
+ * @zk-counters
175
+ * - 400 steps
176
+ * - 10 binary
177
+ * - %MAX_CNT_POSEIDON_SLOAD_SSTORE*3 poseidon
178
+ * @process-opcode
179
+ * - stack input: [key, value]
180
+ * - stack output: none
181
+ */
182
+ opSSTORE:
183
+ ; checks zk-counters
184
+ %MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
185
+ %MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary)
186
+ %MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*3 :JMPN(outOfCountersPoseidon)
187
+
188
+ ; check stack underflow
189
+ SP - 2 => SP :JMPN(stackUnderflow)
190
+
191
+ $ => C :MLOAD(SP+1) ; [key => C]
192
+ C :MSTORE(tmpVarCsstore)
193
+ $ => D :MLOAD(SP) ; [value => D]
194
+
195
+ $${eventLog(onUpdateStorage(C, D))}
196
+
197
+ ; check out-of-gas
198
+ GAS - %SSTORE_ENTRY_EIP_2200_GAS - 1 :JMPN(outOfGas)
199
+ ; check is static call
200
+ $ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
201
+ ; check if is a create call
202
+ $ => A :MLOAD(isCreateContract), JMPNZ(deploymentSSTORE)
203
+ ; load current storage address
204
+ $ => A :MLOAD(storageAddr), JMP(opSSTOREinit)
205
+
206
+ deploymentSSTORE:
207
+ ; in case of create, the storage address is the create contract address
208
+ $ => A :MLOAD(createContractAddress)
209
+
210
+ opSSTOREinit:
211
+ ; set key for smt storage query
212
+ %SMT_KEY_SC_STORAGE => B
213
+ $ => E :SLOAD
214
+ ; change context to check storage original value
215
+ SR :MSTORE(auxSR)
216
+ $ => SR :MLOAD(originSR)
217
+ $ => B :SLOAD ; origin value
218
+ ; set key(C) as warmed storage for address(A)
219
+ $ => SR :MLOAD(auxSR), CALL(isColdSlot)
220
+ B => C ; origin value
221
+ ; check out-of-gas
222
+ GAS - A*%COLD_SLOAD_COST => GAS :JMPN(outOfGas)
223
+ E => A
224
+ D => B
225
+ $ => A :EQ, JMPNC(opSSTOREdif)
226
+ ; if new_val == current_val
227
+ ; check out-of-gas
228
+ GAS - %SSTORE_DYNAMIC_GAS => GAS:JMPN(outOfGas, opSSTOREend)
229
+
230
+ opSSTOREdif:
231
+ ; if new_val != current_val
232
+ C => A
233
+ E => B
234
+ $ => A :EQ, JMPNC(opSSTOREdifA)
235
+ ; if current_val == orig_val
236
+ 0 => A
237
+ C => B
238
+ $ => A :EQ, JMPNC(opSSTOREdifB)
239
+ ; if origin_val == 0
240
+ ; check out-of-gas
241
+ GAS - %SSTORE_SET_GAS => GAS :JMPN(outOfGas, opSSTOREend)
242
+
243
+ opSSTOREdifA:
244
+ ; if current_val != orig_val
245
+ GAS - %SSTORE_DYNAMIC_GAS => GAS:JMPN(outOfGas)
246
+ 0 => A
247
+ C => B
248
+ $ => A :EQ, JMPNC(opSSTOREdifA1)
249
+ ; if origin_value == 0
250
+
251
+ opSSTOREdifAB:
252
+ ; if origin_value == 0
253
+ D => A
254
+ C => B
255
+ $ => A :EQ, JMPNC(opSSTOREend)
256
+ ; if new_val == orig_val
257
+ 0 => A
258
+ $ => A :EQ, JMPNC(opSSTOREdifA2)
259
+ ; if orig_val == 0
260
+ $ => A :MLOAD(gasRefund)
261
+ A + %SSTORE_SET_GAS_REDUCED => A :MSTORE(gasRefund), JMP(opSSTOREend)
262
+
263
+ opSSTOREdifA1:
264
+ ; if orig_val != 0
265
+ 0 => A
266
+ E => B
267
+ $ => A :EQ, JMPNC(opSSTOREdifA12)
268
+ ; if current_val == 0
269
+ ; compute gas refund
270
+ $ => A :MLOAD(gasRefund)
271
+ A - %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund), JMP(opSSTOREdifAB)
272
+ opSSTOREdifA12:
273
+ ;if current_val != 0
274
+ 0 => A
275
+ D => B
276
+ $ => A :EQ, JMPNC(opSSTOREdifAB)
277
+ ; if new_val == 0
278
+ ; compute gas refund
279
+ $ => A :MLOAD(gasRefund)
280
+ A + %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund), JMP(opSSTOREdifAB)
281
+
282
+ opSSTOREdifA2:
283
+ ; if orig_val != 0
284
+ ; compute gas refund
285
+ $ => A :MLOAD(gasRefund)
286
+ A + %SSTORE_RESET_GAS_REDUCED => A :MSTORE(gasRefund), JMP(opSSTOREend)
287
+
288
+ opSSTOREdifB:
289
+ ; if orig_val != 0
290
+ ; check out-of-gas
291
+ GAS - %SSTORE_RESET_GAS => GAS :JMPN(outOfGas)
292
+ 0 => A
293
+ D => B
294
+ $ => A :EQ, JMPNC(opSSTOREend)
295
+ ; if new_val == 0
296
+ ; compute gas refund
297
+ $ => A :MLOAD(gasRefund)
298
+ A + %SSTORE_CLEARS_SCHEDULE => A :MSTORE(gasRefund)
299
+ :JMP(opSSTOREend)
300
+
301
+ opSSTOREend:
302
+ $ => A :MLOAD(isCreateContract), JMPNZ(mloadContract)
303
+ $ => A :MLOAD(storageAddr), JMP(opSSTOREsr)
304
+
305
+ mloadContract:
306
+ ; if is a create, use create contract address storage
307
+ $ => A :MLOAD(createContractAddress)
308
+
309
+ opSSTOREsr:
310
+ ; set key for smt storage query
311
+ %SMT_KEY_SC_STORAGE => B
312
+ $ => C :MLOAD(tmpVarCsstore); key => C
313
+ $ => SR :SSTORE, JMP(readCode)
@@ -0,0 +1,245 @@
1
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
+ ;; PRE: P1,P2 ∈ E'(Fp2)
3
+ ;; POST: The resulting coordinates are in the range [0,BN254_P) because if falls back to FP2 arithmetic
4
+ ;;
5
+ ;; addPointBN254:
6
+ ;; in: P1 = (P1.x1 + P1.x2·u, P1.y1 + P1.y2·u), P2 = (P2.x1 + P2.x2·u, P2.y1 + P2.y2·u) ∈ E'(Fp2)
7
+ ;; out: P1 + P2 = (P3.x1 + P3.x2·u, P3.y1 + P3.y2·u) ∈ E'(Fp2)
8
+ ;;
9
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
+
11
+ ; addPointBN254 assumes both P1 and P2 belong to E'(Fp2), since it is checked in the pairing.
12
+ ; However, it must be implemented if addPointBN254 wants to be used independently.
13
+
14
+ ; Since the curve is E'/Fp2: y² = x³ + 3/(9+u), there is no issue in representing the point at infinity as (0, 0).
15
+
16
+ VAR GLOBAL addPointBN254_P1_x1
17
+ VAR GLOBAL addPointBN254_P1_x2
18
+ VAR GLOBAL addPointBN254_P1_y1
19
+ VAR GLOBAL addPointBN254_P1_y2
20
+ VAR GLOBAL addPointBN254_P2_x1
21
+ VAR GLOBAL addPointBN254_P2_x2
22
+ VAR GLOBAL addPointBN254_P2_y1
23
+ VAR GLOBAL addPointBN254_P2_y2
24
+ VAR GLOBAL addPointBN254_P3_x1
25
+ VAR GLOBAL addPointBN254_P3_x2
26
+ VAR GLOBAL addPointBN254_P3_y1
27
+ VAR GLOBAL addPointBN254_P3_y2
28
+ VAR GLOBAL addPointBN254_lambda_x
29
+ VAR GLOBAL addPointBN254_lambda_y
30
+ VAR GLOBAL addPointBN254_RR
31
+
32
+ addPointBN254:
33
+ RR :MSTORE(addPointBN254_RR)
34
+
35
+ ; Is P1 = O?
36
+ 0n => B
37
+ $ => A :MLOAD(addPointBN254_P1_x1)
38
+ $ :EQ, JMPNC(__addPointBN254_P1_continue)
39
+ $ => A :MLOAD(addPointBN254_P1_x2)
40
+ $ :EQ, JMPNC(__addPointBN254_P1_continue)
41
+ $ => A :MLOAD(addPointBN254_P1_y1)
42
+ $ :EQ, JMPNC(__addPointBN254_P1_continue)
43
+ $ => A :MLOAD(addPointBN254_P1_y2)
44
+ $ :EQ, JMPC(addPointBN254_P1_is_zero)
45
+ __addPointBN254_P1_continue:
46
+
47
+ ; Is P2 = 0?
48
+ 0n => B
49
+ $ => A :MLOAD(addPointBN254_P2_x1)
50
+ $ :EQ, JMPNC(__addPointBN254_P2_continue)
51
+ $ => A :MLOAD(addPointBN254_P2_x2)
52
+ $ :EQ, JMPNC(__addPointBN254_P2_continue)
53
+ $ => A :MLOAD(addPointBN254_P2_y1)
54
+ $ :EQ, JMPNC(__addPointBN254_P2_continue)
55
+ $ => A :MLOAD(addPointBN254_P2_y2)
56
+ $ :EQ, JMPC(addPointBN254_P2_is_zero)
57
+ __addPointBN254_P2_continue:
58
+
59
+ ; P1 and P2 are not 0, let's check whether they are different points, the same point or inverses of each other
60
+ ; Is P1.x == P2.x?
61
+ $ => A :MLOAD(addPointBN254_P1_x1)
62
+ $ => B :MLOAD(addPointBN254_P2_x1)
63
+ $ :EQ, JMPNC(addPointBN254_different)
64
+ $ => A :MLOAD(addPointBN254_P1_x2)
65
+ $ => B :MLOAD(addPointBN254_P2_x2)
66
+ $ :EQ, JMPNC(addPointBN254_different)
67
+
68
+ ; Is P1.y == P2.y?
69
+ $ => A :MLOAD(addPointBN254_P1_y1)
70
+ $ => B :MLOAD(addPointBN254_P2_y1)
71
+ $ :EQ, JMPNC(addPointBN254_P1_and_P2_are_inverted)
72
+ $ => A :MLOAD(addPointBN254_P1_y2)
73
+ $ => B :MLOAD(addPointBN254_P2_y2)
74
+ $ :EQ, JMPNC(addPointBN254_P1_and_P2_are_inverted)
75
+
76
+ ; P1 == P2
77
+ :JMP(addPointBN254_same)
78
+
79
+ addPointBN254_P1_is_zero:
80
+ ; P3 = P2
81
+ $ => A :MLOAD(addPointBN254_P2_x1)
82
+ $ => B :MLOAD(addPointBN254_P2_x2)
83
+ $ => C :MLOAD(addPointBN254_P2_y1)
84
+ $ => D :MLOAD(addPointBN254_P2_y2)
85
+ A :MSTORE(addPointBN254_P3_x1)
86
+ B :MSTORE(addPointBN254_P3_x2)
87
+ C :MSTORE(addPointBN254_P3_y1)
88
+ D :MSTORE(addPointBN254_P3_y2)
89
+
90
+ :JMP(addPointBN254_end)
91
+
92
+ addPointBN254_P2_is_zero:
93
+ ; P3 = P1
94
+ $ => A :MLOAD(addPointBN254_P1_x1)
95
+ $ => B :MLOAD(addPointBN254_P1_x2)
96
+ $ => C :MLOAD(addPointBN254_P1_y1)
97
+ $ => D :MLOAD(addPointBN254_P1_y2)
98
+ A :MSTORE(addPointBN254_P3_x1)
99
+ B :MSTORE(addPointBN254_P3_x2)
100
+ C :MSTORE(addPointBN254_P3_y1)
101
+ D :MSTORE(addPointBN254_P3_y2)
102
+
103
+ :JMP(addPointBN254_end)
104
+
105
+ addPointBN254_P1_and_P2_are_inverted:
106
+ ; Check -P1.y == P2.y
107
+ %BN254_P => A
108
+ $ => B :MLOAD(addPointBN254_P1_y1)
109
+ $ => C :SUB
110
+ $ => B :MLOAD(addPointBN254_P1_y2)
111
+ $ => D :SUB
112
+
113
+ $ => A :MLOAD(addPointBN254_P2_y1)
114
+ C :ASSERT
115
+ $ => A :MLOAD(addPointBN254_P2_y2)
116
+ D :ASSERT
117
+
118
+ ; P3 = O
119
+ 0n :MSTORE(addPointBN254_P3_x1)
120
+ 0n :MSTORE(addPointBN254_P3_x2)
121
+ 0n :MSTORE(addPointBN254_P3_y1)
122
+ 0n :MSTORE(addPointBN254_P3_y2)
123
+
124
+ :JMP(addPointBN254_end)
125
+
126
+ addPointBN254_same:
127
+ $ => A :MLOAD(addPointBN254_P1_y1)
128
+ $ => B :MLOAD(addPointBN254_P1_y2)
129
+ $ => C :MLOAD(addPointBN254_P1_y1)
130
+ $ => D :MLOAD(addPointBN254_P1_y2), CALL(addFp2BN254)
131
+ ; E + C·u = 2y
132
+
133
+ E => A
134
+ C => B :CALL(invFp2BN254)
135
+ ; C + D·u = 1 / 2y
136
+
137
+ 3n => A :CALL(escalarMulFp2BN254)
138
+ ; E + C·u = 3/2y
139
+
140
+ $ => A :MLOAD(addPointBN254_P1_x1)
141
+ $ => B :MLOAD(addPointBN254_P1_x2)
142
+ C => D
143
+ E => C :CALL(mulFp2BN254)
144
+ ; E + C·u = 3x/2y
145
+
146
+ $ => A :MLOAD(addPointBN254_P1_x1)
147
+ $ => B :MLOAD(addPointBN254_P1_x2)
148
+ C => D
149
+ E => C :CALL(mulFp2BN254)
150
+ ; E + C·u = lambda = 3x²/2y
151
+
152
+ E :MSTORE(addPointBN254_lambda_x)
153
+ C :MSTORE(addPointBN254_lambda_y)
154
+ ; E + C·u = lambda
155
+
156
+ E => A
157
+ C => B :CALL(squareFp2BN254)
158
+ ; E + C·u = lambda²
159
+
160
+ E => A
161
+ C => B
162
+ $ => C :MLOAD(addPointBN254_P1_x1)
163
+ $ => D :MLOAD(addPointBN254_P1_x2), CALL(subFp2BN254)
164
+ ; E + C·u = lambda² - x
165
+
166
+ E => A
167
+ C => B
168
+ $ => C :MLOAD(addPointBN254_P1_x1)
169
+ $ => D :MLOAD(addPointBN254_P1_x2), CALL(subFp2BN254)
170
+ ; E + C·u = lambda² - x - x
171
+
172
+ :JMP(addPointBN254_common_calculate)
173
+
174
+ addPointBN254_different:
175
+ $ => A :MLOAD(addPointBN254_P2_x1)
176
+ $ => B :MLOAD(addPointBN254_P2_x2)
177
+ $ => C :MLOAD(addPointBN254_P1_x1)
178
+ $ => D :MLOAD(addPointBN254_P1_x2), CALL(subFp2BN254)
179
+ ; E + C·u = P2.x - P1.x
180
+
181
+ E => A
182
+ C => B :CALL(invFp2BN254)
183
+ ; C + D·u = 1 / (P2_x - P1_x)
184
+ C :MSTORE(addPointBN254_lambda_x)
185
+ D :MSTORE(addPointBN254_lambda_y)
186
+
187
+ $ => A :MLOAD(addPointBN254_P2_y1)
188
+ $ => B :MLOAD(addPointBN254_P2_y2)
189
+ $ => C :MLOAD(addPointBN254_P1_y1)
190
+ $ => D :MLOAD(addPointBN254_P1_y2), CALL(subFp2BN254)
191
+ ; E + C·u = P2.y - P1.y
192
+
193
+ $ => A :MLOAD(addPointBN254_lambda_x)
194
+ $ => B :MLOAD(addPointBN254_lambda_y)
195
+ C => D
196
+ E => C :CALL(mulFp2BN254)
197
+ ; E + C·u = lambda = (P2_y - P1_y) / (P2_x - P1_x)
198
+ E :MSTORE(addPointBN254_lambda_x)
199
+ C :MSTORE(addPointBN254_lambda_y)
200
+
201
+ E => A
202
+ C => B :CALL(squareFp2BN254)
203
+ ; E + C·u = lambda²
204
+
205
+ E => A
206
+ C => B
207
+ $ => C :MLOAD(addPointBN254_P1_x1)
208
+ $ => D :MLOAD(addPointBN254_P1_x2), CALL(subFp2BN254)
209
+ ; E + C·u = lambda² - P1.x
210
+
211
+ E => A
212
+ C => B
213
+ $ => C :MLOAD(addPointBN254_P2_x1)
214
+ $ => D :MLOAD(addPointBN254_P2_x2), CALL(subFp2BN254)
215
+ ; E + C·u = lambda² - P1.x - P2.x
216
+
217
+ addPointBN254_common_calculate:
218
+ E :MSTORE(addPointBN254_P3_x1)
219
+ C :MSTORE(addPointBN254_P3_x2)
220
+ ; P3.x = lambda² - P1.x - P2.x
221
+
222
+ $ => A :MLOAD(addPointBN254_P1_x1)
223
+ $ => B :MLOAD(addPointBN254_P1_x2)
224
+ C => D
225
+ E => C :CALL(subFp2BN254)
226
+ ; E + C·u = P1.x - P3.x
227
+
228
+ $ => A :MLOAD(addPointBN254_lambda_x)
229
+ $ => B :MLOAD(addPointBN254_lambda_y)
230
+ C => D
231
+ E => C :CALL(mulFp2BN254)
232
+ ; E + C·u = lambda·(P1.x - P3.x)
233
+
234
+ E => A
235
+ C => B
236
+ $ => C :MLOAD(addPointBN254_P1_y1)
237
+ $ => D :MLOAD(addPointBN254_P1_y2), CALL(subFp2BN254)
238
+ ; E + C·u = lambda·(P1.x - P3.x) - P1.y
239
+
240
+ E :MSTORE(addPointBN254_P3_y1)
241
+ C :MSTORE(addPointBN254_P3_y2)
242
+
243
+ addPointBN254_end:
244
+ $ => RR :MLOAD(addPointBN254_RR)
245
+ :RETURN