@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.
- package/dest/acvm/acvm.d.ts +4 -1
- package/dest/acvm/acvm.d.ts.map +1 -1
- package/dest/acvm/acvm.js +5 -5
- package/dest/acvm/deserialize.d.ts +3 -5
- package/dest/acvm/deserialize.d.ts.map +1 -1
- package/dest/acvm/deserialize.js +6 -9
- package/dest/acvm/oracle/oracle.d.ts +2 -0
- package/dest/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/oracle.js +12 -4
- package/dest/acvm/oracle/typed_oracle.d.ts +2 -0
- package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/typed_oracle.js +7 -1
- package/dest/avm/avm_execution_environment.d.ts +4 -2
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +7 -5
- package/dest/avm/avm_gas.js +2 -2
- package/dest/avm/avm_machine_state.d.ts +2 -0
- package/dest/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/avm/avm_machine_state.js +24 -6
- package/dest/avm/avm_memory_types.js +2 -2
- package/dest/avm/avm_simulator.js +2 -2
- package/dest/avm/fixtures/index.d.ts +3 -0
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +11 -4
- package/dest/avm/journal/journal.d.ts +1 -0
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +12 -1
- package/dest/avm/opcodes/accrued_substate.d.ts +2 -1
- package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
- package/dest/avm/opcodes/accrued_substate.js +13 -5
- package/dest/avm/opcodes/context_getters.js +2 -2
- package/dest/avm/opcodes/external_calls.d.ts +2 -2
- package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
- package/dest/avm/opcodes/external_calls.js +7 -6
- package/dest/avm/opcodes/hashing.d.ts +8 -8
- package/dest/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/avm/opcodes/hashing.js +35 -43
- package/dest/avm/serialization/instruction_serialization.d.ts +1 -1
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +2 -2
- package/dest/client/client_execution_context.d.ts +13 -3
- package/dest/client/client_execution_context.d.ts.map +1 -1
- package/dest/client/client_execution_context.js +23 -8
- package/dest/client/private_execution.d.ts.map +1 -1
- package/dest/client/private_execution.js +8 -6
- package/dest/client/simulator.d.ts.map +1 -1
- package/dest/client/simulator.js +6 -5
- package/dest/client/unconstrained_execution.d.ts.map +1 -1
- package/dest/client/unconstrained_execution.js +5 -4
- package/dest/common/index.d.ts +1 -1
- package/dest/common/index.d.ts.map +1 -1
- package/dest/common/index.js +2 -2
- package/dest/common/packed_values_cache.d.ts +28 -0
- package/dest/common/packed_values_cache.d.ts.map +1 -0
- package/dest/common/packed_values_cache.js +50 -0
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/mocks/fixtures.d.ts +42 -0
- package/dest/mocks/fixtures.d.ts.map +1 -0
- package/dest/mocks/fixtures.js +84 -0
- package/dest/mocks/index.d.ts +2 -0
- package/dest/mocks/index.d.ts.map +1 -0
- package/dest/mocks/index.js +2 -0
- package/dest/public/abstract_phase_manager.d.ts +82 -0
- package/dest/public/abstract_phase_manager.d.ts.map +1 -0
- package/dest/public/abstract_phase_manager.js +320 -0
- package/dest/public/app_logic_phase_manager.d.ts +29 -0
- package/dest/public/app_logic_phase_manager.d.ts.map +1 -0
- package/dest/public/app_logic_phase_manager.js +50 -0
- package/dest/public/execution.d.ts +3 -0
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +1 -1
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +17 -13
- package/dest/public/hints_builder.d.ts +23 -0
- package/dest/public/hints_builder.d.ts.map +1 -0
- package/dest/public/hints_builder.js +62 -0
- package/dest/public/index.d.ts +5 -0
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +6 -1
- package/dest/public/phase_manager_factory.d.ts +18 -0
- package/dest/public/phase_manager_factory.d.ts.map +1 -0
- package/dest/public/phase_manager_factory.js +56 -0
- package/dest/public/public_execution_context.d.ts +15 -5
- package/dest/public/public_execution_context.d.ts.map +1 -1
- package/dest/public/public_execution_context.js +28 -12
- package/dest/public/public_executor.d.ts +79 -0
- package/dest/public/public_executor.d.ts.map +1 -0
- package/dest/public/public_executor.js +198 -0
- package/dest/public/public_kernel.d.ts +37 -0
- package/dest/public/public_kernel.d.ts.map +1 -0
- package/dest/public/public_kernel.js +97 -0
- package/dest/public/public_kernel_circuit_simulator.d.ts +31 -0
- package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -0
- package/dest/public/public_kernel_circuit_simulator.js +2 -0
- package/dest/public/public_processor.d.ts +53 -0
- package/dest/public/public_processor.d.ts.map +1 -0
- package/dest/public/public_processor.js +144 -0
- package/dest/public/setup_phase_manager.d.ts +30 -0
- package/dest/public/setup_phase_manager.d.ts.map +1 -0
- package/dest/public/setup_phase_manager.js +46 -0
- package/dest/public/tail_phase_manager.d.ts +30 -0
- package/dest/public/tail_phase_manager.d.ts.map +1 -0
- package/dest/public/tail_phase_manager.js +60 -0
- package/dest/public/teardown_phase_manager.d.ts +30 -0
- package/dest/public/teardown_phase_manager.d.ts.map +1 -0
- package/dest/public/teardown_phase_manager.js +46 -0
- package/dest/public/transitional_adaptors.d.ts +2 -1
- package/dest/public/transitional_adaptors.d.ts.map +1 -1
- package/dest/public/transitional_adaptors.js +11 -10
- package/dest/public/utils.d.ts +8 -0
- package/dest/public/utils.d.ts.map +1 -0
- package/dest/public/utils.js +29 -0
- package/dest/simulator/acvm_native.d.ts +19 -3
- package/dest/simulator/acvm_native.d.ts.map +1 -1
- package/dest/simulator/acvm_native.js +75 -48
- package/dest/simulator/acvm_wasm.d.ts.map +1 -1
- package/dest/simulator/acvm_wasm.js +3 -4
- package/package.json +8 -5
- package/src/acvm/acvm.ts +8 -5
- package/src/acvm/deserialize.ts +5 -9
- package/src/acvm/oracle/oracle.ts +13 -3
- package/src/acvm/oracle/typed_oracle.ts +8 -0
- package/src/avm/avm_execution_environment.ts +9 -1
- package/src/avm/avm_gas.ts +1 -1
- package/src/avm/avm_machine_state.ts +26 -5
- package/src/avm/avm_memory_types.ts +1 -1
- package/src/avm/avm_simulator.ts +1 -1
- package/src/avm/fixtures/index.ts +13 -1
- package/src/avm/journal/journal.ts +13 -0
- package/src/avm/opcodes/accrued_substate.ts +16 -4
- package/src/avm/opcodes/context_getters.ts +1 -1
- package/src/avm/opcodes/external_calls.ts +8 -5
- package/src/avm/opcodes/hashing.ts +38 -54
- package/src/avm/serialization/instruction_serialization.ts +1 -1
- package/src/client/client_execution_context.ts +25 -6
- package/src/client/private_execution.ts +7 -6
- package/src/client/simulator.ts +7 -3
- package/src/client/unconstrained_execution.ts +4 -3
- package/src/common/index.ts +1 -1
- package/src/common/packed_values_cache.ts +55 -0
- package/src/index.ts +1 -0
- package/src/mocks/fixtures.ts +169 -0
- package/src/mocks/index.ts +1 -0
- package/src/public/abstract_phase_manager.ts +571 -0
- package/src/public/app_logic_phase_manager.ts +76 -0
- package/src/public/execution.ts +4 -0
- package/src/public/executor.ts +18 -13
- package/src/public/hints_builder.ts +119 -0
- package/src/public/index.ts +5 -0
- package/src/public/phase_manager_factory.ts +126 -0
- package/src/public/public_execution_context.ts +29 -18
- package/src/public/public_executor.ts +267 -0
- package/src/public/public_kernel.ts +139 -0
- package/src/public/public_kernel_circuit_simulator.ts +36 -0
- package/src/public/public_processor.ts +212 -0
- package/src/public/setup_phase_manager.ts +66 -0
- package/src/public/tail_phase_manager.ts +120 -0
- package/src/public/teardown_phase_manager.ts +66 -0
- package/src/public/transitional_adaptors.ts +14 -5
- package/src/public/utils.ts +31 -0
- package/src/simulator/acvm_native.ts +94 -47
- package/src/simulator/acvm_wasm.ts +7 -3
- package/dest/common/packed_args_cache.d.ts +0 -28
- package/dest/common/packed_args_cache.d.ts.map +0 -1
- package/dest/common/packed_args_cache.js +0 -50
- package/src/common/packed_args_cache.ts +0 -55
package/src/public/executor.ts
CHANGED
|
@@ -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,
|
|
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 {
|
|
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
|
-
|
|
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(
|
|
76
|
+
log.verbose(
|
|
77
|
+
`[AVM] ${address.toString()}:${selector} returned, reverted: ${result.reverted}, reason: ${result.revertReason}.`,
|
|
78
|
+
);
|
|
79
79
|
|
|
80
|
-
|
|
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 =
|
|
162
|
+
const returnWitness = witnessMapToFields(returnWitnessMap);
|
|
161
163
|
const {
|
|
162
|
-
|
|
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 =
|
|
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
|
+
}
|
package/src/public/index.ts
CHANGED
|
@@ -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
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
244
|
-
|
|
245
|
-
|
|
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> {
|