@aztec/simulator 0.34.0 → 0.35.0

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 (168) hide show
  1. package/dest/acvm/acvm.d.ts +4 -1
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +5 -5
  4. package/dest/acvm/deserialize.d.ts +3 -5
  5. package/dest/acvm/deserialize.d.ts.map +1 -1
  6. package/dest/acvm/deserialize.js +6 -9
  7. package/dest/acvm/oracle/oracle.d.ts +2 -0
  8. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/oracle.js +12 -4
  10. package/dest/acvm/oracle/typed_oracle.d.ts +2 -0
  11. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  12. package/dest/acvm/oracle/typed_oracle.js +7 -1
  13. package/dest/avm/avm_execution_environment.d.ts +4 -2
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +7 -5
  16. package/dest/avm/avm_gas.js +2 -2
  17. package/dest/avm/avm_machine_state.d.ts +2 -0
  18. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  19. package/dest/avm/avm_machine_state.js +24 -6
  20. package/dest/avm/avm_memory_types.js +2 -2
  21. package/dest/avm/avm_simulator.js +2 -2
  22. package/dest/avm/fixtures/index.d.ts +3 -0
  23. package/dest/avm/fixtures/index.d.ts.map +1 -1
  24. package/dest/avm/fixtures/index.js +11 -4
  25. package/dest/avm/journal/journal.d.ts +1 -0
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +12 -1
  28. package/dest/avm/opcodes/accrued_substate.d.ts +2 -1
  29. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  30. package/dest/avm/opcodes/accrued_substate.js +13 -5
  31. package/dest/avm/opcodes/context_getters.js +2 -2
  32. package/dest/avm/opcodes/external_calls.d.ts +2 -2
  33. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  34. package/dest/avm/opcodes/external_calls.js +7 -6
  35. package/dest/avm/opcodes/hashing.d.ts +8 -8
  36. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  37. package/dest/avm/opcodes/hashing.js +35 -43
  38. package/dest/avm/serialization/instruction_serialization.d.ts +1 -1
  39. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  40. package/dest/avm/serialization/instruction_serialization.js +2 -2
  41. package/dest/client/client_execution_context.d.ts +13 -3
  42. package/dest/client/client_execution_context.d.ts.map +1 -1
  43. package/dest/client/client_execution_context.js +23 -8
  44. package/dest/client/private_execution.d.ts.map +1 -1
  45. package/dest/client/private_execution.js +8 -6
  46. package/dest/client/simulator.d.ts.map +1 -1
  47. package/dest/client/simulator.js +6 -5
  48. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  49. package/dest/client/unconstrained_execution.js +5 -4
  50. package/dest/common/index.d.ts +1 -1
  51. package/dest/common/index.d.ts.map +1 -1
  52. package/dest/common/index.js +2 -2
  53. package/dest/common/packed_values_cache.d.ts +28 -0
  54. package/dest/common/packed_values_cache.d.ts.map +1 -0
  55. package/dest/common/packed_values_cache.js +50 -0
  56. package/dest/index.d.ts +1 -0
  57. package/dest/index.d.ts.map +1 -1
  58. package/dest/index.js +2 -1
  59. package/dest/mocks/fixtures.d.ts +42 -0
  60. package/dest/mocks/fixtures.d.ts.map +1 -0
  61. package/dest/mocks/fixtures.js +84 -0
  62. package/dest/mocks/index.d.ts +2 -0
  63. package/dest/mocks/index.d.ts.map +1 -0
  64. package/dest/mocks/index.js +2 -0
  65. package/dest/public/abstract_phase_manager.d.ts +82 -0
  66. package/dest/public/abstract_phase_manager.d.ts.map +1 -0
  67. package/dest/public/abstract_phase_manager.js +320 -0
  68. package/dest/public/app_logic_phase_manager.d.ts +29 -0
  69. package/dest/public/app_logic_phase_manager.d.ts.map +1 -0
  70. package/dest/public/app_logic_phase_manager.js +50 -0
  71. package/dest/public/execution.d.ts +3 -0
  72. package/dest/public/execution.d.ts.map +1 -1
  73. package/dest/public/execution.js +1 -1
  74. package/dest/public/executor.d.ts.map +1 -1
  75. package/dest/public/executor.js +17 -13
  76. package/dest/public/hints_builder.d.ts +23 -0
  77. package/dest/public/hints_builder.d.ts.map +1 -0
  78. package/dest/public/hints_builder.js +62 -0
  79. package/dest/public/index.d.ts +5 -0
  80. package/dest/public/index.d.ts.map +1 -1
  81. package/dest/public/index.js +6 -1
  82. package/dest/public/phase_manager_factory.d.ts +18 -0
  83. package/dest/public/phase_manager_factory.d.ts.map +1 -0
  84. package/dest/public/phase_manager_factory.js +56 -0
  85. package/dest/public/public_execution_context.d.ts +15 -5
  86. package/dest/public/public_execution_context.d.ts.map +1 -1
  87. package/dest/public/public_execution_context.js +28 -12
  88. package/dest/public/public_executor.d.ts +79 -0
  89. package/dest/public/public_executor.d.ts.map +1 -0
  90. package/dest/public/public_executor.js +198 -0
  91. package/dest/public/public_kernel.d.ts +37 -0
  92. package/dest/public/public_kernel.d.ts.map +1 -0
  93. package/dest/public/public_kernel.js +97 -0
  94. package/dest/public/public_kernel_circuit_simulator.d.ts +31 -0
  95. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -0
  96. package/dest/public/public_kernel_circuit_simulator.js +2 -0
  97. package/dest/public/public_processor.d.ts +53 -0
  98. package/dest/public/public_processor.d.ts.map +1 -0
  99. package/dest/public/public_processor.js +144 -0
  100. package/dest/public/setup_phase_manager.d.ts +30 -0
  101. package/dest/public/setup_phase_manager.d.ts.map +1 -0
  102. package/dest/public/setup_phase_manager.js +46 -0
  103. package/dest/public/tail_phase_manager.d.ts +30 -0
  104. package/dest/public/tail_phase_manager.d.ts.map +1 -0
  105. package/dest/public/tail_phase_manager.js +60 -0
  106. package/dest/public/teardown_phase_manager.d.ts +30 -0
  107. package/dest/public/teardown_phase_manager.d.ts.map +1 -0
  108. package/dest/public/teardown_phase_manager.js +46 -0
  109. package/dest/public/transitional_adaptors.d.ts +2 -1
  110. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  111. package/dest/public/transitional_adaptors.js +11 -10
  112. package/dest/public/utils.d.ts +8 -0
  113. package/dest/public/utils.d.ts.map +1 -0
  114. package/dest/public/utils.js +29 -0
  115. package/dest/simulator/acvm_native.d.ts +19 -3
  116. package/dest/simulator/acvm_native.d.ts.map +1 -1
  117. package/dest/simulator/acvm_native.js +75 -48
  118. package/dest/simulator/acvm_wasm.d.ts.map +1 -1
  119. package/dest/simulator/acvm_wasm.js +3 -4
  120. package/package.json +8 -5
  121. package/src/acvm/acvm.ts +8 -5
  122. package/src/acvm/deserialize.ts +5 -9
  123. package/src/acvm/oracle/oracle.ts +13 -3
  124. package/src/acvm/oracle/typed_oracle.ts +8 -0
  125. package/src/avm/avm_execution_environment.ts +9 -1
  126. package/src/avm/avm_gas.ts +1 -1
  127. package/src/avm/avm_machine_state.ts +26 -5
  128. package/src/avm/avm_memory_types.ts +1 -1
  129. package/src/avm/avm_simulator.ts +1 -1
  130. package/src/avm/fixtures/index.ts +13 -1
  131. package/src/avm/journal/journal.ts +13 -0
  132. package/src/avm/opcodes/accrued_substate.ts +16 -4
  133. package/src/avm/opcodes/context_getters.ts +1 -1
  134. package/src/avm/opcodes/external_calls.ts +8 -5
  135. package/src/avm/opcodes/hashing.ts +38 -54
  136. package/src/avm/serialization/instruction_serialization.ts +1 -1
  137. package/src/client/client_execution_context.ts +25 -6
  138. package/src/client/private_execution.ts +7 -6
  139. package/src/client/simulator.ts +7 -3
  140. package/src/client/unconstrained_execution.ts +4 -3
  141. package/src/common/index.ts +1 -1
  142. package/src/common/packed_values_cache.ts +55 -0
  143. package/src/index.ts +1 -0
  144. package/src/mocks/fixtures.ts +169 -0
  145. package/src/mocks/index.ts +1 -0
  146. package/src/public/abstract_phase_manager.ts +571 -0
  147. package/src/public/app_logic_phase_manager.ts +76 -0
  148. package/src/public/execution.ts +4 -0
  149. package/src/public/executor.ts +18 -13
  150. package/src/public/hints_builder.ts +119 -0
  151. package/src/public/index.ts +5 -0
  152. package/src/public/phase_manager_factory.ts +126 -0
  153. package/src/public/public_execution_context.ts +29 -18
  154. package/src/public/public_executor.ts +267 -0
  155. package/src/public/public_kernel.ts +139 -0
  156. package/src/public/public_kernel_circuit_simulator.ts +36 -0
  157. package/src/public/public_processor.ts +212 -0
  158. package/src/public/setup_phase_manager.ts +66 -0
  159. package/src/public/tail_phase_manager.ts +120 -0
  160. package/src/public/teardown_phase_manager.ts +66 -0
  161. package/src/public/transitional_adaptors.ts +14 -5
  162. package/src/public/utils.ts +31 -0
  163. package/src/simulator/acvm_native.ts +94 -47
  164. package/src/simulator/acvm_wasm.ts +7 -3
  165. package/dest/common/packed_args_cache.d.ts +0 -28
  166. package/dest/common/packed_args_cache.d.ts.map +0 -1
  167. package/dest/common/packed_args_cache.js +0 -50
  168. package/src/common/packed_args_cache.ts +0 -55
@@ -1,12 +1,12 @@
1
1
  import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
2
- import { Fr, type GlobalVariables, type Header, PublicCircuitPublicInputs } from '@aztec/circuits.js';
2
+ import { Fr, Gas, type GlobalVariables, type Header, PublicCircuitPublicInputs } from '@aztec/circuits.js';
3
3
  import { createDebugLogger } from '@aztec/foundation/log';
4
4
 
5
5
  import { spawn } from 'child_process';
6
6
  import fs from 'fs/promises';
7
7
  import path from 'path';
8
8
 
9
- import { Oracle, acvm, extractCallStack, extractReturnWitness } from '../acvm/index.js';
9
+ import { Oracle, acvm, extractCallStack, witnessMapToFields } from '../acvm/index.js';
10
10
  import { AvmContext } from '../avm/avm_context.js';
11
11
  import { AvmMachineState } from '../avm/avm_machine_state.js';
12
12
  import { AvmSimulator } from '../avm/avm_simulator.js';
@@ -15,7 +15,7 @@ import { AvmPersistableStateManager } from '../avm/journal/index.js';
15
15
  import { AcirSimulator } from '../client/simulator.js';
16
16
  import { ExecutionError, createSimulationError } from '../common/errors.js';
17
17
  import { SideEffectCounter } from '../common/index.js';
18
- import { PackedArgsCache } from '../common/packed_args_cache.js';
18
+ import { PackedValuesCache } from '../common/packed_values_cache.js';
19
19
  import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from './db.js';
20
20
  import { type PublicExecution, type PublicExecutionResult, checkValidStaticCall } from './execution.js';
21
21
  import { PublicExecutionContext } from './public_execution_context.js';
@@ -66,19 +66,18 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext
66
66
  executionContext.globalVariables,
67
67
  );
68
68
 
69
- // TODO(@spalladino) Load initial gas from the public execution request
70
- const machineState = new AvmMachineState(1e7, 1e7, 1e7);
71
-
69
+ const machineState = new AvmMachineState(executionContext.execution.callContext.gasLeft);
72
70
  const context = new AvmContext(worldStateJournal, executionEnv, machineState);
73
71
  const simulator = new AvmSimulator(context);
74
72
 
75
73
  const result = await simulator.execute();
76
74
  const newWorldState = context.persistableState.flush();
77
75
 
78
- log.verbose(`[AVM] ${address.toString()}:${selector} returned, reverted: ${result.reverted}.`);
76
+ log.verbose(
77
+ `[AVM] ${address.toString()}:${selector} returned, reverted: ${result.reverted}, reason: ${result.revertReason}.`,
78
+ );
79
79
 
80
- // TODO(@spalladino) Read gas left from machineState and return it
81
- return await convertAvmResults(executionContext, newWorldState, result);
80
+ return await convertAvmResults(executionContext, newWorldState, result, machineState);
82
81
  }
83
82
 
84
83
  async function executePublicFunctionAcvm(
@@ -95,11 +94,12 @@ async function executePublicFunctionAcvm(
95
94
  const initialWitness = context.getInitialWitness();
96
95
  const acvmCallback = new Oracle(context);
97
96
 
98
- const { partialWitness, reverted, revertReason } = await (async () => {
97
+ const { partialWitness, returnWitnessMap, reverted, revertReason } = await (async () => {
99
98
  try {
100
99
  const result = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback);
101
100
  return {
102
101
  partialWitness: result.partialWitness,
102
+ returnWitnessMap: result.returnWitness,
103
103
  reverted: false,
104
104
  revertReason: undefined,
105
105
  };
@@ -121,6 +121,7 @@ async function executePublicFunctionAcvm(
121
121
  } else {
122
122
  return {
123
123
  partialWitness: undefined,
124
+ returnWitnessMap: undefined,
124
125
  reverted: true,
125
126
  revertReason: createSimulationError(ee),
126
127
  };
@@ -150,6 +151,7 @@ async function executePublicFunctionAcvm(
150
151
  unencryptedLogs: UnencryptedFunctionL2Logs.empty(),
151
152
  reverted,
152
153
  revertReason,
154
+ gasLeft: Gas.empty(),
153
155
  };
154
156
  }
155
157
 
@@ -157,9 +159,9 @@ async function executePublicFunctionAcvm(
157
159
  throw new Error('No partial witness returned from ACVM');
158
160
  }
159
161
 
160
- const returnWitness = extractReturnWitness(acir, partialWitness);
162
+ const returnWitness = witnessMapToFields(returnWitnessMap);
161
163
  const {
162
- returnValues,
164
+ returnsHash,
163
165
  nullifierReadRequests: nullifierReadRequestsPadded,
164
166
  nullifierNonExistentReadRequests: nullifierNonExistentReadRequestsPadded,
165
167
  newL2ToL1Msgs,
@@ -168,6 +170,7 @@ async function executePublicFunctionAcvm(
168
170
  startSideEffectCounter,
169
171
  endSideEffectCounter,
170
172
  } = PublicCircuitPublicInputs.fromFields(returnWitness);
173
+ const returnValues = await context.unpackReturns(returnsHash);
171
174
 
172
175
  const nullifierReadRequests = nullifierReadRequestsPadded.filter(v => !v.isEmpty());
173
176
  const nullifierNonExistentReadRequests = nullifierNonExistentReadRequestsPadded.filter(v => !v.isEmpty());
@@ -190,6 +193,7 @@ async function executePublicFunctionAcvm(
190
193
 
191
194
  const nestedExecutions = context.getNestedExecutions();
192
195
  const unencryptedLogs = context.getUnencryptedLogs();
196
+ const gasLeft = context.execution.callContext.gasLeft; // No gas metering for ACVM
193
197
 
194
198
  return {
195
199
  execution,
@@ -207,6 +211,7 @@ async function executePublicFunctionAcvm(
207
211
  unencryptedLogs,
208
212
  reverted: false,
209
213
  revertReason: undefined,
214
+ gasLeft,
210
215
  };
211
216
  }
212
217
 
@@ -235,7 +240,7 @@ export class PublicExecutor {
235
240
  ): Promise<PublicExecutionResult> {
236
241
  // Functions can request to pack arguments before calling other functions.
237
242
  // We use this cache to hold the packed arguments.
238
- const packedArgs = PackedArgsCache.create([]);
243
+ const packedArgs = PackedValuesCache.create([]);
239
244
 
240
245
  const context = new PublicExecutionContext(
241
246
  execution,
@@ -0,0 +1,119 @@
1
+ import { MerkleTreeId } from '@aztec/circuit-types';
2
+ import {
3
+ type Fr,
4
+ MAX_NEW_NULLIFIERS_PER_TX,
5
+ type MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX,
6
+ type MAX_NULLIFIER_READ_REQUESTS_PER_TX,
7
+ MAX_PUBLIC_DATA_READS_PER_TX,
8
+ MembershipWitness,
9
+ NULLIFIER_TREE_HEIGHT,
10
+ PUBLIC_DATA_TREE_HEIGHT,
11
+ type PublicDataRead,
12
+ PublicDataTreeLeafPreimage,
13
+ type ReadRequestContext,
14
+ type SideEffectLinkedToNoteHash,
15
+ buildNullifierNonExistentReadRequestHints,
16
+ buildNullifierReadRequestHints,
17
+ mergeAccumulatedData,
18
+ } from '@aztec/circuits.js';
19
+ import { makeTuple } from '@aztec/foundation/array';
20
+ import { type Tuple } from '@aztec/foundation/serialize';
21
+ import { type MerkleTreeOperations } from '@aztec/world-state';
22
+
23
+ export class HintsBuilder {
24
+ constructor(private db: MerkleTreeOperations) {}
25
+
26
+ getNullifierReadRequestHints(
27
+ nullifierReadRequests: Tuple<ReadRequestContext, typeof MAX_NULLIFIER_READ_REQUESTS_PER_TX>,
28
+ nullifiersNonRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
29
+ nullifiersRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
30
+ ) {
31
+ return buildNullifierReadRequestHints(
32
+ this,
33
+ nullifierReadRequests,
34
+ mergeAccumulatedData(MAX_NEW_NULLIFIERS_PER_TX, nullifiersNonRevertible, nullifiersRevertible),
35
+ );
36
+ }
37
+
38
+ getNullifierNonExistentReadRequestHints(
39
+ nullifierNonExistentReadRequests: Tuple<ReadRequestContext, typeof MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX>,
40
+ nullifiersNonRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
41
+ nullifiersRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
42
+ ) {
43
+ const pendingNullifiers = mergeAccumulatedData(
44
+ MAX_NEW_NULLIFIERS_PER_TX,
45
+ nullifiersNonRevertible,
46
+ nullifiersRevertible,
47
+ );
48
+ return buildNullifierNonExistentReadRequestHints(this, nullifierNonExistentReadRequests, pendingNullifiers);
49
+ }
50
+
51
+ async getNullifierMembershipWitness(nullifier: Fr) {
52
+ const index = await this.db.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer());
53
+ if (index === undefined) {
54
+ return;
55
+ }
56
+
57
+ return this.getNullifierMembershipWitnessWithPreimage(index);
58
+ }
59
+
60
+ async getLowNullifierMembershipWitness(nullifier: Fr) {
61
+ const res = await this.db.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
62
+ if (res === undefined) {
63
+ throw new Error(`Cannot find the low leaf for nullifier ${nullifier.toBigInt()}.`);
64
+ }
65
+
66
+ const { index, alreadyPresent } = res;
67
+ if (alreadyPresent) {
68
+ throw new Error(`Nullifier ${nullifier.toBigInt()} already exists in the tree.`);
69
+ }
70
+
71
+ return this.getNullifierMembershipWitnessWithPreimage(index);
72
+ }
73
+
74
+ private async getNullifierMembershipWitnessWithPreimage(index: bigint) {
75
+ const siblingPath = await this.db.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(MerkleTreeId.NULLIFIER_TREE, index);
76
+ const membershipWitness = new MembershipWitness(
77
+ NULLIFIER_TREE_HEIGHT,
78
+ index,
79
+ siblingPath.toTuple<typeof NULLIFIER_TREE_HEIGHT>(),
80
+ );
81
+
82
+ const leafPreimage = await this.db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
83
+ if (!leafPreimage) {
84
+ throw new Error(`Cannot find the leaf preimage at index ${index}.`);
85
+ }
86
+
87
+ return { membershipWitness, leafPreimage };
88
+ }
89
+
90
+ async getPublicDataReadsInfo(publicDataReads: PublicDataRead[]) {
91
+ const newPublicDataReadsWitnesses: Tuple<
92
+ MembershipWitness<typeof PUBLIC_DATA_TREE_HEIGHT>,
93
+ typeof MAX_PUBLIC_DATA_READS_PER_TX
94
+ > = makeTuple(MAX_PUBLIC_DATA_READS_PER_TX, () => MembershipWitness.empty(PUBLIC_DATA_TREE_HEIGHT, 0n));
95
+
96
+ const newPublicDataReadsPreimages: Tuple<PublicDataTreeLeafPreimage, typeof MAX_PUBLIC_DATA_READS_PER_TX> =
97
+ makeTuple(MAX_PUBLIC_DATA_READS_PER_TX, () => PublicDataTreeLeafPreimage.empty());
98
+
99
+ for (const i in publicDataReads) {
100
+ const leafSlot = publicDataReads[i].leafSlot.value;
101
+ const lowLeafResult = await this.db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
102
+ if (!lowLeafResult) {
103
+ throw new Error(`Public data tree should have one initial leaf`);
104
+ }
105
+ const preimage = await this.db.getLeafPreimage(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
106
+ const path = await this.db.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
107
+ newPublicDataReadsWitnesses[i] = new MembershipWitness(
108
+ PUBLIC_DATA_TREE_HEIGHT,
109
+ BigInt(lowLeafResult.index),
110
+ path.toTuple<typeof PUBLIC_DATA_TREE_HEIGHT>(),
111
+ );
112
+ newPublicDataReadsPreimages[i] = preimage! as PublicDataTreeLeafPreimage;
113
+ }
114
+ return {
115
+ newPublicDataReadsWitnesses,
116
+ newPublicDataReadsPreimages,
117
+ };
118
+ }
119
+ }
@@ -7,3 +7,8 @@ export {
7
7
  collectPublicDataUpdateRequests,
8
8
  } from './execution.js';
9
9
  export { PublicExecutor } from './executor.js';
10
+ export { PublicProcessor, PublicProcessorFactory } from './public_processor.js';
11
+ export * from './public_executor.js';
12
+ export * from './abstract_phase_manager.js';
13
+ export * from './public_kernel_circuit_simulator.js';
14
+ export * from './public_kernel.js';
@@ -0,0 +1,126 @@
1
+ import { type Tx } from '@aztec/circuit-types';
2
+ import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
3
+ import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
4
+ import { type MerkleTreeOperations } from '@aztec/world-state';
5
+
6
+ import { type AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
7
+ import { AppLogicPhaseManager } from './app_logic_phase_manager.js';
8
+ import { type ContractsDataSourcePublicDB } from './public_executor.js';
9
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
10
+ import { SetupPhaseManager } from './setup_phase_manager.js';
11
+ import { TailPhaseManager } from './tail_phase_manager.js';
12
+ import { TeardownPhaseManager } from './teardown_phase_manager.js';
13
+
14
+ export class PhaseDidNotChangeError extends Error {
15
+ constructor(phase: PublicKernelPhase) {
16
+ super(`Tried to advance the phase from [${phase}] when the circuit still needs [${phase}]`);
17
+ }
18
+ }
19
+
20
+ export class CannotTransitionToSetupError extends Error {
21
+ constructor() {
22
+ super('Cannot transition to setup phase');
23
+ }
24
+ }
25
+
26
+ export class PhaseManagerFactory {
27
+ public static phaseFromTx(
28
+ tx: Tx,
29
+ db: MerkleTreeOperations,
30
+ publicExecutor: PublicExecutor,
31
+ publicKernel: PublicKernelCircuitSimulator,
32
+ globalVariables: GlobalVariables,
33
+ historicalHeader: Header,
34
+ publicContractsDB: ContractsDataSourcePublicDB,
35
+ publicStateDB: PublicStateDB,
36
+ ): AbstractPhaseManager | undefined {
37
+ const data = tx.data.forPublic!;
38
+ if (data.needsSetup) {
39
+ return new SetupPhaseManager(
40
+ db,
41
+ publicExecutor,
42
+ publicKernel,
43
+ globalVariables,
44
+ historicalHeader,
45
+ publicContractsDB,
46
+ publicStateDB,
47
+ );
48
+ } else if (data.needsAppLogic) {
49
+ return new AppLogicPhaseManager(
50
+ db,
51
+ publicExecutor,
52
+ publicKernel,
53
+ globalVariables,
54
+ historicalHeader,
55
+ publicContractsDB,
56
+ publicStateDB,
57
+ );
58
+ } else if (data.needsTeardown) {
59
+ return new TeardownPhaseManager(
60
+ db,
61
+ publicExecutor,
62
+ publicKernel,
63
+ globalVariables,
64
+ historicalHeader,
65
+ publicContractsDB,
66
+ publicStateDB,
67
+ );
68
+ } else {
69
+ return undefined;
70
+ }
71
+ }
72
+
73
+ public static phaseFromOutput(
74
+ output: PublicKernelCircuitPublicInputs,
75
+ currentPhaseManager: AbstractPhaseManager,
76
+ db: MerkleTreeOperations,
77
+ publicExecutor: PublicExecutor,
78
+ publicKernel: PublicKernelCircuitSimulator,
79
+ globalVariables: GlobalVariables,
80
+ historicalHeader: Header,
81
+ publicContractsDB: ContractsDataSourcePublicDB,
82
+ publicStateDB: PublicStateDB,
83
+ ): AbstractPhaseManager | undefined {
84
+ if (output.needsSetup) {
85
+ throw new CannotTransitionToSetupError();
86
+ } else if (output.needsAppLogic) {
87
+ if (currentPhaseManager.phase === PublicKernelPhase.APP_LOGIC) {
88
+ throw new PhaseDidNotChangeError(currentPhaseManager.phase);
89
+ }
90
+ return new AppLogicPhaseManager(
91
+ db,
92
+ publicExecutor,
93
+ publicKernel,
94
+ globalVariables,
95
+ historicalHeader,
96
+ publicContractsDB,
97
+ publicStateDB,
98
+ );
99
+ } else if (output.needsTeardown) {
100
+ if (currentPhaseManager.phase === PublicKernelPhase.TEARDOWN) {
101
+ throw new PhaseDidNotChangeError(currentPhaseManager.phase);
102
+ }
103
+ return new TeardownPhaseManager(
104
+ db,
105
+ publicExecutor,
106
+ publicKernel,
107
+ globalVariables,
108
+ historicalHeader,
109
+ publicContractsDB,
110
+ publicStateDB,
111
+ );
112
+ } else if (currentPhaseManager.phase !== PublicKernelPhase.TAIL) {
113
+ return new TailPhaseManager(
114
+ db,
115
+ publicExecutor,
116
+ publicKernel,
117
+ globalVariables,
118
+ historicalHeader,
119
+ publicContractsDB,
120
+ publicStateDB,
121
+ );
122
+ } else {
123
+ return undefined;
124
+ }
125
+ }
126
+ }
@@ -1,8 +1,4 @@
1
- import {
2
- type NullifierMembershipWitness,
3
- UnencryptedFunctionL2Logs,
4
- type UnencryptedL2Log,
5
- } from '@aztec/circuit-types';
1
+ import { UnencryptedFunctionL2Logs, type UnencryptedL2Log } from '@aztec/circuit-types';
6
2
  import {
7
3
  CallContext,
8
4
  FunctionData,
@@ -17,7 +13,7 @@ import { createDebugLogger } from '@aztec/foundation/log';
17
13
  import { type ContractInstance } from '@aztec/types/contracts';
18
14
 
19
15
  import { TypedOracle, toACVMWitness } from '../acvm/index.js';
20
- import { type PackedArgsCache, type SideEffectCounter } from '../common/index.js';
16
+ import { type PackedValuesCache, type SideEffectCounter } from '../common/index.js';
21
17
  import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from './db.js';
22
18
  import { type PublicExecution, type PublicExecutionResult, checkValidStaticCall } from './execution.js';
23
19
  import { executePublicFunction } from './executor.js';
@@ -38,7 +34,7 @@ export class PublicExecutionContext extends TypedOracle {
38
34
  public readonly execution: PublicExecution,
39
35
  public readonly header: Header,
40
36
  public readonly globalVariables: GlobalVariables,
41
- private readonly packedArgsCache: PackedArgsCache,
37
+ private readonly packedValuesCache: PackedValuesCache,
42
38
  private readonly sideEffectCounter: SideEffectCounter,
43
39
  public readonly stateDb: PublicStateDB,
44
40
  public readonly contractsDb: PublicContractsDB,
@@ -98,7 +94,23 @@ export class PublicExecutionContext extends TypedOracle {
98
94
  * @param args - Arguments to pack
99
95
  */
100
96
  public packArguments(args: Fr[]): Promise<Fr> {
101
- return Promise.resolve(this.packedArgsCache.pack(args));
97
+ return Promise.resolve(this.packedValuesCache.pack(args));
98
+ }
99
+
100
+ /**
101
+ * Pack the given returns.
102
+ * @param returns - Returns to pack
103
+ */
104
+ public packReturns(returns: Fr[]): Promise<Fr> {
105
+ return Promise.resolve(this.packedValuesCache.pack(returns));
106
+ }
107
+
108
+ /**
109
+ * Unpack the given returns.
110
+ * @param returnsHash - Returns hash to unpack
111
+ */
112
+ public unpackReturns(returnsHash: Fr): Promise<Fr[]> {
113
+ return Promise.resolve(this.packedValuesCache.unpack(returnsHash));
102
114
  }
103
115
 
104
116
  /**
@@ -186,21 +198,25 @@ export class PublicExecutionContext extends TypedOracle {
186
198
  ) {
187
199
  isStaticCall = isStaticCall || this.execution.callContext.isStaticCall;
188
200
 
189
- const args = this.packedArgsCache.unpack(argsHash);
201
+ const args = this.packedValuesCache.unpack(argsHash);
190
202
  this.log.verbose(
191
203
  `Public function call: addr=${targetContractAddress} selector=${functionSelector} args=${args.join(',')}`,
192
204
  );
193
205
 
194
206
  const portalAddress = (await this.contractsDb.getPortalContractAddress(targetContractAddress)) ?? EthAddress.ZERO;
195
207
  const functionData = new FunctionData(functionSelector, /*isPrivate=*/ false);
208
+ const { transactionFee, gasSettings, gasLeft } = this.execution.callContext;
196
209
  const callContext = CallContext.from({
197
210
  msgSender: isDelegateCall ? this.execution.callContext.msgSender : this.execution.contractAddress,
198
211
  storageContractAddress: isDelegateCall ? this.execution.contractAddress : targetContractAddress,
199
212
  portalContractAddress: portalAddress,
200
213
  functionSelector,
214
+ gasLeft, // Propagate the same gas left as when we started since ACVM public functions don't have any metering
201
215
  isDelegateCall,
202
216
  isStaticCall,
203
217
  sideEffectCounter,
218
+ gasSettings,
219
+ transactionFee,
204
220
  });
205
221
 
206
222
  const nestedExecution: PublicExecution = {
@@ -214,7 +230,7 @@ export class PublicExecutionContext extends TypedOracle {
214
230
  nestedExecution,
215
231
  this.header,
216
232
  this.globalVariables,
217
- this.packedArgsCache,
233
+ this.packedValuesCache,
218
234
  this.sideEffectCounter,
219
235
  this.stateDb,
220
236
  this.contractsDb,
@@ -240,14 +256,9 @@ export class PublicExecutionContext extends TypedOracle {
240
256
  return childExecutionResult.returnValues;
241
257
  }
242
258
 
243
- public async getNullifierMembershipWitness(
244
- blockNumber: number,
245
- nullifier: Fr,
246
- ): Promise<NullifierMembershipWitness | undefined> {
247
- if (!this.header.globalVariables.blockNumber.equals(new Fr(blockNumber))) {
248
- throw new Error(`Public execution oracle can only access nullifier membership witnesses for the current block`);
249
- }
250
- return await this.commitmentsDb.getNullifierMembershipWitnessAtLatestBlock(nullifier);
259
+ public async checkNullifierExists(nullifier: Fr): Promise<boolean> {
260
+ const witness = await this.commitmentsDb.getNullifierMembershipWitnessAtLatestBlock(nullifier);
261
+ return !!witness;
251
262
  }
252
263
 
253
264
  public async getContractInstance(address: AztecAddress): Promise<ContractInstance> {