@aztec/simulator 0.85.0 → 0.86.0-nightly.20250425

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 (133) hide show
  1. package/dest/private/providers/acvm_native.d.ts +1 -1
  2. package/dest/private/providers/acvm_native.d.ts.map +1 -1
  3. package/dest/private/providers/acvm_native.js +4 -3
  4. package/dest/private/providers/acvm_wasm.d.ts +2 -1
  5. package/dest/private/providers/acvm_wasm.d.ts.map +1 -1
  6. package/dest/private/providers/acvm_wasm.js +6 -1
  7. package/dest/private/providers/acvm_wasm_with_blobs.d.ts +2 -1
  8. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -1
  9. package/dest/private/providers/acvm_wasm_with_blobs.js +6 -1
  10. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts +2 -1
  11. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +1 -1
  12. package/dest/private/providers/simulation_provider.d.ts +2 -1
  13. package/dest/private/providers/simulation_provider.d.ts.map +1 -1
  14. package/dest/public/avm/avm_context.d.ts +2 -2
  15. package/dest/public/avm/avm_context.d.ts.map +1 -1
  16. package/dest/public/avm/avm_simulator.d.ts +2 -1
  17. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  18. package/dest/public/avm/avm_simulator.js +2 -1
  19. package/dest/public/avm/avm_simulator_interface.d.ts +11 -0
  20. package/dest/public/avm/avm_simulator_interface.d.ts.map +1 -0
  21. package/dest/public/avm/avm_simulator_interface.js +3 -0
  22. package/dest/public/avm/errors.d.ts +1 -16
  23. package/dest/public/avm/errors.d.ts.map +1 -1
  24. package/dest/public/avm/errors.js +0 -37
  25. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
  26. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  27. package/dest/public/avm/fixtures/avm_simulation_tester.js +1 -1
  28. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +1 -1
  29. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
  30. package/dest/public/avm/fixtures/index.d.ts +2 -85
  31. package/dest/public/avm/fixtures/index.d.ts.map +1 -1
  32. package/dest/public/avm/fixtures/index.js +2 -174
  33. package/dest/public/avm/fixtures/initializers.d.ts +42 -0
  34. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -0
  35. package/dest/public/avm/fixtures/initializers.js +42 -0
  36. package/dest/public/avm/fixtures/utils.d.ts +46 -0
  37. package/dest/public/avm/fixtures/utils.d.ts.map +1 -0
  38. package/dest/public/avm/fixtures/utils.js +136 -0
  39. package/dest/public/avm/index.d.ts +0 -1
  40. package/dest/public/avm/index.d.ts.map +1 -1
  41. package/dest/public/avm/index.js +0 -1
  42. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  43. package/dest/public/avm/opcodes/accrued_substate.js +1 -1
  44. package/dest/public/avm/opcodes/external_calls.d.ts +3 -2
  45. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  46. package/dest/public/avm/opcodes/external_calls.js +14 -9
  47. package/dest/public/avm/opcodes/instruction.d.ts +5 -5
  48. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -1
  49. package/dest/public/avm/opcodes/instruction.js +6 -6
  50. package/dest/public/avm/revert_reason.d.ts +18 -0
  51. package/dest/public/avm/revert_reason.d.ts.map +1 -0
  52. package/dest/public/avm/revert_reason.js +38 -0
  53. package/dest/public/avm/serialization/bytecode_serialization.d.ts +2 -4
  54. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  55. package/dest/public/avm/serialization/bytecode_serialization.js +70 -69
  56. package/dest/{common → public}/debug_fn_name.d.ts +1 -1
  57. package/dest/{common → public}/debug_fn_name.d.ts.map +1 -1
  58. package/dest/public/fixtures/index.d.ts +1 -0
  59. package/dest/public/fixtures/index.d.ts.map +1 -1
  60. package/dest/public/fixtures/index.js +1 -0
  61. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +1 -1
  62. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  63. package/dest/public/fixtures/public_tx_simulation_tester.js +3 -4
  64. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -0
  65. package/dest/public/{avm/fixtures → fixtures}/simple_contract_data_source.js +1 -1
  66. package/dest/public/hinting_db_sources.d.ts +15 -5
  67. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  68. package/dest/public/hinting_db_sources.js +74 -29
  69. package/dest/public/index.d.ts +2 -6
  70. package/dest/public/index.d.ts.map +1 -1
  71. package/dest/public/index.js +2 -6
  72. package/dest/public/public_db_sources.d.ts +19 -52
  73. package/dest/public/public_db_sources.d.ts.map +1 -1
  74. package/dest/public/public_db_sources.js +96 -107
  75. package/dest/public/public_processor/public_processor.d.ts +8 -8
  76. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  77. package/dest/public/public_processor/public_processor.js +26 -28
  78. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +3 -2
  79. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  80. package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +2 -2
  81. package/dest/public/public_tx_simulator/public_tx_context.d.ts +3 -4
  82. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  83. package/dest/public/public_tx_simulator/public_tx_context.js +11 -21
  84. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +5 -4
  85. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  86. package/dest/public/public_tx_simulator/public_tx_simulator.js +21 -10
  87. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +3 -2
  88. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  89. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +2 -2
  90. package/dest/public/side_effect_trace.d.ts +1 -3
  91. package/dest/public/side_effect_trace.d.ts.map +1 -1
  92. package/dest/public/side_effect_trace.js +3 -2
  93. package/dest/public/state_manager/state_manager.d.ts +6 -4
  94. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  95. package/dest/public/state_manager/state_manager.js +20 -41
  96. package/package.json +14 -16
  97. package/src/private/providers/acvm_native.ts +5 -4
  98. package/src/private/providers/acvm_wasm.ts +5 -2
  99. package/src/private/providers/acvm_wasm_with_blobs.ts +5 -3
  100. package/src/private/providers/circuit_recording/simulation_provider_recorder_wrapper.ts +3 -2
  101. package/src/private/providers/simulation_provider.ts +2 -1
  102. package/src/public/avm/avm_context.ts +2 -2
  103. package/src/public/avm/avm_simulator.ts +4 -8
  104. package/src/public/avm/avm_simulator_interface.ts +8 -0
  105. package/src/public/avm/errors.ts +1 -53
  106. package/src/public/avm/fixtures/avm_simulation_tester.ts +1 -1
  107. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
  108. package/src/public/avm/fixtures/index.ts +2 -308
  109. package/src/public/avm/fixtures/initializers.ts +101 -0
  110. package/src/public/avm/fixtures/utils.ts +213 -0
  111. package/src/public/avm/index.ts +0 -1
  112. package/src/public/avm/opcodes/accrued_substate.ts +1 -5
  113. package/src/public/avm/opcodes/external_calls.ts +17 -11
  114. package/src/public/avm/opcodes/instruction.ts +9 -8
  115. package/src/public/avm/revert_reason.ts +55 -0
  116. package/src/public/avm/serialization/bytecode_serialization.ts +72 -74
  117. package/src/{common → public}/debug_fn_name.ts +1 -1
  118. package/src/public/fixtures/index.ts +1 -0
  119. package/src/public/fixtures/public_tx_simulation_tester.ts +3 -5
  120. package/src/public/{avm/fixtures → fixtures}/simple_contract_data_source.ts +1 -1
  121. package/src/public/hinting_db_sources.ts +113 -41
  122. package/src/public/index.ts +2 -6
  123. package/src/public/public_db_sources.ts +111 -164
  124. package/src/public/public_processor/public_processor.ts +29 -31
  125. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +4 -3
  126. package/src/public/public_tx_simulator/public_tx_context.ts +10 -47
  127. package/src/public/public_tx_simulator/public_tx_simulator.ts +25 -10
  128. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +4 -3
  129. package/src/public/side_effect_trace.ts +2 -4
  130. package/src/public/state_manager/state_manager.ts +24 -50
  131. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +0 -1
  132. /package/dest/{common → public}/debug_fn_name.js +0 -0
  133. /package/dest/public/{avm/fixtures → fixtures}/simple_contract_data_source.d.ts +0 -0
@@ -1,3 +1,5 @@
1
+ import { type Bufferable, serializeToBuffer } from '@aztec/foundation/serialize';
2
+
1
3
  import { AvmExecutionError, AvmParsingError, InvalidOpcodeError, InvalidProgramCounterError } from '../errors.js';
2
4
  import {
3
5
  Add,
@@ -53,106 +55,102 @@ import { MAX_OPCODE_VALUE, Opcode } from './instruction_serialization.js';
53
55
 
54
56
  export type InstructionDeserializer = (buf: BufferCursor | Buffer) => Instruction;
55
57
 
56
- export interface Serializable {
57
- serialize(): Buffer;
58
- }
59
-
60
58
  export interface Deserializable {
61
59
  deserialize: InstructionDeserializer;
62
60
  }
63
61
 
64
62
  export type InstructionSet = Map<Opcode, InstructionDeserializer>;
65
63
  export const INSTRUCTION_SET = new Map<Opcode, InstructionDeserializer>([
66
- [Opcode.ADD_8, Add.as(Add.wireFormat8).deserialize],
67
- [Opcode.ADD_16, Add.as(Add.wireFormat16).deserialize],
68
- [Opcode.SUB_8, Sub.as(Sub.wireFormat8).deserialize],
69
- [Opcode.SUB_16, Sub.as(Sub.wireFormat16).deserialize],
70
- [Opcode.MUL_8, Mul.as(Mul.wireFormat8).deserialize],
71
- [Opcode.MUL_16, Mul.as(Mul.wireFormat16).deserialize],
72
- [Opcode.DIV_8, Div.as(Div.wireFormat8).deserialize],
73
- [Opcode.DIV_16, Div.as(Div.wireFormat16).deserialize],
74
- [Opcode.FDIV_8, FieldDiv.as(FieldDiv.wireFormat8).deserialize],
75
- [Opcode.FDIV_16, FieldDiv.as(FieldDiv.wireFormat16).deserialize],
76
- [Opcode.EQ_8, Eq.as(Eq.wireFormat8).deserialize],
77
- [Opcode.EQ_16, Eq.as(Eq.wireFormat16).deserialize],
78
- [Opcode.LT_8, Lt.as(Lt.wireFormat8).deserialize],
79
- [Opcode.LT_16, Lt.as(Lt.wireFormat16).deserialize],
80
- [Opcode.LTE_8, Lte.as(Lte.wireFormat8).deserialize],
81
- [Opcode.LTE_16, Lte.as(Lte.wireFormat16).deserialize],
82
- [Opcode.AND_8, And.as(And.wireFormat8).deserialize],
83
- [Opcode.AND_16, And.as(And.wireFormat16).deserialize],
84
- [Opcode.OR_8, Or.as(Or.wireFormat8).deserialize],
85
- [Opcode.OR_16, Or.as(Or.wireFormat16).deserialize],
86
- [Opcode.XOR_8, Xor.as(Xor.wireFormat8).deserialize],
87
- [Opcode.XOR_16, Xor.as(Xor.wireFormat16).deserialize],
88
- [Opcode.NOT_8, Not.as(Not.wireFormat8).deserialize],
89
- [Opcode.NOT_16, Not.as(Not.wireFormat16).deserialize],
90
- [Opcode.SHL_8, Shl.as(Shl.wireFormat8).deserialize],
91
- [Opcode.SHL_16, Shl.as(Shl.wireFormat16).deserialize],
92
- [Opcode.SHR_8, Shr.as(Shr.wireFormat8).deserialize],
93
- [Opcode.SHR_16, Shr.as(Shr.wireFormat16).deserialize],
94
- [Opcode.CAST_8, Cast.as(Cast.wireFormat8).deserialize],
95
- [Opcode.CAST_16, Cast.as(Cast.wireFormat16).deserialize],
64
+ [Opcode.ADD_8, Add.as(Add.wireFormat8).fromBuffer],
65
+ [Opcode.ADD_16, Add.as(Add.wireFormat16).fromBuffer],
66
+ [Opcode.SUB_8, Sub.as(Sub.wireFormat8).fromBuffer],
67
+ [Opcode.SUB_16, Sub.as(Sub.wireFormat16).fromBuffer],
68
+ [Opcode.MUL_8, Mul.as(Mul.wireFormat8).fromBuffer],
69
+ [Opcode.MUL_16, Mul.as(Mul.wireFormat16).fromBuffer],
70
+ [Opcode.DIV_8, Div.as(Div.wireFormat8).fromBuffer],
71
+ [Opcode.DIV_16, Div.as(Div.wireFormat16).fromBuffer],
72
+ [Opcode.FDIV_8, FieldDiv.as(FieldDiv.wireFormat8).fromBuffer],
73
+ [Opcode.FDIV_16, FieldDiv.as(FieldDiv.wireFormat16).fromBuffer],
74
+ [Opcode.EQ_8, Eq.as(Eq.wireFormat8).fromBuffer],
75
+ [Opcode.EQ_16, Eq.as(Eq.wireFormat16).fromBuffer],
76
+ [Opcode.LT_8, Lt.as(Lt.wireFormat8).fromBuffer],
77
+ [Opcode.LT_16, Lt.as(Lt.wireFormat16).fromBuffer],
78
+ [Opcode.LTE_8, Lte.as(Lte.wireFormat8).fromBuffer],
79
+ [Opcode.LTE_16, Lte.as(Lte.wireFormat16).fromBuffer],
80
+ [Opcode.AND_8, And.as(And.wireFormat8).fromBuffer],
81
+ [Opcode.AND_16, And.as(And.wireFormat16).fromBuffer],
82
+ [Opcode.OR_8, Or.as(Or.wireFormat8).fromBuffer],
83
+ [Opcode.OR_16, Or.as(Or.wireFormat16).fromBuffer],
84
+ [Opcode.XOR_8, Xor.as(Xor.wireFormat8).fromBuffer],
85
+ [Opcode.XOR_16, Xor.as(Xor.wireFormat16).fromBuffer],
86
+ [Opcode.NOT_8, Not.as(Not.wireFormat8).fromBuffer],
87
+ [Opcode.NOT_16, Not.as(Not.wireFormat16).fromBuffer],
88
+ [Opcode.SHL_8, Shl.as(Shl.wireFormat8).fromBuffer],
89
+ [Opcode.SHL_16, Shl.as(Shl.wireFormat16).fromBuffer],
90
+ [Opcode.SHR_8, Shr.as(Shr.wireFormat8).fromBuffer],
91
+ [Opcode.SHR_16, Shr.as(Shr.wireFormat16).fromBuffer],
92
+ [Opcode.CAST_8, Cast.as(Cast.wireFormat8).fromBuffer],
93
+ [Opcode.CAST_16, Cast.as(Cast.wireFormat16).fromBuffer],
96
94
  // Execution Environment
97
- [Opcode.GETENVVAR_16, GetEnvVar.as(GetEnvVar.wireFormat16).deserialize],
98
- [CalldataCopy.opcode, Instruction.deserialize.bind(CalldataCopy)],
99
- [SuccessCopy.opcode, Instruction.deserialize.bind(SuccessCopy)],
100
- [Opcode.RETURNDATASIZE, Instruction.deserialize.bind(ReturndataSize)],
101
- [Opcode.RETURNDATACOPY, Instruction.deserialize.bind(ReturndataCopy)],
95
+ [Opcode.GETENVVAR_16, GetEnvVar.as(GetEnvVar.wireFormat16).fromBuffer],
96
+ [CalldataCopy.opcode, Instruction.fromBuffer.bind(CalldataCopy)],
97
+ [SuccessCopy.opcode, Instruction.fromBuffer.bind(SuccessCopy)],
98
+ [Opcode.RETURNDATASIZE, Instruction.fromBuffer.bind(ReturndataSize)],
99
+ [Opcode.RETURNDATACOPY, Instruction.fromBuffer.bind(ReturndataCopy)],
102
100
 
103
101
  // Machine State - Internal Control Flow
104
- [Jump.opcode, Instruction.deserialize.bind(Jump)],
105
- [JumpI.opcode, Instruction.deserialize.bind(JumpI)],
106
- [InternalCall.opcode, Instruction.deserialize.bind(InternalCall)],
107
- [InternalReturn.opcode, Instruction.deserialize.bind(InternalReturn)],
108
- [Opcode.SET_8, Set.as(Set.wireFormat8).deserialize],
109
- [Opcode.SET_16, Set.as(Set.wireFormat16).deserialize],
110
- [Opcode.SET_32, Set.as(Set.wireFormat32).deserialize],
111
- [Opcode.SET_64, Set.as(Set.wireFormat64).deserialize],
112
- [Opcode.SET_128, Set.as(Set.wireFormat128).deserialize],
113
- [Opcode.SET_FF, Set.as(Set.wireFormatFF).deserialize],
114
- [Opcode.MOV_8, Mov.as(Mov.wireFormat8).deserialize],
115
- [Opcode.MOV_16, Mov.as(Mov.wireFormat16).deserialize],
102
+ [Jump.opcode, Instruction.fromBuffer.bind(Jump)],
103
+ [JumpI.opcode, Instruction.fromBuffer.bind(JumpI)],
104
+ [InternalCall.opcode, Instruction.fromBuffer.bind(InternalCall)],
105
+ [InternalReturn.opcode, Instruction.fromBuffer.bind(InternalReturn)],
106
+ [Opcode.SET_8, Set.as(Set.wireFormat8).fromBuffer],
107
+ [Opcode.SET_16, Set.as(Set.wireFormat16).fromBuffer],
108
+ [Opcode.SET_32, Set.as(Set.wireFormat32).fromBuffer],
109
+ [Opcode.SET_64, Set.as(Set.wireFormat64).fromBuffer],
110
+ [Opcode.SET_128, Set.as(Set.wireFormat128).fromBuffer],
111
+ [Opcode.SET_FF, Set.as(Set.wireFormatFF).fromBuffer],
112
+ [Opcode.MOV_8, Mov.as(Mov.wireFormat8).fromBuffer],
113
+ [Opcode.MOV_16, Mov.as(Mov.wireFormat16).fromBuffer],
116
114
 
117
115
  // World State
118
- [SLoad.opcode, Instruction.deserialize.bind(SLoad)], // Public Storage
119
- [SStore.opcode, Instruction.deserialize.bind(SStore)], // Public Storage
120
- [NoteHashExists.opcode, Instruction.deserialize.bind(NoteHashExists)], // Notes & Nullifiers
121
- [EmitNoteHash.opcode, Instruction.deserialize.bind(EmitNoteHash)], // Notes & Nullifiers
122
- [NullifierExists.opcode, Instruction.deserialize.bind(NullifierExists)], // Notes & Nullifiers
123
- [EmitNullifier.opcode, Instruction.deserialize.bind(EmitNullifier)], // Notes & Nullifiers
124
- [L1ToL2MessageExists.opcode, Instruction.deserialize.bind(L1ToL2MessageExists)], // Messages
116
+ [SLoad.opcode, Instruction.fromBuffer.bind(SLoad)], // Public Storage
117
+ [SStore.opcode, Instruction.fromBuffer.bind(SStore)], // Public Storage
118
+ [NoteHashExists.opcode, Instruction.fromBuffer.bind(NoteHashExists)], // Notes & Nullifiers
119
+ [EmitNoteHash.opcode, Instruction.fromBuffer.bind(EmitNoteHash)], // Notes & Nullifiers
120
+ [NullifierExists.opcode, Instruction.fromBuffer.bind(NullifierExists)], // Notes & Nullifiers
121
+ [EmitNullifier.opcode, Instruction.fromBuffer.bind(EmitNullifier)], // Notes & Nullifiers
122
+ [L1ToL2MessageExists.opcode, Instruction.fromBuffer.bind(L1ToL2MessageExists)], // Messages
125
123
 
126
124
  // Accrued Substate
127
- [EmitUnencryptedLog.opcode, Instruction.deserialize.bind(EmitUnencryptedLog)],
128
- [SendL2ToL1Message.opcode, Instruction.deserialize.bind(SendL2ToL1Message)],
129
- [GetContractInstance.opcode, Instruction.deserialize.bind(GetContractInstance)],
125
+ [EmitUnencryptedLog.opcode, Instruction.fromBuffer.bind(EmitUnencryptedLog)],
126
+ [SendL2ToL1Message.opcode, Instruction.fromBuffer.bind(SendL2ToL1Message)],
127
+ [GetContractInstance.opcode, Instruction.fromBuffer.bind(GetContractInstance)],
130
128
 
131
129
  // Control Flow - Contract Calls
132
- [Call.opcode, Instruction.deserialize.bind(Call)],
133
- [StaticCall.opcode, Instruction.deserialize.bind(StaticCall)],
134
- [Return.opcode, Instruction.deserialize.bind(Return)],
135
- [Opcode.REVERT_8, Revert.as(Revert.wireFormat8).deserialize],
136
- [Opcode.REVERT_16, Revert.as(Revert.wireFormat16).deserialize],
130
+ [Call.opcode, Instruction.fromBuffer.bind(Call)],
131
+ [StaticCall.opcode, Instruction.fromBuffer.bind(StaticCall)],
132
+ [Return.opcode, Instruction.fromBuffer.bind(Return)],
133
+ [Opcode.REVERT_8, Revert.as(Revert.wireFormat8).fromBuffer],
134
+ [Opcode.REVERT_16, Revert.as(Revert.wireFormat16).fromBuffer],
137
135
 
138
136
  // Misc
139
- [DebugLog.opcode, Instruction.deserialize.bind(DebugLog)],
137
+ [DebugLog.opcode, Instruction.fromBuffer.bind(DebugLog)],
140
138
 
141
139
  // Gadgets
142
- [EcAdd.opcode, Instruction.deserialize.bind(EcAdd)],
143
- [Poseidon2.opcode, Instruction.deserialize.bind(Poseidon2)],
144
- [Sha256Compression.opcode, Instruction.deserialize.bind(Sha256Compression)],
145
- [KeccakF1600.opcode, Instruction.deserialize.bind(KeccakF1600)],
140
+ [EcAdd.opcode, Instruction.fromBuffer.bind(EcAdd)],
141
+ [Poseidon2.opcode, Instruction.fromBuffer.bind(Poseidon2)],
142
+ [Sha256Compression.opcode, Instruction.fromBuffer.bind(Sha256Compression)],
143
+ [KeccakF1600.opcode, Instruction.fromBuffer.bind(KeccakF1600)],
146
144
 
147
145
  // Conversions
148
- [ToRadixBE.opcode, Instruction.deserialize.bind(ToRadixBE)],
146
+ [ToRadixBE.opcode, Instruction.fromBuffer.bind(ToRadixBE)],
149
147
  ]);
150
148
 
151
149
  /**
152
150
  * Serializes an array of instructions to bytecode.
153
151
  */
154
- export function encodeToBytecode(instructions: Serializable[]): Buffer {
155
- return Buffer.concat(instructions.map(i => i.serialize()));
152
+ export function encodeToBytecode(instructions: Bufferable[]): Buffer {
153
+ return serializeToBuffer(instructions);
156
154
  }
157
155
 
158
156
  // For testing only
@@ -2,7 +2,7 @@ import type { Fr } from '@aztec/foundation/fields';
2
2
  import { FunctionSelector } from '@aztec/stdlib/abi';
3
3
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
4
 
5
- import type { PublicContractsDBInterface } from '../public/db_interfaces.js';
5
+ import type { PublicContractsDBInterface } from './db_interfaces.js';
6
6
 
7
7
  export async function getPublicFunctionDebugName(
8
8
  db: PublicContractsDBInterface,
@@ -1,2 +1,3 @@
1
1
  export * from './public_tx_simulation_tester.js';
2
2
  export * from './utils.js';
3
+ export * from './simple_contract_data_source.js';
@@ -10,11 +10,11 @@ import { NativeWorldStateService } from '@aztec/world-state';
10
10
 
11
11
  import { BaseAvmSimulationTester } from '../avm/fixtures/base_avm_simulation_tester.js';
12
12
  import { DEFAULT_BLOCK_NUMBER, getContractFunctionAbi, getFunctionSelector } from '../avm/fixtures/index.js';
13
- import { SimpleContractDataSource } from '../avm/fixtures/simple_contract_data_source.js';
14
- import { PublicContractsDB, PublicTreesDB } from '../public_db_sources.js';
13
+ import { PublicContractsDB } from '../public_db_sources.js';
15
14
  import { MeasuredPublicTxSimulator } from '../public_tx_simulator/measured_public_tx_simulator.js';
16
15
  import type { PublicTxResult } from '../public_tx_simulator/public_tx_simulator.js';
17
16
  import { TestExecutorMetrics } from '../test_executor_metrics.js';
17
+ import { SimpleContractDataSource } from './simple_contract_data_source.js';
18
18
  import { createTxForPublicCalls } from './utils.js';
19
19
 
20
20
  const TIMESTAMP = new Fr(99833);
@@ -47,11 +47,9 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
47
47
  ) {
48
48
  super(contractDataSource, merkleTree);
49
49
 
50
- const treesDB = new PublicTreesDB(merkleTree);
51
50
  const contractsDB = new PublicContractsDB(contractDataSource);
52
-
53
51
  this.simulator = new MeasuredPublicTxSimulator(
54
- treesDB,
52
+ merkleTree,
55
53
  contractsDB,
56
54
  globals,
57
55
  /*doMerkleOperations=*/ true,
@@ -4,7 +4,7 @@ import type { ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
4
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
5
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
6
6
 
7
- import { getFunctionSelector } from './index.js';
7
+ import { getFunctionSelector } from '../avm/fixtures/index.js';
8
8
 
9
9
  /**
10
10
  * This class is used during public/avm testing to function as a database of
@@ -24,9 +24,11 @@ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
24
24
  import type { ContractClassPublic, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
25
25
  import {
26
26
  AppendOnlyTreeSnapshot,
27
+ type BatchInsertionResult,
27
28
  type IndexedTreeId,
28
29
  MerkleTreeId,
29
30
  type MerkleTreeLeafType,
31
+ type MerkleTreeWriteOperations,
30
32
  NullifierLeaf,
31
33
  NullifierLeafPreimage,
32
34
  PublicDataTreeLeaf,
@@ -35,11 +37,11 @@ import {
35
37
  getTreeName,
36
38
  merkleTreeIds,
37
39
  } from '@aztec/stdlib/trees';
40
+ import { TreeSnapshots } from '@aztec/stdlib/tx';
38
41
 
39
42
  import { strict as assert } from 'assert';
40
43
 
41
44
  import type { PublicContractsDBInterface } from './db_interfaces.js';
42
- import { PublicTreesDB } from './public_db_sources.js';
43
45
 
44
46
  /**
45
47
  * A public contracts database that forwards requests and collects AVM hints.
@@ -101,10 +103,10 @@ export class HintingPublicContractsDB implements PublicContractsDBInterface {
101
103
  }
102
104
 
103
105
  /**
104
- * A public trees database that forwards requests and collects AVM hints.
106
+ * A low-level merkle DB that collects hints.
105
107
  */
106
- export class HintingPublicTreesDB extends PublicTreesDB {
107
- private static readonly log: Logger = createLogger('HintingPublicTreesDB');
108
+ export class HintingMerkleWriteOperations implements MerkleTreeWriteOperations {
109
+ private static readonly log: Logger = createLogger('simulator:hinting-merkle-db');
108
110
  // This stack is only for debugging purposes.
109
111
  // The top of the stack is the current checkpoint id.
110
112
  // We need the stack to be non-empty and use 0 as an arbitrary initial checkpoint id.
@@ -113,19 +115,31 @@ export class HintingPublicTreesDB extends PublicTreesDB {
113
115
  private nextCheckpointId: number = 1;
114
116
  private checkpointActionCounter: number = 0; // yes, a side-effect counter.
115
117
 
116
- constructor(db: PublicTreesDB, private hints: AvmExecutionHints) {
117
- super(db);
118
+ public static async create(db: MerkleTreeWriteOperations, hints: AvmExecutionHints) {
119
+ const hintingTreesDB = new HintingMerkleWriteOperations(db, hints);
120
+ const startStateReference = await db.getStateReference();
121
+ hints.startingTreeRoots = new TreeSnapshots(
122
+ startStateReference.l1ToL2MessageTree,
123
+ startStateReference.partial.noteHashTree,
124
+ startStateReference.partial.nullifierTree,
125
+ startStateReference.partial.publicDataTree,
126
+ );
127
+
128
+ return hintingTreesDB;
118
129
  }
119
130
 
131
+ // Use create() to instantiate.
132
+ private constructor(private db: MerkleTreeWriteOperations, private hints: AvmExecutionHints) {}
133
+
120
134
  // Getters.
121
- public override async getSiblingPath<N extends number>(treeId: MerkleTreeId, index: bigint): Promise<SiblingPath<N>> {
122
- const path = await super.getSiblingPath<N>(treeId, index);
135
+ public async getSiblingPath<N extends number>(treeId: MerkleTreeId, index: bigint): Promise<SiblingPath<N>> {
136
+ const path = await this.db.getSiblingPath<N>(treeId, index);
123
137
  const key = await this.getHintKey(treeId);
124
138
  this.hints.getSiblingPathHints.push(new AvmGetSiblingPathHint(key, treeId, index, path.toFields()));
125
139
  return Promise.resolve(path);
126
140
  }
127
141
 
128
- public override async getPreviousValueIndex<ID extends IndexedTreeId>(
142
+ public async getPreviousValueIndex<ID extends IndexedTreeId>(
129
143
  treeId: ID,
130
144
  value: bigint,
131
145
  ): Promise<
@@ -135,7 +149,7 @@ export class HintingPublicTreesDB extends PublicTreesDB {
135
149
  }
136
150
  | undefined
137
151
  > {
138
- const result = await super.getPreviousValueIndex(treeId, value);
152
+ const result = await this.db.getPreviousValueIndex(treeId, value);
139
153
  if (result === undefined) {
140
154
  throw new Error(
141
155
  `getPreviousValueIndex(${getTreeName(
@@ -150,11 +164,11 @@ export class HintingPublicTreesDB extends PublicTreesDB {
150
164
  return result;
151
165
  }
152
166
 
153
- public override async getLeafPreimage<ID extends IndexedTreeId>(
167
+ public async getLeafPreimage<ID extends IndexedTreeId>(
154
168
  treeId: ID,
155
169
  index: bigint,
156
170
  ): Promise<IndexedTreeLeafPreimage | undefined> {
157
- const preimage = await super.getLeafPreimage<ID>(treeId, index);
171
+ const preimage = await this.db.getLeafPreimage<ID>(treeId, index);
158
172
  if (preimage) {
159
173
  const key = await this.getHintKey(treeId);
160
174
 
@@ -179,14 +193,14 @@ export class HintingPublicTreesDB extends PublicTreesDB {
179
193
  return preimage;
180
194
  }
181
195
 
182
- public override async getLeafValue<ID extends MerkleTreeId>(
196
+ public async getLeafValue<ID extends MerkleTreeId>(
183
197
  treeId: ID,
184
198
  index: bigint,
185
199
  ): Promise<MerkleTreeLeafType<typeof treeId> | undefined> {
186
200
  // Use getLeafPreimage for PublicDataTree and NullifierTree.
187
201
  assert(treeId == MerkleTreeId.NOTE_HASH_TREE || treeId == MerkleTreeId.L1_TO_L2_MESSAGE_TREE);
188
202
 
189
- const value = await super.getLeafValue<ID>(treeId, index);
203
+ const value = await this.db.getLeafValue<ID>(treeId, index);
190
204
  if (value) {
191
205
  const key = await this.getHintKey(treeId);
192
206
  // We can cast to Fr because we know the type of the tree.
@@ -199,7 +213,7 @@ export class HintingPublicTreesDB extends PublicTreesDB {
199
213
  // State modification.
200
214
  // FIXME(fcarreiro): This is a horrible interface (in the merkle ops). It's receiving the leaves as buffers,
201
215
  // from a leaf class that is NOT the one that will be used to write. Make this type safe.
202
- public override async sequentialInsert<TreeHeight extends number, ID extends IndexedTreeId>(
216
+ public async sequentialInsert<TreeHeight extends number, ID extends IndexedTreeId>(
203
217
  treeId: ID,
204
218
  leaves: Buffer[],
205
219
  ): Promise<SequentialInsertionResult<TreeHeight>> {
@@ -210,11 +224,10 @@ export class HintingPublicTreesDB extends PublicTreesDB {
210
224
 
211
225
  const beforeState = await this.getHintKey(treeId);
212
226
 
213
- const result = await super.sequentialInsert<TreeHeight, ID>(treeId, leaves);
227
+ const result = await this.db.sequentialInsert<TreeHeight, ID>(treeId, leaves);
214
228
 
215
229
  const afterState = await this.getHintKey(treeId);
216
- HintingPublicTreesDB.log.debug('[sequentialInsert] Evolved tree state.');
217
- HintingPublicTreesDB.logTreeChange(beforeState, afterState, treeId);
230
+ HintingMerkleWriteOperations.logTreeChange('sequentialInsert', beforeState, afterState, treeId);
218
231
 
219
232
  switch (treeId) {
220
233
  case MerkleTreeId.PUBLIC_DATA_TREE:
@@ -265,53 +278,57 @@ export class HintingPublicTreesDB extends PublicTreesDB {
265
278
  return result;
266
279
  }
267
280
 
268
- public override async appendLeaves<ID extends MerkleTreeId>(
269
- treeId: ID,
270
- leaves: MerkleTreeLeafType<ID>[],
271
- ): Promise<void> {
281
+ public async appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<ID>[]): Promise<void> {
272
282
  // Use sequentialInsert for PublicDataTree and NullifierTree.
273
283
  assert(treeId == MerkleTreeId.NOTE_HASH_TREE || treeId == MerkleTreeId.L1_TO_L2_MESSAGE_TREE);
274
284
 
275
285
  // We need to process each leaf individually because we need the sibling path after insertion, to be able to constraint the insertion.
276
286
  // TODO(https://github.com/AztecProtocol/aztec-packages/issues/13380): This can be changed if the world state appendLeaves returns the sibling paths.
277
- for (const leaf of leaves) {
278
- await this.appendLeafInternal(treeId, leaf);
287
+ if (leaves.length === 1) {
288
+ await this.appendLeafInternal(treeId, leaves[0]);
289
+ return;
290
+ } else {
291
+ // TODO(dbanks12): NON-HINTING! We skip hinting here for now because:
292
+ // 1. We only ever append multiple leaves (for now) when padding (all empty leaves).
293
+ // 2. We don't need hints per-item when padding.
294
+ // 3. In order to get per-item hints today, you need to append one-at-a-time (mentioned above), which is VERY slow.
295
+ await this.db.appendLeaves<ID>(treeId, leaves);
279
296
  }
280
297
  }
281
298
 
282
- public override async createCheckpoint(): Promise<void> {
299
+ public async createCheckpoint(): Promise<void> {
283
300
  const actionCounter = this.checkpointActionCounter++;
284
301
  const oldCheckpointId = this.getCurrentCheckpointId();
285
302
  const treesStateHash = await this.getTreesStateHash();
286
303
 
287
- await super.createCheckpoint();
304
+ await this.db.createCheckpoint();
288
305
  this.checkpointStack.push(this.nextCheckpointId++);
289
306
  const newCheckpointId = this.getCurrentCheckpointId();
290
307
 
291
308
  this.hints.createCheckpointHints.push(new AvmCreateCheckpointHint(actionCounter, oldCheckpointId, newCheckpointId));
292
309
 
293
- HintingPublicTreesDB.log.debug(
310
+ HintingMerkleWriteOperations.log.trace(
294
311
  `[createCheckpoint:${actionCounter}] Checkpoint evolved ${oldCheckpointId} -> ${newCheckpointId} at trees state ${treesStateHash}.`,
295
312
  );
296
313
  }
297
314
 
298
- public override async commitCheckpoint(): Promise<void> {
315
+ public async commitCheckpoint(): Promise<void> {
299
316
  const actionCounter = this.checkpointActionCounter++;
300
317
  const oldCheckpointId = this.getCurrentCheckpointId();
301
318
  const treesStateHash = await this.getTreesStateHash();
302
319
 
303
- await super.commitCheckpoint();
320
+ await this.db.commitCheckpoint();
304
321
  this.checkpointStack.pop();
305
322
  const newCheckpointId = this.getCurrentCheckpointId();
306
323
 
307
324
  this.hints.commitCheckpointHints.push(new AvmCommitCheckpointHint(actionCounter, oldCheckpointId, newCheckpointId));
308
325
 
309
- HintingPublicTreesDB.log.debug(
326
+ HintingMerkleWriteOperations.log.trace(
310
327
  `[commitCheckpoint:${actionCounter}] Checkpoint evolved ${oldCheckpointId} -> ${newCheckpointId} at trees state ${treesStateHash}.`,
311
328
  );
312
329
  }
313
330
 
314
- public override async revertCheckpoint(): Promise<void> {
331
+ public async revertCheckpoint(): Promise<void> {
315
332
  const actionCounter = this.checkpointActionCounter++;
316
333
  const oldCheckpointId = this.getCurrentCheckpointId();
317
334
  const treesStateHash = await this.getTreesStateHash();
@@ -324,7 +341,7 @@ export class HintingPublicTreesDB extends PublicTreesDB {
324
341
  [MerkleTreeId.ARCHIVE]: await this.getHintKey(MerkleTreeId.ARCHIVE),
325
342
  };
326
343
 
327
- await super.revertCheckpoint();
344
+ await this.db.revertCheckpoint();
328
345
  this.checkpointStack.pop();
329
346
  const newCheckpointId = this.getCurrentCheckpointId();
330
347
 
@@ -340,17 +357,17 @@ export class HintingPublicTreesDB extends PublicTreesDB {
340
357
  AvmRevertCheckpointHint.create(actionCounter, oldCheckpointId, newCheckpointId, beforeState, afterState),
341
358
  );
342
359
 
343
- HintingPublicTreesDB.log.debug(
360
+ HintingMerkleWriteOperations.log.trace(
344
361
  `[revertCheckpoint:${actionCounter}] Checkpoint evolved ${oldCheckpointId} -> ${newCheckpointId} at trees state ${treesStateHash}.`,
345
362
  );
346
363
  for (const treeId of merkleTreeIds()) {
347
- HintingPublicTreesDB.logTreeChange(beforeState[treeId], afterState[treeId], treeId);
364
+ HintingMerkleWriteOperations.logTreeChange('revertCheckpoint', beforeState[treeId], afterState[treeId], treeId);
348
365
  }
349
366
  }
350
367
 
351
368
  // Private methods.
352
369
  private async getHintKey(treeId: MerkleTreeId): Promise<AppendOnlyTreeSnapshot> {
353
- const treeInfo = await super.getTreeInfo(treeId);
370
+ const treeInfo = await this.db.getTreeInfo(treeId);
354
371
  return new AppendOnlyTreeSnapshot(Fr.fromBuffer(treeInfo.root), Number(treeInfo.size));
355
372
  }
356
373
 
@@ -360,18 +377,19 @@ export class HintingPublicTreesDB extends PublicTreesDB {
360
377
 
361
378
  // For logging/debugging purposes.
362
379
  private async getTreesStateHash(): Promise<Fr> {
363
- const stateReferenceFields = (await super.getStateReference()).toFields();
380
+ const stateReferenceFields = (await this.db.getStateReference()).toFields();
364
381
  return Fr.fromBuffer(sha256Trunc(Buffer.concat(stateReferenceFields.map(field => field.toBuffer()))));
365
382
  }
366
383
 
367
384
  private static logTreeChange(
385
+ action: string,
368
386
  beforeState: AppendOnlyTreeSnapshot,
369
387
  afterState: AppendOnlyTreeSnapshot,
370
388
  treeId: MerkleTreeId,
371
389
  ) {
372
390
  const treeName = getTreeName(treeId);
373
- HintingPublicTreesDB.log.debug(
374
- `[${treeName}] Evolved tree state: ${beforeState.root}, ${beforeState.nextAvailableLeafIndex} -> ${afterState.root}, ${afterState.nextAvailableLeafIndex}.`,
391
+ HintingMerkleWriteOperations.log.trace(
392
+ `[${action}] ${treeName} tree state: ${beforeState.root}, ${beforeState.nextAvailableLeafIndex} -> ${afterState.root}, ${afterState.nextAvailableLeafIndex}.`,
375
393
  );
376
394
  }
377
395
 
@@ -384,15 +402,69 @@ export class HintingPublicTreesDB extends PublicTreesDB {
384
402
 
385
403
  const beforeState = await this.getHintKey(treeId);
386
404
 
387
- await super.appendLeaves<ID>(treeId, [leaf]);
405
+ await this.db.appendLeaves<ID>(treeId, [leaf]);
388
406
 
389
407
  const afterState = await this.getHintKey(treeId);
390
408
 
391
- HintingPublicTreesDB.log.debug('[appendLeaves] Evolved tree state.');
392
- HintingPublicTreesDB.logTreeChange(beforeState, afterState, treeId);
409
+ HintingMerkleWriteOperations.logTreeChange('appendLeaves', beforeState, afterState, treeId);
393
410
 
394
411
  this.hints.appendLeavesHints.push(new AvmAppendLeavesHint(beforeState, afterState, treeId, [leaf as Fr]));
395
412
 
396
413
  return await this.getSiblingPath<N>(treeId, BigInt(beforeState.nextAvailableLeafIndex));
397
414
  }
415
+
416
+ // Non-hinted required methods from MerkleTreeWriteOperations interface
417
+ public async getTreeInfo(treeId: MerkleTreeId) {
418
+ return await this.db.getTreeInfo(treeId);
419
+ }
420
+
421
+ public async getStateReference() {
422
+ return await this.db.getStateReference();
423
+ }
424
+
425
+ public getInitialHeader() {
426
+ return this.db.getInitialHeader();
427
+ }
428
+
429
+ public async updateArchive(header: any): Promise<void> {
430
+ return await this.db.updateArchive(header);
431
+ }
432
+
433
+ public async batchInsert<
434
+ TreeHeight extends number,
435
+ SubtreeSiblingPathHeight extends number,
436
+ ID extends IndexedTreeId,
437
+ >(
438
+ treeId: ID,
439
+ leaves: Buffer[],
440
+ subtreeHeight: number,
441
+ ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
442
+ return await this.db.batchInsert<TreeHeight, SubtreeSiblingPathHeight, ID>(treeId, leaves, subtreeHeight);
443
+ }
444
+
445
+ public async close(): Promise<void> {
446
+ return await this.db.close();
447
+ }
448
+
449
+ public async findLeafIndices<ID extends MerkleTreeId>(
450
+ treeId: ID,
451
+ values: MerkleTreeLeafType<ID>[],
452
+ ): Promise<(bigint | undefined)[]> {
453
+ return await this.db.findLeafIndices(treeId, values);
454
+ }
455
+
456
+ public async findLeafIndicesAfter<ID extends MerkleTreeId>(
457
+ treeId: ID,
458
+ values: MerkleTreeLeafType<ID>[],
459
+ startIndex: bigint,
460
+ ): Promise<(bigint | undefined)[]> {
461
+ return await this.db.findLeafIndicesAfter(treeId, values, startIndex);
462
+ }
463
+
464
+ public async getBlockNumbersForLeafIndices<ID extends MerkleTreeId>(
465
+ treeId: ID,
466
+ leafIndices: bigint[],
467
+ ): Promise<(bigint | undefined)[]> {
468
+ return await this.db.getBlockNumbersForLeafIndices(treeId, leafIndices);
469
+ }
398
470
  }
@@ -1,8 +1,4 @@
1
- export * from './db_interfaces.js';
2
- export * from './public_tx_simulator/index.js';
3
- export * from './public_db_sources.js';
1
+ export { PublicContractsDB } from './public_db_sources.js';
2
+ export { type PublicTxResult, PublicTxSimulator, TelemetryPublicTxSimulator } from './public_tx_simulator/index.js';
4
3
  export { PublicProcessor, PublicProcessorFactory } from './public_processor/public_processor.js';
5
- export { SideEffectTrace } from './side_effect_trace.js';
6
- export { PublicTxSimulationTester } from './fixtures/index.js';
7
- export * from './avm/index.js';
8
4
  export { getCallRequestsWithCalldataByPhase } from './utils.js';