@zkasm/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 @zkasm/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,49 @@
1
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
+ ;;
3
+ ;; array_trim:
4
+ ;; in:
5
+ ;; · C ∈ [1, 3636], the len of in
6
+ ;; · in ∈ [0, 2²⁵⁶ - 1]^C, the input array
7
+ ;;
8
+ ;; output:
9
+ ;; · C, the new length of in
10
+ ;;
11
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
+
13
+ ; function array_trim(a: bigint[]): void {
14
+ ; let i = a.length;
15
+ ;
16
+ ; while (a[--i] === 0n);
17
+ ;
18
+ ; a.length = i + 1;
19
+ ; }
20
+
21
+ VAR GLOBAL array_trim_in[%ARRAY_MAX_LEN_DOUBLED]
22
+
23
+ VAR GLOBAL array_trim_RR
24
+
25
+ array_trim:
26
+ %MAX_CNT_STEPS - STEP - 5 :JMPN(outOfCountersStep)
27
+
28
+ RR :MSTORE(array_trim_RR)
29
+
30
+ 0 => B ; used for comparison in the whole loop
31
+
32
+ C => E
33
+ ; scan from the last chunk to the first chunks until we find a non-zero chunk
34
+ ; in case of zero input array, we return 1
35
+ array_trim_loop:
36
+ %MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
37
+ %MAX_CNT_STEPS - STEP - 3 :JMPN(outOfCountersStep)
38
+
39
+ E - 1 => E :JMPZ(array_trim_end)
40
+
41
+ $ => A :MLOAD(array_trim_in + E)
42
+ $ :EQ, JMPZ(array_trim_end, array_trim_loop)
43
+
44
+ array_trim_end:
45
+ %MAX_CNT_STEPS - STEP - 2 :JMPN(outOfCountersStep)
46
+
47
+ $ => RR :MLOAD(array_trim_RR)
48
+
49
+ E + 1 => C :RETURN
@@ -0,0 +1,5 @@
1
+ ; See the discussion [https://github.com/0xPolygonHermez/zkevm-rom-internal/issues/43] for more details.
2
+ CONST %ARRAY_MAX_LEN = 32
3
+ CONST %ARRAY_MAX_LEN_PLUS_ONE = 33
4
+ CONST %ARRAY_MAX_LEN_DOUBLED = 64
5
+ CONST %MODEXP_MAX_LEN = 32
@@ -0,0 +1,296 @@
1
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
+ ;; PRE: B, E, M have been trimmed.
3
+ ;; POST: out is trimmed
4
+ ;;
5
+ ;; modexp:
6
+ ;; ----------------------------------------
7
+ ;; input:
8
+ ;; · Blen ∈ [1, 32], the len of B
9
+ ;; · Elen ∈ [1, 32], the len of E
10
+ ;; · Mlen ∈ [1, 32], the len of M
11
+ ;; · B ∈ [0, 2²⁵⁶ - 1]^Blen, the base represented in little-endian
12
+ ;; · E ∈ [0, 2²⁵⁶ - 1]^Elen, the exponent represented in little-endian
13
+ ;; · M ∈ [0, 2²⁵⁶ - 1]^Mlen, the modulus represented in little-endian
14
+ ;;
15
+ ;; output:
16
+ ;; · B^E (mod M) ∈ [0, 2²⁵⁶ - 1]^Mlen
17
+ ;;
18
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19
+
20
+ ;; function modexp(b: bigint[], exp: bigint[], mod: bigint[], base: bigint): bigint[] {
21
+ ;; if (array_is_zero(mod) || array_is_one(mod)) return [0n];
22
+ ;; if (array_is_zero(b)) return [0n];
23
+ ;; if (array_is_one(b)) return [1n];
24
+ ;; if (array_is_zero(e)) return [1n];
25
+ ;;
26
+ ;; let r = [1n];
27
+ ;; let base = array_div(b, mod, base)[1];
28
+ ;; while (!array_is_zero(exp)) {
29
+ ;; if (array_is_zero(base)) return [0n];
30
+ ;; if (isOdd(exp)) {
31
+ ;; r = array_div(array_mul(r, base, base),mod,base)[1];
32
+ ;; }
33
+ ;; exp = array_div_short(exp, 2n, base)[0];
34
+ ;; base = array_div(array_square(base, base),mod,base)[1];
35
+ ;; }
36
+ ;; return r;
37
+ ;; };
38
+
39
+ ;; RESOURCES (assuming a worst case scenario):
40
+ ;; -------------------------------------------
41
+ ;; cost(pre_loop) = 3·cost(isZero) + 2·cost(isOne) + cost(array_div)
42
+ ;; nIterations = ⌊log₂(E)⌋ + 1
43
+ ;; nTimesEIsOdd = HammingWeight(E) (i.e., number of 1s in the binary representation of E)
44
+ ;; nTimesEIsEven = nIterations - nTimesEIsOdd
45
+ ;; cost(iteration1) (if E is odd) = cost(isZero) + cost(isOdd) + 2·cost(array_div) + cost(array_mul) + cost(array_div_short) + cost(array_square)
46
+ ;; cost(iteration2) (if E is even) = cost(isZero) + cost(isOdd) + cost(array_div) + cost(array_div_short) + cost(array_square)
47
+ ;; ------------
48
+ ;; cost(total) = cost(pre_loop) + nTimesEIsOdd·cost(iteration1) + nTimesEIsEven·cost(iteration2)
49
+ ;; ------------
50
+
51
+ VAR GLOBAL modexp_Blen
52
+ VAR GLOBAL modexp_Elen
53
+ VAR GLOBAL modexp_Mlen
54
+ VAR GLOBAL modexp_B[%MODEXP_MAX_LEN]
55
+ VAR GLOBAL modexp_E[%MODEXP_MAX_LEN]
56
+ VAR GLOBAL modexp_M[%MODEXP_MAX_LEN]
57
+
58
+ VAR GLOBAL modexp_out[%MODEXP_MAX_LEN]
59
+ VAR GLOBAL modexp_outlen
60
+
61
+ VAR GLOBAL modexp_RR
62
+
63
+ modexp:
64
+
65
+ %MAX_CNT_STEPS - STEP - 8 :JMPN(outOfCountersStep)
66
+
67
+ RR :MSTORE(modexp_RR)
68
+
69
+ ; I do not need to cover edge cases here since they are covered in the pre-modexp file
70
+ ; Therefore, I can assume that M > 1, B > 1, E > 0
71
+
72
+ 1 :MSTORE(modexp_out)
73
+ 1 :MSTORE(modexp_outlen)
74
+
75
+ ; prepare for computing B % M
76
+ $ => C :MLOAD(modexp_Blen)
77
+ $ => D :MLOAD(modexp_Mlen)
78
+ C - 1 => RR
79
+ D - 1 => E
80
+
81
+ %MAX_CNT_STEPS - STEP - 3*C - 3*D - 1 :JMPN(outOfCountersStep)
82
+
83
+ ; Compute B = B % M
84
+ ; -------------------
85
+ modexp_B_to_div:
86
+ $ => A :MLOAD(modexp_B + RR)
87
+ A :MSTORE(array_div_inA + RR)
88
+ RR - 1 => RR :JMPN(modexp_M_to_div1, modexp_B_to_div)
89
+
90
+ modexp_M_to_div1:
91
+ $ => A :MLOAD(modexp_M + E)
92
+ A :MSTORE(array_div_inB + E)
93
+ E - 1 => E :JMPN(modexp_div_B_and_M, modexp_M_to_div1)
94
+
95
+ modexp_div_B_and_M:
96
+ :CALL(array_div)
97
+
98
+ %MAX_CNT_STEPS - STEP - 4 :JMPN(outOfCountersStep)
99
+
100
+ $ => C :MLOAD(array_div_len_rem)
101
+ C :MSTORE(modexp_Blen)
102
+ C - 1 => RR
103
+
104
+ %MAX_CNT_STEPS - STEP - 3*C :JMPN(outOfCountersStep)
105
+
106
+ modexp_rem_from_div1:
107
+ $ => A :MLOAD(array_div_rem + RR)
108
+ A :MSTORE(modexp_B + RR)
109
+ RR - 1 => RR :JMPN(modexp_pre_loop, modexp_rem_from_div1)
110
+ ; -------------------
111
+
112
+ ; Begin of edge cases
113
+ modexp_B_is_zero:
114
+ ; (0^E) % M = 0.
115
+ 1 :MSTORE(modexp_outlen)
116
+ 0 :MSTORE(modexp_out), JMP(modexp_end)
117
+ ; End of edge cases
118
+
119
+ ; Begin of branching
120
+ modexp_loop_multiply:
121
+ $ => C :MLOAD(modexp_outlen)
122
+ $ => D :MLOAD(modexp_Blen)
123
+ C - 1 => RR
124
+ D - 1 => E
125
+
126
+ %MAX_CNT_STEPS - STEP - 3*C - 3*D - 1 :JMPN(outOfCountersStep)
127
+
128
+ ; Compute out * B
129
+ ; -------------------
130
+ modexp_out_to_mul_long:
131
+ $ => A :MLOAD(modexp_out + RR)
132
+ A :MSTORE(array_mul_inA + RR)
133
+ RR - 1 => RR :JMPN(modexp_B_to_mul_long, modexp_out_to_mul_long)
134
+
135
+ modexp_B_to_mul_long:
136
+ $ => A :MLOAD(modexp_B + E)
137
+ A :MSTORE(array_mul_inB + E)
138
+ E - 1 => E :JMPN(modexp_mul_long_out_and_B, modexp_B_to_mul_long)
139
+
140
+ modexp_mul_long_out_and_B:
141
+ :CALL(array_mul)
142
+
143
+ %MAX_CNT_STEPS - STEP - 5 :JMPN(outOfCountersStep)
144
+
145
+ $ => C :MLOAD(array_mul_len_out)
146
+ $ => D :MLOAD(modexp_Mlen)
147
+ C - 1 => RR
148
+ D - 1 => E
149
+
150
+ %MAX_CNT_STEPS - STEP - 3*C - 3*D - 1 :JMPN(outOfCountersStep)
151
+
152
+ ; Compute out = (out * B) % M
153
+ modexp_out_to_div1:
154
+ $ => A :MLOAD(array_mul_out + RR)
155
+ A :MSTORE(array_div_inA + RR)
156
+ RR - 1 => RR :JMPN(modexp_M_to_div, modexp_out_to_div1)
157
+
158
+ modexp_M_to_div:
159
+ $ => A :MLOAD(modexp_M + E)
160
+ A :MSTORE(array_div_inB + E)
161
+ E - 1 => E :JMPN(modexp_div_out_and_M2, modexp_M_to_div)
162
+
163
+ modexp_div_out_and_M2:
164
+ :CALL(array_div)
165
+
166
+ %MAX_CNT_STEPS - STEP - 4 :JMPN(outOfCountersStep)
167
+
168
+ $ => C :MLOAD(array_div_len_rem)
169
+ C :MSTORE(modexp_outlen)
170
+ C - 1 => RR
171
+
172
+ %MAX_CNT_STEPS - STEP - 3*C - 1 :JMPN(outOfCountersStep)
173
+
174
+ modexp_rem_from_div2:
175
+ $ => A :MLOAD(array_div_rem + RR)
176
+ A :MSTORE(modexp_out + RR)
177
+ RR - 1 => RR :JMPN(return_modexp_loop_multiply, modexp_rem_from_div2)
178
+ ; -------------------
179
+ ; End of branching
180
+
181
+ modexp_pre_loop:
182
+ ; In the worst case, the exponent is odd in each iteration
183
+ %MAX_CNT_BINARY - CNT_BINARY - 3 :JMPN(outOfCountersBinary)
184
+ %MAX_CNT_STEPS - STEP - 13 :JMPN(outOfCountersStep)
185
+
186
+ ; Is Elen = 1 and E = 0?
187
+ 1 => B
188
+ $ => A :MLOAD(modexp_Elen)
189
+ A - B :JMPNZ(__modexp_E_continue)
190
+ $ => A :MLOAD(modexp_E)
191
+ $ :LT, JMPC(modexp_end) ; we are done
192
+ __modexp_E_continue:
193
+
194
+ modexp_loop:
195
+ ; Is Blen = 1 and B = 0?
196
+ $ => A :MLOAD(modexp_Blen)
197
+ A - B :JMPNZ(__modexp_B_continue)
198
+ $ => A :MLOAD(modexp_B)
199
+ $ :LT, JMPC(modexp_B_is_zero)
200
+ __modexp_B_continue:
201
+
202
+ ; Is E is odd?
203
+ ; The base is 2^256, so I only need to check if the first chunk is odd to conclude that the whole number is odd.
204
+ $ => A :MLOAD(modexp_E)
205
+ 1 => B
206
+ $ :AND, JMPNZ(modexp_loop_multiply)
207
+ return_modexp_loop_multiply:
208
+
209
+ %MAX_CNT_STEPS - STEP - 3 :JMPN(outOfCountersStep)
210
+
211
+ $ => C :MLOAD(modexp_Elen)
212
+ C - 1 => RR
213
+
214
+ %MAX_CNT_STEPS - STEP - 3*C - 2 :JMPN(outOfCountersStep)
215
+
216
+ ; Compute E = E // 2
217
+ ; -------------------
218
+ modexp_E_to_div_short:
219
+ $ => A :MLOAD(modexp_E + RR)
220
+ A :MSTORE(array_div_short_inA + RR)
221
+ RR - 1 => RR :JMPN(modexp_div_E_and_2, modexp_E_to_div_short)
222
+
223
+ modexp_div_E_and_2:
224
+ 2 :MSTORE(array_div_short_inB)
225
+ :CALL(array_div_short)
226
+
227
+ %MAX_CNT_STEPS - STEP - 4 :JMPN(outOfCountersStep)
228
+
229
+ $ => C :MLOAD(array_div_short_len_quo)
230
+ C :MSTORE(modexp_Elen)
231
+ C - 1 => RR
232
+
233
+ %MAX_CNT_STEPS - STEP - 3*C - 3 :JMPN(outOfCountersStep)
234
+
235
+ modexp_quo_from_div_short:
236
+ $ => A :MLOAD(array_div_short_quo + RR)
237
+ A :MSTORE(modexp_E + RR)
238
+ RR - 1 => RR :JMPN(modexp_pre_B_square, modexp_quo_from_div_short)
239
+ ; -------------------
240
+
241
+ ; Compute B^2
242
+ ; -------------------
243
+ modexp_pre_B_square:
244
+ $ => C :MLOAD(modexp_Blen)
245
+ C - 1 => RR
246
+
247
+ %MAX_CNT_STEPS - STEP - 3*C - 1 :JMPN(outOfCountersStep)
248
+
249
+ modexp_B_to_square1:
250
+ $ => A :MLOAD(modexp_B + RR)
251
+ A :MSTORE(array_square_in + RR)
252
+ RR - 1 => RR :JMPN(modexp_square_B, modexp_B_to_square1)
253
+
254
+ modexp_square_B:
255
+ :CALL(array_square)
256
+
257
+ %MAX_CNT_STEPS - STEP - 5 :JMPN(outOfCountersStep)
258
+
259
+ $ => C :MLOAD(array_square_len_out)
260
+ $ => D :MLOAD(modexp_Mlen)
261
+ C - 1 => RR
262
+ D - 1 => E
263
+
264
+ %MAX_CNT_STEPS - STEP - 3*C - 3*D - 1 :JMPN(outOfCountersStep)
265
+
266
+ ; Compute B = (B^2) % M
267
+ modexp_out_to_div2:
268
+ $ => A :MLOAD(array_square_out + RR)
269
+ A :MSTORE(array_div_inA + RR)
270
+ RR - 1 => RR :JMPN(modexp_M_to_div2, modexp_out_to_div2)
271
+
272
+ modexp_M_to_div2:
273
+ $ => A :MLOAD(modexp_M + E)
274
+ A :MSTORE(array_div_inB + E)
275
+ E - 1 => E :JMPN(modexp_div_out_and_M1, modexp_M_to_div2)
276
+
277
+ modexp_div_out_and_M1:
278
+ :CALL(array_div)
279
+
280
+ %MAX_CNT_STEPS - STEP - 4 :JMPN(outOfCountersStep)
281
+
282
+ $ => C :MLOAD(array_div_len_rem)
283
+ C :MSTORE(modexp_Blen)
284
+ C - 1 => RR
285
+
286
+ %MAX_CNT_STEPS - STEP - 3*C - 2 :JMPN(outOfCountersStep)
287
+
288
+ modexp_rem_from_div3:
289
+ $ => A :MLOAD(array_div_rem + RR)
290
+ A :MSTORE(modexp_B + RR)
291
+ RR - 1 => RR :JMPN(modexp_pre_loop, modexp_rem_from_div3)
292
+ ; -------------------
293
+
294
+ modexp_end:
295
+ $ => RR :MLOAD(modexp_RR)
296
+ :RETURN
@@ -0,0 +1,230 @@
1
+ VAR GLOBAL tmpVarAmodexp
2
+ VAR GLOBAL tmpVarBmodexp
3
+ VAR GLOBAL tmpVarCmodexp
4
+ VAR GLOBAL tmpVarDmodexp
5
+ VAR GLOBAL offsetInitModexp
6
+ VAR GLOBAL tmpVarEmodexp
7
+ VAR GLOBAL tmpZkPCmodexp
8
+ VAR GLOBAL modExpArrayIndex
9
+
10
+ modexp_getBase:
11
+ %MAX_CNT_STEPS - STEP - 15 :JMPN(outOfCountersStep)
12
+
13
+ RR :MSTORE(tmpZkPCmodexp)
14
+ A :MSTORE(tmpVarAmodexp)
15
+ B :MSTORE(tmpVarBmodexp)
16
+ C :MSTORE(tmpVarCmodexp)
17
+ D :MSTORE(tmpVarDmodexp)
18
+ ; offset init
19
+ E :MSTORE(offsetInitModexp)
20
+ ;E = offset final
21
+ E + C => E
22
+ 0 :MSTORE(modExpArrayIndex)
23
+ 0 :MSTORE(modexp_Blen)
24
+ 32 :MSTORE(readXFromCalldataLength)
25
+
26
+ modexp_getBaseLoop:
27
+
28
+ %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary)
29
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
30
+ ; C length to read
31
+ C => A
32
+ 0 => B
33
+ ; if C (length) == 0 --> modexp_saveBaseLen
34
+ $ :EQ,JMPC(modexp_saveBaseLen)
35
+ 32 => B
36
+ ; if C (length) < 32 --> modexp_getBaseMloadX
37
+ $ :LT,JMPC(modexp_getBaseMloadX)
38
+ E - 32 => E
39
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
40
+ $ => A :MLOAD(readXFromCalldataResult)
41
+ C - 32 => C :JMP(modexp_getBaseMstore)
42
+
43
+ modexp_getBaseMloadX:
44
+ C :MSTORE(readXFromCalldataLength)
45
+ E - C => E
46
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
47
+ $ => A :MLOAD(readXFromCalldataResult)
48
+ 32 - C => D :CALL(SHRarith)
49
+ 0 => C
50
+
51
+ modexp_getBaseMstore:
52
+ ; mstore base at index E
53
+ E :MSTORE(tmpVarEmodexp)
54
+ $ => E :MLOAD(modExpArrayIndex)
55
+ A :MSTORE(modexp_B+E)
56
+ ; update modExpArrayIndex + 1
57
+ E + 1 => B :MSTORE(modExpArrayIndex)
58
+
59
+ modexp_getBaseFinal:
60
+ $ => E :MLOAD(tmpVarEmodexp),JMP(modexp_getBaseLoop)
61
+
62
+ modexp_saveBaseLen:
63
+
64
+ %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
65
+ %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
66
+
67
+ ; if modExpArrayIndex == 0 --> modexp_getReturn
68
+ $ => A :MLOAD(modExpArrayIndex)
69
+ 0 => B
70
+ $ :EQ,JMPC(modexp_getReturn)
71
+ ; update modExpArrayIndex = modExpArrayIndex - 1
72
+ A - 1 => E :MSTORE(modExpArrayIndex)
73
+ ; get value of the last index
74
+ $ => A :MLOAD(modexp_B + E)
75
+ ; if last value == 0 --> modexp_saveBaseLen
76
+ $ :EQ,JMPC(modexp_saveBaseLen)
77
+ ; else Blen == modExpArrayIndex + 1
78
+ E + 1 :MSTORE(modexp_Blen),JMP(modexp_getReturn)
79
+
80
+ modexp_getExp:
81
+
82
+ %MAX_CNT_STEPS - STEP - 15 :JMPN(outOfCountersStep)
83
+
84
+ RR :MSTORE(tmpZkPCmodexp)
85
+ A :MSTORE(tmpVarAmodexp)
86
+ B :MSTORE(tmpVarBmodexp)
87
+ C :MSTORE(tmpVarCmodexp)
88
+ D :MSTORE(tmpVarDmodexp)
89
+ ; offset init
90
+ E :MSTORE(offsetInitModexp)
91
+ ;E = offset final
92
+ E + C => E
93
+ 0 :MSTORE(modExpArrayIndex)
94
+ 0 :MSTORE(modexp_Elen)
95
+ 32 :MSTORE(readXFromCalldataLength)
96
+
97
+ modexp_getExpLoop:
98
+
99
+ %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary)
100
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
101
+ ; C length to read
102
+ C => A
103
+ 0 => B
104
+ ; if C (length) == 0 --> modexp_saveExpLen
105
+ $ :EQ,JMPC(modexp_saveExpLen)
106
+ 32 => B
107
+ ; if C (length) < 32 --> modexp_getExpMloadX
108
+ $ :LT,JMPC(modexp_getExpMloadX)
109
+ E - 32 => E
110
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
111
+ $ => A :MLOAD(readXFromCalldataResult)
112
+ C - 32 => C :JMP(modexp_getExpMstore)
113
+
114
+ modexp_getExpMloadX:
115
+ C :MSTORE(readXFromCalldataLength)
116
+ E - C => E
117
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
118
+ $ => A :MLOAD(readXFromCalldataResult)
119
+ 32 - C => D :CALL(SHRarith)
120
+ 0 => C
121
+
122
+ modexp_getExpMstore:
123
+ ; mstore exp at index E
124
+ E :MSTORE(tmpVarEmodexp)
125
+ $ => E :MLOAD(modExpArrayIndex)
126
+ A :MSTORE(modexp_E+E)
127
+ ; update modExpArrayIndex + 1
128
+ E + 1 => B :MSTORE(modExpArrayIndex)
129
+
130
+ modexp_getExpFinal:
131
+ $ => E :MLOAD(tmpVarEmodexp),JMP(modexp_getExpLoop)
132
+
133
+ modexp_saveExpLen:
134
+
135
+ %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
136
+ %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
137
+
138
+ ; if modExpArrayIndex == 0 --> modexp_getReturn
139
+ $ => A :MLOAD(modExpArrayIndex)
140
+ 0 => B
141
+ $ :EQ,JMPC(modexp_getReturn)
142
+ ; update modExpArrayIndex = modExpArrayIndex - 1
143
+ A - 1 => E :MSTORE(modExpArrayIndex)
144
+ ; get value of the last index
145
+ $ => A :MLOAD(modexp_E + E)
146
+ ; if last value == 0 --> modexp_saveExpLen
147
+ $ :EQ,JMPC(modexp_saveExpLen)
148
+ ; else Elen == modExpArrayIndex + 1
149
+ E + 1 :MSTORE(modexp_Elen),JMP(modexp_getReturn)
150
+
151
+ modexp_getMod:
152
+
153
+ %MAX_CNT_STEPS - STEP - 15 :JMPN(outOfCountersStep)
154
+
155
+ RR :MSTORE(tmpZkPCmodexp)
156
+ A :MSTORE(tmpVarAmodexp)
157
+ B :MSTORE(tmpVarBmodexp)
158
+ C :MSTORE(tmpVarCmodexp)
159
+ D :MSTORE(tmpVarDmodexp)
160
+ ; offset init
161
+ E :MSTORE(offsetInitModexp)
162
+ ;E = offset final
163
+ E + C => E
164
+ 0 :MSTORE(modExpArrayIndex)
165
+ 0 :MSTORE(modexp_Mlen)
166
+ 32 :MSTORE(readXFromCalldataLength)
167
+
168
+ modexp_getModLoop:
169
+
170
+ %MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary)
171
+ %MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
172
+ ; C length to read
173
+ C => A
174
+ 0 => B
175
+ ; if C (length) == 0 --> modexp_saveModLen
176
+ $ :EQ,JMPC(modexp_saveModLen)
177
+ 32 => B
178
+ ; if C (length) < 32 --> modexp_getModMloadX
179
+ $ :LT,JMPC(modexp_getModMloadX)
180
+ E - 32 => E
181
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
182
+ $ => A :MLOAD(readXFromCalldataResult)
183
+ C - 32 => C :JMP(modexp_getModMstore)
184
+
185
+ modexp_getModMloadX:
186
+ C :MSTORE(readXFromCalldataLength)
187
+ E - C => E
188
+ E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
189
+ $ => A :MLOAD(readXFromCalldataResult)
190
+ 32 - C => D :CALL(SHRarith)
191
+ 0 => C
192
+
193
+ modexp_getModMstore:
194
+ ; mstore mod at index E
195
+ E :MSTORE(tmpVarEmodexp)
196
+ $ => E :MLOAD(modExpArrayIndex)
197
+ A :MSTORE(modexp_M+E)
198
+ ; update modExpArrayIndex + 1
199
+ E + 1 => B :MSTORE(modExpArrayIndex)
200
+
201
+ modexp_getModFinal:
202
+ $ => E :MLOAD(tmpVarEmodexp),JMP(modexp_getModLoop)
203
+
204
+ modexp_saveModLen:
205
+
206
+ %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
207
+ %MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
208
+
209
+ ; if modExpArrayIndex == 0 --> modexp_getReturn
210
+ $ => A :MLOAD(modExpArrayIndex)
211
+ 0 => B
212
+ $ :EQ,JMPC(modexp_getReturn)
213
+ ; update modExpArrayIndex = modExpArrayIndex - 1
214
+ A - 1 => E :MSTORE(modExpArrayIndex)
215
+ ; get value of the last index
216
+ $ => A :MLOAD(modexp_M + E)
217
+ ; if last value == 0 --> modexp_saveModLen
218
+ $ :EQ,JMPC(modexp_saveModLen)
219
+ ; else Mlen == modExpArrayIndex + 1
220
+ E + 1 :MSTORE(modexp_Mlen),JMP(modexp_getReturn)
221
+
222
+ modexp_getReturn:
223
+ $ => RR :MLOAD(tmpZkPCmodexp)
224
+ $ => A :MLOAD(tmpVarAmodexp)
225
+ $ => B :MLOAD(tmpVarBmodexp)
226
+ $ => C :MLOAD(tmpVarCmodexp)
227
+ $ => D :MLOAD(tmpVarDmodexp)
228
+ $ => E :MLOAD(offsetInitModexp)
229
+ E + C => E
230
+ :RETURN