@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.
- package/dest/acvm/deserialize.d.ts +5 -0
- package/dest/acvm/deserialize.d.ts.map +1 -1
- package/dest/acvm/deserialize.js +8 -1
- package/dest/acvm/oracle/oracle.d.ts +7 -6
- package/dest/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/oracle.js +28 -15
- package/dest/acvm/oracle/typed_oracle.d.ts +9 -11
- package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/typed_oracle.js +11 -11
- package/dest/avm/avm_context.d.ts +4 -4
- package/dest/avm/avm_context.d.ts.map +1 -1
- package/dest/avm/avm_context.js +6 -6
- package/dest/avm/avm_execution_environment.d.ts +3 -2
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +6 -5
- package/dest/avm/avm_memory_types.d.ts +127 -37
- package/dest/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/avm/avm_memory_types.js +98 -106
- package/dest/avm/avm_simulator.d.ts +6 -4
- package/dest/avm/avm_simulator.d.ts.map +1 -1
- package/dest/avm/avm_simulator.js +17 -19
- package/dest/avm/errors.d.ts +3 -1
- package/dest/avm/errors.d.ts.map +1 -1
- package/dest/avm/errors.js +9 -3
- package/dest/avm/fixtures/index.d.ts +21 -5
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +28 -9
- package/dest/avm/journal/host_storage.d.ts +1 -1
- package/dest/avm/journal/host_storage.d.ts.map +1 -1
- package/dest/avm/journal/host_storage.js +1 -1
- package/dest/avm/journal/journal.d.ts +78 -50
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +125 -169
- package/dest/avm/journal/nullifiers.d.ts +85 -0
- package/dest/avm/journal/nullifiers.d.ts.map +1 -0
- package/dest/avm/journal/nullifiers.js +147 -0
- package/dest/avm/journal/public_storage.d.ts +88 -0
- package/dest/avm/journal/public_storage.d.ts.map +1 -0
- package/dest/avm/journal/public_storage.js +135 -0
- package/dest/avm/journal/trace.d.ts +43 -0
- package/dest/avm/journal/trace.d.ts.map +1 -0
- package/dest/avm/journal/trace.js +204 -0
- package/dest/avm/journal/trace_types.d.ts +26 -0
- package/dest/avm/journal/trace_types.d.ts.map +1 -0
- package/dest/avm/journal/trace_types.js +6 -0
- package/dest/avm/opcodes/accrued_substate.d.ts +37 -4
- package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
- package/dest/avm/opcodes/accrued_substate.js +109 -12
- package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
- package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
- package/dest/avm/opcodes/addressing_mode.js +62 -0
- package/dest/avm/opcodes/environment_getters.d.ts +14 -13
- package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/environment_getters.js +1 -1
- package/dest/avm/opcodes/external_calls.js +5 -5
- package/dest/avm/opcodes/hashing.d.ts +48 -0
- package/dest/avm/opcodes/hashing.d.ts.map +1 -0
- package/dest/avm/opcodes/hashing.js +127 -0
- package/dest/avm/opcodes/instruction.d.ts +4 -4
- package/dest/avm/opcodes/instruction.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction.js +1 -1
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +5 -3
- package/dest/avm/opcodes/storage.d.ts.map +1 -1
- package/dest/avm/opcodes/storage.js +3 -3
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +28 -22
- package/dest/avm/serialization/instruction_serialization.d.ts +21 -16
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +23 -18
- package/dest/avm/temporary_executor_migration.d.ts +25 -0
- package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
- package/dest/avm/temporary_executor_migration.js +71 -0
- package/dest/client/client_execution_context.d.ts +13 -7
- package/dest/client/client_execution_context.d.ts.map +1 -1
- package/dest/client/client_execution_context.js +52 -27
- package/dest/client/db_oracle.d.ts +7 -0
- package/dest/client/db_oracle.d.ts.map +1 -1
- package/dest/client/db_oracle.js +1 -1
- package/dest/client/execution_note_cache.js +1 -1
- package/dest/client/execution_result.d.ts +4 -2
- package/dest/client/execution_result.d.ts.map +1 -1
- package/dest/client/execution_result.js +1 -1
- package/dest/client/private_execution.d.ts.map +1 -1
- package/dest/client/private_execution.js +4 -4
- package/dest/client/simulator.d.ts +11 -6
- package/dest/client/simulator.d.ts.map +1 -1
- package/dest/client/simulator.js +21 -12
- package/dest/client/unconstrained_execution.js +2 -2
- package/dest/client/view_data_oracle.d.ts +9 -2
- package/dest/client/view_data_oracle.d.ts.map +1 -1
- package/dest/client/view_data_oracle.js +13 -5
- package/dest/public/db.d.ts +17 -4
- package/dest/public/db.d.ts.map +1 -1
- package/dest/public/execution.d.ts +9 -4
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +18 -5
- package/dest/public/executor.d.ts +7 -0
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +40 -6
- package/dest/public/public_execution_context.d.ts +5 -4
- package/dest/public/public_execution_context.d.ts.map +1 -1
- package/dest/public/public_execution_context.js +24 -13
- package/dest/public/state_actions.d.ts +1 -1
- package/dest/public/state_actions.d.ts.map +1 -1
- package/dest/public/state_actions.js +6 -7
- package/dest/test/utils.js +4 -4
- package/dest/utils.d.ts +5 -20
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +4 -21
- package/package.json +9 -6
- package/src/acvm/acvm.ts +156 -0
- package/src/acvm/acvm_types.ts +11 -0
- package/src/acvm/deserialize.ts +44 -0
- package/src/acvm/index.ts +5 -0
- package/src/acvm/oracle/debug.ts +109 -0
- package/src/acvm/oracle/index.ts +17 -0
- package/src/acvm/oracle/oracle.ts +356 -0
- package/src/acvm/oracle/typed_oracle.ts +225 -0
- package/src/acvm/serialize.ts +75 -0
- package/src/avm/avm_context.ts +63 -0
- package/src/avm/avm_execution_environment.ts +98 -0
- package/src/avm/avm_machine_state.ts +93 -0
- package/src/avm/avm_memory_types.ts +324 -0
- package/src/avm/avm_message_call_result.ts +29 -0
- package/src/avm/avm_simulator.ts +87 -0
- package/src/avm/errors.ts +57 -0
- package/src/avm/fixtures/index.ts +115 -0
- package/src/avm/journal/host_storage.ts +14 -0
- package/src/avm/journal/index.ts +2 -0
- package/src/avm/journal/journal.ts +231 -0
- package/src/avm/journal/nullifiers.ts +170 -0
- package/src/avm/journal/public_storage.ts +149 -0
- package/src/avm/journal/trace.ts +223 -0
- package/src/avm/journal/trace_types.ts +79 -0
- package/src/avm/opcodes/.eslintrc.cjs +8 -0
- package/src/avm/opcodes/accrued_substate.ts +214 -0
- package/src/avm/opcodes/addressing_mode.ts +66 -0
- package/src/avm/opcodes/arithmetic.ts +79 -0
- package/src/avm/opcodes/bitwise.ts +129 -0
- package/src/avm/opcodes/comparators.ts +69 -0
- package/src/avm/opcodes/control_flow.ts +129 -0
- package/src/avm/opcodes/environment_getters.ts +201 -0
- package/src/avm/opcodes/external_calls.ts +122 -0
- package/src/avm/opcodes/hashing.ts +170 -0
- package/src/avm/opcodes/index.ts +10 -0
- package/src/avm/opcodes/instruction.ts +64 -0
- package/src/avm/opcodes/instruction_impl.ts +52 -0
- package/src/avm/opcodes/memory.ts +194 -0
- package/src/avm/opcodes/storage.ts +79 -0
- package/src/avm/serialization/buffer_cursor.ts +109 -0
- package/src/avm/serialization/bytecode_serialization.ts +179 -0
- package/src/avm/serialization/instruction_serialization.ts +170 -0
- package/src/avm/temporary_executor_migration.ts +109 -0
- package/src/client/client_execution_context.ts +502 -0
- package/src/client/db_oracle.ts +192 -0
- package/src/client/execution_note_cache.ts +90 -0
- package/src/client/execution_result.ts +89 -0
- package/src/client/index.ts +3 -0
- package/src/client/pick_notes.ts +125 -0
- package/src/client/private_execution.ts +79 -0
- package/src/client/simulator.ts +317 -0
- package/src/client/unconstrained_execution.ts +49 -0
- package/src/client/view_data_oracle.ts +253 -0
- package/src/common/errors.ts +61 -0
- package/src/common/index.ts +3 -0
- package/src/common/packed_args_cache.ts +55 -0
- package/src/common/side_effect_counter.ts +12 -0
- package/src/index.ts +3 -0
- package/src/public/db.ts +100 -0
- package/src/public/execution.ts +161 -0
- package/src/public/executor.ts +178 -0
- package/src/public/index.ts +9 -0
- package/src/public/public_execution_context.ts +241 -0
- package/src/public/state_actions.ts +100 -0
- package/src/test/utils.ts +38 -0
- package/src/utils.ts +18 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { AztecNode, FunctionCall, Note, TxExecutionRequest } from '@aztec/circuit-types';
|
|
2
|
+
import { CallContext, FunctionData } from '@aztec/circuits.js';
|
|
3
|
+
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
4
|
+
import {
|
|
5
|
+
ArrayType,
|
|
6
|
+
FunctionArtifactWithDebugMetadata,
|
|
7
|
+
FunctionSelector,
|
|
8
|
+
FunctionType,
|
|
9
|
+
encodeArguments,
|
|
10
|
+
} from '@aztec/foundation/abi';
|
|
11
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
12
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
13
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
14
|
+
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
|
|
15
|
+
|
|
16
|
+
import { WasmBlackBoxFunctionSolver, createBlackBoxSolver } from '@noir-lang/acvm_js';
|
|
17
|
+
|
|
18
|
+
import { createSimulationError } from '../common/errors.js';
|
|
19
|
+
import { PackedArgsCache } from '../common/packed_args_cache.js';
|
|
20
|
+
import { ClientExecutionContext } from './client_execution_context.js';
|
|
21
|
+
import { DBOracle } from './db_oracle.js';
|
|
22
|
+
import { ExecutionNoteCache } from './execution_note_cache.js';
|
|
23
|
+
import { ExecutionResult } from './execution_result.js';
|
|
24
|
+
import { executePrivateFunction } from './private_execution.js';
|
|
25
|
+
import { executeUnconstrainedFunction } from './unconstrained_execution.js';
|
|
26
|
+
import { ViewDataOracle } from './view_data_oracle.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The ACIR simulator.
|
|
30
|
+
*/
|
|
31
|
+
export class AcirSimulator {
|
|
32
|
+
private static solver: Promise<WasmBlackBoxFunctionSolver>; // ACVM's backend
|
|
33
|
+
private log: DebugLogger;
|
|
34
|
+
|
|
35
|
+
constructor(private db: DBOracle, private node: AztecNode) {
|
|
36
|
+
this.log = createDebugLogger('aztec:simulator');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Gets or initializes the ACVM WasmBlackBoxFunctionSolver.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
*
|
|
44
|
+
* Occurs only once across all instances of AcirSimulator.
|
|
45
|
+
* Speeds up execution by only performing setup tasks (like pedersen
|
|
46
|
+
* generator initialization) one time.
|
|
47
|
+
* TODO(https://github.com/AztecProtocol/aztec-packages/issues/1627):
|
|
48
|
+
* determine whether this requires a lock
|
|
49
|
+
*
|
|
50
|
+
* @returns ACVM WasmBlackBoxFunctionSolver
|
|
51
|
+
*/
|
|
52
|
+
public static getSolver(): Promise<WasmBlackBoxFunctionSolver> {
|
|
53
|
+
if (!this.solver) {
|
|
54
|
+
this.solver = createBlackBoxSolver();
|
|
55
|
+
}
|
|
56
|
+
return this.solver;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Runs a private function.
|
|
61
|
+
* @param request - The transaction request.
|
|
62
|
+
* @param entryPointArtifact - The artifact of the entry point function.
|
|
63
|
+
* @param contractAddress - The address of the contract (should match request.origin)
|
|
64
|
+
* @param portalContractAddress - The address of the portal contract.
|
|
65
|
+
* @param msgSender - The address calling the function. This can be replaced to simulate a call from another contract or a specific account.
|
|
66
|
+
* @returns The result of the execution.
|
|
67
|
+
*/
|
|
68
|
+
public async run(
|
|
69
|
+
request: TxExecutionRequest,
|
|
70
|
+
entryPointArtifact: FunctionArtifactWithDebugMetadata,
|
|
71
|
+
contractAddress: AztecAddress,
|
|
72
|
+
portalContractAddress: EthAddress,
|
|
73
|
+
msgSender = AztecAddress.ZERO,
|
|
74
|
+
): Promise<ExecutionResult> {
|
|
75
|
+
if (entryPointArtifact.functionType !== FunctionType.SECRET) {
|
|
76
|
+
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as secret`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (request.origin !== contractAddress) {
|
|
80
|
+
this.log.warn(
|
|
81
|
+
`Request origin does not match contract address in simulation. Request origin: ${request.origin}, contract address: ${contractAddress}`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const curve = new Grumpkin();
|
|
86
|
+
|
|
87
|
+
const header = await this.db.getHeader();
|
|
88
|
+
const callContext = new CallContext(
|
|
89
|
+
msgSender,
|
|
90
|
+
contractAddress,
|
|
91
|
+
portalContractAddress,
|
|
92
|
+
FunctionSelector.fromNameAndParameters(entryPointArtifact.name, entryPointArtifact.parameters),
|
|
93
|
+
false,
|
|
94
|
+
false,
|
|
95
|
+
request.functionData.isConstructor,
|
|
96
|
+
// TODO: when contract deployment is done in-app, we should only reserve one counter for the tx hash
|
|
97
|
+
// 2 counters are reserved for tx hash and contract deployment nullifier
|
|
98
|
+
request.txContext.isContractDeploymentTx ? 2 : 1,
|
|
99
|
+
);
|
|
100
|
+
const context = new ClientExecutionContext(
|
|
101
|
+
contractAddress,
|
|
102
|
+
request.argsHash,
|
|
103
|
+
request.txContext,
|
|
104
|
+
callContext,
|
|
105
|
+
header,
|
|
106
|
+
request.authWitnesses,
|
|
107
|
+
PackedArgsCache.create(request.packedArguments),
|
|
108
|
+
new ExecutionNoteCache(),
|
|
109
|
+
this.db,
|
|
110
|
+
curve,
|
|
111
|
+
this.node,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const executionResult = await executePrivateFunction(
|
|
116
|
+
context,
|
|
117
|
+
entryPointArtifact,
|
|
118
|
+
contractAddress,
|
|
119
|
+
request.functionData,
|
|
120
|
+
);
|
|
121
|
+
return executionResult;
|
|
122
|
+
} catch (err) {
|
|
123
|
+
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Runs an unconstrained function.
|
|
129
|
+
* @param request - The transaction request.
|
|
130
|
+
* @param entryPointArtifact - The artifact of the entry point function.
|
|
131
|
+
* @param contractAddress - The address of the contract.
|
|
132
|
+
* @param aztecNode - The AztecNode instance.
|
|
133
|
+
*/
|
|
134
|
+
public async runUnconstrained(
|
|
135
|
+
request: FunctionCall,
|
|
136
|
+
entryPointArtifact: FunctionArtifactWithDebugMetadata,
|
|
137
|
+
contractAddress: AztecAddress,
|
|
138
|
+
) {
|
|
139
|
+
if (entryPointArtifact.functionType !== FunctionType.UNCONSTRAINED) {
|
|
140
|
+
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as constrained`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const context = new ViewDataOracle(contractAddress, [], this.db, this.node);
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
return await executeUnconstrainedFunction(
|
|
147
|
+
context,
|
|
148
|
+
entryPointArtifact,
|
|
149
|
+
contractAddress,
|
|
150
|
+
request.functionData,
|
|
151
|
+
request.args,
|
|
152
|
+
);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Computes the inner nullifier of a note.
|
|
160
|
+
* @param contractAddress - The address of the contract.
|
|
161
|
+
* @param nonce - The nonce of the note hash.
|
|
162
|
+
* @param storageSlot - The storage slot.
|
|
163
|
+
* @param noteTypeId - The note type identifier.
|
|
164
|
+
* @param note - The note.
|
|
165
|
+
* @returns The nullifier.
|
|
166
|
+
*/
|
|
167
|
+
public async computeNoteHashAndNullifier(
|
|
168
|
+
contractAddress: AztecAddress,
|
|
169
|
+
nonce: Fr,
|
|
170
|
+
storageSlot: Fr,
|
|
171
|
+
noteTypeId: Fr,
|
|
172
|
+
note: Note,
|
|
173
|
+
) {
|
|
174
|
+
const artifact: FunctionArtifactWithDebugMetadata | undefined = await this.db.getFunctionArtifactByName(
|
|
175
|
+
contractAddress,
|
|
176
|
+
'compute_note_hash_and_nullifier',
|
|
177
|
+
);
|
|
178
|
+
if (!artifact) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`Mandatory implementation of "compute_note_hash_and_nullifier" missing in noir contract ${contractAddress.toString()}.`,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (artifact.parameters.length != 5) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`Expected 5 parameters in mandatory implementation of "compute_note_hash_and_nullifier", but found ${
|
|
187
|
+
artifact.parameters.length
|
|
188
|
+
} in noir contract ${contractAddress.toString()}.`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const maxNoteFields = (artifact.parameters[artifact.parameters.length - 1].type as ArrayType).length;
|
|
193
|
+
if (maxNoteFields < note.items.length) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
`The note being processed has ${note.items.length} fields, while "compute_note_hash_and_nullifier" can only handle a maximum of ${maxNoteFields} fields. Please consider increasing the allowed field size to accommodate all notes generated from the contract.`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const extendedNoteItems = note.items.concat(Array(maxNoteFields - note.items.length).fill(Fr.ZERO));
|
|
200
|
+
|
|
201
|
+
const execRequest: FunctionCall = {
|
|
202
|
+
to: contractAddress,
|
|
203
|
+
functionData: FunctionData.empty(),
|
|
204
|
+
args: encodeArguments(artifact, [contractAddress, nonce, storageSlot, noteTypeId, extendedNoteItems]),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const [innerNoteHash, siloedNoteHash, uniqueSiloedNoteHash, innerNullifier] = (await this.runUnconstrained(
|
|
208
|
+
execRequest,
|
|
209
|
+
artifact,
|
|
210
|
+
contractAddress,
|
|
211
|
+
)) as bigint[];
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
innerNoteHash: new Fr(innerNoteHash),
|
|
215
|
+
siloedNoteHash: new Fr(siloedNoteHash),
|
|
216
|
+
uniqueSiloedNoteHash: new Fr(uniqueSiloedNoteHash),
|
|
217
|
+
innerNullifier: new Fr(innerNullifier),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Computes the inner note hash of a note, which contains storage slot and the custom note hash.
|
|
223
|
+
* @param contractAddress - The address of the contract.
|
|
224
|
+
* @param storageSlot - The storage slot.
|
|
225
|
+
* @param noteTypeId - The note type identifier.
|
|
226
|
+
* @param note - The note.
|
|
227
|
+
* @returns The note hash.
|
|
228
|
+
*/
|
|
229
|
+
public async computeInnerNoteHash(contractAddress: AztecAddress, storageSlot: Fr, noteTypeId: Fr, note: Note) {
|
|
230
|
+
const { innerNoteHash } = await this.computeNoteHashAndNullifier(
|
|
231
|
+
contractAddress,
|
|
232
|
+
Fr.ZERO,
|
|
233
|
+
storageSlot,
|
|
234
|
+
noteTypeId,
|
|
235
|
+
note,
|
|
236
|
+
);
|
|
237
|
+
return innerNoteHash;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Computes the unique note hash of a note.
|
|
242
|
+
* @param contractAddress - The address of the contract.
|
|
243
|
+
* @param nonce - The nonce of the note hash.
|
|
244
|
+
* @param storageSlot - The storage slot.
|
|
245
|
+
* @param noteTypeId - The note type identifier.
|
|
246
|
+
* @param note - The note.
|
|
247
|
+
* @returns The note hash.
|
|
248
|
+
*/
|
|
249
|
+
public async computeUniqueSiloedNoteHash(
|
|
250
|
+
contractAddress: AztecAddress,
|
|
251
|
+
nonce: Fr,
|
|
252
|
+
storageSlot: Fr,
|
|
253
|
+
noteTypeId: Fr,
|
|
254
|
+
note: Note,
|
|
255
|
+
) {
|
|
256
|
+
const { uniqueSiloedNoteHash } = await this.computeNoteHashAndNullifier(
|
|
257
|
+
contractAddress,
|
|
258
|
+
nonce,
|
|
259
|
+
storageSlot,
|
|
260
|
+
noteTypeId,
|
|
261
|
+
note,
|
|
262
|
+
);
|
|
263
|
+
return uniqueSiloedNoteHash;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Computes the siloed note hash of a note.
|
|
268
|
+
* @param contractAddress - The address of the contract.
|
|
269
|
+
* @param nonce - The nonce of the note hash.
|
|
270
|
+
* @param storageSlot - The storage slot.
|
|
271
|
+
* @param noteTypeId - The note type identifier.
|
|
272
|
+
* @param note - The note.
|
|
273
|
+
* @returns The note hash.
|
|
274
|
+
*/
|
|
275
|
+
public async computeSiloedNoteHash(
|
|
276
|
+
contractAddress: AztecAddress,
|
|
277
|
+
nonce: Fr,
|
|
278
|
+
storageSlot: Fr,
|
|
279
|
+
noteTypeId: Fr,
|
|
280
|
+
note: Note,
|
|
281
|
+
) {
|
|
282
|
+
const { siloedNoteHash } = await this.computeNoteHashAndNullifier(
|
|
283
|
+
contractAddress,
|
|
284
|
+
nonce,
|
|
285
|
+
storageSlot,
|
|
286
|
+
noteTypeId,
|
|
287
|
+
note,
|
|
288
|
+
);
|
|
289
|
+
return siloedNoteHash;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Computes the inner note hash of a note, which contains storage slot and the custom note hash.
|
|
294
|
+
* @param contractAddress - The address of the contract.
|
|
295
|
+
* @param nonce - The nonce of the unique note hash.
|
|
296
|
+
* @param storageSlot - The storage slot.
|
|
297
|
+
* @param noteTypeId - The note type identifier.
|
|
298
|
+
* @param note - The note.
|
|
299
|
+
* @returns The note hash.
|
|
300
|
+
*/
|
|
301
|
+
public async computeInnerNullifier(
|
|
302
|
+
contractAddress: AztecAddress,
|
|
303
|
+
nonce: Fr,
|
|
304
|
+
storageSlot: Fr,
|
|
305
|
+
noteTypeId: Fr,
|
|
306
|
+
note: Note,
|
|
307
|
+
) {
|
|
308
|
+
const { innerNullifier } = await this.computeNoteHashAndNullifier(
|
|
309
|
+
contractAddress,
|
|
310
|
+
nonce,
|
|
311
|
+
storageSlot,
|
|
312
|
+
noteTypeId,
|
|
313
|
+
note,
|
|
314
|
+
);
|
|
315
|
+
return innerNullifier;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { FunctionData } from '@aztec/circuits.js';
|
|
2
|
+
import { DecodedReturn, 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
|
+
|
|
7
|
+
import { extractReturnWitness } from '../acvm/deserialize.js';
|
|
8
|
+
import { Oracle, acvm, extractCallStack, toACVMWitness } from '../acvm/index.js';
|
|
9
|
+
import { ExecutionError } from '../common/errors.js';
|
|
10
|
+
import { AcirSimulator } from './simulator.js';
|
|
11
|
+
import { ViewDataOracle } from './view_data_oracle.js';
|
|
12
|
+
|
|
13
|
+
// docs:start:execute_unconstrained_function
|
|
14
|
+
/**
|
|
15
|
+
* Execute an unconstrained function and return the decoded values.
|
|
16
|
+
*/
|
|
17
|
+
export async function executeUnconstrainedFunction(
|
|
18
|
+
oracle: ViewDataOracle,
|
|
19
|
+
artifact: FunctionArtifactWithDebugMetadata,
|
|
20
|
+
contractAddress: AztecAddress,
|
|
21
|
+
functionData: FunctionData,
|
|
22
|
+
args: Fr[],
|
|
23
|
+
log = createDebugLogger('aztec:simulator:unconstrained_execution'),
|
|
24
|
+
): Promise<DecodedReturn> {
|
|
25
|
+
const functionSelector = functionData.selector;
|
|
26
|
+
log(`Executing unconstrained function ${contractAddress}:${functionSelector}`);
|
|
27
|
+
|
|
28
|
+
const acir = Buffer.from(artifact.bytecode, 'base64');
|
|
29
|
+
const initialWitness = toACVMWitness(0, args);
|
|
30
|
+
const { partialWitness } = await acvm(
|
|
31
|
+
await AcirSimulator.getSolver(),
|
|
32
|
+
acir,
|
|
33
|
+
initialWitness,
|
|
34
|
+
new Oracle(oracle),
|
|
35
|
+
).catch((err: Error) => {
|
|
36
|
+
throw new ExecutionError(
|
|
37
|
+
err.message,
|
|
38
|
+
{
|
|
39
|
+
contractAddress,
|
|
40
|
+
functionSelector,
|
|
41
|
+
},
|
|
42
|
+
extractCallStack(err, artifact.debug),
|
|
43
|
+
{ cause: err },
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return decodeReturnValues(artifact, extractReturnWitness(acir, partialWitness));
|
|
48
|
+
}
|
|
49
|
+
// docs:end:execute_unconstrained_function
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthWitness,
|
|
3
|
+
AztecNode,
|
|
4
|
+
CompleteAddress,
|
|
5
|
+
MerkleTreeId,
|
|
6
|
+
NoteStatus,
|
|
7
|
+
NullifierMembershipWitness,
|
|
8
|
+
PublicDataWitness,
|
|
9
|
+
} from '@aztec/circuit-types';
|
|
10
|
+
import { Header } from '@aztec/circuits.js';
|
|
11
|
+
import { siloNullifier } from '@aztec/circuits.js/hash';
|
|
12
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
13
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
14
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
15
|
+
import { ContractInstance } from '@aztec/types/contracts';
|
|
16
|
+
|
|
17
|
+
import { NoteData, TypedOracle } from '../acvm/index.js';
|
|
18
|
+
import { DBOracle } from './db_oracle.js';
|
|
19
|
+
import { pickNotes } from './pick_notes.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The execution context for a client view tx simulation.
|
|
23
|
+
* It only reads data from data sources. Nothing will be updated or created during this simulation.
|
|
24
|
+
*/
|
|
25
|
+
export class ViewDataOracle extends TypedOracle {
|
|
26
|
+
constructor(
|
|
27
|
+
protected readonly contractAddress: AztecAddress,
|
|
28
|
+
/** List of transient auth witnesses to be used during this simulation */
|
|
29
|
+
protected readonly authWitnesses: AuthWitness[],
|
|
30
|
+
protected readonly db: DBOracle,
|
|
31
|
+
protected readonly aztecNode: AztecNode,
|
|
32
|
+
protected log = createDebugLogger('aztec:simulator:client_view_context'),
|
|
33
|
+
) {
|
|
34
|
+
super();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Return the nullifier key pair of an account to use in a specific contract.
|
|
39
|
+
* @param account - The account address of the nullifier key.
|
|
40
|
+
*/
|
|
41
|
+
public getNullifierKeyPair(account: AztecAddress) {
|
|
42
|
+
return this.db.getNullifierKeyPair(account, this.contractAddress);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Fetches the index and sibling path of a leaf at a given block from a given tree.
|
|
47
|
+
* @param blockNumber - The block number at which to get the membership witness.
|
|
48
|
+
* @param treeId - Id of the tree to get the sibling path from.
|
|
49
|
+
* @param leafValue - The leaf value
|
|
50
|
+
* @returns The index and sibling path concatenated [index, sibling_path]
|
|
51
|
+
*/
|
|
52
|
+
public async getMembershipWitness(blockNumber: number, treeId: MerkleTreeId, leafValue: Fr): Promise<Fr[]> {
|
|
53
|
+
const index = await this.db.findLeafIndex(blockNumber, treeId, leafValue);
|
|
54
|
+
if (!index) {
|
|
55
|
+
throw new Error(`Leaf value: ${leafValue} not found in ${MerkleTreeId[treeId]}`);
|
|
56
|
+
}
|
|
57
|
+
const siblingPath = await this.db.getSiblingPath(blockNumber, treeId, index);
|
|
58
|
+
return [new Fr(index), ...siblingPath];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Fetches a sibling path at a given block and index from a tree specified by `treeId`.
|
|
63
|
+
* @param blockNumber - The block number at which to get the membership witness.
|
|
64
|
+
* @param treeId - Id of the tree to get the sibling path from.
|
|
65
|
+
* @param leafIndex - Index of the leaf to get sibling path for
|
|
66
|
+
* @returns The sibling path.
|
|
67
|
+
*/
|
|
68
|
+
public getSiblingPath(blockNumber: number, treeId: MerkleTreeId, leafIndex: Fr): Promise<Fr[]> {
|
|
69
|
+
return this.db.getSiblingPath(blockNumber, treeId, leafIndex.toBigInt());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns a nullifier membership witness for a given nullifier at a given block.
|
|
74
|
+
* @param blockNumber - The block number at which to get the index.
|
|
75
|
+
* @param nullifier - Nullifier we try to find witness for.
|
|
76
|
+
* @returns The nullifier membership witness (if found).
|
|
77
|
+
*/
|
|
78
|
+
public async getNullifierMembershipWitness(
|
|
79
|
+
blockNumber: number,
|
|
80
|
+
nullifier: Fr,
|
|
81
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
82
|
+
return await this.db.getNullifierMembershipWitness(blockNumber, nullifier);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns a low nullifier membership witness for a given nullifier at a given block.
|
|
87
|
+
* @param blockNumber - The block number at which to get the index.
|
|
88
|
+
* @param nullifier - Nullifier we try to find the low nullifier witness for.
|
|
89
|
+
* @returns The low nullifier membership witness (if found).
|
|
90
|
+
* @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
|
|
91
|
+
* list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
|
|
92
|
+
* we are trying to prove non-inclusion for.
|
|
93
|
+
*/
|
|
94
|
+
public async getLowNullifierMembershipWitness(
|
|
95
|
+
blockNumber: number,
|
|
96
|
+
nullifier: Fr,
|
|
97
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
98
|
+
return await this.db.getLowNullifierMembershipWitness(blockNumber, nullifier);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Returns a public data tree witness for a given leaf slot at a given block.
|
|
103
|
+
* @param blockNumber - The block number at which to get the index.
|
|
104
|
+
* @param leafSlot - The slot of the public data tree to get the witness for.
|
|
105
|
+
* @returns - The witness
|
|
106
|
+
*/
|
|
107
|
+
public async getPublicDataTreeWitness(blockNumber: number, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
|
|
108
|
+
return await this.db.getPublicDataTreeWitness(blockNumber, leafSlot);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Fetches a block header of a given block.
|
|
113
|
+
* @param blockNumber - The number of a block of which to get the block header.
|
|
114
|
+
* @returns Block extracted from a block with block number `blockNumber`.
|
|
115
|
+
*/
|
|
116
|
+
public async getHeader(blockNumber: number): Promise<Header | undefined> {
|
|
117
|
+
const block = await this.db.getBlock(blockNumber);
|
|
118
|
+
if (!block) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return block.header;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Retrieve the complete address associated to a given address.
|
|
126
|
+
* @param address - Address to fetch the complete address for.
|
|
127
|
+
* @returns A complete address associated with the input address.
|
|
128
|
+
*/
|
|
129
|
+
public getCompleteAddress(address: AztecAddress): Promise<CompleteAddress> {
|
|
130
|
+
return this.db.getCompleteAddress(address);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns a contract instance associated with an address or throws if not found.
|
|
135
|
+
* @param address - Address.
|
|
136
|
+
* @returns A contract instance.
|
|
137
|
+
*/
|
|
138
|
+
public getContractInstance(address: AztecAddress): Promise<ContractInstance> {
|
|
139
|
+
return this.db.getContractInstance(address);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns an auth witness for the given message hash. Checks on the list of transient witnesses
|
|
144
|
+
* for this transaction first, and falls back to the local database if not found.
|
|
145
|
+
* @param messageHash - Hash of the message to authenticate.
|
|
146
|
+
* @returns Authentication witness for the requested message hash.
|
|
147
|
+
*/
|
|
148
|
+
public getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
|
|
149
|
+
return Promise.resolve(
|
|
150
|
+
this.authWitnesses.find(w => w.requestHash.equals(messageHash))?.witness ?? this.db.getAuthWitness(messageHash),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Pops a capsule from the capsule dispenser
|
|
156
|
+
* @returns The capsule values
|
|
157
|
+
* @remarks A capsule is a "blob" of data that is passed to the contract through an oracle.
|
|
158
|
+
*/
|
|
159
|
+
public popCapsule(): Promise<Fr[]> {
|
|
160
|
+
return this.db.popCapsule();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Gets some notes for a contract address and storage slot.
|
|
165
|
+
* Returns a flattened array containing filtered notes.
|
|
166
|
+
*
|
|
167
|
+
* @remarks
|
|
168
|
+
* Check for pending notes with matching slot.
|
|
169
|
+
* Real notes coming from DB will have a leafIndex which
|
|
170
|
+
* represents their index in the note hash tree.
|
|
171
|
+
*
|
|
172
|
+
* @param storageSlot - The storage slot.
|
|
173
|
+
* @param numSelects - The number of valid selects in selectBy and selectValues.
|
|
174
|
+
* @param selectBy - An array of indices of the fields to selects.
|
|
175
|
+
* @param selectValues - The values to match.
|
|
176
|
+
* @param selectComparators - The comparators to use to match values.
|
|
177
|
+
* @param sortBy - An array of indices of the fields to sort.
|
|
178
|
+
* @param sortOrder - The order of the corresponding index in sortBy. (1: DESC, 2: ASC, 0: Do nothing)
|
|
179
|
+
* @param limit - The number of notes to retrieve per query.
|
|
180
|
+
* @param offset - The starting index for pagination.
|
|
181
|
+
* @param status - The status of notes to fetch.
|
|
182
|
+
* @returns Array of note data.
|
|
183
|
+
*/
|
|
184
|
+
public async getNotes(
|
|
185
|
+
storageSlot: Fr,
|
|
186
|
+
numSelects: number,
|
|
187
|
+
selectBy: number[],
|
|
188
|
+
selectValues: Fr[],
|
|
189
|
+
selectComparators: number[],
|
|
190
|
+
sortBy: number[],
|
|
191
|
+
sortOrder: number[],
|
|
192
|
+
limit: number,
|
|
193
|
+
offset: number,
|
|
194
|
+
status: NoteStatus,
|
|
195
|
+
): Promise<NoteData[]> {
|
|
196
|
+
const dbNotes = await this.db.getNotes(this.contractAddress, storageSlot, status);
|
|
197
|
+
return pickNotes<NoteData>(dbNotes, {
|
|
198
|
+
selects: selectBy
|
|
199
|
+
.slice(0, numSelects)
|
|
200
|
+
.map((index, i) => ({ index, value: selectValues[i], comparator: selectComparators[i] })),
|
|
201
|
+
sorts: sortBy.map((index, i) => ({ index, order: sortOrder[i] })),
|
|
202
|
+
limit,
|
|
203
|
+
offset,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Check if a nullifier exists in the nullifier tree.
|
|
209
|
+
* @param innerNullifier - The inner nullifier.
|
|
210
|
+
* @returns A boolean indicating whether the nullifier exists in the tree or not.
|
|
211
|
+
*/
|
|
212
|
+
public async checkNullifierExists(innerNullifier: Fr) {
|
|
213
|
+
const nullifier = siloNullifier(this.contractAddress, innerNullifier!);
|
|
214
|
+
const index = await this.db.getNullifierIndex(nullifier);
|
|
215
|
+
return index !== undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Fetches the a message from the db, given its key.
|
|
220
|
+
* @param entryKey - A buffer representing the entry key.
|
|
221
|
+
* @returns The l1 to l2 message data
|
|
222
|
+
*/
|
|
223
|
+
public async getL1ToL2MembershipWitness(entryKey: Fr) {
|
|
224
|
+
return await this.db.getL1ToL2MembershipWitness(entryKey);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Retrieves the portal contract address associated with the given contract address.
|
|
229
|
+
* Throws an error if the input contract address is not found or invalid.
|
|
230
|
+
* @param contractAddress - The address of the contract whose portal address is to be fetched.
|
|
231
|
+
* @returns The portal contract address.
|
|
232
|
+
*/
|
|
233
|
+
public getPortalContractAddress(contractAddress: AztecAddress) {
|
|
234
|
+
return this.db.getPortalContractAddress(contractAddress);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Read the public storage data.
|
|
239
|
+
* @param startStorageSlot - The starting storage slot.
|
|
240
|
+
* @param numberOfElements - Number of elements to read from the starting storage slot.
|
|
241
|
+
*/
|
|
242
|
+
public async storageRead(startStorageSlot: Fr, numberOfElements: number) {
|
|
243
|
+
const values = [];
|
|
244
|
+
for (let i = 0n; i < numberOfElements; i++) {
|
|
245
|
+
const storageSlot = new Fr(startStorageSlot.value + i);
|
|
246
|
+
const value = await this.aztecNode.getPublicStorageAt(this.contractAddress, storageSlot);
|
|
247
|
+
|
|
248
|
+
this.log(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
|
|
249
|
+
values.push(value);
|
|
250
|
+
}
|
|
251
|
+
return values;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { FailingFunction, NoirCallStack, SimulationError } from '@aztec/circuit-types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An error that occurred during the execution of a function.
|
|
5
|
+
*/
|
|
6
|
+
export class ExecutionError extends Error {
|
|
7
|
+
constructor(
|
|
8
|
+
message: string,
|
|
9
|
+
/**
|
|
10
|
+
* The function that failed.
|
|
11
|
+
*/
|
|
12
|
+
public failingFunction: FailingFunction,
|
|
13
|
+
/**
|
|
14
|
+
* The noir call stack of the function that failed.
|
|
15
|
+
*/
|
|
16
|
+
public noirCallStack?: NoirCallStack,
|
|
17
|
+
options?: ErrorOptions,
|
|
18
|
+
) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Traverses the cause chain of an error.
|
|
25
|
+
* @param error - The error to start from.
|
|
26
|
+
* @param callback - A callback on every error, including the first one.
|
|
27
|
+
*/
|
|
28
|
+
export function traverseCauseChain(error: Error, callback: (error: Error) => void) {
|
|
29
|
+
let currentError: Error | undefined = error;
|
|
30
|
+
while (currentError) {
|
|
31
|
+
callback(currentError);
|
|
32
|
+
if (currentError.cause && currentError.cause instanceof Error) {
|
|
33
|
+
currentError = currentError.cause;
|
|
34
|
+
} else {
|
|
35
|
+
currentError = undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a simulation error from an error chain generated during the execution of a function.
|
|
42
|
+
* @param error - The error thrown during execution.
|
|
43
|
+
* @returns - A simulation error.
|
|
44
|
+
*/
|
|
45
|
+
export function createSimulationError(error: Error): SimulationError {
|
|
46
|
+
let rootCause = error;
|
|
47
|
+
let noirCallStack: NoirCallStack | undefined = undefined;
|
|
48
|
+
const aztecCallStack: FailingFunction[] = [];
|
|
49
|
+
|
|
50
|
+
traverseCauseChain(error, cause => {
|
|
51
|
+
rootCause = cause;
|
|
52
|
+
if (cause instanceof ExecutionError) {
|
|
53
|
+
aztecCallStack.push(cause.failingFunction);
|
|
54
|
+
if (cause.noirCallStack) {
|
|
55
|
+
noirCallStack = cause.noirCallStack;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return new SimulationError(rootCause.message, aztecCallStack, noirCallStack, { cause: rootCause });
|
|
61
|
+
}
|