@aztec/simulator 0.76.4 → 0.77.0-testnet-ignition.21

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 (517) hide show
  1. package/README.md +3 -3
  2. package/dest/client.d.ts +5 -0
  3. package/dest/client.d.ts.map +1 -0
  4. package/dest/client.js +3 -0
  5. package/dest/{public → common}/db_interfaces.d.ts +8 -33
  6. package/dest/common/db_interfaces.d.ts.map +1 -0
  7. package/dest/common/db_interfaces.js +1 -0
  8. package/dest/common/debug_fn_name.d.ts +3 -2
  9. package/dest/common/debug_fn_name.d.ts.map +1 -1
  10. package/dest/common/debug_fn_name.js +2 -5
  11. package/dest/common/errors.d.ts +4 -4
  12. package/dest/common/errors.d.ts.map +1 -1
  13. package/dest/common/errors.js +30 -39
  14. package/dest/common/index.d.ts +2 -1
  15. package/dest/common/index.d.ts.map +1 -1
  16. package/dest/common/index.js +2 -2
  17. package/dest/common/message_load_oracle_inputs.d.ts +2 -2
  18. package/dest/common/message_load_oracle_inputs.d.ts.map +1 -1
  19. package/dest/common/message_load_oracle_inputs.js +8 -8
  20. package/dest/common/stats/index.d.ts.map +1 -0
  21. package/dest/{stats → common/stats}/stats.d.ts +2 -2
  22. package/dest/common/stats/stats.d.ts.map +1 -0
  23. package/dest/common/stats/stats.js +10 -0
  24. package/dest/{acvm → private/acvm}/acvm.d.ts +4 -4
  25. package/dest/private/acvm/acvm.d.ts.map +1 -0
  26. package/dest/private/acvm/acvm.js +70 -0
  27. package/dest/{acvm → private/acvm}/acvm_types.d.ts +1 -1
  28. package/dest/private/acvm/acvm_types.d.ts.map +1 -0
  29. package/dest/private/acvm/acvm_types.js +3 -0
  30. package/dest/{acvm → private/acvm}/deserialize.d.ts +2 -2
  31. package/dest/private/acvm/deserialize.d.ts.map +1 -0
  32. package/dest/private/acvm/deserialize.js +42 -0
  33. package/dest/private/acvm/index.d.ts.map +1 -0
  34. package/dest/private/acvm/index.js +5 -0
  35. package/dest/{acvm → private/acvm}/oracle/index.d.ts +1 -1
  36. package/dest/private/acvm/oracle/index.d.ts.map +1 -0
  37. package/dest/private/acvm/oracle/index.js +2 -0
  38. package/dest/{acvm → private/acvm}/oracle/oracle.d.ts +4 -3
  39. package/dest/private/acvm/oracle/oracle.d.ts.map +1 -0
  40. package/dest/private/acvm/oracle/oracle.js +263 -0
  41. package/dest/{acvm → private/acvm}/oracle/typed_oracle.d.ts +12 -6
  42. package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -0
  43. package/dest/private/acvm/oracle/typed_oracle.js +132 -0
  44. package/dest/{acvm → private/acvm}/serialize.d.ts +7 -3
  45. package/dest/private/acvm/serialize.d.ts.map +1 -0
  46. package/dest/private/acvm/serialize.js +46 -0
  47. package/dest/{client → private}/client_execution_context.d.ts +20 -17
  48. package/dest/private/client_execution_context.d.ts.map +1 -0
  49. package/dest/private/client_execution_context.js +378 -0
  50. package/dest/{client → private}/db_oracle.d.ts +19 -7
  51. package/dest/private/db_oracle.d.ts.map +1 -0
  52. package/dest/private/db_oracle.js +14 -0
  53. package/dest/{client → private}/execution_note_cache.d.ts +2 -2
  54. package/dest/private/execution_note_cache.d.ts.map +1 -0
  55. package/dest/private/execution_note_cache.js +180 -0
  56. package/dest/{common → private}/hashed_values_cache.d.ts +2 -2
  57. package/dest/private/hashed_values_cache.d.ts.map +1 -0
  58. package/dest/private/hashed_values_cache.js +46 -0
  59. package/dest/private/index.d.ts +13 -0
  60. package/dest/private/index.d.ts.map +1 -0
  61. package/dest/private/index.js +12 -0
  62. package/dest/{client → private}/pick_notes.d.ts +1 -1
  63. package/dest/private/pick_notes.d.ts.map +1 -0
  64. package/dest/private/pick_notes.js +51 -0
  65. package/dest/private/private_execution.d.ts +24 -0
  66. package/dest/private/private_execution.d.ts.map +1 -0
  67. package/dest/private/private_execution.js +90 -0
  68. package/dest/{providers → private/providers}/acvm_native.d.ts +5 -5
  69. package/dest/private/providers/acvm_native.d.ts.map +1 -0
  70. package/dest/private/providers/acvm_native.js +139 -0
  71. package/dest/{providers → private/providers}/acvm_wasm.d.ts +4 -4
  72. package/dest/private/providers/acvm_wasm.d.ts.map +1 -0
  73. package/dest/private/providers/acvm_wasm.js +62 -0
  74. package/dest/{providers → private/providers}/acvm_wasm_with_blobs.d.ts +5 -5
  75. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -0
  76. package/dest/{providers → private/providers}/acvm_wasm_with_blobs.js +3 -6
  77. package/dest/{providers → private/providers}/factory.d.ts +1 -1
  78. package/dest/private/providers/factory.d.ts.map +1 -0
  79. package/dest/private/providers/factory.js +27 -0
  80. package/dest/{common → private/providers}/simulation_provider.d.ts +5 -5
  81. package/dest/private/providers/simulation_provider.d.ts.map +1 -0
  82. package/dest/private/providers/simulation_provider.js +24 -0
  83. package/dest/private/simulator.d.ts +36 -0
  84. package/dest/private/simulator.d.ts.map +1 -0
  85. package/dest/private/simulator.js +78 -0
  86. package/dest/{client → private}/unconstrained_execution.d.ts +5 -5
  87. package/dest/private/unconstrained_execution.d.ts.map +1 -0
  88. package/dest/private/unconstrained_execution.js +27 -0
  89. package/dest/{client → private}/view_data_oracle.d.ts +15 -7
  90. package/dest/private/view_data_oracle.d.ts.map +1 -0
  91. package/dest/private/view_data_oracle.js +260 -0
  92. package/dest/{avm → public/avm}/avm_context.d.ts +7 -5
  93. package/dest/public/avm/avm_context.d.ts.map +1 -0
  94. package/dest/public/avm/avm_context.js +44 -0
  95. package/dest/{avm → public/avm}/avm_contract_call_result.d.ts +5 -5
  96. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -0
  97. package/dest/public/avm/avm_contract_call_result.js +50 -0
  98. package/dest/{avm → public/avm}/avm_execution_environment.d.ts +2 -2
  99. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -0
  100. package/dest/public/avm/avm_execution_environment.js +31 -0
  101. package/dest/public/avm/avm_gas.d.ts.map +1 -0
  102. package/dest/public/avm/avm_gas.js +202 -0
  103. package/dest/{avm → public/avm}/avm_machine_state.d.ts +2 -2
  104. package/dest/public/avm/avm_machine_state.d.ts.map +1 -0
  105. package/dest/public/avm/avm_machine_state.js +106 -0
  106. package/dest/{avm → public/avm}/avm_memory_types.d.ts +2 -2
  107. package/dest/public/avm/avm_memory_types.d.ts.map +1 -0
  108. package/dest/public/avm/avm_memory_types.js +340 -0
  109. package/dest/{avm → public/avm}/avm_simulator.d.ts +5 -3
  110. package/dest/public/avm/avm_simulator.d.ts.map +1 -0
  111. package/dest/public/avm/avm_simulator.js +202 -0
  112. package/dest/public/avm/bytecode_utils.d.ts.map +1 -0
  113. package/dest/public/avm/bytecode_utils.js +17 -0
  114. package/dest/{avm → public/avm}/errors.d.ts +5 -4
  115. package/dest/public/avm/errors.d.ts.map +1 -0
  116. package/dest/public/avm/errors.js +179 -0
  117. package/dest/{avm → public/avm}/fixtures/avm_simulation_tester.d.ts +6 -6
  118. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -0
  119. package/dest/public/avm/fixtures/avm_simulation_tester.js +76 -0
  120. package/dest/{avm → public/avm}/fixtures/base_avm_simulation_tester.d.ts +13 -12
  121. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -0
  122. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +83 -0
  123. package/dest/{avm → public/avm}/fixtures/index.d.ts +24 -7
  124. package/dest/public/avm/fixtures/index.d.ts.map +1 -0
  125. package/dest/public/avm/fixtures/index.js +175 -0
  126. package/dest/{avm → public/avm}/fixtures/simple_contract_data_source.d.ts +10 -6
  127. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +1 -0
  128. package/dest/public/avm/fixtures/simple_contract_data_source.js +74 -0
  129. package/dest/public/avm/index.d.ts +4 -0
  130. package/dest/public/avm/index.d.ts.map +1 -0
  131. package/dest/{avm/index.d.ts → public/avm/index.js} +1 -2
  132. package/dest/public/avm/journal/index.d.ts.map +1 -0
  133. package/dest/{avm → public/avm}/journal/journal.d.ts +51 -18
  134. package/dest/public/avm/journal/journal.d.ts.map +1 -0
  135. package/dest/public/avm/journal/journal.js +486 -0
  136. package/dest/{avm → public/avm}/journal/nullifiers.d.ts +8 -6
  137. package/dest/public/avm/journal/nullifiers.d.ts.map +1 -0
  138. package/dest/public/avm/journal/nullifiers.js +97 -0
  139. package/dest/{avm → public/avm}/journal/public_storage.d.ts +2 -2
  140. package/dest/public/avm/journal/public_storage.d.ts.map +1 -0
  141. package/dest/public/avm/journal/public_storage.js +145 -0
  142. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -0
  143. package/dest/public/avm/opcodes/accrued_substate.js +252 -0
  144. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -0
  145. package/dest/public/avm/opcodes/addressing_mode.js +74 -0
  146. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -0
  147. package/dest/public/avm/opcodes/arithmetic.js +73 -0
  148. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -0
  149. package/dest/public/avm/opcodes/bitwise.js +106 -0
  150. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -0
  151. package/dest/public/avm/opcodes/comparators.js +43 -0
  152. package/dest/public/avm/opcodes/contract.d.ts.map +1 -0
  153. package/dest/public/avm/opcodes/contract.js +66 -0
  154. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -0
  155. package/dest/public/avm/opcodes/control_flow.js +105 -0
  156. package/dest/{avm → public/avm}/opcodes/conversion.d.ts +1 -1
  157. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -0
  158. package/dest/public/avm/opcodes/conversion.js +69 -0
  159. package/dest/{avm → public/avm}/opcodes/ec_add.d.ts +1 -1
  160. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -0
  161. package/dest/public/avm/opcodes/ec_add.js +84 -0
  162. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -0
  163. package/dest/public/avm/opcodes/environment_getters.js +79 -0
  164. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -0
  165. package/dest/public/avm/opcodes/external_calls.js +180 -0
  166. package/dest/{avm → public/avm}/opcodes/hashing.d.ts +1 -1
  167. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -0
  168. package/dest/public/avm/opcodes/hashing.js +114 -0
  169. package/dest/public/avm/opcodes/index.d.ts.map +1 -0
  170. package/dest/{avm → public/avm}/opcodes/instruction.d.ts +2 -2
  171. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -0
  172. package/dest/public/avm/opcodes/instruction.js +91 -0
  173. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -0
  174. package/dest/public/avm/opcodes/instruction_impl.js +30 -0
  175. package/dest/public/avm/opcodes/memory.d.ts.map +1 -0
  176. package/dest/public/avm/opcodes/memory.js +253 -0
  177. package/dest/{avm → public/avm}/opcodes/misc.d.ts +1 -1
  178. package/dest/public/avm/opcodes/misc.d.ts.map +1 -0
  179. package/dest/public/avm/opcodes/misc.js +48 -0
  180. package/dest/public/avm/opcodes/storage.d.ts.map +1 -0
  181. package/dest/public/avm/opcodes/storage.js +66 -0
  182. package/dest/public/avm/serialization/buffer_cursor.d.ts.map +1 -0
  183. package/dest/public/avm/serialization/buffer_cursor.js +99 -0
  184. package/dest/{avm → public/avm}/serialization/bytecode_serialization.d.ts +1 -1
  185. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -0
  186. package/dest/public/avm/serialization/bytecode_serialization.js +328 -0
  187. package/dest/{avm → public/avm}/serialization/instruction_serialization.d.ts +1 -2
  188. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -0
  189. package/dest/public/avm/serialization/instruction_serialization.js +225 -0
  190. package/dest/{avm → public/avm}/test_utils.d.ts +6 -4
  191. package/dest/public/avm/test_utils.d.ts.map +1 -0
  192. package/dest/public/avm/test_utils.js +52 -0
  193. package/dest/public/bytecode_errors.js +1 -2
  194. package/dest/public/execution.d.ts +8 -2
  195. package/dest/public/execution.d.ts.map +1 -1
  196. package/dest/public/execution.js +4 -5
  197. package/dest/public/executor_metrics.js +16 -12
  198. package/dest/public/fixtures/index.js +0 -1
  199. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +15 -9
  200. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  201. package/dest/public/fixtures/public_tx_simulation_tester.js +54 -53
  202. package/dest/public/fixtures/utils.d.ts +7 -11
  203. package/dest/public/fixtures/utils.d.ts.map +1 -1
  204. package/dest/public/fixtures/utils.js +92 -32
  205. package/dest/public/index.d.ts +5 -4
  206. package/dest/public/index.d.ts.map +1 -1
  207. package/dest/public/index.js +5 -5
  208. package/dest/public/public_db_sources.d.ts +82 -34
  209. package/dest/public/public_db_sources.d.ts.map +1 -1
  210. package/dest/public/public_db_sources.js +171 -196
  211. package/dest/public/{public_processor.d.ts → public_processor/public_processor.d.ts} +10 -8
  212. package/dest/public/public_processor/public_processor.d.ts.map +1 -0
  213. package/dest/public/public_processor/public_processor.js +379 -0
  214. package/dest/public/{public_processor_metrics.d.ts → public_processor/public_processor_metrics.d.ts} +3 -3
  215. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -0
  216. package/dest/public/public_processor/public_processor_metrics.js +125 -0
  217. package/dest/public/{public_tx_context.d.ts → public_tx_simulator/public_tx_context.d.ts} +14 -8
  218. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -0
  219. package/dest/public/public_tx_simulator/public_tx_context.js +320 -0
  220. package/dest/public/{public_tx_simulator.d.ts → public_tx_simulator/public_tx_simulator.d.ts} +10 -7
  221. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -0
  222. package/dest/public/public_tx_simulator/public_tx_simulator.js +325 -0
  223. package/dest/public/side_effect_errors.js +1 -2
  224. package/dest/public/side_effect_trace.d.ts +16 -10
  225. package/dest/public/side_effect_trace.d.ts.map +1 -1
  226. package/dest/public/side_effect_trace.js +82 -149
  227. package/dest/public/side_effect_trace_interface.d.ts +9 -7
  228. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  229. package/dest/public/side_effect_trace_interface.js +1 -2
  230. package/dest/public/tx_contract_cache.d.ts +41 -0
  231. package/dest/public/tx_contract_cache.d.ts.map +1 -0
  232. package/dest/public/tx_contract_cache.js +49 -0
  233. package/dest/public/unique_class_ids.js +23 -28
  234. package/dest/public/utils.d.ts +2 -2
  235. package/dest/public/utils.d.ts.map +1 -1
  236. package/dest/public/utils.js +17 -12
  237. package/dest/server.d.ts +4 -4
  238. package/dest/server.d.ts.map +1 -1
  239. package/dest/server.js +3 -5
  240. package/dest/test/utils.d.ts +3 -2
  241. package/dest/test/utils.d.ts.map +1 -1
  242. package/dest/test/utils.js +9 -7
  243. package/package.json +19 -21
  244. package/src/client.ts +4 -0
  245. package/src/{public → common}/db_interfaces.ts +8 -40
  246. package/src/common/debug_fn_name.ts +4 -2
  247. package/src/common/errors.ts +5 -5
  248. package/src/common/index.ts +2 -1
  249. package/src/common/message_load_oracle_inputs.ts +2 -2
  250. package/src/common/stats/index.ts +1 -0
  251. package/src/{stats → common/stats}/stats.ts +2 -2
  252. package/src/{acvm → private/acvm}/acvm.ts +5 -5
  253. package/src/{acvm → private/acvm}/acvm_types.ts +1 -1
  254. package/src/{acvm → private/acvm}/deserialize.ts +2 -2
  255. package/src/{acvm → private/acvm}/oracle/index.ts +1 -1
  256. package/src/{acvm → private/acvm}/oracle/oracle.ts +22 -12
  257. package/src/{acvm → private/acvm}/oracle/typed_oracle.ts +50 -55
  258. package/src/{acvm → private/acvm}/serialize.ts +10 -3
  259. package/src/{client → private}/client_execution_context.ts +54 -37
  260. package/src/{client → private}/db_oracle.ts +20 -20
  261. package/src/{client → private}/execution_note_cache.ts +3 -3
  262. package/src/{common → private}/hashed_values_cache.ts +2 -2
  263. package/src/private/index.ts +12 -0
  264. package/src/{client → private}/pick_notes.ts +1 -1
  265. package/src/{client → private}/private_execution.ts +46 -14
  266. package/src/{providers → private/providers}/acvm_native.ts +6 -6
  267. package/src/{providers → private/providers}/acvm_wasm.ts +4 -4
  268. package/src/{providers → private/providers}/acvm_wasm_with_blobs.ts +5 -5
  269. package/src/{providers → private/providers}/factory.ts +1 -1
  270. package/src/{common → private/providers}/simulation_provider.ts +5 -5
  271. package/src/{client → private}/simulator.ts +31 -123
  272. package/src/{client → private}/unconstrained_execution.ts +7 -7
  273. package/src/{client → private}/view_data_oracle.ts +25 -28
  274. package/src/{avm → public/avm}/avm_context.ts +12 -7
  275. package/src/{avm → public/avm}/avm_contract_call_result.ts +6 -6
  276. package/src/{avm → public/avm}/avm_execution_environment.ts +2 -2
  277. package/src/{avm → public/avm}/avm_gas.ts +1 -3
  278. package/src/{avm → public/avm}/avm_machine_state.ts +2 -2
  279. package/src/{avm → public/avm}/avm_memory_types.ts +4 -4
  280. package/src/{avm → public/avm}/avm_simulator.ts +12 -6
  281. package/src/{avm → public/avm}/errors.ts +5 -4
  282. package/src/{avm → public/avm}/fixtures/avm_simulation_tester.ts +16 -15
  283. package/src/{avm → public/avm}/fixtures/base_avm_simulation_tester.ts +45 -40
  284. package/src/{avm → public/avm}/fixtures/index.ts +77 -12
  285. package/src/{avm → public/avm}/fixtures/simple_contract_data_source.ts +26 -19
  286. package/src/{avm → public/avm}/index.ts +1 -1
  287. package/src/public/avm/journal/index.ts +1 -0
  288. package/src/{avm → public/avm}/journal/journal.ts +330 -305
  289. package/src/{avm → public/avm}/journal/nullifiers.ts +20 -19
  290. package/src/{avm → public/avm}/journal/public_storage.ts +2 -2
  291. package/src/{avm → public/avm}/opcodes/contract.ts +1 -1
  292. package/src/{avm → public/avm}/opcodes/control_flow.ts +1 -1
  293. package/src/{avm → public/avm}/opcodes/conversion.ts +1 -1
  294. package/src/{avm → public/avm}/opcodes/ec_add.ts +2 -2
  295. package/src/{avm → public/avm}/opcodes/external_calls.ts +5 -6
  296. package/src/{avm → public/avm}/opcodes/hashing.ts +1 -1
  297. package/src/public/avm/opcodes/index.ts +15 -0
  298. package/src/{avm → public/avm}/opcodes/instruction.ts +2 -2
  299. package/src/{avm → public/avm}/opcodes/misc.ts +1 -1
  300. package/src/public/avm/serialization/bytecode_serialization.ts +204 -0
  301. package/src/{avm → public/avm}/serialization/instruction_serialization.ts +0 -1
  302. package/src/{avm → public/avm}/test_utils.ts +10 -7
  303. package/src/public/execution.ts +9 -12
  304. package/src/public/fixtures/public_tx_simulation_tester.ts +66 -72
  305. package/src/public/fixtures/utils.ts +122 -33
  306. package/src/public/index.ts +5 -4
  307. package/src/public/public_db_sources.ts +216 -222
  308. package/src/public/{public_processor.ts → public_processor/public_processor.ts} +78 -53
  309. package/src/public/{public_processor_metrics.ts → public_processor/public_processor_metrics.ts} +3 -3
  310. package/src/public/{public_tx_context.ts → public_tx_simulator/public_tx_context.ts} +70 -75
  311. package/src/public/{public_tx_simulator.ts → public_tx_simulator/public_tx_simulator.ts} +104 -91
  312. package/src/public/side_effect_trace.ts +85 -160
  313. package/src/public/side_effect_trace_interface.ts +10 -23
  314. package/src/public/tx_contract_cache.ts +69 -0
  315. package/src/public/unique_class_ids.ts +1 -1
  316. package/src/public/utils.ts +2 -2
  317. package/src/server.ts +4 -4
  318. package/src/test/utils.ts +5 -3
  319. package/dest/acvm/acvm.d.ts.map +0 -1
  320. package/dest/acvm/acvm.js +0 -73
  321. package/dest/acvm/acvm_types.d.ts.map +0 -1
  322. package/dest/acvm/acvm_types.js +0 -2
  323. package/dest/acvm/deserialize.d.ts.map +0 -1
  324. package/dest/acvm/deserialize.js +0 -46
  325. package/dest/acvm/index.d.ts.map +0 -1
  326. package/dest/acvm/index.js +0 -6
  327. package/dest/acvm/oracle/index.d.ts.map +0 -1
  328. package/dest/acvm/oracle/index.js +0 -3
  329. package/dest/acvm/oracle/oracle.d.ts.map +0 -1
  330. package/dest/acvm/oracle/oracle.js +0 -230
  331. package/dest/acvm/oracle/typed_oracle.d.ts.map +0 -1
  332. package/dest/acvm/oracle/typed_oracle.js +0 -131
  333. package/dest/acvm/serialize.d.ts.map +0 -1
  334. package/dest/acvm/serialize.js +0 -48
  335. package/dest/avm/avm_context.d.ts.map +0 -1
  336. package/dest/avm/avm_context.js +0 -44
  337. package/dest/avm/avm_contract_call_result.d.ts.map +0 -1
  338. package/dest/avm/avm_contract_call_result.js +0 -45
  339. package/dest/avm/avm_execution_environment.d.ts.map +0 -1
  340. package/dest/avm/avm_execution_environment.js +0 -28
  341. package/dest/avm/avm_gas.d.ts.map +0 -1
  342. package/dest/avm/avm_gas.js +0 -170
  343. package/dest/avm/avm_machine_state.d.ts.map +0 -1
  344. package/dest/avm/avm_machine_state.js +0 -112
  345. package/dest/avm/avm_memory_types.d.ts.map +0 -1
  346. package/dest/avm/avm_memory_types.js +0 -327
  347. package/dest/avm/avm_simulator.d.ts.map +0 -1
  348. package/dest/avm/avm_simulator.js +0 -193
  349. package/dest/avm/avm_tree.d.ts +0 -282
  350. package/dest/avm/avm_tree.d.ts.map +0 -1
  351. package/dest/avm/avm_tree.js +0 -684
  352. package/dest/avm/bytecode_utils.d.ts.map +0 -1
  353. package/dest/avm/bytecode_utils.js +0 -15
  354. package/dest/avm/errors.d.ts.map +0 -1
  355. package/dest/avm/errors.js +0 -196
  356. package/dest/avm/fixtures/avm_simulation_tester.d.ts.map +0 -1
  357. package/dest/avm/fixtures/avm_simulation_tester.js +0 -73
  358. package/dest/avm/fixtures/base_avm_simulation_tester.d.ts.map +0 -1
  359. package/dest/avm/fixtures/base_avm_simulation_tester.js +0 -76
  360. package/dest/avm/fixtures/index.d.ts.map +0 -1
  361. package/dest/avm/fixtures/index.js +0 -134
  362. package/dest/avm/fixtures/simple_contract_data_source.d.ts.map +0 -1
  363. package/dest/avm/fixtures/simple_contract_data_source.js +0 -75
  364. package/dest/avm/index.d.ts.map +0 -1
  365. package/dest/avm/index.js +0 -4
  366. package/dest/avm/journal/index.d.ts.map +0 -1
  367. package/dest/avm/journal/index.js +0 -2
  368. package/dest/avm/journal/journal.d.ts.map +0 -1
  369. package/dest/avm/journal/journal.js +0 -499
  370. package/dest/avm/journal/nullifiers.d.ts.map +0 -1
  371. package/dest/avm/journal/nullifiers.js +0 -99
  372. package/dest/avm/journal/public_storage.d.ts.map +0 -1
  373. package/dest/avm/journal/public_storage.js +0 -159
  374. package/dest/avm/opcodes/accrued_substate.d.ts.map +0 -1
  375. package/dest/avm/opcodes/accrued_substate.js +0 -215
  376. package/dest/avm/opcodes/addressing_mode.d.ts.map +0 -1
  377. package/dest/avm/opcodes/addressing_mode.js +0 -81
  378. package/dest/avm/opcodes/arithmetic.d.ts.map +0 -1
  379. package/dest/avm/opcodes/arithmetic.js +0 -70
  380. package/dest/avm/opcodes/bitwise.d.ts.map +0 -1
  381. package/dest/avm/opcodes/bitwise.js +0 -90
  382. package/dest/avm/opcodes/comparators.d.ts.map +0 -1
  383. package/dest/avm/opcodes/comparators.js +0 -40
  384. package/dest/avm/opcodes/contract.d.ts.map +0 -1
  385. package/dest/avm/opcodes/contract.js +0 -63
  386. package/dest/avm/opcodes/control_flow.d.ts.map +0 -1
  387. package/dest/avm/opcodes/control_flow.js +0 -97
  388. package/dest/avm/opcodes/conversion.d.ts.map +0 -1
  389. package/dest/avm/opcodes/conversion.js +0 -64
  390. package/dest/avm/opcodes/ec_add.d.ts.map +0 -1
  391. package/dest/avm/opcodes/ec_add.js +0 -82
  392. package/dest/avm/opcodes/environment_getters.d.ts.map +0 -1
  393. package/dest/avm/opcodes/environment_getters.js +0 -78
  394. package/dest/avm/opcodes/external_calls.d.ts.map +0 -1
  395. package/dest/avm/opcodes/external_calls.js +0 -169
  396. package/dest/avm/opcodes/hashing.d.ts.map +0 -1
  397. package/dest/avm/opcodes/hashing.js +0 -103
  398. package/dest/avm/opcodes/index.d.ts.map +0 -1
  399. package/dest/avm/opcodes/index.js +0 -16
  400. package/dest/avm/opcodes/instruction.d.ts.map +0 -1
  401. package/dest/avm/opcodes/instruction.js +0 -100
  402. package/dest/avm/opcodes/instruction_impl.d.ts.map +0 -1
  403. package/dest/avm/opcodes/instruction_impl.js +0 -33
  404. package/dest/avm/opcodes/memory.d.ts.map +0 -1
  405. package/dest/avm/opcodes/memory.js +0 -226
  406. package/dest/avm/opcodes/misc.d.ts.map +0 -1
  407. package/dest/avm/opcodes/misc.js +0 -45
  408. package/dest/avm/opcodes/multi_scalar_mul.d.ts +0 -16
  409. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +0 -1
  410. package/dest/avm/opcodes/multi_scalar_mul.js +0 -112
  411. package/dest/avm/opcodes/storage.d.ts.map +0 -1
  412. package/dest/avm/opcodes/storage.js +0 -61
  413. package/dest/avm/serialization/buffer_cursor.d.ts.map +0 -1
  414. package/dest/avm/serialization/buffer_cursor.js +0 -99
  415. package/dest/avm/serialization/bytecode_serialization.d.ts.map +0 -1
  416. package/dest/avm/serialization/bytecode_serialization.js +0 -132
  417. package/dest/avm/serialization/instruction_serialization.d.ts.map +0 -1
  418. package/dest/avm/serialization/instruction_serialization.js +0 -187
  419. package/dest/avm/test_utils.d.ts.map +0 -1
  420. package/dest/avm/test_utils.js +0 -52
  421. package/dest/client/client_execution_context.d.ts.map +0 -1
  422. package/dest/client/client_execution_context.js +0 -392
  423. package/dest/client/db_oracle.d.ts.map +0 -1
  424. package/dest/client/db_oracle.js +0 -17
  425. package/dest/client/execution_note_cache.d.ts.map +0 -1
  426. package/dest/client/execution_note_cache.js +0 -181
  427. package/dest/client/index.d.ts +0 -16
  428. package/dest/client/index.d.ts.map +0 -1
  429. package/dest/client/index.js +0 -15
  430. package/dest/client/pick_notes.d.ts.map +0 -1
  431. package/dest/client/pick_notes.js +0 -54
  432. package/dest/client/private_execution.d.ts +0 -19
  433. package/dest/client/private_execution.d.ts.map +0 -1
  434. package/dest/client/private_execution.js +0 -72
  435. package/dest/client/simulator.d.ts +0 -60
  436. package/dest/client/simulator.d.ts.map +0 -1
  437. package/dest/client/simulator.js +0 -136
  438. package/dest/client/unconstrained_execution.d.ts.map +0 -1
  439. package/dest/client/unconstrained_execution.js +0 -30
  440. package/dest/client/view_data_oracle.d.ts.map +0 -1
  441. package/dest/client/view_data_oracle.js +0 -268
  442. package/dest/common/hashed_values_cache.d.ts.map +0 -1
  443. package/dest/common/hashed_values_cache.js +0 -50
  444. package/dest/common/simulation_provider.d.ts.map +0 -1
  445. package/dest/common/simulation_provider.js +0 -27
  446. package/dest/common.d.ts +0 -2
  447. package/dest/common.d.ts.map +0 -1
  448. package/dest/common.js +0 -2
  449. package/dest/providers/acvm_native.d.ts.map +0 -1
  450. package/dest/providers/acvm_native.js +0 -125
  451. package/dest/providers/acvm_wasm.d.ts.map +0 -1
  452. package/dest/providers/acvm_wasm.js +0 -54
  453. package/dest/providers/acvm_wasm_with_blobs.d.ts.map +0 -1
  454. package/dest/providers/factory.d.ts.map +0 -1
  455. package/dest/providers/factory.js +0 -27
  456. package/dest/providers/index.d.ts +0 -5
  457. package/dest/providers/index.d.ts.map +0 -1
  458. package/dest/providers/index.js +0 -5
  459. package/dest/public/db_interfaces.d.ts.map +0 -1
  460. package/dest/public/db_interfaces.js +0 -2
  461. package/dest/public/fee_payment.d.ts +0 -11
  462. package/dest/public/fee_payment.d.ts.map +0 -1
  463. package/dest/public/fee_payment.js +0 -21
  464. package/dest/public/public_processor.d.ts.map +0 -1
  465. package/dest/public/public_processor.js +0 -352
  466. package/dest/public/public_processor_metrics.d.ts.map +0 -1
  467. package/dest/public/public_processor_metrics.js +0 -106
  468. package/dest/public/public_tx_context.d.ts.map +0 -1
  469. package/dest/public/public_tx_context.js +0 -341
  470. package/dest/public/public_tx_simulator.d.ts.map +0 -1
  471. package/dest/public/public_tx_simulator.js +0 -333
  472. package/dest/stats/index.d.ts.map +0 -1
  473. package/dest/stats/index.js +0 -2
  474. package/dest/stats/stats.d.ts.map +0 -1
  475. package/dest/stats/stats.js +0 -11
  476. package/src/avm/avm_tree.ts +0 -887
  477. package/src/avm/opcodes/multi_scalar_mul.ts +0 -121
  478. package/src/avm/serialization/bytecode_serialization.ts +0 -212
  479. package/src/client/index.ts +0 -15
  480. package/src/common.ts +0 -1
  481. package/src/providers/index.ts +0 -4
  482. package/src/public/fee_payment.ts +0 -23
  483. /package/dest/{stats → common/stats}/index.d.ts +0 -0
  484. /package/{src/stats/index.ts → dest/common/stats/index.js} +0 -0
  485. /package/dest/{acvm → private/acvm}/index.d.ts +0 -0
  486. /package/dest/{avm → public/avm}/avm_gas.d.ts +0 -0
  487. /package/dest/{avm → public/avm}/bytecode_utils.d.ts +0 -0
  488. /package/dest/{avm → public/avm}/journal/index.d.ts +0 -0
  489. /package/{src/avm/journal/index.ts → dest/public/avm/journal/index.js} +0 -0
  490. /package/dest/{avm → public/avm}/opcodes/accrued_substate.d.ts +0 -0
  491. /package/dest/{avm → public/avm}/opcodes/addressing_mode.d.ts +0 -0
  492. /package/dest/{avm → public/avm}/opcodes/arithmetic.d.ts +0 -0
  493. /package/dest/{avm → public/avm}/opcodes/bitwise.d.ts +0 -0
  494. /package/dest/{avm → public/avm}/opcodes/comparators.d.ts +0 -0
  495. /package/dest/{avm → public/avm}/opcodes/contract.d.ts +0 -0
  496. /package/dest/{avm → public/avm}/opcodes/control_flow.d.ts +0 -0
  497. /package/dest/{avm → public/avm}/opcodes/environment_getters.d.ts +0 -0
  498. /package/dest/{avm → public/avm}/opcodes/external_calls.d.ts +0 -0
  499. /package/dest/{avm → public/avm}/opcodes/index.d.ts +0 -0
  500. /package/{src/avm/opcodes/index.ts → dest/public/avm/opcodes/index.js} +0 -0
  501. /package/dest/{avm → public/avm}/opcodes/instruction_impl.d.ts +0 -0
  502. /package/dest/{avm → public/avm}/opcodes/memory.d.ts +0 -0
  503. /package/dest/{avm → public/avm}/opcodes/storage.d.ts +0 -0
  504. /package/dest/{avm → public/avm}/serialization/buffer_cursor.d.ts +0 -0
  505. /package/src/{acvm → private/acvm}/index.ts +0 -0
  506. /package/src/{avm → public/avm}/bytecode_utils.ts +0 -0
  507. /package/src/{avm → public/avm}/opcodes/.eslintrc.cjs +0 -0
  508. /package/src/{avm → public/avm}/opcodes/accrued_substate.ts +0 -0
  509. /package/src/{avm → public/avm}/opcodes/addressing_mode.ts +0 -0
  510. /package/src/{avm → public/avm}/opcodes/arithmetic.ts +0 -0
  511. /package/src/{avm → public/avm}/opcodes/bitwise.ts +0 -0
  512. /package/src/{avm → public/avm}/opcodes/comparators.ts +0 -0
  513. /package/src/{avm → public/avm}/opcodes/environment_getters.ts +0 -0
  514. /package/src/{avm → public/avm}/opcodes/instruction_impl.ts +0 -0
  515. /package/src/{avm → public/avm}/opcodes/memory.ts +0 -0
  516. /package/src/{avm → public/avm}/opcodes/storage.ts +0 -0
  517. /package/src/{avm → public/avm}/serialization/buffer_cursor.ts +0 -0
@@ -1,887 +0,0 @@
1
- import { type IndexedTreeId, MerkleTreeId, type MerkleTreeReadOperations } from '@aztec/circuit-types';
2
- import { AppendOnlyTreeSnapshot, NullifierLeafPreimage, PublicDataTreeLeafPreimage } from '@aztec/circuits.js';
3
- import { poseidon2Hash } from '@aztec/foundation/crypto';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { type IndexedTreeLeafPreimage, type TreeLeafPreimage } from '@aztec/foundation/trees';
6
-
7
- import { strict as assert } from 'assert';
8
- import cloneDeep from 'lodash.clonedeep';
9
-
10
- const MAX_TREE_DEPTH = 128;
11
-
12
- /**
13
- * Helper function to precompute zero hashes
14
- */
15
- async function preComputeZeroHashes(): Promise<Fr[]> {
16
- let currentHash = Fr.zero();
17
- const zeroHashes: Fr[] = [];
18
- for (let i = 0; i < MAX_TREE_DEPTH; i++) {
19
- zeroHashes.push(currentHash);
20
- currentHash = await poseidon2Hash([currentHash, currentHash]);
21
- }
22
- return zeroHashes;
23
- }
24
-
25
- /****************************************************/
26
- /****** Structs Used by the AvmEphemeralForest ******/
27
- /****************************************************/
28
-
29
- /**
30
- * The preimage and the leaf index of the Low Leaf (Low Nullifier or Low Public Data Leaf)
31
- */
32
- type PreimageWitness<T extends IndexedTreeLeafPreimage> = {
33
- preimage: T;
34
- index: bigint;
35
- };
36
-
37
- /**
38
- * The result of fetching a leaf from an indexed tree. Contains the preimage and wether the leaf was already present
39
- * or it's a low leaf.
40
- */
41
- type GetLeafResult<T extends IndexedTreeLeafPreimage> = PreimageWitness<T> & {
42
- alreadyPresent: boolean;
43
- };
44
-
45
- /**
46
- * Contains the low sibling path and a boolean if the next index pointed to
47
- * by the low leaf is an update or an insertion (i.e. exists or not).
48
- */
49
- type LeafWitness<T extends IndexedTreeLeafPreimage> = PreimageWitness<T> & {
50
- siblingPath: Fr[];
51
- };
52
-
53
- /**
54
- * The result of an update in an indexed merkle tree (no new leaf inserted)
55
- */
56
- type IndexedUpdateResult<T extends IndexedTreeLeafPreimage> = {
57
- element: T;
58
- lowWitness: LeafWitness<T>;
59
- };
60
-
61
- /**
62
- * The result of an insertion in an indexed merkle tree.
63
- * This will be used to hint the circuit
64
- */
65
- export type IndexedInsertResult<T extends IndexedTreeLeafPreimage> = IndexedUpdateResult<T> & {
66
- leafIndex: bigint;
67
- insertionPath: Fr[];
68
- };
69
-
70
- /**
71
- * The result of an indexed upsert in an indexed merkle tree.
72
- * This will be used to hint the circuit
73
- */
74
- export type IndexedUpsertResult<T extends IndexedTreeLeafPreimage> =
75
- | (IndexedUpdateResult<T> & { update: true })
76
- | (IndexedInsertResult<T> & { update: false });
77
-
78
- /****************************************************/
79
- /****** The AvmEphemeralForest Class ****************/
80
- /****************************************************/
81
-
82
- /**
83
- * This provides a forkable abstraction over the EphemeralAvmTree class
84
- * It contains the logic to look up into a read-only MerkleTreeDb to discover
85
- * the sibling paths and low witnesses that weren't inserted as part of this tx
86
- */
87
- export class AvmEphemeralForest {
88
- constructor(
89
- public treeDb: MerkleTreeReadOperations,
90
- public treeMap: Map<MerkleTreeId, EphemeralAvmTree>,
91
- // This contains the [leaf index,indexed leaf preimages] tuple that were updated or inserted in the ephemeral tree
92
- // This is needed since we have a sparse collection of keys sorted leaves in the ephemeral tree
93
- public indexedUpdates: Map<IndexedTreeId, Map<bigint, IndexedTreeLeafPreimage>>,
94
- public indexedSortedKeys: Map<IndexedTreeId, [Fr, bigint][]>,
95
- ) {}
96
-
97
- static async create(treeDb: MerkleTreeReadOperations): Promise<AvmEphemeralForest> {
98
- const treeMap = new Map<MerkleTreeId, EphemeralAvmTree>();
99
- for (const treeType of [MerkleTreeId.NULLIFIER_TREE, MerkleTreeId.NOTE_HASH_TREE, MerkleTreeId.PUBLIC_DATA_TREE]) {
100
- const treeInfo = await treeDb.getTreeInfo(treeType);
101
- const tree = await EphemeralAvmTree.create(treeInfo.size, treeInfo.depth, treeDb, treeType);
102
- treeMap.set(treeType, tree);
103
- }
104
- const indexedSortedKeys = new Map<IndexedTreeId, [Fr, bigint][]>();
105
- indexedSortedKeys.set(MerkleTreeId.NULLIFIER_TREE as IndexedTreeId, []);
106
- indexedSortedKeys.set(MerkleTreeId.PUBLIC_DATA_TREE as IndexedTreeId, []);
107
- return new AvmEphemeralForest(treeDb, treeMap, new Map(), indexedSortedKeys);
108
- }
109
-
110
- fork(): AvmEphemeralForest {
111
- return new AvmEphemeralForest(
112
- this.treeDb,
113
- cloneDeep(this.treeMap),
114
- cloneDeep(this.indexedUpdates),
115
- cloneDeep(this.indexedSortedKeys),
116
- );
117
- }
118
-
119
- /**
120
- * Gets sibling path for a leaf - if the sibling path is not found in the tree, it is fetched from the DB
121
- * @param treeId - The tree to be queried for a sibling path.
122
- * @param index - The index of the leaf for which a sibling path should be returned.
123
- * @returns The sibling path of the leaf.
124
- */
125
- async getSiblingPath(treeId: MerkleTreeId, index: bigint): Promise<Fr[]> {
126
- const tree = this.treeMap.get(treeId)!;
127
- let path = await tree.getSiblingPath(index);
128
- if (path === undefined) {
129
- // We dont have the sibling path in our tree - we have to get it from the DB
130
- path = (await this.treeDb.getSiblingPath(treeId, index)).toFields();
131
- // Since the sibling path could be outdated, we compare it with nodes in our tree
132
- // if we encounter a mismatch, we replace it with the node we found in our tree.
133
- for (let i = 0; i < path.length; i++) {
134
- const siblingIndex = index ^ 1n;
135
- const node = tree.getNode(siblingIndex, tree.depth - i);
136
- if (node !== undefined) {
137
- const nodeHash = await tree.hashTree(node, i + 1);
138
- if (!nodeHash.equals(path[i])) {
139
- path[i] = nodeHash;
140
- }
141
- }
142
- index >>= 1n;
143
- }
144
- }
145
- return path;
146
- }
147
-
148
- /**
149
- * This does the work of appending the new leaf and updating the low witness
150
- * @param treeId - The tree to be queried for a sibling path.
151
- * @param lowWitnessIndex - The index of the low leaf in the tree.
152
- * @param lowWitness - The preimage of the low leaf.
153
- * @param newLeafPreimage - The preimage of the new leaf to be inserted.
154
- * @returns The sibling path of the new leaf (i.e. the insertion path)
155
- */
156
- async appendIndexedTree<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
157
- treeId: ID,
158
- lowLeafIndex: bigint,
159
- lowLeafPreimage: T,
160
- newLeafPreimage: T,
161
- ): Promise<Fr[]> {
162
- const tree = this.treeMap.get(treeId)!;
163
- const newLeaf = await this.hashPreimage(newLeafPreimage);
164
- const insertIndex = tree.leafCount;
165
-
166
- const lowLeaf = await this.hashPreimage(lowLeafPreimage);
167
- // Update the low nullifier hash
168
- this.setIndexedUpdates(treeId, lowLeafIndex, lowLeafPreimage);
169
- tree.updateLeaf(lowLeaf, lowLeafIndex);
170
- // Append the new leaf
171
- tree.appendLeaf(newLeaf);
172
- this.setIndexedUpdates(treeId, insertIndex, newLeafPreimage);
173
-
174
- return (await tree.getSiblingPath(insertIndex))!;
175
- }
176
-
177
- /**
178
- * This writes or updates a slot in the public data tree with a value
179
- * @param slot - The slot to be written to.
180
- * @param newValue - The value to be written or updated to
181
- * @returns The insertion result which contains the insertion path, low leaf and the new leaf index
182
- */
183
- async writePublicStorage(slot: Fr, newValue: Fr): Promise<IndexedUpsertResult<PublicDataTreeLeafPreimage>> {
184
- // This only works for the public data tree
185
- const treeId = MerkleTreeId.PUBLIC_DATA_TREE;
186
- const tree = this.treeMap.get(treeId)!;
187
- const [leafOrLowLeafInfo, pathAbsentInEphemeralTree] = await this._getLeafOrLowLeafInfo<
188
- typeof treeId,
189
- PublicDataTreeLeafPreimage
190
- >(treeId, slot);
191
- const { preimage, index: lowLeafIndex, alreadyPresent: update } = leafOrLowLeafInfo;
192
- const siblingPath = await this.getSiblingPath(treeId, lowLeafIndex);
193
-
194
- if (pathAbsentInEphemeralTree) {
195
- // Since we have never seen this before - we should insert it into our tree as it is about to be updated.
196
- this.treeMap.get(treeId)!.insertSiblingPath(lowLeafIndex, siblingPath);
197
- }
198
-
199
- if (update) {
200
- const updatedPreimage = cloneDeep(preimage);
201
- const existingPublicDataSiblingPath = siblingPath;
202
- updatedPreimage.value = newValue;
203
-
204
- tree.updateLeaf(await this.hashPreimage(updatedPreimage), lowLeafIndex);
205
- this.setIndexedUpdates(treeId, lowLeafIndex, updatedPreimage);
206
- this._updateSortedKeys(treeId, [updatedPreimage.slot], [lowLeafIndex]);
207
-
208
- return {
209
- element: updatedPreimage,
210
- lowWitness: {
211
- preimage: preimage,
212
- index: lowLeafIndex,
213
- siblingPath: existingPublicDataSiblingPath,
214
- },
215
- update: true,
216
- };
217
- }
218
- // We are writing to a new slot, so our preimage is a lowNullifier
219
- const insertionIndex = tree.leafCount;
220
- const updatedLowLeaf = cloneDeep(preimage);
221
- updatedLowLeaf.nextSlot = slot;
222
- updatedLowLeaf.nextIndex = insertionIndex;
223
-
224
- const newPublicDataLeaf = new PublicDataTreeLeafPreimage(
225
- slot,
226
- newValue,
227
- new Fr(preimage.getNextKey()),
228
- preimage.getNextIndex(),
229
- );
230
- const insertionPath = await this.appendIndexedTree(treeId, lowLeafIndex, updatedLowLeaf, newPublicDataLeaf);
231
-
232
- // Even though the low leaf key is not updated, we still need to update the sorted keys in case we have
233
- // not seen the low leaf before
234
- this._updateSortedKeys(treeId, [newPublicDataLeaf.slot, updatedLowLeaf.slot], [insertionIndex, lowLeafIndex]);
235
-
236
- return {
237
- element: newPublicDataLeaf,
238
- lowWitness: {
239
- preimage,
240
- index: lowLeafIndex,
241
- siblingPath,
242
- },
243
- update: false,
244
- leafIndex: insertionIndex,
245
- insertionPath: insertionPath,
246
- };
247
- }
248
-
249
- private _updateSortedKeys(treeId: IndexedTreeId, keys: Fr[], index: bigint[]): void {
250
- // This is a reference
251
- const existingKeyVector = this.indexedSortedKeys.get(treeId)!;
252
- // Should already be sorted so not need to re-sort if we just update or splice
253
- for (let i = 0; i < keys.length; i++) {
254
- const foundIndex = existingKeyVector.findIndex(x => x[1] === index[i]);
255
- if (foundIndex === -1) {
256
- // New element, we splice it into the correct location
257
- const spliceIndex =
258
- indexOrNextLowestInArray(
259
- keys[i],
260
- existingKeyVector.map(x => x[0]),
261
- ) + 1;
262
- existingKeyVector.splice(spliceIndex, 0, [keys[i], index[i]]);
263
- } else {
264
- // Update the existing element
265
- existingKeyVector[foundIndex][0] = keys[i];
266
- }
267
- }
268
- }
269
-
270
- /**
271
- * This appends a nullifier to the nullifier tree, and throws if the nullifier already exists
272
- * @param value - The nullifier to be appended
273
- * @returns The insertion result which contains the insertion path, low leaf and the new leaf index
274
- */
275
- async appendNullifier(nullifier: Fr): Promise<IndexedInsertResult<NullifierLeafPreimage>> {
276
- const treeId = MerkleTreeId.NULLIFIER_TREE;
277
- const tree = this.treeMap.get(treeId)!;
278
- const [leafOrLowLeafInfo, pathAbsentInEphemeralTree] = await this._getLeafOrLowLeafInfo<
279
- typeof treeId,
280
- NullifierLeafPreimage
281
- >(treeId, nullifier);
282
- const { preimage, index, alreadyPresent } = leafOrLowLeafInfo;
283
- const siblingPath = await this.getSiblingPath(treeId, index);
284
-
285
- if (pathAbsentInEphemeralTree) {
286
- // Since we have never seen this before - we should insert it into our tree as it is about to be updated.
287
- this.treeMap.get(treeId)!.insertSiblingPath(index, siblingPath);
288
- }
289
-
290
- assert(!alreadyPresent, 'Nullifier already exists in the tree. Cannot update a nullifier!');
291
-
292
- // We are writing a new entry
293
- const insertionIndex = tree.leafCount;
294
- const updatedLowNullifier = cloneDeep(preimage);
295
- updatedLowNullifier.nextNullifier = nullifier;
296
- updatedLowNullifier.nextIndex = insertionIndex;
297
-
298
- const newNullifierLeaf = new NullifierLeafPreimage(nullifier, preimage.nextNullifier, preimage.nextIndex);
299
- const insertionPath = await this.appendIndexedTree(treeId, index, updatedLowNullifier, newNullifierLeaf);
300
-
301
- // Even though the low nullifier key is not updated, we still need to update the sorted keys in case we have
302
- // not seen the low nullifier before
303
- this._updateSortedKeys(
304
- treeId,
305
- [newNullifierLeaf.nullifier, updatedLowNullifier.nullifier],
306
- [insertionIndex, index],
307
- );
308
-
309
- return {
310
- element: newNullifierLeaf,
311
- lowWitness: {
312
- preimage,
313
- index,
314
- siblingPath,
315
- },
316
- leafIndex: insertionIndex,
317
- insertionPath: insertionPath,
318
- };
319
- }
320
-
321
- /**
322
- * This appends a note hash to the note hash tree
323
- * @param value - The note hash to be appended
324
- * @returns The insertion result which contains the insertion path
325
- */
326
- async appendNoteHash(noteHash: Fr): Promise<Fr[]> {
327
- const tree = this.treeMap.get(MerkleTreeId.NOTE_HASH_TREE)!;
328
- tree.appendLeaf(noteHash);
329
- // We use leafCount - 1 here because we would have just appended a leaf
330
- const insertionPath = await tree.getSiblingPath(tree.leafCount - 1n);
331
- return insertionPath!;
332
- }
333
-
334
- /**
335
- * This is wrapper around treeId to set values in the indexedUpdates map
336
- */
337
- private setIndexedUpdates<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
338
- treeId: ID,
339
- index: bigint,
340
- preimage: T,
341
- ): void {
342
- let updates = this.indexedUpdates.get(treeId);
343
- if (updates === undefined) {
344
- updates = new Map();
345
- this.indexedUpdates.set(treeId, updates);
346
- }
347
- updates.set(index, preimage);
348
- }
349
-
350
- /**
351
- * This is wrapper around treeId to get values in the indexedUpdates map.
352
- * Should only be called if we know the value exists.
353
- */
354
- private getIndexedUpdate<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(treeId: ID, index: bigint): T {
355
- const updates = this.indexedUpdates.get(treeId);
356
- assert(updates !== undefined, `No updates exist in the ephemeral ${MerkleTreeId[treeId]} tree.`);
357
- const preimage = updates.get(index);
358
- assert(
359
- updates !== undefined,
360
- `No update exists in the ephemeral ${MerkleTreeId[treeId]} tree for leafIndex ${index}.`,
361
- );
362
- return preimage as T;
363
- }
364
-
365
- /**
366
- * This is wrapper around treeId to check membership (i.e. has()) of index in the indexedUpdates map
367
- */
368
- private hasLocalUpdates<ID extends IndexedTreeId>(treeId: ID, index: bigint): boolean {
369
- const updates = this.indexedUpdates.get(treeId);
370
- if (updates === undefined) {
371
- return false;
372
- }
373
- return updates.has(index);
374
- }
375
-
376
- /**
377
- * Get the leaf or low leaf preimage and its index in the indexed tree given a key (slot or nullifier value).
378
- * If the key is not found in the tree, it does an external lookup to the underlying merkle DB.
379
- * @param treeId - The tree we are looking up in
380
- * @param key - The key for which we are look up the leaf or low leaf for.
381
- * @param T - The type of the preimage (PublicData or Nullifier)
382
- * @returns The leaf or low leaf info (preimage & leaf index).
383
- */
384
- async getLeafOrLowLeafInfo<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
385
- treeId: ID,
386
- key: Fr,
387
- ): Promise<GetLeafResult<T>> {
388
- const [leafOrLowLeafInfo, _] = await this._getLeafOrLowLeafInfo<ID, T>(treeId, key);
389
- return leafOrLowLeafInfo;
390
- }
391
-
392
- /**
393
- * Internal helper to get the leaf or low leaf preimage and its index in the indexed tree given a key (slot or nullifier value).
394
- * If the key is not found in the tree, it does an external lookup to the underlying merkle DB.
395
- * Indicates whethe the sibling path is absent in the ephemeral tree.
396
- * @param treeId - The tree we are looking up in
397
- * @param key - The key for which we are look up the leaf or low leaf for.
398
- * @param T - The type of the preimage (PublicData or Nullifier)
399
- * @returns [
400
- * getLeafResult - The leaf or low leaf info (preimage & leaf index),
401
- * pathAbsentInEphemeralTree - whether its sibling path is absent in the ephemeral tree (useful during insertions)
402
- * ]
403
- */
404
- async _getLeafOrLowLeafInfo<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
405
- treeId: ID,
406
- key: Fr,
407
- ): Promise<[GetLeafResult<T>, /*pathAbsentInEphemeralTree=*/ boolean]> {
408
- const bigIntKey = key.toBigInt();
409
- // In this function, "min" refers to the leaf with the
410
- // largest key <= the specified key in the indexedUpdates.
411
- // In other words, the leaf with the "next lowest" key in indexedUpdates.
412
-
413
- // First, search the indexed updates (no DB fallback) to find
414
- // the leafIndex of the leaf with the largest key <= the specified key.
415
- const minIndexedLeafIndex = this._getLeafIndexOrNextLowestInIndexedUpdates(treeId, key);
416
-
417
- // Then we search on the external DB
418
- const leafOrLowLeafWitnessFromExternalDb: GetLeafResult<T> = await this._getLeafOrLowLeafWitnessInExternalDb(
419
- treeId,
420
- bigIntKey,
421
- );
422
-
423
- // If the indexed updates are empty, we can return the leaf from the DB
424
- if (minIndexedLeafIndex === -1n) {
425
- return [leafOrLowLeafWitnessFromExternalDb, /*pathAbsentInEphemeralTree=*/ true];
426
- }
427
-
428
- // Otherwise, we return the closest one. First fetch the leaf from the indexed updates.
429
- const minIndexedUpdate: T = this.getIndexedUpdate(treeId, minIndexedLeafIndex);
430
-
431
- // And get both keys
432
- const keyFromIndexed = minIndexedUpdate.getKey();
433
- const keyFromExternal = leafOrLowLeafWitnessFromExternalDb.preimage.getKey();
434
-
435
- if (keyFromExternal > keyFromIndexed) {
436
- // this.log.debug(`Using leaf from external DB for ${MerkleTreeId[treeId]}`);
437
- return [leafOrLowLeafWitnessFromExternalDb, /*pathAbsentInEphemeralTree=*/ true];
438
- } else {
439
- // this.log.debug(`Using leaf from indexed DB for ${MerkleTreeId[treeId]}`);
440
- const leafInfo = {
441
- preimage: minIndexedUpdate,
442
- index: minIndexedLeafIndex,
443
- alreadyPresent: keyFromIndexed === bigIntKey,
444
- };
445
- return [leafInfo, /*pathAbsentInEphemeralTree=*/ false];
446
- }
447
- }
448
-
449
- /**
450
- * Helper to search for the leaf with the specified key in the indexedUpdates
451
- * and return its leafIndex.
452
- * If not present, return the leafIndex of the largest leaf <= the specified key
453
- * (the leafIndex of the next lowest key).
454
- *
455
- * If no entry exists in indexedUpdates <= the specified key, return -1.
456
- * @returns - The leafIndex of the leaf with the largest key <= the specified key.
457
- */
458
- private _getLeafIndexOrNextLowestInIndexedUpdates<ID extends IndexedTreeId>(treeId: ID, key: Fr): bigint {
459
- const keyOrderedVector = this.indexedSortedKeys.get(treeId)!;
460
-
461
- const indexInVector = indexOrNextLowestInArray(
462
- key,
463
- keyOrderedVector.map(x => x[0]),
464
- );
465
-
466
- if (indexInVector !== -1) {
467
- const [_, leafIndex] = keyOrderedVector[indexInVector];
468
- return leafIndex;
469
- } else {
470
- // no leaf <= the specified key was found
471
- return -1n;
472
- }
473
- }
474
-
475
- /**
476
- * Query the external DB to get leaf if present, low leaf if absent
477
- */
478
- private async _getLeafOrLowLeafWitnessInExternalDb<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
479
- treeId: ID,
480
- key: bigint,
481
- ): Promise<GetLeafResult<T>> {
482
- // "key" is siloed slot (leafSlot) or siloed nullifier
483
- const previousValueIndex = await this.treeDb.getPreviousValueIndex(treeId, key);
484
- assert(
485
- previousValueIndex !== undefined,
486
- `${MerkleTreeId[treeId]} low leaf index should always be found (even if target leaf does not exist)`,
487
- );
488
- const { index: leafIndex, alreadyPresent } = previousValueIndex;
489
-
490
- const leafPreimage = await this.treeDb.getLeafPreimage(treeId, leafIndex);
491
- assert(
492
- leafPreimage !== undefined,
493
- `${MerkleTreeId[treeId]} low leaf preimage should never be undefined (even if target leaf does not exist)`,
494
- );
495
-
496
- return { preimage: leafPreimage as T, index: leafIndex, alreadyPresent };
497
- }
498
-
499
- /**
500
- * This hashes the preimage to a field element
501
- */
502
- hashPreimage<T extends TreeLeafPreimage>(preimage: T): Promise<Fr> {
503
- const input = preimage.toHashInputs().map(x => Fr.fromBuffer(x));
504
- return poseidon2Hash(input);
505
- }
506
-
507
- async getTreeSnapshot(id: MerkleTreeId): Promise<AppendOnlyTreeSnapshot> {
508
- const tree = this.treeMap.get(id)!;
509
- const root = await tree.getRoot();
510
- return new AppendOnlyTreeSnapshot(root, Number(tree.leafCount));
511
- }
512
- }
513
-
514
- /****************************************************/
515
- /****** Some useful Structs and Enums **************/
516
- /****************************************************/
517
- enum TreeType {
518
- LEAF,
519
- NODE,
520
- }
521
-
522
- type Leaf = {
523
- tag: TreeType.LEAF;
524
- value: Fr;
525
- };
526
- type Node = {
527
- tag: TreeType.NODE;
528
- leftTree: Tree;
529
- rightTree: Tree;
530
- };
531
-
532
- type Tree = Leaf | Node;
533
-
534
- enum SiblingStatus {
535
- MEMBER,
536
- NONMEMBER,
537
- ERROR,
538
- }
539
-
540
- type AccumulatedSiblingPath = {
541
- path: Fr[];
542
- status: SiblingStatus;
543
- };
544
-
545
- /****************************************************/
546
- /****** Some Helpful Constructors for Trees ********/
547
- /****************************************************/
548
- const Node = (left: Tree, right: Tree): Node => ({
549
- tag: TreeType.NODE,
550
- leftTree: left,
551
- rightTree: right,
552
- });
553
-
554
- const Leaf = (value: Fr): Leaf => ({
555
- tag: TreeType.LEAF,
556
- value,
557
- });
558
-
559
- /****************************************************/
560
- /****** The EphemeralAvmTree Class *****************/
561
- /****************************************************/
562
-
563
- /**
564
- * This class contains a recursively defined tree that has leaves at different heights
565
- * It is seeded by an existing merkle treeDb for which it derives a frontier
566
- * It is intended to be a lightweight tree that contains only the necessary information to suppport appends or updates
567
- */
568
- export class EphemeralAvmTree {
569
- private tree: Tree;
570
- public frontier: Fr[];
571
-
572
- private static precomputedZeroHashes: Fr[] | undefined;
573
-
574
- private constructor(private root: Leaf, public leafCount: bigint, public depth: number, private zeroHashes: Fr[]) {
575
- this.tree = root;
576
- this.frontier = [];
577
- }
578
-
579
- static async create(
580
- forkedLeafCount: bigint,
581
- depth: number,
582
- treeDb: MerkleTreeReadOperations,
583
- merkleId: MerkleTreeId,
584
- ): Promise<EphemeralAvmTree> {
585
- let zeroHashes = EphemeralAvmTree.precomputedZeroHashes;
586
- if (zeroHashes === undefined) {
587
- zeroHashes = await preComputeZeroHashes();
588
- EphemeralAvmTree.precomputedZeroHashes = zeroHashes;
589
- }
590
- const zeroHash = zeroHashes[depth];
591
- const tree = new EphemeralAvmTree(Leaf(zeroHash), forkedLeafCount, depth, zeroHashes);
592
- await tree.initializeFrontier(treeDb, merkleId);
593
- return tree;
594
- }
595
-
596
- /**
597
- * This is a recursive function that inserts a leaf into the tree
598
- * @param value - The value of the leaf to be inserted
599
- */
600
- appendLeaf(value: Fr): void {
601
- const insertPath = this._derivePathLE(this.leafCount);
602
- this.tree = this._insertLeaf(value, insertPath, this.depth, this.tree);
603
- this.leafCount++;
604
- }
605
-
606
- /**
607
- * This is a recursive function that upserts a leaf into the tree at a index and depth
608
- * @param value - The value of the leaf to be inserted
609
- * @param index - The index of the leaf to be inserted
610
- * @param depth - The depth of the leaf to be inserted (defaults to the bottom of the tree)
611
- */
612
- updateLeaf(value: Fr, index: bigint, depth = this.depth): void {
613
- const insertPath = this._derivePathLE(index, depth);
614
- this.tree = this._insertLeaf(value, insertPath, depth, this.tree);
615
- }
616
-
617
- /**
618
- * Get the sibling path of a leaf in the tree
619
- * @param index - The index of the leaf for which a sibling path should be returned.
620
- * @returns The sibling path of the leaf, can fail if the path is not found
621
- */
622
- async getSiblingPath(index: bigint): Promise<Fr[] | undefined> {
623
- const searchPath = this._derivePathLE(index);
624
- // Handle cases where we error out
625
- const { path, status } = await this._getSiblingPath(searchPath, this.tree, []);
626
- if (status === SiblingStatus.ERROR) {
627
- return undefined;
628
- }
629
- return path;
630
- }
631
-
632
- /**
633
- * This upserts the nodes of the sibling path into the tree
634
- * @param index - The index of the leaf that the sibling path is derived from
635
- * @param siblingPath - The sibling path of the index
636
- */
637
- insertSiblingPath(index: bigint, siblingPath: Fr[]): void {
638
- for (let i = 0; i < siblingPath.length; i++) {
639
- // Flip(XOR) the last bit because we are inserting siblings of the leaf
640
- const sibIndex = index ^ 1n;
641
- const node = this.getNode(sibIndex, this.depth - i);
642
- // If we are inserting a sibling path and we already have a branch at that index in our
643
- // ephemeral tree, we should not overwrite it
644
- if (node === undefined || node.tag === TreeType.LEAF) {
645
- this.updateLeaf(siblingPath[i], sibIndex, this.depth - i);
646
- }
647
- index >>= 1n;
648
- }
649
- }
650
-
651
- /**
652
- * This is a helper function that computes the index of the frontier nodes at each depth
653
- * @param leafCount - The number of leaves in the tree
654
- * @returns An array of frontier indices at each depth, sorted from leaf to root
655
- */
656
- // Do we really need LeafCount to be a bigint - log2 is on numbers only
657
- static computeFrontierLeafIndices(leafCount: number): number[] {
658
- const numFrontierEntries = Math.floor(Math.log2(leafCount)) + 1;
659
- const frontierIndices = [];
660
- for (let i = 0; i < numFrontierEntries; i++) {
661
- if (leafCount === 0) {
662
- frontierIndices.push(0);
663
- } else if (leafCount % 2 === 0) {
664
- frontierIndices.push(leafCount - 2);
665
- } else {
666
- frontierIndices.push(leafCount - 1);
667
- }
668
- leafCount >>= 1;
669
- }
670
- return frontierIndices;
671
- }
672
-
673
- /**
674
- * This derives the frontier and inserts them into the tree
675
- * @param treeDb - The treeDb to be queried for sibling paths
676
- * @param merkleId - The treeId of the tree to be queried for sibling paths
677
- */
678
- async initializeFrontier(treeDb: MerkleTreeReadOperations, merkleId: MerkleTreeId): Promise<void> {
679
- // The frontier indices are sorted from the leaf to root
680
- const frontierIndices = EphemeralAvmTree.computeFrontierLeafIndices(Number(this.leafCount));
681
- // The frontier indices are level-based - i.e. index N at level L.
682
- // Since we can only ask the DB for paths from the root to the leaf, we do the following complicated calculations
683
- // 1) The goal is to insert the frontier node N at level L into the tree.
684
- // 2) We get the path to a leaf that passes through the frontier node we want (there are multiple paths so we just pick one)
685
- // 3) We can only get sibling paths from the root to the leaf, so we get the sibling path of the leaf from (2)
686
- // NOTE: This is terribly inefficient and we should probably change the DB API to allow for getting paths to a node
687
-
688
- const frontierValues = [];
689
- // These are leaf indexes that pass through the frontier nodes
690
- for (let i = 0; i < frontierIndices.length; i++) {
691
- // Given the index to a frontier, we first xor it so we can get its sibling index at depth L
692
- // We then extend the path to that sibling index by shifting left the requisite number of times (for simplicity we just go left down the tree - it doesnt matter)
693
- // This provides us the leaf index such that if we ask for this leafIndex's sibling path, it will pass through the frontier node
694
- const index = BigInt(frontierIndices[i] ^ 1) << BigInt(i);
695
- // This path passes through our frontier node at depth - i
696
- const path = await treeDb.getSiblingPath(merkleId, index);
697
-
698
- // We derive the path that we can walk and truncate it so that it terminates exactly at the frontier node
699
- const frontierPath = this._derivePathLE(BigInt(frontierIndices[i]), this.depth - i);
700
- // The value of the frontier is the at the i-th index of the sibling path
701
- const frontierValue = path.toFields()[i];
702
- frontierValues.push(frontierValue);
703
- // We insert it at depth - i (the truncated position)
704
- // Note this is a leaf node that wont necessarily be at the bottom of the tree (besides the first frontier)
705
- this.tree = this._insertLeaf(frontierValue, frontierPath, this.depth - i, this.tree);
706
- }
707
- this.frontier = frontierValues;
708
- }
709
-
710
- /**
711
- * Computes the root of the tree
712
- */
713
- public getRoot(): Promise<Fr> {
714
- return this.hashTree(this.tree, this.depth);
715
- }
716
-
717
- /**
718
- * Recursively hashes the subtree
719
- * @param tree - The tree to be hashed
720
- * @param depth - The depth of the tree
721
- */
722
- public async hashTree(tree: Tree, depth: number): Promise<Fr> {
723
- switch (tree.tag) {
724
- case TreeType.NODE: {
725
- return poseidon2Hash([
726
- await this.hashTree(tree.leftTree, depth - 1),
727
- await this.hashTree(tree.rightTree, depth - 1),
728
- ]);
729
- }
730
- case TreeType.LEAF: {
731
- return tree.value;
732
- }
733
- }
734
- }
735
-
736
- /**
737
- * Extracts the subtree from a given index and depth
738
- * @param index - The index of the node to be extracted
739
- * @param depth - The depth of the node to be extracted
740
- * @returns The subtree rooted at the index and depth
741
- */
742
- public getNode(index: bigint, depth: number): Tree | undefined {
743
- const path = this._derivePathBE(index, depth);
744
- const truncatedPath = path.slice(0, depth);
745
- truncatedPath.reverse();
746
- try {
747
- return this._getNode(truncatedPath, this.tree);
748
- } catch (e) {
749
- return undefined;
750
- }
751
- }
752
-
753
- /**
754
- * This is the recursive helper for getNode
755
- */
756
- private _getNode(nodePath: number[], tree: Tree): Tree {
757
- if (nodePath.length === 0) {
758
- return tree;
759
- }
760
- switch (tree.tag) {
761
- case TreeType.NODE:
762
- return nodePath.pop() === 0 ? this._getNode(nodePath, tree.leftTree) : this._getNode(nodePath, tree.rightTree);
763
-
764
- case TreeType.LEAF:
765
- throw new Error('Node not found');
766
- }
767
- }
768
-
769
- /** Our tree traversal uses an array of 1s and 0s to represent the path to a leaf and expects them to be in LE order
770
- * This helps with deriving it given an index and (optionally a depth)
771
- * @param index - The index to derive a path to within the tree, does not have to terminate at a leaf
772
- * @param depth - The depth to traverse, if not provided it will traverse to the bottom of the tree
773
- * @returns The path to the leaf in LE order
774
- */
775
- private _derivePathLE(index: bigint, depth = this.depth): number[] {
776
- return this._derivePathBE(index, depth).reverse();
777
- }
778
-
779
- /** Sometimes we want it in BE order, to make truncating easier
780
- * @param index - The index to derive a path to within the tree, does not have to terminate at a leaf
781
- * @param depth - The depth to traverse, if not provided it will traverse to the bottom of the tree
782
- * @returns The path to the leaf in LE order
783
- */
784
- private _derivePathBE(index: bigint, depth = this.depth): number[] {
785
- return index
786
- .toString(2)
787
- .padStart(depth, '0')
788
- .split('')
789
- .map(x => parseInt(x));
790
- }
791
-
792
- /**
793
- * This is a recursive function that upserts a leaf into the tree given a path
794
- * @param value - The value of the leaf to be upserted
795
- * @param insertPath - The path to the leaf, this should be ordered from leaf to root (i.e. LE encoded)
796
- * @param depth - The depth of the tree
797
- * @param tree - The current tree
798
- * @param appendMode - If true we append the relevant zeroHashes to the tree as we traverse
799
- */
800
- private _insertLeaf(value: Fr, insertPath: number[], depth: number, tree: Tree): Tree {
801
- if (insertPath.length > this.depth || depth > this.depth) {
802
- throw new Error('PATH EXCEEDS DEPTH');
803
- }
804
- if (depth === 0 || insertPath.length === 0) {
805
- return Leaf(value);
806
- }
807
- switch (tree.tag) {
808
- case TreeType.NODE: {
809
- return insertPath.pop() === 0
810
- ? Node(this._insertLeaf(value, insertPath, depth - 1, tree.leftTree), tree.rightTree)
811
- : Node(tree.leftTree, this._insertLeaf(value, insertPath, depth - 1, tree.rightTree));
812
- }
813
- case TreeType.LEAF: {
814
- const zeroLeaf = Leaf(this.zeroHashes[depth - 1]);
815
- return insertPath.pop() === 0
816
- ? Node(this._insertLeaf(value, insertPath, depth - 1, zeroLeaf), zeroLeaf)
817
- : Node(zeroLeaf, this._insertLeaf(value, insertPath, depth - 1, zeroLeaf));
818
- }
819
- }
820
- }
821
-
822
- /* Recursive helper for getSiblingPath, this only looks inside the tree and does not resolve using
823
- * a DB. If a path is not found, it returns an error status that is expected to be handled by the caller
824
- * @param searchPath - The path to the leaf for which we would like the sibling pathin LE order
825
- * @param tree - The current tree
826
- * @param acc - The accumulated sibling path
827
- */
828
- private async _getSiblingPath(searchPath: number[], tree: Tree, acc: Fr[]): Promise<AccumulatedSiblingPath> {
829
- // If we have reached the end of the path, we should be at a leaf or empty node
830
- // If it is a leaf, we check if the value is equal to the leaf value
831
- // If it is empty we check if the value is equal to zero
832
- if (searchPath.length === 0) {
833
- switch (tree.tag) {
834
- case TreeType.LEAF:
835
- return { path: acc, status: SiblingStatus.MEMBER };
836
- case TreeType.NODE:
837
- return { path: [], status: SiblingStatus.ERROR };
838
- }
839
- }
840
- // Keep exploring here
841
- switch (tree.tag) {
842
- case TreeType.NODE: {
843
- // Look at the next element of the path to decided if we go left or right, note this mutates!
844
- return searchPath.pop() === 0
845
- ? await this._getSiblingPath(
846
- searchPath,
847
- tree.leftTree,
848
- [await this.hashTree(tree.rightTree, searchPath.length)].concat(acc),
849
- )
850
- : await this._getSiblingPath(
851
- searchPath,
852
- tree.rightTree,
853
- [await this.hashTree(tree.leftTree, searchPath.length)].concat(acc),
854
- );
855
- }
856
- // In these two situations we are exploring a subtree we dont have information about
857
- // We should return an error and look inside the DB
858
- case TreeType.LEAF:
859
- return { path: [], status: SiblingStatus.ERROR };
860
- }
861
- }
862
- }
863
-
864
- /**
865
- * Return the index of the key in the array, or index-1 if they key is not found.
866
- */
867
- function indexOrNextLowestInArray(key: Fr, arr: Fr[]): number {
868
- // We are looking for the index of the largest element in the array that is less than the key
869
- let start = 0;
870
- let end = arr.length;
871
- // Note that the easiest way is to increment the search key by 1 and then do a binary search
872
- const keyPlus1 = key.add(Fr.ONE);
873
- while (start < end) {
874
- const mid = Math.floor((start + end) / 2);
875
- if (arr[mid].cmp(keyPlus1) < 0) {
876
- // The key + 1 is greater than the midpoint, so we can continue searching the top half
877
- start = mid + 1;
878
- } else {
879
- // The key + 1 is LT or EQ the arr element, so we can continue searching the bottom half
880
- end = mid;
881
- }
882
- }
883
- // We either found key + 1 or start is now at the index of the largest element that we would have inserted key + 1
884
- // Therefore start - 1 is the index of the element just below - note it can be -1 if the first element in the array is
885
- // greater than the key
886
- return start - 1;
887
- }