@aztec/simulator 0.23.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/dest/acvm/deserialize.d.ts +5 -0
  2. package/dest/acvm/deserialize.d.ts.map +1 -1
  3. package/dest/acvm/deserialize.js +8 -1
  4. package/dest/acvm/oracle/oracle.d.ts +7 -6
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +28 -15
  7. package/dest/acvm/oracle/typed_oracle.d.ts +9 -11
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +11 -11
  10. package/dest/avm/avm_context.d.ts +4 -4
  11. package/dest/avm/avm_context.d.ts.map +1 -1
  12. package/dest/avm/avm_context.js +6 -6
  13. package/dest/avm/avm_execution_environment.d.ts +3 -2
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +6 -5
  16. package/dest/avm/avm_memory_types.d.ts +127 -37
  17. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  18. package/dest/avm/avm_memory_types.js +98 -106
  19. package/dest/avm/avm_simulator.d.ts +6 -4
  20. package/dest/avm/avm_simulator.d.ts.map +1 -1
  21. package/dest/avm/avm_simulator.js +17 -19
  22. package/dest/avm/errors.d.ts +3 -1
  23. package/dest/avm/errors.d.ts.map +1 -1
  24. package/dest/avm/errors.js +9 -3
  25. package/dest/avm/fixtures/index.d.ts +21 -5
  26. package/dest/avm/fixtures/index.d.ts.map +1 -1
  27. package/dest/avm/fixtures/index.js +28 -9
  28. package/dest/avm/journal/host_storage.d.ts +1 -1
  29. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  30. package/dest/avm/journal/host_storage.js +1 -1
  31. package/dest/avm/journal/journal.d.ts +78 -50
  32. package/dest/avm/journal/journal.d.ts.map +1 -1
  33. package/dest/avm/journal/journal.js +125 -169
  34. package/dest/avm/journal/nullifiers.d.ts +85 -0
  35. package/dest/avm/journal/nullifiers.d.ts.map +1 -0
  36. package/dest/avm/journal/nullifiers.js +147 -0
  37. package/dest/avm/journal/public_storage.d.ts +88 -0
  38. package/dest/avm/journal/public_storage.d.ts.map +1 -0
  39. package/dest/avm/journal/public_storage.js +135 -0
  40. package/dest/avm/journal/trace.d.ts +43 -0
  41. package/dest/avm/journal/trace.d.ts.map +1 -0
  42. package/dest/avm/journal/trace.js +204 -0
  43. package/dest/avm/journal/trace_types.d.ts +26 -0
  44. package/dest/avm/journal/trace_types.d.ts.map +1 -0
  45. package/dest/avm/journal/trace_types.js +6 -0
  46. package/dest/avm/opcodes/accrued_substate.d.ts +37 -4
  47. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  48. package/dest/avm/opcodes/accrued_substate.js +109 -12
  49. package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
  50. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
  51. package/dest/avm/opcodes/addressing_mode.js +62 -0
  52. package/dest/avm/opcodes/environment_getters.d.ts +14 -13
  53. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  54. package/dest/avm/opcodes/environment_getters.js +1 -1
  55. package/dest/avm/opcodes/external_calls.js +5 -5
  56. package/dest/avm/opcodes/hashing.d.ts +48 -0
  57. package/dest/avm/opcodes/hashing.d.ts.map +1 -0
  58. package/dest/avm/opcodes/hashing.js +127 -0
  59. package/dest/avm/opcodes/instruction.d.ts +4 -4
  60. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  61. package/dest/avm/opcodes/instruction.js +1 -1
  62. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  63. package/dest/avm/opcodes/memory.js +5 -3
  64. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  65. package/dest/avm/opcodes/storage.js +3 -3
  66. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  67. package/dest/avm/serialization/bytecode_serialization.js +28 -22
  68. package/dest/avm/serialization/instruction_serialization.d.ts +21 -16
  69. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  70. package/dest/avm/serialization/instruction_serialization.js +23 -18
  71. package/dest/avm/temporary_executor_migration.d.ts +25 -0
  72. package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
  73. package/dest/avm/temporary_executor_migration.js +71 -0
  74. package/dest/client/client_execution_context.d.ts +13 -7
  75. package/dest/client/client_execution_context.d.ts.map +1 -1
  76. package/dest/client/client_execution_context.js +52 -27
  77. package/dest/client/db_oracle.d.ts +7 -0
  78. package/dest/client/db_oracle.d.ts.map +1 -1
  79. package/dest/client/db_oracle.js +1 -1
  80. package/dest/client/execution_note_cache.js +1 -1
  81. package/dest/client/execution_result.d.ts +4 -2
  82. package/dest/client/execution_result.d.ts.map +1 -1
  83. package/dest/client/execution_result.js +1 -1
  84. package/dest/client/private_execution.d.ts.map +1 -1
  85. package/dest/client/private_execution.js +4 -4
  86. package/dest/client/simulator.d.ts +11 -6
  87. package/dest/client/simulator.d.ts.map +1 -1
  88. package/dest/client/simulator.js +21 -12
  89. package/dest/client/unconstrained_execution.js +2 -2
  90. package/dest/client/view_data_oracle.d.ts +9 -2
  91. package/dest/client/view_data_oracle.d.ts.map +1 -1
  92. package/dest/client/view_data_oracle.js +13 -5
  93. package/dest/public/db.d.ts +17 -4
  94. package/dest/public/db.d.ts.map +1 -1
  95. package/dest/public/execution.d.ts +9 -4
  96. package/dest/public/execution.d.ts.map +1 -1
  97. package/dest/public/execution.js +18 -5
  98. package/dest/public/executor.d.ts +7 -0
  99. package/dest/public/executor.d.ts.map +1 -1
  100. package/dest/public/executor.js +40 -6
  101. package/dest/public/public_execution_context.d.ts +5 -4
  102. package/dest/public/public_execution_context.d.ts.map +1 -1
  103. package/dest/public/public_execution_context.js +24 -13
  104. package/dest/public/state_actions.d.ts +1 -1
  105. package/dest/public/state_actions.d.ts.map +1 -1
  106. package/dest/public/state_actions.js +6 -7
  107. package/dest/test/utils.js +4 -4
  108. package/dest/utils.d.ts +5 -20
  109. package/dest/utils.d.ts.map +1 -1
  110. package/dest/utils.js +4 -21
  111. package/package.json +9 -6
  112. package/src/acvm/acvm.ts +156 -0
  113. package/src/acvm/acvm_types.ts +11 -0
  114. package/src/acvm/deserialize.ts +44 -0
  115. package/src/acvm/index.ts +5 -0
  116. package/src/acvm/oracle/debug.ts +109 -0
  117. package/src/acvm/oracle/index.ts +17 -0
  118. package/src/acvm/oracle/oracle.ts +356 -0
  119. package/src/acvm/oracle/typed_oracle.ts +225 -0
  120. package/src/acvm/serialize.ts +75 -0
  121. package/src/avm/avm_context.ts +63 -0
  122. package/src/avm/avm_execution_environment.ts +98 -0
  123. package/src/avm/avm_machine_state.ts +93 -0
  124. package/src/avm/avm_memory_types.ts +324 -0
  125. package/src/avm/avm_message_call_result.ts +29 -0
  126. package/src/avm/avm_simulator.ts +87 -0
  127. package/src/avm/errors.ts +57 -0
  128. package/src/avm/fixtures/index.ts +115 -0
  129. package/src/avm/journal/host_storage.ts +14 -0
  130. package/src/avm/journal/index.ts +2 -0
  131. package/src/avm/journal/journal.ts +231 -0
  132. package/src/avm/journal/nullifiers.ts +170 -0
  133. package/src/avm/journal/public_storage.ts +149 -0
  134. package/src/avm/journal/trace.ts +223 -0
  135. package/src/avm/journal/trace_types.ts +79 -0
  136. package/src/avm/opcodes/.eslintrc.cjs +8 -0
  137. package/src/avm/opcodes/accrued_substate.ts +214 -0
  138. package/src/avm/opcodes/addressing_mode.ts +66 -0
  139. package/src/avm/opcodes/arithmetic.ts +79 -0
  140. package/src/avm/opcodes/bitwise.ts +129 -0
  141. package/src/avm/opcodes/comparators.ts +69 -0
  142. package/src/avm/opcodes/control_flow.ts +129 -0
  143. package/src/avm/opcodes/environment_getters.ts +201 -0
  144. package/src/avm/opcodes/external_calls.ts +122 -0
  145. package/src/avm/opcodes/hashing.ts +170 -0
  146. package/src/avm/opcodes/index.ts +10 -0
  147. package/src/avm/opcodes/instruction.ts +64 -0
  148. package/src/avm/opcodes/instruction_impl.ts +52 -0
  149. package/src/avm/opcodes/memory.ts +194 -0
  150. package/src/avm/opcodes/storage.ts +79 -0
  151. package/src/avm/serialization/buffer_cursor.ts +109 -0
  152. package/src/avm/serialization/bytecode_serialization.ts +179 -0
  153. package/src/avm/serialization/instruction_serialization.ts +170 -0
  154. package/src/avm/temporary_executor_migration.ts +109 -0
  155. package/src/client/client_execution_context.ts +502 -0
  156. package/src/client/db_oracle.ts +192 -0
  157. package/src/client/execution_note_cache.ts +90 -0
  158. package/src/client/execution_result.ts +89 -0
  159. package/src/client/index.ts +3 -0
  160. package/src/client/pick_notes.ts +125 -0
  161. package/src/client/private_execution.ts +79 -0
  162. package/src/client/simulator.ts +317 -0
  163. package/src/client/unconstrained_execution.ts +49 -0
  164. package/src/client/view_data_oracle.ts +253 -0
  165. package/src/common/errors.ts +61 -0
  166. package/src/common/index.ts +3 -0
  167. package/src/common/packed_args_cache.ts +55 -0
  168. package/src/common/side_effect_counter.ts +12 -0
  169. package/src/index.ts +3 -0
  170. package/src/public/db.ts +100 -0
  171. package/src/public/execution.ts +161 -0
  172. package/src/public/executor.ts +178 -0
  173. package/src/public/index.ts +9 -0
  174. package/src/public/public_execution_context.ts +241 -0
  175. package/src/public/state_actions.ts +100 -0
  176. package/src/test/utils.ts +38 -0
  177. package/src/utils.ts +18 -0
@@ -0,0 +1,90 @@
1
+ import { siloNullifier } from '@aztec/circuits.js/hash';
2
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+
5
+ import { NoteData } from '../acvm/index.js';
6
+
7
+ /**
8
+ * Data that's accessible by all the function calls in an execution.
9
+ */
10
+ export class ExecutionNoteCache {
11
+ /**
12
+ * New notes created in this transaction.
13
+ * This mapping maps from a contract address to the notes in the contract.
14
+ */
15
+ private newNotes: Map<bigint, NoteData[]> = new Map();
16
+
17
+ /**
18
+ * The list of nullifiers created in this transaction.
19
+ * This mapping maps from a contract address to the nullifiers emitted from the contract.
20
+ * The note which is nullified might be new or not (i.e., was generated in a previous transaction).
21
+ * Note that their value (bigint representation) is used because Frs cannot be looked up in Sets.
22
+ */
23
+ private nullifiers: Map<bigint, Set<bigint>> = new Map();
24
+
25
+ /**
26
+ * Add a new note to cache.
27
+ * @param note - New note created during execution.
28
+ */
29
+ public addNewNote(note: NoteData) {
30
+ const notes = this.newNotes.get(note.contractAddress.toBigInt()) ?? [];
31
+ notes.push(note);
32
+ this.newNotes.set(note.contractAddress.toBigInt(), notes);
33
+ }
34
+
35
+ /**
36
+ * Add a nullifier to cache. It could be for a db note or a new note created during execution.
37
+ * @param contractAddress - Contract address of the note.
38
+ * @param storageSlot - Storage slot of the note.
39
+ * @param innerNullifier - Inner nullifier of the note.
40
+ * @param innerNoteHash - Inner note hash of the note. If this value equals 0, it means the
41
+ * note being nullified is from a previous transaction (and thus not a new note).
42
+ */
43
+ public nullifyNote(contractAddress: AztecAddress, innerNullifier: Fr, innerNoteHash: Fr) {
44
+ const siloedNullifier = siloNullifier(contractAddress, innerNullifier);
45
+ const nullifiers = this.getNullifiers(contractAddress);
46
+ nullifiers.add(siloedNullifier.value);
47
+ this.nullifiers.set(contractAddress.toBigInt(), nullifiers);
48
+
49
+ // Find and remove the matching new note if the emitted innerNoteHash is not empty.
50
+ if (!innerNoteHash.equals(Fr.ZERO)) {
51
+ const notes = this.newNotes.get(contractAddress.toBigInt()) ?? [];
52
+ const noteIndexToRemove = notes.findIndex(n => n.innerNoteHash.equals(innerNoteHash));
53
+ if (noteIndexToRemove === -1) {
54
+ throw new Error('Attempt to remove a pending note that does not exist.');
55
+ }
56
+ notes.splice(noteIndexToRemove, 1);
57
+ this.newNotes.set(contractAddress.toBigInt(), notes);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Return notes created up to current point in execution.
63
+ * If a nullifier for a note in this list is emitted, the note will be deleted.
64
+ * @param contractAddress - Contract address of the notes.
65
+ * @param storageSlot - Storage slot of the notes.
66
+ **/
67
+ public getNotes(contractAddress: AztecAddress, storageSlot: Fr) {
68
+ const notes = this.newNotes.get(contractAddress.toBigInt()) ?? [];
69
+ return notes.filter(n => n.storageSlot.equals(storageSlot));
70
+ }
71
+
72
+ /**
73
+ * Check if a note exists in the newNotes array.
74
+ * @param contractAddress - Contract address of the note.
75
+ * @param storageSlot - Storage slot of the note.
76
+ * @param innerNoteHash - Inner note hash of the note.
77
+ **/
78
+ public checkNoteExists(contractAddress: AztecAddress, innerNoteHash: Fr) {
79
+ const notes = this.newNotes.get(contractAddress.toBigInt()) ?? [];
80
+ return notes.some(n => n.innerNoteHash.equals(innerNoteHash));
81
+ }
82
+
83
+ /**
84
+ * Return all nullifiers emitted from a contract.
85
+ * @param contractAddress - Address of the contract.
86
+ */
87
+ public getNullifiers(contractAddress: AztecAddress): Set<bigint> {
88
+ return this.nullifiers.get(contractAddress.toBigInt()) ?? new Set();
89
+ }
90
+ }
@@ -0,0 +1,89 @@
1
+ import { FunctionL2Logs, Note } from '@aztec/circuit-types';
2
+ import { NoteHashReadRequestMembershipWitness, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
3
+ import { DecodedReturn } from '@aztec/foundation/abi';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+
6
+ import { ACVMField } from '../acvm/index.js';
7
+
8
+ /**
9
+ * The contents of a new note.
10
+ */
11
+ export interface NoteAndSlot {
12
+ /** The note. */
13
+ note: Note;
14
+ /** The storage slot of the note. */
15
+ storageSlot: Fr;
16
+ /** The note type identifier. */
17
+ noteTypeId: Fr;
18
+ }
19
+
20
+ /**
21
+ * The result of executing a private function.
22
+ */
23
+ export interface ExecutionResult {
24
+ // Needed for prover
25
+ /** The ACIR bytecode. */
26
+ acir: Buffer;
27
+ /** The verification key. */
28
+ vk: Buffer;
29
+ /** The partial witness. */
30
+ partialWitness: Map<number, ACVMField>;
31
+ // Needed for the verifier (kernel)
32
+ /** The call stack item. */
33
+ callStackItem: PrivateCallStackItem;
34
+ /** The partially filled-in read request membership witnesses for commitments being read. */
35
+ noteHashReadRequestPartialWitnesses: NoteHashReadRequestMembershipWitness[];
36
+ // Needed when we enable chained txs. The new notes can be cached and used in a later transaction.
37
+ /** The notes created in the executed function. */
38
+ newNotes: NoteAndSlot[];
39
+ /** The decoded return values of the executed function. */
40
+ returnValues: DecodedReturn;
41
+ /** The nested executions. */
42
+ nestedExecutions: this[];
43
+ /** Enqueued public function execution requests to be picked up by the sequencer. */
44
+ enqueuedPublicFunctionCalls: PublicCallRequest[];
45
+ /**
46
+ * Encrypted logs emitted during execution of this function call.
47
+ * Note: These are preimages to `encryptedLogsHash`.
48
+ */
49
+ encryptedLogs: FunctionL2Logs;
50
+ /**
51
+ * Unencrypted logs emitted during execution of this function call.
52
+ * Note: These are preimages to `unencryptedLogsHash`.
53
+ */
54
+ unencryptedLogs: FunctionL2Logs;
55
+ }
56
+
57
+ /**
58
+ * Collect all encrypted logs across all nested executions.
59
+ * @param execResult - The topmost execution result.
60
+ * @returns All encrypted logs.
61
+ */
62
+ export function collectEncryptedLogs(execResult: ExecutionResult): FunctionL2Logs[] {
63
+ // without the .reverse(), the logs will be in a queue like fashion which is wrong as the kernel processes it like a stack.
64
+ return [execResult.encryptedLogs, ...[...execResult.nestedExecutions].reverse().flatMap(collectEncryptedLogs)];
65
+ }
66
+
67
+ /**
68
+ * Collect all unencrypted logs across all nested executions.
69
+ * @param execResult - The topmost execution result.
70
+ * @returns All unencrypted logs.
71
+ */
72
+ export function collectUnencryptedLogs(execResult: ExecutionResult): FunctionL2Logs[] {
73
+ // without the .reverse(), the logs will be in a queue like fashion which is wrong as the kernel processes it like a stack.
74
+ return [execResult.unencryptedLogs, ...[...execResult.nestedExecutions].reverse().flatMap(collectUnencryptedLogs)];
75
+ }
76
+
77
+ /**
78
+ * Collect all enqueued public function calls across all nested executions.
79
+ * @param execResult - The topmost execution result.
80
+ * @returns All enqueued public function calls.
81
+ */
82
+ export function collectEnqueuedPublicFunctionCalls(execResult: ExecutionResult): PublicCallRequest[] {
83
+ // without the reverse sort, the logs will be in a queue like fashion which is wrong
84
+ // as the kernel processes it like a stack, popping items off and pushing them to output
85
+ return [
86
+ ...execResult.enqueuedPublicFunctionCalls,
87
+ ...[...execResult.nestedExecutions].flatMap(collectEnqueuedPublicFunctionCalls),
88
+ ].sort((a, b) => b.callContext.startSideEffectCounter - a.callContext.startSideEffectCounter);
89
+ }
@@ -0,0 +1,3 @@
1
+ export * from './simulator.js';
2
+ export * from './db_oracle.js';
3
+ export * from './execution_result.js';
@@ -0,0 +1,125 @@
1
+ import { Comparator, Note } from '@aztec/circuit-types';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+
4
+ /**
5
+ * Configuration for selecting values.
6
+ */
7
+ export interface Select {
8
+ /**
9
+ * Index of the field to select and match.
10
+ */
11
+ index: number;
12
+ /**
13
+ * Required value of the field.
14
+ */
15
+ value: Fr;
16
+ /**
17
+ * The comparator to use
18
+ */
19
+ comparator: Comparator;
20
+ }
21
+
22
+ /**
23
+ * The order to sort an array.
24
+ */
25
+ export enum SortOrder {
26
+ NADA = 0,
27
+ DESC = 1,
28
+ ASC = 2,
29
+ }
30
+
31
+ /**
32
+ * Configuration for sorting values.
33
+ */
34
+ export interface Sort {
35
+ /**
36
+ * Index of the field to sort.
37
+ */
38
+ index: number;
39
+ /**
40
+ * Order to sort the field.
41
+ */
42
+ order: SortOrder;
43
+ }
44
+
45
+ /**
46
+ * Options for picking items from an array of BasicNoteData.
47
+ */
48
+ interface GetOptions {
49
+ /**
50
+ * Configurations for selecting items.
51
+ * Default: empty array.
52
+ */
53
+ selects?: Select[];
54
+ /**
55
+ * Configurations for sorting items.
56
+ * Default: empty array.
57
+ */
58
+ sorts?: Sort[];
59
+ /**
60
+ * The number of items to retrieve per query.
61
+ * Default: 0. No limit.
62
+ */
63
+ limit?: number;
64
+ /**
65
+ * The starting index for pagination.
66
+ * Default: 0.
67
+ */
68
+ offset?: number;
69
+ }
70
+
71
+ /**
72
+ * Data needed from to perform sort.
73
+ */
74
+ interface ContainsNote {
75
+ /**
76
+ * The note.
77
+ */
78
+ note: Note;
79
+ }
80
+
81
+ const selectNotes = <T extends ContainsNote>(noteDatas: T[], selects: Select[]): T[] =>
82
+ noteDatas.filter(noteData =>
83
+ selects.every(({ index, value, comparator }) => {
84
+ const comparatorSelector = {
85
+ [Comparator.EQ]: () => noteData.note.items[index].equals(value),
86
+ [Comparator.NEQ]: () => !noteData.note.items[index].equals(value),
87
+ [Comparator.LT]: () => noteData.note.items[index].lt(value),
88
+ [Comparator.LTE]: () => noteData.note.items[index].lt(value) || noteData.note.items[index].equals(value),
89
+ [Comparator.GT]: () => !noteData.note.items[index].lt(value) && !noteData.note.items[index].equals(value),
90
+ [Comparator.GTE]: () => !noteData.note.items[index].lt(value),
91
+ };
92
+
93
+ return comparatorSelector[comparator]();
94
+ }),
95
+ );
96
+
97
+ const sortNotes = (a: Fr[], b: Fr[], sorts: Sort[], level = 0): number => {
98
+ if (sorts[level] === undefined) {
99
+ return 0;
100
+ }
101
+
102
+ const { index, order } = sorts[level];
103
+ if (order === 0) {
104
+ return 0;
105
+ }
106
+
107
+ const dir = order === 1 ? [-1, 1] : [1, -1];
108
+ return a[index].value === b[index].value
109
+ ? sortNotes(a, b, sorts, level + 1)
110
+ : a[index].value > b[index].value
111
+ ? dir[0]
112
+ : dir[1];
113
+ };
114
+
115
+ /**
116
+ * Pick from a note array a number of notes that meet the criteria.
117
+ */
118
+ export function pickNotes<T extends ContainsNote>(
119
+ noteDatas: T[],
120
+ { selects = [], sorts = [], limit = 0, offset = 0 }: GetOptions,
121
+ ): T[] {
122
+ return selectNotes(noteDatas, selects)
123
+ .sort((a, b) => sortNotes(a.note.items, b.note.items, sorts))
124
+ .slice(offset, limit ? offset + limit : undefined);
125
+ }
@@ -0,0 +1,79 @@
1
+ import { FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
2
+ import { FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
3
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+ import { createDebugLogger } from '@aztec/foundation/log';
6
+ import { to2Fields } from '@aztec/foundation/serialize';
7
+
8
+ import { extractReturnWitness } from '../acvm/deserialize.js';
9
+ import { Oracle, acvm, extractCallStack } from '../acvm/index.js';
10
+ import { ExecutionError } from '../common/errors.js';
11
+ import { ClientExecutionContext } from './client_execution_context.js';
12
+ import { ExecutionResult } from './execution_result.js';
13
+ import { AcirSimulator } from './simulator.js';
14
+
15
+ /**
16
+ * Execute a private function and return the execution result.
17
+ */
18
+ export async function executePrivateFunction(
19
+ context: ClientExecutionContext,
20
+ artifact: FunctionArtifactWithDebugMetadata,
21
+ contractAddress: AztecAddress,
22
+ functionData: FunctionData,
23
+ log = createDebugLogger('aztec:simulator:secret_execution'),
24
+ ): Promise<ExecutionResult> {
25
+ const functionSelector = functionData.selector;
26
+ log(`Executing external function ${contractAddress}:${functionSelector}(${artifact.name})`);
27
+ const acir = Buffer.from(artifact.bytecode, 'base64');
28
+ const initialWitness = context.getInitialWitness(artifact);
29
+ const acvmCallback = new Oracle(context);
30
+ const { partialWitness } = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback).catch(
31
+ (err: Error) => {
32
+ throw new ExecutionError(
33
+ err.message,
34
+ {
35
+ contractAddress,
36
+ functionSelector,
37
+ },
38
+ extractCallStack(err, artifact.debug),
39
+ { cause: err },
40
+ );
41
+ },
42
+ );
43
+
44
+ const returnWitness = extractReturnWitness(acir, partialWitness);
45
+ const publicInputs = PrivateCircuitPublicInputs.fromFields(returnWitness);
46
+
47
+ const encryptedLogs = context.getEncryptedLogs();
48
+ const unencryptedLogs = context.getUnencryptedLogs();
49
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) --> set this in Noir
50
+ publicInputs.encryptedLogsHash = to2Fields(encryptedLogs.hash());
51
+ publicInputs.encryptedLogPreimagesLength = new Fr(encryptedLogs.getSerializedLength());
52
+ publicInputs.unencryptedLogsHash = to2Fields(unencryptedLogs.hash());
53
+ publicInputs.unencryptedLogPreimagesLength = new Fr(unencryptedLogs.getSerializedLength());
54
+
55
+ const callStackItem = new PrivateCallStackItem(contractAddress, functionData, publicInputs);
56
+ const returnValues = decodeReturnValues(artifact, publicInputs.returnValues);
57
+ const noteHashReadRequestPartialWitnesses = context.getNoteHashReadRequestPartialWitnesses(
58
+ publicInputs.noteHashReadRequests,
59
+ );
60
+ const newNotes = context.getNewNotes();
61
+ const nestedExecutions = context.getNestedExecutions();
62
+ const enqueuedPublicFunctionCalls = context.getEnqueuedPublicFunctionCalls();
63
+
64
+ log(`Returning from call to ${contractAddress.toString()}:${functionSelector}`);
65
+
66
+ return {
67
+ acir,
68
+ partialWitness,
69
+ callStackItem,
70
+ returnValues,
71
+ noteHashReadRequestPartialWitnesses,
72
+ newNotes,
73
+ vk: Buffer.from(artifact.verificationKey!, 'hex'),
74
+ nestedExecutions,
75
+ enqueuedPublicFunctionCalls,
76
+ encryptedLogs,
77
+ unencryptedLogs,
78
+ };
79
+ }