@aztec/simulator 0.0.1-commit.fce3e4f → 0.0.1-commit.ff7989d6c

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.
Files changed (331) hide show
  1. package/README.md +6 -4
  2. package/dest/common/errors.d.ts +8 -2
  3. package/dest/common/errors.d.ts.map +1 -1
  4. package/dest/private/acvm/acvm.d.ts +4 -2
  5. package/dest/private/acvm/acvm.d.ts.map +1 -1
  6. package/dest/private/acvm/acvm.js +4 -3
  7. package/dest/private/acvm/deserialize.d.ts +2 -2
  8. package/dest/private/acvm/deserialize.d.ts.map +1 -1
  9. package/dest/private/acvm/deserialize.js +1 -1
  10. package/dest/private/acvm/serialize.d.ts +2 -2
  11. package/dest/private/acvm/serialize.d.ts.map +1 -1
  12. package/dest/private/acvm/serialize.js +1 -1
  13. package/dest/private/acvm_native.d.ts +5 -3
  14. package/dest/private/acvm_native.d.ts.map +1 -1
  15. package/dest/private/acvm_native.js +8 -6
  16. package/dest/private/acvm_wasm.d.ts +4 -3
  17. package/dest/private/acvm_wasm.d.ts.map +1 -1
  18. package/dest/private/acvm_wasm.js +4 -4
  19. package/dest/private/circuit_recording/circuit_recorder.d.ts +4 -3
  20. package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
  21. package/dest/private/circuit_recording/circuit_recorder.js +21 -18
  22. package/dest/private/circuit_recording/file_circuit_recorder.d.ts +3 -2
  23. package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
  24. package/dest/private/circuit_recording/file_circuit_recorder.js +2 -2
  25. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +7 -2
  26. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
  27. package/dest/private/circuit_recording/memory_circuit_recorder.js +4 -4
  28. package/dest/private/factory.d.ts +3 -3
  29. package/dest/private/factory.d.ts.map +1 -1
  30. package/dest/private/factory.js +7 -4
  31. package/dest/public/avm/avm_context.d.ts +3 -3
  32. package/dest/public/avm/avm_context.d.ts.map +1 -1
  33. package/dest/public/avm/avm_contract_call_result.d.ts +6 -6
  34. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
  35. package/dest/public/avm/avm_contract_call_result.js +3 -3
  36. package/dest/public/avm/avm_execution_environment.d.ts +7 -6
  37. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  38. package/dest/public/avm/avm_execution_environment.js +1 -1
  39. package/dest/public/avm/avm_gas.d.ts +1 -1
  40. package/dest/public/avm/avm_gas.d.ts.map +1 -1
  41. package/dest/public/avm/avm_gas.js +3 -3
  42. package/dest/public/avm/avm_machine_state.d.ts +7 -6
  43. package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
  44. package/dest/public/avm/avm_machine_state.js +3 -2
  45. package/dest/public/avm/avm_memory_types.d.ts +2 -2
  46. package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
  47. package/dest/public/avm/avm_memory_types.js +4 -1
  48. package/dest/public/avm/avm_simulator.d.ts +4 -3
  49. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  50. package/dest/public/avm/avm_simulator.js +11 -9
  51. package/dest/public/avm/calldata.d.ts +51 -0
  52. package/dest/public/avm/calldata.d.ts.map +1 -0
  53. package/dest/public/avm/calldata.js +63 -0
  54. package/dest/public/avm/errors.d.ts +8 -2
  55. package/dest/public/avm/errors.d.ts.map +1 -1
  56. package/dest/public/avm/errors.js +14 -2
  57. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts +2 -0
  58. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts.map +1 -0
  59. package/dest/public/avm/fixtures/account_proof_fetcher.js +152 -0
  60. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
  61. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  62. package/dest/public/avm/fixtures/avm_simulation_tester.js +4 -3
  63. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +2 -2
  64. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
  65. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +1 -1
  66. package/dest/public/avm/fixtures/initializers.d.ts +2 -2
  67. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  68. package/dest/public/avm/fixtures/initializers.js +5 -4
  69. package/dest/public/avm/fixtures/utils.d.ts +4 -3
  70. package/dest/public/avm/fixtures/utils.d.ts.map +1 -1
  71. package/dest/public/avm/fixtures/utils.js +3 -2
  72. package/dest/public/avm/opcodes/accrued_substate.d.ts +17 -18
  73. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  74. package/dest/public/avm/opcodes/accrued_substate.js +42 -43
  75. package/dest/public/avm/opcodes/addressing_mode.js +2 -2
  76. package/dest/public/avm/opcodes/arithmetic.d.ts +10 -8
  77. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
  78. package/dest/public/avm/opcodes/arithmetic.js +12 -2
  79. package/dest/public/avm/opcodes/bitwise.d.ts +7 -7
  80. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -1
  81. package/dest/public/avm/opcodes/bitwise.js +5 -5
  82. package/dest/public/avm/opcodes/comparators.d.ts +4 -4
  83. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -1
  84. package/dest/public/avm/opcodes/comparators.js +1 -1
  85. package/dest/public/avm/opcodes/contract.d.ts +3 -3
  86. package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
  87. package/dest/public/avm/opcodes/contract.js +8 -8
  88. package/dest/public/avm/opcodes/control_flow.d.ts +3 -3
  89. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -1
  90. package/dest/public/avm/opcodes/control_flow.js +4 -4
  91. package/dest/public/avm/opcodes/conversion.d.ts +3 -3
  92. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
  93. package/dest/public/avm/opcodes/conversion.js +4 -4
  94. package/dest/public/avm/opcodes/ec_add.d.ts +4 -4
  95. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
  96. package/dest/public/avm/opcodes/ec_add.js +17 -8
  97. package/dest/public/avm/opcodes/environment_getters.d.ts +5 -5
  98. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
  99. package/dest/public/avm/opcodes/environment_getters.js +6 -6
  100. package/dest/public/avm/opcodes/external_calls.d.ts +9 -9
  101. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  102. package/dest/public/avm/opcodes/external_calls.js +23 -22
  103. package/dest/public/avm/opcodes/hashing.d.ts +8 -8
  104. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
  105. package/dest/public/avm/opcodes/hashing.js +21 -16
  106. package/dest/public/avm/opcodes/instruction_impl.d.ts +4 -4
  107. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -1
  108. package/dest/public/avm/opcodes/instruction_impl.js +4 -4
  109. package/dest/public/avm/opcodes/memory.d.ts +14 -14
  110. package/dest/public/avm/opcodes/memory.d.ts.map +1 -1
  111. package/dest/public/avm/opcodes/memory.js +26 -26
  112. package/dest/public/avm/opcodes/misc.d.ts +3 -3
  113. package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
  114. package/dest/public/avm/opcodes/misc.js +6 -6
  115. package/dest/public/avm/opcodes/storage.d.ts +16 -15
  116. package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
  117. package/dest/public/avm/opcodes/storage.js +34 -24
  118. package/dest/public/avm/revert_reason.d.ts +2 -2
  119. package/dest/public/avm/revert_reason.d.ts.map +1 -1
  120. package/dest/public/avm/revert_reason.js +3 -2
  121. package/dest/public/avm/serialization/bytecode_serialization.d.ts +1 -1
  122. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  123. package/dest/public/avm/serialization/bytecode_serialization.js +12 -9
  124. package/dest/public/avm/serialization/instruction_serialization.d.ts +2 -2
  125. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  126. package/dest/public/avm/serialization/instruction_serialization.js +2 -2
  127. package/dest/public/avm/test_utils.d.ts +2 -2
  128. package/dest/public/avm/test_utils.d.ts.map +1 -1
  129. package/dest/public/avm/test_utils.js +1 -1
  130. package/dest/public/contracts_db_checkpoint.d.ts +2 -2
  131. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
  132. package/dest/public/db_interfaces.d.ts +2 -2
  133. package/dest/public/db_interfaces.d.ts.map +1 -1
  134. package/dest/public/debug_fn_name.d.ts +16 -3
  135. package/dest/public/debug_fn_name.d.ts.map +1 -1
  136. package/dest/public/debug_fn_name.js +31 -3
  137. package/dest/public/executor_metrics.d.ts +1 -1
  138. package/dest/public/executor_metrics.d.ts.map +1 -1
  139. package/dest/public/executor_metrics.js +12 -33
  140. package/dest/public/fixtures/amm_test.js +4 -4
  141. package/dest/public/fixtures/bulk_test.d.ts +3 -3
  142. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  143. package/dest/public/fixtures/bulk_test.js +5 -69
  144. package/dest/public/fixtures/custom_bytecode_tester.d.ts +28 -6
  145. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
  146. package/dest/public/fixtures/custom_bytecode_tester.js +36 -12
  147. package/dest/public/fixtures/custom_bytecode_tests.d.ts +11 -8
  148. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
  149. package/dest/public/fixtures/custom_bytecode_tests.js +83 -18
  150. package/dest/public/fixtures/index.d.ts +4 -2
  151. package/dest/public/fixtures/index.d.ts.map +1 -1
  152. package/dest/public/fixtures/index.js +3 -1
  153. package/dest/public/fixtures/minimal_public_tx.d.ts +2 -7
  154. package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
  155. package/dest/public/fixtures/minimal_public_tx.js +4 -14
  156. package/dest/public/fixtures/opcode_spammer.d.ts +122 -0
  157. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -0
  158. package/dest/public/fixtures/opcode_spammer.js +1653 -0
  159. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +22 -3
  160. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  161. package/dest/public/fixtures/public_tx_simulation_tester.js +50 -13
  162. package/dest/public/fixtures/simple_contract_data_source.d.ts +5 -4
  163. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
  164. package/dest/public/fixtures/simple_contract_data_source.js +4 -4
  165. package/dest/public/fixtures/token_test.d.ts +6 -2
  166. package/dest/public/fixtures/token_test.d.ts.map +1 -1
  167. package/dest/public/fixtures/token_test.js +13 -11
  168. package/dest/public/fixtures/utils.d.ts +2 -2
  169. package/dest/public/fixtures/utils.d.ts.map +1 -1
  170. package/dest/public/fixtures/utils.js +8 -7
  171. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +60 -0
  172. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
  173. package/dest/public/fuzzing/avm_fuzzer_simulator.js +171 -0
  174. package/dest/public/fuzzing/avm_simulator_bin.d.ts +2 -0
  175. package/dest/public/fuzzing/avm_simulator_bin.d.ts.map +1 -0
  176. package/dest/public/fuzzing/avm_simulator_bin.js +118 -0
  177. package/dest/public/hinting_db_sources.d.ts +5 -3
  178. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  179. package/dest/public/hinting_db_sources.js +7 -2
  180. package/dest/public/index.d.ts +2 -2
  181. package/dest/public/index.d.ts.map +1 -1
  182. package/dest/public/index.js +1 -1
  183. package/dest/public/public_db_sources.d.ts +5 -4
  184. package/dest/public/public_db_sources.d.ts.map +1 -1
  185. package/dest/public/public_db_sources.js +5 -5
  186. package/dest/public/public_processor/guarded_merkle_tree.d.ts +4 -2
  187. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  188. package/dest/public/public_processor/guarded_merkle_tree.js +5 -0
  189. package/dest/public/public_processor/public_processor.d.ts +10 -7
  190. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  191. package/dest/public/public_processor/public_processor.js +454 -51
  192. package/dest/public/public_processor/public_processor_metrics.d.ts +2 -2
  193. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  194. package/dest/public/public_processor/public_processor_metrics.js +28 -45
  195. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +3 -14
  196. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  197. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +20 -55
  198. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +24 -37
  199. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
  200. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +67 -145
  201. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +42 -0
  202. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -0
  203. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +86 -0
  204. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +30 -0
  205. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -0
  206. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +171 -0
  207. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts +23 -0
  208. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts.map +1 -0
  209. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.js +52 -0
  210. package/dest/public/public_tx_simulator/factories.d.ts +14 -0
  211. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -0
  212. package/dest/public/public_tx_simulator/factories.js +28 -0
  213. package/dest/public/public_tx_simulator/index.d.ts +4 -1
  214. package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
  215. package/dest/public/public_tx_simulator/index.js +3 -0
  216. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +3 -3
  217. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  218. package/dest/public/public_tx_simulator/public_tx_context.d.ts +5 -4
  219. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  220. package/dest/public/public_tx_simulator/public_tx_context.js +9 -9
  221. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +8 -6
  222. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  223. package/dest/public/public_tx_simulator/public_tx_simulator.js +23 -12
  224. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +24 -1
  225. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -1
  226. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +2 -2
  227. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  228. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +395 -19
  229. package/dest/public/side_effect_trace.d.ts +6 -5
  230. package/dest/public/side_effect_trace.d.ts.map +1 -1
  231. package/dest/public/side_effect_trace.js +4 -4
  232. package/dest/public/side_effect_trace_interface.d.ts +2 -2
  233. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  234. package/dest/public/state_manager/nullifiers.d.ts +2 -2
  235. package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
  236. package/dest/public/state_manager/public_storage.d.ts +2 -2
  237. package/dest/public/state_manager/public_storage.d.ts.map +1 -1
  238. package/dest/public/state_manager/public_storage.js +1 -1
  239. package/dest/public/state_manager/state_manager.d.ts +16 -5
  240. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  241. package/dest/public/state_manager/state_manager.js +19 -9
  242. package/dest/public/test_executor_metrics.d.ts +3 -2
  243. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  244. package/dest/public/test_executor_metrics.js +2 -2
  245. package/package.json +20 -20
  246. package/src/common/errors.ts +1 -1
  247. package/src/private/acvm/acvm.ts +4 -3
  248. package/src/private/acvm/deserialize.ts +1 -1
  249. package/src/private/acvm/serialize.ts +1 -1
  250. package/src/private/acvm_native.ts +11 -5
  251. package/src/private/acvm_wasm.ts +7 -3
  252. package/src/private/circuit_recording/circuit_recorder.ts +22 -19
  253. package/src/private/circuit_recording/file_circuit_recorder.ts +7 -2
  254. package/src/private/circuit_recording/memory_circuit_recorder.ts +6 -4
  255. package/src/private/factory.ts +7 -4
  256. package/src/public/avm/avm_context.ts +2 -2
  257. package/src/public/avm/avm_contract_call_result.ts +8 -6
  258. package/src/public/avm/avm_execution_environment.ts +10 -5
  259. package/src/public/avm/avm_gas.ts +5 -5
  260. package/src/public/avm/avm_machine_state.ts +7 -6
  261. package/src/public/avm/avm_memory_types.ts +5 -1
  262. package/src/public/avm/avm_simulator.ts +16 -10
  263. package/src/public/avm/calldata.ts +100 -0
  264. package/src/public/avm/errors.ts +17 -3
  265. package/src/public/avm/fixtures/account_proof.json +553 -0
  266. package/src/public/avm/fixtures/account_proof_fetcher.ts +166 -0
  267. package/src/public/avm/fixtures/avm_simulation_tester.ts +9 -3
  268. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
  269. package/src/public/avm/fixtures/initializers.ts +5 -4
  270. package/src/public/avm/fixtures/utils.ts +3 -2
  271. package/src/public/avm/opcodes/accrued_substate.ts +31 -34
  272. package/src/public/avm/opcodes/addressing_mode.ts +2 -2
  273. package/src/public/avm/opcodes/arithmetic.ts +14 -2
  274. package/src/public/avm/opcodes/bitwise.ts +3 -3
  275. package/src/public/avm/opcodes/comparators.ts +1 -1
  276. package/src/public/avm/opcodes/contract.ts +4 -7
  277. package/src/public/avm/opcodes/control_flow.ts +2 -2
  278. package/src/public/avm/opcodes/conversion.ts +3 -3
  279. package/src/public/avm/opcodes/ec_add.ts +15 -6
  280. package/src/public/avm/opcodes/environment_getters.ts +7 -7
  281. package/src/public/avm/opcodes/external_calls.ts +17 -15
  282. package/src/public/avm/opcodes/hashing.ts +16 -10
  283. package/src/public/avm/opcodes/instruction_impl.ts +2 -2
  284. package/src/public/avm/opcodes/memory.ts +20 -20
  285. package/src/public/avm/opcodes/misc.ts +4 -4
  286. package/src/public/avm/opcodes/storage.ts +30 -22
  287. package/src/public/avm/revert_reason.ts +6 -3
  288. package/src/public/avm/serialization/bytecode_serialization.ts +19 -8
  289. package/src/public/avm/serialization/instruction_serialization.ts +2 -2
  290. package/src/public/avm/test_utils.ts +1 -1
  291. package/src/public/contracts_db_checkpoint.ts +1 -1
  292. package/src/public/db_interfaces.ts +1 -1
  293. package/src/public/debug_fn_name.ts +39 -5
  294. package/src/public/executor_metrics.ts +9 -33
  295. package/src/public/fixtures/amm_test.ts +4 -4
  296. package/src/public/fixtures/bulk_test.ts +9 -9
  297. package/src/public/fixtures/custom_bytecode_tester.ts +53 -19
  298. package/src/public/fixtures/custom_bytecode_tests.ts +111 -18
  299. package/src/public/fixtures/index.ts +7 -1
  300. package/src/public/fixtures/minimal_public_tx.ts +6 -15
  301. package/src/public/fixtures/opcode_spammer.ts +1717 -0
  302. package/src/public/fixtures/public_tx_simulation_tester.ts +69 -13
  303. package/src/public/fixtures/simple_contract_data_source.ts +8 -9
  304. package/src/public/fixtures/token_test.ts +18 -9
  305. package/src/public/fixtures/utils.ts +6 -7
  306. package/src/public/fuzzing/avm_fuzzer_simulator.ts +288 -0
  307. package/src/public/fuzzing/avm_simulator_bin.ts +175 -0
  308. package/src/public/hinting_db_sources.ts +8 -3
  309. package/src/public/index.ts +7 -1
  310. package/src/public/public_db_sources.ts +16 -6
  311. package/src/public/public_processor/guarded_merkle_tree.ts +6 -1
  312. package/src/public/public_processor/public_processor.ts +81 -45
  313. package/src/public/public_processor/public_processor_metrics.ts +16 -44
  314. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +25 -63
  315. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +83 -178
  316. package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +134 -0
  317. package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +239 -0
  318. package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +83 -0
  319. package/src/public/public_tx_simulator/factories.ts +43 -0
  320. package/src/public/public_tx_simulator/index.ts +3 -0
  321. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +1 -1
  322. package/src/public/public_tx_simulator/public_tx_context.ts +14 -7
  323. package/src/public/public_tx_simulator/public_tx_simulator.ts +42 -16
  324. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +23 -0
  325. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +1 -1
  326. package/src/public/side_effect_trace.ts +6 -3
  327. package/src/public/side_effect_trace_interface.ts +1 -1
  328. package/src/public/state_manager/nullifiers.ts +1 -1
  329. package/src/public/state_manager/public_storage.ts +1 -1
  330. package/src/public/state_manager/state_manager.ts +39 -23
  331. package/src/public/test_executor_metrics.ts +3 -3
@@ -0,0 +1,1653 @@
1
+ /**
2
+ * Opcode Spammer - A minimal, data-driven opcode spammer for AVM gas benchmarking.
3
+ *
4
+ * Design principles:
5
+ * 1. Data over code: Opcode behavior is configuration, not control flow
6
+ * 2. Derive, don't declare: Categories and strategies follow from the data
7
+ * 3. Maximize coverage: Fill bytecode to the limit for accurate gas measurement
8
+ * 4. Smallest wire format: Use _8 variants over _16 to fit more instructions per loop
9
+ * 5. Single file: Everything in one module
10
+ *
11
+ * ## Architecture
12
+ *
13
+ * ```
14
+ * ┌─────────────────────────────────────────────────────────────────┐
15
+ * │ SPAM_CONFIGS │
16
+ * │ Record<Opcode, SpamConfig[]> │
17
+ * │ │
18
+ * │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
19
+ * │ │ ADD_8 │ │ POSEIDON2 │ │EMITNULLIFIER│ ... │
20
+ * │ │ [7 configs] │ │ [1 config] │ │ [1 config] │ │
21
+ * │ │ (per type) │ │ │ │ (limit=63) │ │
22
+ * │ └─────────────┘ └─────────────┘ └─────────────┘ │
23
+ * └─────────────────────────────────────────────────────────────────┘
24
+ * │
25
+ * ▼
26
+ * ┌─────────────────────────────────────────────────────────────────┐
27
+ * │ getSpamConfigsPerOpcode() │
28
+ * │ Returns { opcodes, config[] } for test iteration │
29
+ * └─────────────────────────────────────────────────────────────────┘
30
+ * │
31
+ * ▼
32
+ * ┌─────────────────────────────────────────────────────────────────┐
33
+ * │ testOpcodeSpamCase() │
34
+ * │ Routes to appropriate bytecode generator & executes test │
35
+ * │ │
36
+ * │ config.limit === undefined? │
37
+ * │ YES → testStandardOpcodeSpam() │
38
+ * │ NO → testSideEffectOpcodeSpam() │
39
+ * └─────────────────────────────────────────────────────────────────┘
40
+ * ```
41
+ *
42
+ * ## Two Execution Strategies
43
+ *
44
+ * ### Strategy 1: Standard Opcodes (Gas-Limited)
45
+ *
46
+ * For opcodes without per-TX limits (arithmetic, comparisons, memory ops, etc.), we create a single contract with an infinite loop:
47
+ *
48
+ * ```
49
+ * ┌────────────────────────────────────────────────────────────────┐
50
+ * │ SINGLE CONTRACT │
51
+ * │ │
52
+ * │ ┌──────────────────────────────────────────────────────────┐ │
53
+ * │ │ SETUP PHASE │ │
54
+ * │ │ SET mem[0] = initial_value │ │
55
+ * │ │ SET mem[1] = operand │ │
56
+ * │ │ ... │ │
57
+ * │ └──────────────────────────────────────────────────────────┘ │
58
+ * │ │ │
59
+ * │ ▼ │
60
+ * │ ┌──────────────────────────────────────────────────────────┐ │
61
+ * │ │ LOOP (fills remaining bytecode space) ◄─────┐ │ │
62
+ * │ │ TARGET_OPCODE ─┐ │ │ │
63
+ * │ │ TARGET_OPCODE │ unrolled N times │ │ │
64
+ * │ │ TARGET_OPCODE │ (N = available_bytes / instr_size)│ │ │
65
+ * │ │ ... ─┘ │ │ │
66
+ * │ │ JUMP back ──────────────────────────────────────────┘ │ │
67
+ * │ └──────────────────────────────────────────────────────────┘ │
68
+ * │ │
69
+ * │ Executes until: OUT OF GAS │
70
+ * └────────────────────────────────────────────────────────────────┘
71
+ * ```
72
+ *
73
+ * **Bytecode Layout:**
74
+ * ```
75
+ * ┌─────────────────────────────────────────────────────────────────┐
76
+ * │ 0x00: SET instructions (setup) │
77
+ * │ ... │
78
+ * │ 0xNN: ┌─── LOOP START ◄──────────────────────────────────────┐ │
79
+ * │ │ TARGET_OPCODE │ │
80
+ * │ │ TARGET_OPCODE (unrolled to fill max bytecode size) │ │
81
+ * │ │ TARGET_OPCODE │ │
82
+ * │ │ ... │ │
83
+ * │ └─► JUMP 0xNN ─────────────────────────────────────────┘ │
84
+ * │ MAX_BYTECODE_BYTES │
85
+ * └─────────────────────────────────────────────────────────────────┘
86
+ * ```
87
+ *
88
+ * ### Strategy 2: Side-Effect Limited Opcodes (Nested Call Pattern)
89
+ *
90
+ * For opcodes with per-TX limits (EMITNOTEHASH, EMITNULLIFIER, SENDL2TOL1MSG, etc.), we use a two-contract pattern where the inner contract executes side effects up to the limit, then REVERTs to discard them:
91
+ *
92
+ * ```
93
+ * ┌─────────────────────────────────────────────────────────────────┐
94
+ * │ OUTER CONTRACT │
95
+ * │ │
96
+ * │ ┌───────────────────────────────────────────────────────────┐ │
97
+ * │ │ SETUP │ │
98
+ * │ │ CALLDATACOPY inner_address from calldata[0] │ │
99
+ * │ │ SET l2Gas = MAX_UINT32 │ │
100
+ * │ │ SET daGas = MAX_UINT32 │ │
101
+ * │ └───────────────────────────────────────────────────────────┘ │
102
+ * │ │ │
103
+ * │ ▼ │
104
+ * │ ┌───────────────────────────────────────────────────────────┐ │
105
+ * │ │ LOOP ◄────┐ │ │
106
+ * │ │ CALL inner_contract ──────────────────────┐ │ │ │
107
+ * │ │ JUMP back ─────────────────────────────────────────────┘ │ │
108
+ * │ └───────────────────────────────────────────────────────────┘ │
109
+ * │ │ │
110
+ * │ Executes until: OUT OF GAS │ │
111
+ * └───────────────────────────────────────────────│─────────────────┘
112
+ * │
113
+ * ▼
114
+ * ┌─────────────────────────────────────────────────────────────────┐
115
+ * │ INNER CONTRACT │
116
+ * │ │
117
+ * │ ┌───────────────────────────────────────────────────────────┐ │
118
+ * │ │ SETUP │ │
119
+ * │ │ SET initial values for side-effect opcode │ │
120
+ * │ └───────────────────────────────────────────────────────────┘ │
121
+ * │ │ │
122
+ * │ ▼ │
123
+ * │ ┌───────────────────────────────────────────────────────────┐ │
124
+ * │ │ BODY (unrolled, NOT a loop) │ │
125
+ * │ │ SIDE_EFFECT_OPCODE ─┐ │ │
126
+ * │ │ SIDE_EFFECT_OPCODE │ repeated `limit` times │ │
127
+ * │ │ SIDE_EFFECT_OPCODE │ (e.g., 64 for EMITNOTEHASH) │ │
128
+ * │ │ ... ─┘ │ │
129
+ * │ └───────────────────────────────────────────────────────────┘ │
130
+ * │ │ │
131
+ * │ ▼ │
132
+ * │ ┌───────────────────────────────────────────────────────────┐ │
133
+ * │ │ CLEANUP │ │
134
+ * │ │ REVERT (discards all side effects from this call) │ │
135
+ * │ └───────────────────────────────────────────────────────────┘ │
136
+ * │ │
137
+ * └─────────────────────────────────────────────────────────────────┘
138
+ * ```
139
+ *
140
+ * **Why this pattern?**
141
+ *
142
+ * Side-effect opcodes have per-TX limits:
143
+ * - `EMITNOTEHASH`: max 64 per TX
144
+ * - `EMITNULLIFIER`: max 63 per TX (one reserved for TX nullifier)
145
+ * - `SENDL2TOL1MSG`: max 8 per TX
146
+ * - `EMITPUBLICLOG`: limited by total log payload size
147
+ *
148
+ * By having the inner contract REVERT after emitting side effects, those effects are discarded, allowing the outer contract to call it again. This enables thousands of opcode executions per TX instead of just the limit.
149
+ *
150
+ */ import { FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_PUBLIC_LOG_SIZE_IN_FIELDS, PUBLIC_LOG_HEADER_LENGTH } from '@aztec/constants';
151
+ import { Grumpkin } from '@aztec/foundation/crypto/grumpkin';
152
+ import { randomBigInt } from '@aztec/foundation/crypto/random';
153
+ import { Fr } from '@aztec/foundation/curves/bn254';
154
+ import { PublicDataWrite } from '@aztec/stdlib/avm';
155
+ import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
156
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
157
+ import assert from 'assert';
158
+ import { Field, TaggedMemory, TypeTag, Uint1, Uint32, Uint64 } from '../avm/avm_memory_types.js';
159
+ import { Add, And, Call, CalldataCopy, Cast, DebugLog, Div, EcAdd, EmitNoteHash, EmitNullifier, EmitPublicLog, Eq, FieldDiv, GetContractInstance, GetEnvVar, InternalCall, InternalReturn, Jump, JumpI, KeccakF1600, L1ToL2MessageExists, Lt, Lte, Mov, Mul, Not, NoteHashExists, NullifierExists, Or, Poseidon2, Return, ReturndataCopy, ReturndataSize, Revert, SLoad, SStore, SendL2ToL1Message, Set, Sha256Compression, Shl, Shr, StaticCall, Sub, SuccessCopy, ToRadixBE, Xor } from '../avm/opcodes/index.js';
160
+ import { encodeToBytecode } from '../avm/serialization/bytecode_serialization.js';
161
+ import { Opcode } from '../avm/serialization/instruction_serialization.js';
162
+ import { deployCustomBytecode, executeCustomBytecode } from './custom_bytecode_tester.js';
163
+ // ============================================================================
164
+ // Constants
165
+ // ============================================================================
166
+ /**
167
+ * Constants for "warm" tree reads - these values are inserted into the trees
168
+ * before running the spammer so that existence checks can find them.
169
+ */ export const WARM_NOTE_HASH = new Fr(0xdeadbeefn);
170
+ export const WARM_L1_TO_L2_MSG = new Fr(0xcafebabedeadbeefn);
171
+ /** Warm nullifier constant - a pre-siloed nullifier value inserted directly into the tree */ export const WARM_SILOED_NULLIFIER = new Fr(0xdeadbeef0001n);
172
+ /** Warm storage constants - storage is inserted for the deployed contract's address */ export const WARM_STORAGE_SLOT = new Fr(0xdeadbeef0002n);
173
+ export const WARM_STORAGE_VALUE = new Fr(0xcafebabe0003n);
174
+ /**
175
+ * Leaf indices inserted to by insertWarmTreeEntries().
176
+ * Ideally we'd getTreeInfo and set dynamically, but that doesn't
177
+ * work easily with static spam configs, so we assume intial index 0.
178
+ */ export const WARM_NOTE_HASH_LEAF_INDEX = 0n;
179
+ export const WARM_L1_TO_L2_MSG_LEAF_INDEX = 0n;
180
+ /**
181
+ * Insert entries into the trees so that "warm" configs can find them with existence checks.
182
+ * Call this before running the opcode spammer to enable warm tree reads.
183
+ *
184
+ * Inserts:
185
+ * - Note hash into NOTE_HASH_TREE
186
+ * - L1 to L2 message into L1_TO_L2_MESSAGE_TREE
187
+ * - Siloed nullifier into NULLIFIER_TREE (for NULLIFIEREXISTS warm check)
188
+ * - Storage value into PUBLIC_DATA_TREE (for SLOAD warm check)
189
+ */ export async function insertWarmTreeEntries(merkleTrees, contractAddress) {
190
+ // Insert into note hash tree
191
+ await merkleTrees.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, [
192
+ WARM_NOTE_HASH
193
+ ]);
194
+ // Insert into L1 to L2 message tree
195
+ await merkleTrees.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [
196
+ WARM_L1_TO_L2_MSG
197
+ ]);
198
+ // Insert siloed nullifier into nullifier tree (already siloed - used directly by NULLIFIEREXISTS)
199
+ await merkleTrees.sequentialInsert(MerkleTreeId.NULLIFIER_TREE, [
200
+ WARM_SILOED_NULLIFIER.toBuffer()
201
+ ]);
202
+ // Insert storage value into public data tree
203
+ const leafSlot = await computePublicDataTreeLeafSlot(contractAddress, WARM_STORAGE_SLOT);
204
+ const publicDataWrite = new PublicDataWrite(leafSlot, WARM_STORAGE_VALUE);
205
+ await merkleTrees.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [
206
+ publicDataWrite.toBuffer()
207
+ ]);
208
+ }
209
+ /**
210
+ * Maximum bytecode size in bytes.
211
+ *
212
+ * Bytecode is encoded as fields using bufferAsFields():
213
+ * - 1 field for the byte length
214
+ * - ceil(byteLength / 31) fields for the data (31 bytes per field)
215
+ *
216
+ * So: 1 + ceil(byteLength / 31) <= MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS
217
+ * ceil(byteLength / 31) <= MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS - 1
218
+ * byteLength <= (MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS - 1) * 31
219
+ */ const BYTES_PER_FIELD = Fr.SIZE_IN_BYTES - 1; // 31 bytes of data per field
220
+ const MAX_BYTECODE_BYTES = (MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS - 1) * BYTES_PER_FIELD;
221
+ const JUMP_SIZE = encodeToBytecode([
222
+ new Jump(0)
223
+ ]).length; // JUMP_32
224
+ const INTERNALCALL_SIZE = encodeToBytecode([
225
+ new InternalCall(0)
226
+ ]).length;
227
+ // ============================================================================
228
+ // Type Variant Helpers (for generating multiple configs per opcode)
229
+ // ============================================================================
230
+ // Not using these sets directly because we want to control order
231
+ //const ALL_TAGS = Array.from(VALID_TAGS);
232
+ //const INT_TAGS = Array.from(INTEGRAL_TAGS);
233
+ // Ordered so that limiting #configs per opcode still tests max size (Field)
234
+ const ALL_TAGS = [
235
+ TypeTag.FIELD,
236
+ TypeTag.UINT1,
237
+ TypeTag.UINT8,
238
+ TypeTag.UINT16,
239
+ TypeTag.UINT32,
240
+ TypeTag.UINT64,
241
+ TypeTag.UINT128
242
+ ];
243
+ // ordered so that limiting #configs per opcode still tests max size
244
+ const INT_TAGS = [
245
+ TypeTag.UINT128,
246
+ TypeTag.UINT1,
247
+ TypeTag.UINT8,
248
+ TypeTag.UINT16,
249
+ TypeTag.UINT32,
250
+ TypeTag.UINT64
251
+ ];
252
+ /** Build from tag truncating - shorter name */ function withTag(v, tag) {
253
+ return TaggedMemory.buildFromTagTruncating(v, tag);
254
+ }
255
+ // ============================================================================
256
+ // Random Value Helpers (seeded via SEED env var for reproducibility)
257
+ // ============================================================================
258
+ /** Modulus (really just max+1) for each integer type tag */ const TAG_MODULI = {
259
+ [TypeTag.UINT1]: 2n,
260
+ [TypeTag.UINT8]: 256n,
261
+ [TypeTag.UINT16]: 65536n,
262
+ [TypeTag.UINT32]: 0x1_0000_0000n,
263
+ [TypeTag.UINT64]: 0x1_0000_0000_0000_0000n,
264
+ [TypeTag.UINT128]: 0x1_0000_0000_0000_0000_0000_0000_0000_0000n,
265
+ [TypeTag.FIELD]: Fr.MODULUS
266
+ };
267
+ /** Generate a random value with the given type tag. Uses SEED env var if set. */ function randomWithTag(tag) {
268
+ const modulus = TAG_MODULI[tag];
269
+ if (modulus === undefined) {
270
+ throw new Error(`Unsupported tag for random generation: ${TypeTag[tag]}`);
271
+ }
272
+ const value = randomBigInt(modulus);
273
+ return TaggedMemory.buildFromTagTruncating(value, tag);
274
+ }
275
+ /** Generate a random non-zero value with the given type tag (for division). */ function randomNonZeroWithTag(tag) {
276
+ const modulus = TAG_MODULI[tag];
277
+ if (modulus === undefined) {
278
+ throw new Error(`Unsupported tag for random generation: ${TypeTag[tag]}`);
279
+ }
280
+ // Generate random in range [1, max) by generating [0, max-1) and adding 1
281
+ const value = randomBigInt(modulus - 1n) + 1n;
282
+ return TaggedMemory.buildFromTagTruncating(value, tag);
283
+ }
284
+ /** Generate a random non-zero Field value (for field division). */ function randomNonZeroField() {
285
+ return new Field(randomBigInt(Fr.MODULUS - 1n) + 1n);
286
+ }
287
+ /** Reserved memory offsets for external call loop (used by CALL spam and side-effect opcodes) */ const CONST_0_OFFSET = 0; // Uint32(0)
288
+ const CONST_1_OFFSET = 1; // Uint32(1)
289
+ const CONST_MAX_U32_OFFSET = 2; // Uint32(MAX_U32)
290
+ const CALL_ADDR_OFFSET = 3; // copy addr from calldata to here, and then use this addr for CALL
291
+ const CALL_ARGS_OFFSET = CALL_ADDR_OFFSET; // address is the arg to send to CALL
292
+ const CALL_COPY_SIZE_OFFSET = CONST_1_OFFSET; // copy size = 1 (forward calldata[0])
293
+ const CALL_CALLDATA_INDEX_OFFSET = CONST_0_OFFSET; // calldata[0]
294
+ const CALL_L2_GAS_OFFSET = CONST_MAX_U32_OFFSET; // MAX_U32 gets capped to remaining gas by AVM
295
+ const CALL_DA_GAS_OFFSET = CONST_MAX_U32_OFFSET; // MAX_U32 gets capped to remaining gas by AVM
296
+ const CALL_ARGS_SIZE_OFFSET = CONST_1_OFFSET; // argsSize = 1 (forward calldata[0] - might contain contract address)
297
+ const MAX_U32 = 0xffffffffn;
298
+ /**
299
+ * A SpamConfig for to make external CALLs to an address specified in calldata[0].
300
+ */ export const EXTERNAL_CALL_CONFIG = {
301
+ setup: [
302
+ // calldata will contain 1 item: the external call address
303
+ {
304
+ offset: CONST_0_OFFSET,
305
+ value: new Uint32(0)
306
+ },
307
+ {
308
+ offset: CONST_1_OFFSET,
309
+ value: new Uint32(1)
310
+ },
311
+ {
312
+ offset: CONST_MAX_U32_OFFSET,
313
+ value: new Uint32(MAX_U32)
314
+ },
315
+ ()=>[
316
+ new CalldataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ CALL_COPY_SIZE_OFFSET, /*cdStartOffset=*/ CALL_CALLDATA_INDEX_OFFSET, /*dstOffset=*/ CALL_ADDR_OFFSET)
317
+ ]
318
+ ],
319
+ targetInstructions: ()=>[
320
+ new Call(/*addressing_mode=*/ 0, /*l2GasOffset=*/ CALL_L2_GAS_OFFSET, /*daGasOffset=*/ CALL_DA_GAS_OFFSET, /*addrOffset=*/ CALL_ADDR_OFFSET, /*argsSizeOffset=*/ CALL_ARGS_SIZE_OFFSET, /*argsOffset=*/ CALL_ARGS_OFFSET)
321
+ ],
322
+ addressAsCalldata: true
323
+ };
324
+ const STATIC_CALL_CONFIG = {
325
+ setup: [
326
+ // calldata will contain 1 item: the external call address
327
+ {
328
+ offset: CONST_0_OFFSET,
329
+ value: new Uint32(0)
330
+ },
331
+ {
332
+ offset: CONST_1_OFFSET,
333
+ value: new Uint32(1)
334
+ },
335
+ {
336
+ offset: CONST_MAX_U32_OFFSET,
337
+ value: new Uint32(MAX_U32)
338
+ },
339
+ ()=>[
340
+ new CalldataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ CALL_COPY_SIZE_OFFSET, /*cdStartOffset=*/ CALL_CALLDATA_INDEX_OFFSET, /*dstOffset=*/ CALL_ADDR_OFFSET)
341
+ ]
342
+ ],
343
+ targetInstructions: ()=>[
344
+ new StaticCall(/*addressing_mode=*/ 0, /*l2GasOffset=*/ CALL_L2_GAS_OFFSET, /*daGasOffset=*/ CALL_DA_GAS_OFFSET, /*addrOffset=*/ CALL_ADDR_OFFSET, /*argsSizeOffset=*/ CALL_ARGS_SIZE_OFFSET, /*argsOffset=*/ CALL_ARGS_OFFSET)
345
+ ],
346
+ addressAsCalldata: true
347
+ };
348
+ // ============================================================================
349
+ // Configuration Map
350
+ // ============================================================================
351
+ /**
352
+ * Opcode spammer configs for ~all opcodes.
353
+ * Each opcode maps to an array of configs (usually one, but can be multiple for type variants, etc.)
354
+ * Uses smallest wire format (_8) for maximum instruction density.
355
+ */ export const SPAM_CONFIGS = {
356
+ // ═══════════════════════════════════════════════════════════════════════════
357
+ // ARITHMETIC - Test with all type variants (random values, seeded via SEED env var)
358
+ // ═══════════════════════════════════════════════════════════════════════════
359
+ [Opcode.ADD_8]: ALL_TAGS.map((tag)=>({
360
+ label: TypeTag[tag],
361
+ setup: [
362
+ {
363
+ offset: 0,
364
+ value: randomWithTag(tag)
365
+ },
366
+ {
367
+ offset: 1,
368
+ value: randomWithTag(tag)
369
+ }
370
+ ],
371
+ targetInstructions: ()=>[
372
+ new Add(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.ADD_8, Add.wireFormat8)
373
+ ]
374
+ })),
375
+ [Opcode.SUB_8]: ALL_TAGS.map((tag)=>({
376
+ label: TypeTag[tag],
377
+ setup: [
378
+ {
379
+ offset: 0,
380
+ value: randomWithTag(tag)
381
+ },
382
+ {
383
+ offset: 1,
384
+ value: randomWithTag(tag)
385
+ }
386
+ ],
387
+ targetInstructions: ()=>[
388
+ new Sub(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.SUB_8, Sub.wireFormat8)
389
+ ]
390
+ })),
391
+ [Opcode.MUL_8]: ALL_TAGS.map((tag)=>({
392
+ label: TypeTag[tag],
393
+ setup: [
394
+ {
395
+ offset: 0,
396
+ value: randomWithTag(tag)
397
+ },
398
+ {
399
+ offset: 1,
400
+ value: randomWithTag(tag)
401
+ }
402
+ ],
403
+ targetInstructions: ()=>[
404
+ new Mul(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.MUL_8, Mul.wireFormat8)
405
+ ]
406
+ })),
407
+ // DIV doesn't support FIELD type
408
+ [Opcode.DIV_8]: INT_TAGS.map((tag)=>({
409
+ label: TypeTag[tag],
410
+ setup: [
411
+ {
412
+ offset: 0,
413
+ value: randomWithTag(tag)
414
+ },
415
+ {
416
+ offset: 1,
417
+ value: randomNonZeroWithTag(tag)
418
+ }
419
+ ],
420
+ targetInstructions: ()=>[
421
+ new Div(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.DIV_8, Div.wireFormat8)
422
+ ]
423
+ })),
424
+ // Field-only
425
+ [Opcode.FDIV_8]: [
426
+ {
427
+ setup: [
428
+ {
429
+ offset: 0,
430
+ value: new Field(Fr.random())
431
+ },
432
+ {
433
+ offset: 1,
434
+ value: randomNonZeroField()
435
+ }
436
+ ],
437
+ targetInstructions: ()=>[
438
+ new FieldDiv(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.FDIV_8, FieldDiv.wireFormat8)
439
+ ]
440
+ }
441
+ ],
442
+ // ═══════════════════════════════════════════════════════════════════════════
443
+ // COMPARATORS - Test with all type variants (random values)
444
+ // ═══════════════════════════════════════════════════════════════════════════
445
+ [Opcode.EQ_8]: ALL_TAGS.map((tag)=>({
446
+ label: TypeTag[tag],
447
+ setup: [
448
+ {
449
+ offset: 0,
450
+ value: randomWithTag(tag)
451
+ },
452
+ {
453
+ offset: 1,
454
+ value: randomWithTag(tag)
455
+ }
456
+ ],
457
+ targetInstructions: ()=>[
458
+ new Eq(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.EQ_8, Eq.wireFormat8)
459
+ ]
460
+ })),
461
+ [Opcode.LT_8]: ALL_TAGS.map((tag)=>({
462
+ label: TypeTag[tag],
463
+ setup: [
464
+ {
465
+ offset: 0,
466
+ value: randomWithTag(tag)
467
+ },
468
+ {
469
+ offset: 1,
470
+ value: randomWithTag(tag)
471
+ }
472
+ ],
473
+ targetInstructions: ()=>[
474
+ new Lt(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.LT_8, Lt.wireFormat8)
475
+ ]
476
+ })),
477
+ [Opcode.LTE_8]: ALL_TAGS.map((tag)=>({
478
+ label: TypeTag[tag],
479
+ setup: [
480
+ {
481
+ offset: 0,
482
+ value: randomWithTag(tag)
483
+ },
484
+ {
485
+ offset: 1,
486
+ value: randomWithTag(tag)
487
+ }
488
+ ],
489
+ targetInstructions: ()=>[
490
+ new Lte(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.LTE_8, Lte.wireFormat8)
491
+ ]
492
+ })),
493
+ // ═══════════════════════════════════════════════════════════════════════════
494
+ // BITWISE - Integer types only (no FIELD) (random values)
495
+ // ═══════════════════════════════════════════════════════════════════════════
496
+ [Opcode.AND_8]: INT_TAGS.map((tag)=>({
497
+ label: TypeTag[tag],
498
+ setup: [
499
+ {
500
+ offset: 0,
501
+ value: randomWithTag(tag)
502
+ },
503
+ {
504
+ offset: 1,
505
+ value: randomWithTag(tag)
506
+ }
507
+ ],
508
+ targetInstructions: ()=>[
509
+ new And(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.AND_8, And.wireFormat8)
510
+ ]
511
+ })),
512
+ [Opcode.OR_8]: INT_TAGS.map((tag)=>({
513
+ label: TypeTag[tag],
514
+ setup: [
515
+ {
516
+ offset: 0,
517
+ value: randomWithTag(tag)
518
+ },
519
+ {
520
+ offset: 1,
521
+ value: randomWithTag(tag)
522
+ }
523
+ ],
524
+ targetInstructions: ()=>[
525
+ new Or(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.OR_8, Or.wireFormat8)
526
+ ]
527
+ })),
528
+ [Opcode.XOR_8]: INT_TAGS.map((tag)=>({
529
+ label: TypeTag[tag],
530
+ setup: [
531
+ {
532
+ offset: 0,
533
+ value: randomWithTag(tag)
534
+ },
535
+ {
536
+ offset: 1,
537
+ value: randomWithTag(tag)
538
+ }
539
+ ],
540
+ targetInstructions: ()=>[
541
+ new Xor(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.XOR_8, Xor.wireFormat8)
542
+ ]
543
+ })),
544
+ [Opcode.NOT_8]: INT_TAGS.map((tag)=>({
545
+ label: TypeTag[tag],
546
+ setup: [
547
+ {
548
+ offset: 0,
549
+ value: randomWithTag(tag)
550
+ }
551
+ ],
552
+ targetInstructions: ()=>[
553
+ new Not(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*dstOffset=*/ 0).as(Opcode.NOT_8, Not.wireFormat8)
554
+ ]
555
+ })),
556
+ [Opcode.SHL_8]: INT_TAGS.map((tag)=>({
557
+ label: TypeTag[tag],
558
+ setup: [
559
+ {
560
+ offset: 0,
561
+ value: randomWithTag(tag)
562
+ },
563
+ {
564
+ offset: 1,
565
+ value: withTag(1n, tag)
566
+ }
567
+ ],
568
+ targetInstructions: ()=>[
569
+ new Shl(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.SHL_8, Shl.wireFormat8)
570
+ ]
571
+ })),
572
+ [Opcode.SHR_8]: INT_TAGS.map((tag)=>({
573
+ label: TypeTag[tag],
574
+ setup: [
575
+ {
576
+ offset: 0,
577
+ value: randomWithTag(tag)
578
+ },
579
+ {
580
+ offset: 1,
581
+ value: withTag(1n, tag)
582
+ }
583
+ ],
584
+ targetInstructions: ()=>[
585
+ new Shr(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.SHR_8, Shr.wireFormat8)
586
+ ]
587
+ })),
588
+ // ═══════════════════════════════════════════════════════════════════════════
589
+ // CAST / MOV - Test with all type variants (random values)
590
+ // ═══════════════════════════════════════════════════════════════════════════
591
+ [Opcode.CAST_8]: ALL_TAGS.map((tag)=>({
592
+ label: TypeTag[tag],
593
+ setup: [
594
+ {
595
+ offset: 0,
596
+ value: randomWithTag(tag)
597
+ }
598
+ ],
599
+ targetInstructions: ()=>[
600
+ new Cast(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*dstOffset=*/ 1, /*dstTag=*/ TypeTag.UINT32).as(Opcode.CAST_8, Cast.wireFormat8)
601
+ ]
602
+ })),
603
+ [Opcode.MOV_8]: ALL_TAGS.map((tag)=>({
604
+ label: TypeTag[tag],
605
+ setup: [
606
+ {
607
+ offset: 0,
608
+ value: randomWithTag(tag)
609
+ }
610
+ ],
611
+ targetInstructions: ()=>[
612
+ new Mov(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*dstOffset=*/ 1).as(Opcode.MOV_8, Mov.wireFormat8)
613
+ ]
614
+ })),
615
+ // ═══════════════════════════════════════════════════════════════════════════
616
+ // MEMORY - SET
617
+ // ═══════════════════════════════════════════════════════════════════════════
618
+ // Not testing all wire formats as they should be roughly the same in terms of simulation
619
+ // and proving time
620
+ //[Opcode.SET_8]: [
621
+ // {
622
+ // setup: [],
623
+ // targetInstructions: () => [new Set(0, 0, TypeTag.UINT8, 42).as(Opcode.SET_8, Set.wireFormat8)],
624
+ // },
625
+ //],
626
+ //[Opcode.SET_16]: [
627
+ // {
628
+ // setup: [],
629
+ // targetInstructions: () => [new Set(0, 0, TypeTag.UINT16, 4242).as(Opcode.SET_16, Set.wireFormat16)],
630
+ // },
631
+ //],
632
+ //[Opcode.SET_32]: [
633
+ // {
634
+ // setup: [],
635
+ // targetInstructions: () => [new Set(0, 0, TypeTag.UINT32, 424242).as(Opcode.SET_32, Set.wireFormat32)],
636
+ // },
637
+ //],
638
+ //[Opcode.SET_64]: [
639
+ // {
640
+ // setup: [],
641
+ // targetInstructions: () => [new Set(0, 0, TypeTag.UINT64, 42424242n).as(Opcode.SET_64, Set.wireFormat64)],
642
+ // },
643
+ //],
644
+ [Opcode.SET_128]: [
645
+ {
646
+ setup: [],
647
+ targetInstructions: ()=>[
648
+ new Set(/*addressing_mode=*/ 0, /*dstOffset=*/ 0, /*inTag=*/ TypeTag.UINT128, /*value=*/ 4242424242424242n).as(Opcode.SET_128, Set.wireFormat128)
649
+ ]
650
+ }
651
+ ],
652
+ //[Opcode.SET_FF]: [
653
+ // {
654
+ // setup: [],
655
+ // targetInstructions: () => [new Set(0, 0, TypeTag.FIELD, 42n).as(Opcode.SET_FF, Set.wireFormatFF)],
656
+ // },
657
+ //],
658
+ // ═══════════════════════════════════════════════════════════════════════════
659
+ // CONTROL FLOW
660
+ // ═══════════════════════════════════════════════════════════════════════════
661
+ [Opcode.JUMP_32]: [
662
+ {
663
+ setup: [],
664
+ // Target will be overwritten by loop builder
665
+ targetInstructions: ()=>[
666
+ new Jump(/*jumpOffset=*/ 0)
667
+ ]
668
+ }
669
+ ],
670
+ [Opcode.JUMPI_32]: [
671
+ {
672
+ setup: [
673
+ {
674
+ offset: 0,
675
+ value: new Uint1(0n)
676
+ }
677
+ ],
678
+ targetInstructions: ()=>[
679
+ new JumpI(/*addressing_mode=*/ 0, /*condOffset=*/ 0, /*loc=*/ 0)
680
+ ]
681
+ }
682
+ ],
683
+ // INTERNALCALL: calls itself infinitely by jumping to its own PC (PC 0, since no setup)
684
+ // Creates infinite recursion until OOG (internal call stack grows forever)
685
+ [Opcode.INTERNALCALL]: [
686
+ {
687
+ setup: [],
688
+ targetInstructions: ()=>[
689
+ new InternalCall(/*loc=*/ 0)
690
+ ]
691
+ }
692
+ ],
693
+ // INTERNALRETURN: needs INTERNALCALL to return without error
694
+ // Layout: INTERNALCALL(10) -> JUMP(0) -> INTERNALRETURN
695
+ // INTERNALCALL jumps to INTERNALRETURN, which returns to JUMP, which loops back
696
+ [Opcode.INTERNALRETURN]: [
697
+ {
698
+ setup: [],
699
+ targetInstructions: ()=>[
700
+ new InternalCall(/*loc=*/ INTERNALCALL_SIZE + JUMP_SIZE),
701
+ new Jump(/*jumpOffset=*/ 0),
702
+ new InternalReturn()
703
+ ]
704
+ }
705
+ ],
706
+ // CALL (EXTERNALCALL): calls the current contract address (self) in a loop
707
+ // Contract address is passed via calldata[0] and propagated to nested calls
708
+ [Opcode.CALL]: [
709
+ EXTERNAL_CALL_CONFIG
710
+ ],
711
+ [Opcode.STATICCALL]: [
712
+ STATIC_CALL_CONFIG
713
+ ],
714
+ // RETURN: terminates execution, so we need to use the two-contract pattern
715
+ // Outer contract CALLs inner contract in a loop, inner contract does RETURN
716
+ [Opcode.RETURN]: [
717
+ {
718
+ setup: [
719
+ {
720
+ offset: 0,
721
+ value: new Uint32(0)
722
+ }
723
+ ],
724
+ targetInstructions: ()=>[
725
+ new Return(/*addressing_mode=*/ 0, /*returnSizeOffset=*/ 0, /*returnOffset=*/ 0)
726
+ ],
727
+ // Use the side-effect-limit pattern (even though it's not a side-effect) as it fits
728
+ // this case (we want to CALL, RETURN, then CALL again back in parent). We omit "cleanup"
729
+ // because we don't need to REVERT as we do for real side-effects.
730
+ limit: 1
731
+ }
732
+ ],
733
+ // REVERT: terminates execution, so we need to use the two-contract pattern
734
+ // Outer contract CALLs inner contract in a loop, inner contract does REVERT
735
+ [Opcode.REVERT_8]: [
736
+ {
737
+ setup: [
738
+ {
739
+ offset: 0,
740
+ value: new Uint32(0)
741
+ }
742
+ ],
743
+ targetInstructions: ()=>[
744
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 0, /*returnOffset=*/ 1).as(Opcode.REVERT_8, Revert.wireFormat8)
745
+ ],
746
+ limit: 1
747
+ }
748
+ ],
749
+ // ═══════════════════════════════════════════════════════════════════════════
750
+ // ENVIRONMENT
751
+ // ═══════════════════════════════════════════════════════════════════════════
752
+ [Opcode.GETENVVAR_16]: [
753
+ {
754
+ setup: [],
755
+ targetInstructions: ()=>[
756
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 0, /*varEnum=*/ 0).as(Opcode.GETENVVAR_16, GetEnvVar.wireFormat16)
757
+ ]
758
+ }
759
+ ],
760
+ // CALLDATACOPY has dynamic gas scaling with copySize
761
+ [Opcode.CALLDATACOPY]: [
762
+ {
763
+ label: 'Min copy size',
764
+ // CalldataCopy with copySize=0 is a no-op but still executes the opcode
765
+ setup: [
766
+ {
767
+ offset: 0,
768
+ value: new Uint32(0n)
769
+ },
770
+ {
771
+ offset: 1,
772
+ value: new Uint32(0n)
773
+ }
774
+ ],
775
+ targetInstructions: ()=>[
776
+ new CalldataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*cdStartOffset=*/ 1, /*dstOffset=*/ 2)
777
+ ]
778
+ },
779
+ {
780
+ label: 'Large copy size',
781
+ // Large copySize with large dynamic gas - will OOG quickly
782
+ // NOTE: we don't want it so large that it exceeds memory bounds (MAX_MEMORY_SIZE = 2^32)
783
+ // and we really want it small enough that we run at least 1 successful target opcode.
784
+ setup: [
785
+ {
786
+ offset: 0,
787
+ value: new Uint32(1000n)
788
+ },
789
+ {
790
+ offset: 1,
791
+ value: new Uint32(0n)
792
+ }
793
+ ],
794
+ targetInstructions: ()=>[
795
+ new CalldataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*cdStartOffset=*/ 1, /*dstOffset=*/ 2)
796
+ ]
797
+ },
798
+ {
799
+ label: 'Near min copy size of 1',
800
+ // Near-min but actually copies data (more meaningful than size=0 no-op)
801
+ setup: [
802
+ {
803
+ offset: 0,
804
+ value: new Uint32(1n)
805
+ },
806
+ {
807
+ offset: 1,
808
+ value: new Uint32(0n)
809
+ }
810
+ ],
811
+ targetInstructions: ()=>[
812
+ new CalldataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*cdStartOffset=*/ 1, /*dstOffset=*/ 2)
813
+ ]
814
+ }
815
+ ],
816
+ [Opcode.SUCCESSCOPY]: [
817
+ {
818
+ setup: [],
819
+ targetInstructions: ()=>[
820
+ new SuccessCopy(/*addressing_mode=*/ 0, /*dstOffset=*/ 0)
821
+ ]
822
+ }
823
+ ],
824
+ [Opcode.RETURNDATASIZE]: [
825
+ {
826
+ setup: [],
827
+ targetInstructions: ()=>[
828
+ new ReturndataSize(/*addressing_mode=*/ 0, /*dstOffset=*/ 0)
829
+ ]
830
+ }
831
+ ],
832
+ // RETURNDATACOPY has dynamic gas scaling with copySize
833
+ [Opcode.RETURNDATACOPY]: [
834
+ {
835
+ label: 'Min copy size',
836
+ setup: [
837
+ {
838
+ offset: 0,
839
+ value: new Uint32(0n)
840
+ },
841
+ {
842
+ offset: 1,
843
+ value: new Uint32(0n)
844
+ }
845
+ ],
846
+ targetInstructions: ()=>[
847
+ new ReturndataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*rdStartOffset=*/ 1, /*dstOffset=*/ 2)
848
+ ]
849
+ },
850
+ {
851
+ label: 'Large copy size',
852
+ // Large copySize to maximize dynamic gas - will OOG quickly
853
+ // NOTE: we don't want it so large that it exceeds memory bounds (MAX_MEMORY_SIZE = 2^32)
854
+ // and we really want it small enough that we run at least 1 successful target opcode.
855
+ setup: [
856
+ {
857
+ offset: 0,
858
+ value: new Uint32(1000n)
859
+ },
860
+ {
861
+ offset: 1,
862
+ value: new Uint32(0n)
863
+ }
864
+ ],
865
+ targetInstructions: ()=>[
866
+ new ReturndataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*rdStartOffset=*/ 1, /*dstOffset=*/ 2)
867
+ ]
868
+ },
869
+ {
870
+ label: 'Near min copy size of 1',
871
+ // Near-min but actually copies data (more meaningful than size=0 no-op)
872
+ setup: [
873
+ {
874
+ offset: 0,
875
+ value: new Uint32(1n)
876
+ },
877
+ {
878
+ offset: 1,
879
+ value: new Uint32(0n)
880
+ }
881
+ ],
882
+ targetInstructions: ()=>[
883
+ new ReturndataCopy(/*addressing_mode=*/ 0, /*copySizeOffset=*/ 0, /*rdStartOffset=*/ 1, /*dstOffset=*/ 2)
884
+ ]
885
+ }
886
+ ],
887
+ // ═══════════════════════════════════════════════════════════════════════════
888
+ // WORLD STATE READS
889
+ // ═══════════════════════════════════════════════════════════════════════════
890
+ [Opcode.SLOAD]: [
891
+ {
892
+ label: 'Cold read (slot not written)',
893
+ setup: [
894
+ {
895
+ offset: 0,
896
+ value: new Field(Fr.random())
897
+ },
898
+ ()=>[
899
+ // Get current contract address into offset 1
900
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 1, /*varEnum=*/ 0).as(Opcode.GETENVVAR_16, GetEnvVar.wireFormat16)
901
+ ]
902
+ ],
903
+ targetInstructions: ()=>[
904
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 1, /*dstOffset=*/ 2)
905
+ ]
906
+ },
907
+ {
908
+ label: 'Warm read (from tree)',
909
+ // Uses pre-inserted storage from insertWarmTreeEntries() which is called after contract deployment
910
+ setup: [
911
+ {
912
+ offset: 0,
913
+ value: new Field(WARM_STORAGE_SLOT)
914
+ },
915
+ ()=>[
916
+ // Get current contract address into offset 1
917
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 1, /*varEnum=*/ 0).as(Opcode.GETENVVAR_16, GetEnvVar.wireFormat16)
918
+ ]
919
+ ],
920
+ targetInstructions: ()=>[
921
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 1, /*dstOffset=*/ 2)
922
+ ]
923
+ },
924
+ {
925
+ label: 'Warm read (SSTORE first, unique slot per SLOAD)',
926
+ // Memory layout: slot (incremented), value, constant 1, contract address (from GETENVVAR), revertSize, loaded value
927
+ setup: [
928
+ {
929
+ offset: 0,
930
+ value: new Field(Fr.random())
931
+ },
932
+ {
933
+ offset: 1,
934
+ value: new Field(Fr.random())
935
+ },
936
+ {
937
+ offset: 2,
938
+ value: new Field(1n)
939
+ },
940
+ ()=>[
941
+ // Get current contract address into offset 3
942
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 3, /*varEnum=*/ 0).as(Opcode.GETENVVAR_16, GetEnvVar.wireFormat16)
943
+ ],
944
+ {
945
+ offset: 4,
946
+ value: new Uint32(0n)
947
+ }
948
+ ],
949
+ targetInstructions: ()=>[
950
+ new SStore(/*addressing_mode=*/ 0, /*srcOffset=*/ 1, /*slotOffset=*/ 0),
951
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 3, /*dstOffset=*/ 5),
952
+ new Add(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 0).as(Opcode.ADD_8, Add.wireFormat8)
953
+ ],
954
+ cleanupInstructions: ()=>[
955
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 4, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
956
+ ],
957
+ limit: MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
958
+ }
959
+ ],
960
+ [Opcode.NOTEHASHEXISTS]: [
961
+ {
962
+ label: 'Cold (non-existent)',
963
+ // Note: Can't easily do "write first" version - would need to know the leaf index
964
+ // that EMITNOTEHASH will produce, which depends on tree state
965
+ setup: [
966
+ {
967
+ offset: 0,
968
+ value: new Field(Fr.random())
969
+ },
970
+ {
971
+ offset: 1,
972
+ value: randomWithTag(TypeTag.UINT64)
973
+ }
974
+ ],
975
+ targetInstructions: ()=>[
976
+ new NoteHashExists(/*addressing_mode=*/ 0, /*noteHashOffset=*/ 0, /*leafIndexOffset=*/ 1, /*existsOffset=*/ 2)
977
+ ]
978
+ },
979
+ {
980
+ label: 'Warm (exists in tree)',
981
+ // Uses pre-inserted note hash from insertWarmTreeEntries()
982
+ setup: [
983
+ {
984
+ offset: 0,
985
+ value: new Field(WARM_NOTE_HASH)
986
+ },
987
+ {
988
+ offset: 1,
989
+ value: new Uint64(WARM_NOTE_HASH_LEAF_INDEX)
990
+ }
991
+ ],
992
+ targetInstructions: ()=>[
993
+ new NoteHashExists(/*addressing_mode=*/ 0, /*noteHashOffset=*/ 0, /*leafIndexOffset=*/ 1, /*existsOffset=*/ 2)
994
+ ]
995
+ }
996
+ ],
997
+ [Opcode.NULLIFIEREXISTS]: [
998
+ {
999
+ label: 'Non-existent nullifier',
1000
+ // NULLIFIEREXISTS now takes a siloed nullifier directly (no address parameter)
1001
+ setup: [
1002
+ {
1003
+ offset: 0,
1004
+ value: new Field(Fr.random())
1005
+ }
1006
+ ],
1007
+ targetInstructions: ()=>[
1008
+ new NullifierExists(/*addressing_mode=*/ 0, /*siloedNullifierOffset=*/ 0, /*existsOffset=*/ 1)
1009
+ ]
1010
+ },
1011
+ {
1012
+ label: 'Existing nullifier (warm - from tree)',
1013
+ // Uses pre-inserted siloed nullifier from insertWarmTreeEntries()
1014
+ // NULLIFIEREXISTS now takes a siloed nullifier directly
1015
+ setup: [
1016
+ {
1017
+ offset: 0,
1018
+ value: new Field(WARM_SILOED_NULLIFIER)
1019
+ }
1020
+ ],
1021
+ targetInstructions: ()=>[
1022
+ new NullifierExists(/*addressing_mode=*/ 0, /*siloedNullifierOffset=*/ 0, /*existsOffset=*/ 1)
1023
+ ]
1024
+ }
1025
+ ],
1026
+ [Opcode.L1TOL2MSGEXISTS]: [
1027
+ {
1028
+ label: 'Cold (non-existent)',
1029
+ setup: [
1030
+ {
1031
+ offset: 0,
1032
+ value: new Field(Fr.random())
1033
+ },
1034
+ {
1035
+ offset: 1,
1036
+ value: randomWithTag(TypeTag.UINT64)
1037
+ }
1038
+ ],
1039
+ targetInstructions: ()=>[
1040
+ new L1ToL2MessageExists(/*addressing_mode=*/ 0, /*msgHashOffset=*/ 0, /*msgLeafIndexOffset=*/ 1, /*existsOffset=*/ 2)
1041
+ ]
1042
+ },
1043
+ {
1044
+ label: 'Warm (exists in tree)',
1045
+ // Uses pre-inserted L1 to L2 message from insertWarmTreeEntries()
1046
+ setup: [
1047
+ {
1048
+ offset: 0,
1049
+ value: new Field(WARM_L1_TO_L2_MSG)
1050
+ },
1051
+ {
1052
+ offset: 1,
1053
+ value: new Uint64(WARM_L1_TO_L2_MSG_LEAF_INDEX)
1054
+ }
1055
+ ],
1056
+ targetInstructions: ()=>[
1057
+ new L1ToL2MessageExists(/*addressing_mode=*/ 0, /*msgHashOffset=*/ 0, /*msgLeafIndexOffset=*/ 1, /*existsOffset=*/ 2)
1058
+ ]
1059
+ }
1060
+ ],
1061
+ [Opcode.GETCONTRACTINSTANCE]: [
1062
+ {
1063
+ // Use GETENVVAR to get current contract address (varEnum 0 = ADDRESS)
1064
+ // This ensures we're querying a valid deployed contract
1065
+ setup: [
1066
+ ()=>[
1067
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 0, /*varEnum=*/ 0).as(Opcode.GETENVVAR_16, GetEnvVar.wireFormat16)
1068
+ ]
1069
+ ],
1070
+ // memberEnum 0 = DEPLOYER
1071
+ targetInstructions: ()=>[
1072
+ new GetContractInstance(/*addressing_mode=*/ 0, /*addressOffset=*/ 0, /*dstOffset=*/ 1, /*memberEnum=*/ 0)
1073
+ ]
1074
+ }
1075
+ ],
1076
+ // ═══════════════════════════════════════════════════════════════════════════
1077
+ // SIDE-EFFECT LIMITED (have per-TX limit, use nested call pattern)
1078
+ // ═══════════════════════════════════════════════════════════════════════════
1079
+ [Opcode.EMITNOTEHASH]: [
1080
+ {
1081
+ setup: [
1082
+ {
1083
+ offset: 0,
1084
+ value: new Field(Fr.random())
1085
+ },
1086
+ {
1087
+ offset: 1,
1088
+ value: new Uint32(0n)
1089
+ }
1090
+ ],
1091
+ targetInstructions: ()=>[
1092
+ new EmitNoteHash(/*addressing_mode=*/ 0, /*noteHashOffset=*/ 0)
1093
+ ],
1094
+ cleanupInstructions: ()=>[
1095
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 1, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1096
+ ],
1097
+ limit: MAX_NOTE_HASHES_PER_TX
1098
+ }
1099
+ ],
1100
+ [Opcode.EMITNULLIFIER]: [
1101
+ {
1102
+ // Nullifiers must be unique - increment value after each emit
1103
+ // Memory layout: offset 0 = nullifier value, offset 1 = constant 1 for incrementing
1104
+ setup: [
1105
+ {
1106
+ offset: 0,
1107
+ value: new Field(Fr.random())
1108
+ },
1109
+ {
1110
+ offset: 1,
1111
+ value: new Field(1n)
1112
+ },
1113
+ {
1114
+ offset: 2,
1115
+ value: new Uint32(0n)
1116
+ }
1117
+ ],
1118
+ targetInstructions: ()=>[
1119
+ new EmitNullifier(/*addressing_mode=*/ 0, /*nullifierOffset=*/ 0),
1120
+ new Add(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 0).as(Opcode.ADD_8, Add.wireFormat8)
1121
+ ],
1122
+ cleanupInstructions: ()=>[
1123
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 2, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1124
+ ],
1125
+ limit: MAX_NULLIFIERS_PER_TX - 1
1126
+ }
1127
+ ],
1128
+ [Opcode.SENDL2TOL1MSG]: [
1129
+ {
1130
+ setup: [
1131
+ {
1132
+ offset: 0,
1133
+ value: new Field(Fr.random())
1134
+ },
1135
+ {
1136
+ offset: 1,
1137
+ value: new Field(Fr.random())
1138
+ },
1139
+ {
1140
+ offset: 2,
1141
+ value: new Uint32(0n)
1142
+ }
1143
+ ],
1144
+ targetInstructions: ()=>[
1145
+ new SendL2ToL1Message(/*addressing_mode=*/ 0, /*recipientOffset=*/ 0, /*contentOffset=*/ 1)
1146
+ ],
1147
+ cleanupInstructions: ()=>[
1148
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 2, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1149
+ ],
1150
+ limit: MAX_L2_TO_L1_MSGS_PER_TX
1151
+ }
1152
+ ],
1153
+ // SSTORE has two modes:
1154
+ // 1. Same slot: Writing to the same slot repeatedly has no per-TX limit - it just overwrites.
1155
+ // 2. Unique slots: Writing to unique slots is limited by MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX.
1156
+ [Opcode.SSTORE]: [
1157
+ {
1158
+ label: 'Same slot (no limit)',
1159
+ setup: [
1160
+ {
1161
+ offset: 0,
1162
+ value: new Field(Fr.random())
1163
+ },
1164
+ {
1165
+ offset: 1,
1166
+ value: new Field(Fr.random())
1167
+ },
1168
+ {
1169
+ offset: 2,
1170
+ value: new Uint32(0n)
1171
+ }
1172
+ ],
1173
+ targetInstructions: ()=>[
1174
+ new SStore(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*slotOffset=*/ 1)
1175
+ ],
1176
+ cleanupInstructions: ()=>[
1177
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 2, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1178
+ ]
1179
+ },
1180
+ {
1181
+ label: 'Unique slots (side-effect limited)',
1182
+ setup: [
1183
+ {
1184
+ offset: 0,
1185
+ value: new Field(Fr.random())
1186
+ },
1187
+ {
1188
+ offset: 1,
1189
+ value: new Field(Fr.random())
1190
+ },
1191
+ {
1192
+ offset: 2,
1193
+ value: new Field(1n)
1194
+ },
1195
+ {
1196
+ offset: 3,
1197
+ value: new Uint32(0n)
1198
+ }
1199
+ ],
1200
+ targetInstructions: ()=>[
1201
+ new SStore(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*slotOffset=*/ 1),
1202
+ new Add(/*addressing_mode=*/ 0, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 1).as(Opcode.ADD_8, Add.wireFormat8)
1203
+ ],
1204
+ cleanupInstructions: ()=>[
1205
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 3, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1206
+ ],
1207
+ limit: MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
1208
+ }
1209
+ ],
1210
+ // EMITPUBLICLOG - two configs: minimal (many small logs) and max-size (one large log)
1211
+ [Opcode.EMITPUBLICLOG]: [
1212
+ {
1213
+ label: 'Many empty logs, revert, repeat',
1214
+ setup: [
1215
+ {
1216
+ offset: 0,
1217
+ value: new Uint32(0n)
1218
+ },
1219
+ {
1220
+ offset: 1,
1221
+ value: new Uint32(0n)
1222
+ }
1223
+ ],
1224
+ targetInstructions: ()=>[
1225
+ new EmitPublicLog(/*addressing_mode=*/ 0, /*logSizeOffset=*/ 0, /*logOffset=*/ 1)
1226
+ ],
1227
+ cleanupInstructions: ()=>[
1228
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 1, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1229
+ ],
1230
+ // Max logs with 0-field content: floor(4096 / 2) = 2048
1231
+ limit: Math.floor(FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH / PUBLIC_LOG_HEADER_LENGTH)
1232
+ },
1233
+ {
1234
+ label: 'One max size log, revert, repeat',
1235
+ setup: [
1236
+ // logSize = MAX_PUBLIC_LOG_SIZE_IN_FIELDS
1237
+ {
1238
+ offset: 0,
1239
+ value: new Uint32(BigInt(MAX_PUBLIC_LOG_SIZE_IN_FIELDS))
1240
+ },
1241
+ {
1242
+ offset: 1,
1243
+ value: new Uint32(0n)
1244
+ }
1245
+ ],
1246
+ targetInstructions: ()=>[
1247
+ new EmitPublicLog(/*addressing_mode=*/ 0, /*logSizeOffset=*/ 0, /*logOffset=*/ 2)
1248
+ ],
1249
+ cleanupInstructions: ()=>[
1250
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 1, /*returnOffset=*/ 0).as(Opcode.REVERT_8, Revert.wireFormat8)
1251
+ ],
1252
+ limit: 1
1253
+ }
1254
+ ],
1255
+ // ═══════════════════════════════════════════════════════════════════════════
1256
+ // GADGETS - Random inputs (seeded via SEED env var)
1257
+ // ═══════════════════════════════════════════════════════════════════════════
1258
+ [Opcode.POSEIDON2]: [
1259
+ {
1260
+ // Poseidon2 takes 4 field elements as input
1261
+ setup: Array.from({
1262
+ length: 4
1263
+ }, (_, i)=>({
1264
+ offset: i,
1265
+ value: new Field(Fr.random())
1266
+ })),
1267
+ // Poseidon hash data at M[0..3], write result to M[0:3] (reuse results as next inputs)
1268
+ targetInstructions: ()=>[
1269
+ new Poseidon2(/*addressing_mode=*/ 0, /*inputStateOffset=*/ 0, /*outputStateOffset=*/ 0)
1270
+ ]
1271
+ }
1272
+ ],
1273
+ [Opcode.SHA256COMPRESSION]: [
1274
+ {
1275
+ setup: [
1276
+ // State: 8 x UINT32 at offsets 0-7 (random initial state)
1277
+ ...Array.from({
1278
+ length: 8
1279
+ }, (_, i)=>({
1280
+ offset: i,
1281
+ value: randomWithTag(TypeTag.UINT32)
1282
+ })),
1283
+ // Inputs: 16 x UINT32 at offsets 8-23 (random message block)
1284
+ ...Array.from({
1285
+ length: 16
1286
+ }, (_, i)=>({
1287
+ offset: 8 + i,
1288
+ value: randomWithTag(TypeTag.UINT32)
1289
+ }))
1290
+ ],
1291
+ targetInstructions: ()=>[
1292
+ new Sha256Compression(/*addressing_mode=*/ 0, /*outputOffset=*/ 0, /*stateOffset=*/ 0, /*inputsOffset=*/ 8)
1293
+ ]
1294
+ }
1295
+ ],
1296
+ [Opcode.KECCAKF1600]: [
1297
+ {
1298
+ // Keccak state: 25 x UINT64 (5x5 lane array) with random values
1299
+ setup: Array.from({
1300
+ length: 25
1301
+ }, (_, i)=>({
1302
+ offset: i,
1303
+ value: randomWithTag(TypeTag.UINT64)
1304
+ })),
1305
+ targetInstructions: ()=>[
1306
+ new KeccakF1600(/*addressing_mode=*/ 0, /*dstOffset=*/ 0, /*inputOffset=*/ 0)
1307
+ ]
1308
+ }
1309
+ ],
1310
+ [Opcode.ECADD]: [
1311
+ {
1312
+ // Use the Grumpkin generator point G for both points (valid curve point)
1313
+ setup: [
1314
+ {
1315
+ offset: 0,
1316
+ value: new Field(Grumpkin.generator.x)
1317
+ },
1318
+ {
1319
+ offset: 1,
1320
+ value: new Field(Grumpkin.generator.y)
1321
+ },
1322
+ {
1323
+ offset: 2,
1324
+ value: new Uint1(0n)
1325
+ },
1326
+ {
1327
+ offset: 3,
1328
+ value: new Field(Grumpkin.generator.x)
1329
+ },
1330
+ {
1331
+ offset: 4,
1332
+ value: new Field(Grumpkin.generator.y)
1333
+ },
1334
+ {
1335
+ offset: 5,
1336
+ value: new Uint1(0n)
1337
+ }
1338
+ ],
1339
+ targetInstructions: ()=>[
1340
+ new EcAdd(/*addressing_mode=*/ 0, /*p1XOffset=*/ 0, /*p1YOffset=*/ 1, /*p1IsInfiniteOffset=*/ 2, /*p2XOffset=*/ 3, /*p2YOffset=*/ 4, /*p2IsInfiniteOffset=*/ 5, /*dstOffset=*/ 0)
1341
+ ]
1342
+ }
1343
+ ],
1344
+ // TORADIXBE has dynamic gas scaling with numLimbs
1345
+ [Opcode.TORADIXBE]: [
1346
+ {
1347
+ label: 'Min limbs',
1348
+ setup: [
1349
+ {
1350
+ offset: 0,
1351
+ value: new Field(1n)
1352
+ },
1353
+ {
1354
+ offset: 1,
1355
+ value: new Uint32(2n)
1356
+ },
1357
+ {
1358
+ offset: 2,
1359
+ value: new Uint32(1n)
1360
+ },
1361
+ {
1362
+ offset: 3,
1363
+ value: new Uint1(0n)
1364
+ }
1365
+ ],
1366
+ targetInstructions: ()=>[
1367
+ new ToRadixBE(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*radixOffset=*/ 1, /*numLimbsOffset=*/ 2, /*outputBitsOffset=*/ 3, /*dstOffset=*/ 4)
1368
+ ]
1369
+ },
1370
+ {
1371
+ label: 'Max limbs',
1372
+ setup: [
1373
+ {
1374
+ offset: 0,
1375
+ value: new Field(Fr.random())
1376
+ },
1377
+ {
1378
+ offset: 1,
1379
+ value: new Uint32(2n)
1380
+ },
1381
+ {
1382
+ offset: 2,
1383
+ value: new Uint32(256n)
1384
+ },
1385
+ {
1386
+ offset: 3,
1387
+ value: new Uint1(0n)
1388
+ }
1389
+ ],
1390
+ targetInstructions: ()=>[
1391
+ new ToRadixBE(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*radixOffset=*/ 1, /*numLimbsOffset=*/ 2, /*outputBitsOffset=*/ 3, /*dstOffset=*/ 4)
1392
+ ]
1393
+ },
1394
+ {
1395
+ label: 'Radix 3 (slow divmod path)',
1396
+ // Radix 3 bypasses the fast path for power-of-2 radixes (4, 8, 16, 32, 64, 128, 256)
1397
+ // and uses the slow divmod implementation instead
1398
+ setup: [
1399
+ {
1400
+ offset: 0,
1401
+ value: new Field(Fr.random())
1402
+ },
1403
+ {
1404
+ offset: 1,
1405
+ value: new Uint32(3n)
1406
+ },
1407
+ {
1408
+ offset: 2,
1409
+ value: new Uint32(161n)
1410
+ },
1411
+ {
1412
+ offset: 3,
1413
+ value: new Uint1(0n)
1414
+ }
1415
+ ],
1416
+ targetInstructions: ()=>[
1417
+ new ToRadixBE(/*addressing_mode=*/ 0, /*srcOffset=*/ 0, /*radixOffset=*/ 1, /*numLimbsOffset=*/ 2, /*outputBitsOffset=*/ 3, /*dstOffset=*/ 4)
1418
+ ]
1419
+ }
1420
+ ],
1421
+ // ═══════════════════════════════════════════════════════════════════════════
1422
+ // MISC
1423
+ // ═══════════════════════════════════════════════════════════════════════════
1424
+ // DEBUGLOG only has base gas (no dynamic gas scaling) - memory reads only happen
1425
+ // when collectDebugLogs config is enabled
1426
+ [Opcode.DEBUGLOG]: [
1427
+ {
1428
+ setup: [
1429
+ {
1430
+ offset: 0,
1431
+ value: new Field(0n)
1432
+ },
1433
+ {
1434
+ offset: 1,
1435
+ value: new Field(0n)
1436
+ },
1437
+ {
1438
+ offset: 2,
1439
+ value: new Field(0n)
1440
+ },
1441
+ {
1442
+ offset: 3,
1443
+ value: new Uint32(0n)
1444
+ }
1445
+ ],
1446
+ // messageSize = 0
1447
+ targetInstructions: ()=>[
1448
+ new DebugLog(/*addressing_mode=*/ 0, /*levelOffset=*/ 0, /*messageOffset=*/ 1, /*fieldsOffset=*/ 2, /*fieldsSizeOffset=*/ 3, /*messageSize=*/ 0)
1449
+ ]
1450
+ }
1451
+ ]
1452
+ };
1453
+ /**
1454
+ * Get all spam test cases grouped by opcode.
1455
+ * This is the main entry point for tests - it handles all the complexity of
1456
+ * type variants, multiple configs, etc.
1457
+ *
1458
+ * Returns hierarchical structure for nested describe blocks in tests.
1459
+ *
1460
+ * @param maxConfigsPerOpcode - Maximum number of configs to include per opcode.
1461
+ * Defaults to Infinity (no limit). Useful for quick
1462
+ * smoke tests where testing all type variants is too slow,
1463
+ * or for proving tests that are inherently slower.
1464
+ */ export function getSpamConfigsPerOpcode(maxConfigsPerOpcode = Infinity) {
1465
+ const groups = [];
1466
+ for (const [opcodeKey, configs] of Object.entries(SPAM_CONFIGS)){
1467
+ const opcode = Opcode[Number(opcodeKey)];
1468
+ if (!configs) {
1469
+ throw new Error(`Opcode ${opcode} listed in spam configs, but empty`);
1470
+ }
1471
+ // Apply the limit to the number of configs per opcode
1472
+ const limitedConfigs = configs.slice(0, maxConfigsPerOpcode);
1473
+ const cases = limitedConfigs.map((config)=>({
1474
+ ...config,
1475
+ // unlabeled configs just get opcode name
1476
+ label: config.label ? `${opcode}/${config.label}` : opcode
1477
+ }));
1478
+ groups.push({
1479
+ opcode: opcode,
1480
+ configs: cases
1481
+ });
1482
+ }
1483
+ return groups;
1484
+ }
1485
+ // ============================================================================
1486
+ // Helper Functions
1487
+ // ============================================================================
1488
+ /**
1489
+ * Create a SET instruction from a MemoryValue.
1490
+ * Chooses smallest SET variant based on offset and value magnitude for optimal bytecode density.
1491
+ */ function createSetInstruction(offset, memValue) {
1492
+ const tag = memValue.getTag();
1493
+ const value = memValue.toBigInt();
1494
+ // SET_8 only supports offset <= 255 and value <= 255
1495
+ if (offset <= 0xff && value <= 0xffn) {
1496
+ return new Set(0, offset, tag, Number(value)).as(Opcode.SET_8, Set.wireFormat8);
1497
+ }
1498
+ // SET_16+ support offset <= 65535
1499
+ if (value <= 0xffffn) {
1500
+ return new Set(0, offset, tag, Number(value)).as(Opcode.SET_16, Set.wireFormat16);
1501
+ }
1502
+ if (value <= 0xffffffffn) {
1503
+ return new Set(0, offset, tag, Number(value)).as(Opcode.SET_32, Set.wireFormat32);
1504
+ }
1505
+ if (value <= 0xffffffffffffffffn) {
1506
+ return new Set(0, offset, tag, value).as(Opcode.SET_64, Set.wireFormat64);
1507
+ }
1508
+ if (value <= 0xffffffffffffffffffffffffffffffffn) {
1509
+ return new Set(0, offset, tag, value).as(Opcode.SET_128, Set.wireFormat128);
1510
+ }
1511
+ return new Set(0, offset, tag, value).as(Opcode.SET_FF, Set.wireFormatFF);
1512
+ }
1513
+ /**
1514
+ * Append (to the instructions array) the SET instructions for the setup.
1515
+ *
1516
+ * @param instructions - the instructions array to append the setup to
1517
+ * @param setup - the setup configuration specifying what SETs to do
1518
+ */ function appendSetupInstructions(instructions, setup) {
1519
+ for (const item of setup){
1520
+ if (typeof item === 'function') {
1521
+ // item is a function that creates setup instructions (like)
1522
+ instructions.push(...item());
1523
+ } else {
1524
+ // MemSetup
1525
+ instructions.push(createSetInstruction(item.offset, item.value));
1526
+ }
1527
+ }
1528
+ }
1529
+ /**
1530
+ * Append (to the instructions array) the target instructions nTimes times.
1531
+ *
1532
+ * @param instructions - the instructions array to append the loop to
1533
+ * @param config - the spam config to use
1534
+ * @param nTimes - the number of times to append the target instructions
1535
+ * @returns the number of target instructions appended
1536
+ */ function appendTargetNTimes(instructions, config, nTimes) {
1537
+ for(let i = 0; i < nTimes; i++){
1538
+ instructions.push(...config.targetInstructions());
1539
+ }
1540
+ }
1541
+ /**
1542
+ * Append (to the instructions array) an infinite loop that maximizes target instruction density.
1543
+ * Fills remaining bytecode space with unrolled target instructions.
1544
+ *
1545
+ * @param instructions - the instructions array to append the loop to
1546
+ * @param config - the spam config to use
1547
+ * @returns the number of target instructions in the loop body
1548
+ */ function appendInfiniteLoop(instructions, config) {
1549
+ const setupBytecode = encodeToBytecode(instructions);
1550
+ const setupSize = setupBytecode.length;
1551
+ // Compute the size of the target instruction(s)
1552
+ const targetSize = encodeToBytecode(config.targetInstructions()).length;
1553
+ // Fill remaining space (loop body) with target instructions
1554
+ const availableForLoopBody = MAX_BYTECODE_BYTES - setupSize - JUMP_SIZE;
1555
+ const numTargetsInLoopBody = Math.floor(availableForLoopBody / targetSize);
1556
+ const loopStartPc = setupSize;
1557
+ appendTargetNTimes(instructions, config, numTargetsInLoopBody);
1558
+ instructions.push(new Jump(loopStartPc)); // JUMP_SIZE (JUMP_32)
1559
+ return numTargetsInLoopBody;
1560
+ }
1561
+ /**
1562
+ * Generate basic opcode spam bytecode from a SpamConfig.
1563
+ * Spams the target instruction(s) in an infinite loop until out-of-gas.
1564
+ */ export function createOpcodeSpamBytecode(config) {
1565
+ assert(config.limit === undefined, 'If config has `limit`, use createSideEffectLimitedSpamInRevertingNestedCall instead');
1566
+ const instructions = [];
1567
+ // 1. Setup memory
1568
+ appendSetupInstructions(instructions, config.setup);
1569
+ // 2. Infinite loop - maximize calls to target until out-of-gas
1570
+ appendInfiniteLoop(instructions, config);
1571
+ return encodeToBytecode(instructions);
1572
+ }
1573
+ /**
1574
+ * Generate a bytecode that spams a side-effect limited opcode #limit times
1575
+ * NOT in a loop, but inline/unrolled. Then revert.
1576
+ *
1577
+ * @param config - the side-effect limited spam config to use
1578
+ * @returns the bytecode for the side-effect limited spam
1579
+ */ export function createSideEffectSpamBytecode(config) {
1580
+ assert(config.limit !== undefined, 'If config has `limit`, use createSideEffectLimitedSpamInRevertingNestedCall instead');
1581
+ const instructions = [];
1582
+ // 1. Setup
1583
+ appendSetupInstructions(instructions, config.setup);
1584
+ // 2. Body - run target instruction(s) #limit times
1585
+ appendTargetNTimes(instructions, config, config.limit);
1586
+ // 3. Cleanup (revert)
1587
+ if (config.cleanupInstructions) {
1588
+ instructions.push(...config.cleanupInstructions());
1589
+ }
1590
+ return encodeToBytecode(instructions);
1591
+ }
1592
+ async function testStandardOpcodeSpam(tester, config, expectToBeTrue) {
1593
+ const bytecode = createOpcodeSpamBytecode(config);
1594
+ const contract = await deployCustomBytecode(bytecode, tester, config.label);
1595
+ await insertWarmTreeEntries(tester.merkleTrees, contract.address);
1596
+ // Should we pass the contract address as calldata?
1597
+ const calldata = config.addressAsCalldata ? [
1598
+ contract.address.toField()
1599
+ ] : [];
1600
+ const result = await executeCustomBytecode(contract, tester, config.label, calldata);
1601
+ // should have halted with out of gas
1602
+ expectToBeTrue(!result.revertCode.isOK());
1603
+ const revertReason = result.findRevertReason()?.message.toLowerCase();
1604
+ const allowedReasons = [
1605
+ 'out of gas',
1606
+ 'not enough l2gas'
1607
+ ];
1608
+ // expect the reason to match ONE of the allowed reasons
1609
+ expectToBeTrue(allowedReasons.some((allowedReason)=>revertReason?.includes(allowedReason)));
1610
+ return result;
1611
+ }
1612
+ async function testSideEffectOpcodeSpam(tester, config, expectToBeTrue) {
1613
+ // Inner contract will spam the side-effect limited opcode up to its limit, then REVERT
1614
+ const innerBytecode = createSideEffectSpamBytecode(config);
1615
+ // Outer contract will CALL to inner contract in a loop
1616
+ const outerBytecode = createOpcodeSpamBytecode(EXTERNAL_CALL_CONFIG);
1617
+ const innerContract = await deployCustomBytecode(innerBytecode, tester, `${config.label}_Inner`);
1618
+ const outerContract = await deployCustomBytecode(outerBytecode, tester, `${config.label}_Outer`);
1619
+ // Outer contract reads calldata[0] as inner contract address to CALL to
1620
+ const result = await executeCustomBytecode(outerContract, tester, config.label, [
1621
+ innerContract.address.toField()
1622
+ ]);
1623
+ // should have halted with out of gas or explicit REVERT (assertion failed)
1624
+ expectToBeTrue(!result.revertCode.isOK());
1625
+ const revertReason = result.findRevertReason()?.message.toLowerCase();
1626
+ const allowedReasons = [
1627
+ 'assertion failed',
1628
+ 'out of gas',
1629
+ 'not enough l2gas'
1630
+ ];
1631
+ // expect the reason to match ONE of the allowed reasons
1632
+ expectToBeTrue(allowedReasons.some((allowedReason)=>revertReason?.includes(allowedReason)));
1633
+ // Top-level should _always_ run out of gas for these tests
1634
+ // Check top-level halting message
1635
+ // WARNING: only the C++ simulator (or TsVsCpp) will have haltingMessage
1636
+ const allowedOuterReasons = [
1637
+ 'out of gas',
1638
+ 'not enough l2gas'
1639
+ ];
1640
+ if (result.callStackMetadata && result.callStackMetadata.length > 0) {
1641
+ const outerCallMetadata = result.callStackMetadata[0];
1642
+ const outerReason = outerCallMetadata.haltingMessage?.toLowerCase();
1643
+ // expect the reason to match ONE of the allowed reasons
1644
+ expectToBeTrue(allowedOuterReasons.some((allowedReason)=>outerReason?.includes(allowedReason)));
1645
+ }
1646
+ return result;
1647
+ }
1648
+ export async function testOpcodeSpamCase(tester, config, expectToBeTrue = ()=>{}) {
1649
+ if (config.limit) {
1650
+ return await testSideEffectOpcodeSpam(tester, config, expectToBeTrue);
1651
+ }
1652
+ return await testStandardOpcodeSpam(tester, config, expectToBeTrue);
1653
+ }