@aztec/simulator 0.55.0 → 0.56.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/avm/avm_execution_environment.d.ts +0 -8
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +1 -19
- package/dest/avm/avm_gas.d.ts +2 -2
- package/dest/avm/avm_gas.d.ts.map +1 -1
- package/dest/avm/avm_gas.js +31 -115
- package/dest/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/avm/avm_machine_state.js +6 -6
- package/dest/avm/avm_memory_types.d.ts +35 -7
- package/dest/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/avm/avm_memory_types.js +25 -11
- package/dest/avm/avm_simulator.d.ts.map +1 -1
- package/dest/avm/avm_simulator.js +5 -6
- package/dest/avm/bytecode_utils.d.ts +1 -2
- package/dest/avm/bytecode_utils.d.ts.map +1 -1
- package/dest/avm/bytecode_utils.js +3 -17
- package/dest/avm/fixtures/index.d.ts +4 -16
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +9 -25
- package/dest/avm/journal/index.d.ts +0 -1
- package/dest/avm/journal/index.d.ts.map +1 -1
- package/dest/avm/journal/index.js +1 -2
- package/dest/avm/journal/journal.d.ts +4 -4
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +14 -13
- package/dest/avm/opcodes/bitwise.d.ts +10 -4
- package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
- package/dest/avm/opcodes/bitwise.js +17 -11
- package/dest/avm/opcodes/commitment.js +2 -2
- package/dest/avm/opcodes/comparators.js +2 -2
- package/dest/avm/opcodes/conversion.d.ts +3 -2
- package/dest/avm/opcodes/conversion.d.ts.map +1 -1
- package/dest/avm/opcodes/conversion.js +18 -11
- package/dest/avm/opcodes/environment_getters.d.ts +27 -63
- package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/environment_getters.js +75 -80
- package/dest/avm/opcodes/hashing.d.ts +7 -5
- package/dest/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/avm/opcodes/hashing.js +40 -24
- package/dest/avm/opcodes/index.d.ts +3 -0
- package/dest/avm/opcodes/index.d.ts.map +1 -1
- package/dest/avm/opcodes/index.js +4 -1
- package/dest/avm/opcodes/instruction_impl.d.ts +0 -26
- package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction_impl.js +1 -51
- package/dest/avm/opcodes/memory.d.ts +7 -2
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +23 -6
- package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
- package/dest/avm/opcodes/multi_scalar_mul.js +7 -6
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +5 -24
- package/dest/avm/serialization/instruction_serialization.d.ts +48 -60
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +50 -63
- package/dest/avm/test_utils.d.ts +8 -8
- package/dest/avm/test_utils.d.ts.map +1 -1
- package/dest/avm/test_utils.js +15 -15
- package/dest/client/simulator.d.ts +1 -1
- package/dest/client/test_utils.d.ts.map +1 -1
- package/dest/client/test_utils.js +12 -6
- package/dest/client/unconstrained_execution.d.ts +2 -2
- package/dest/client/unconstrained_execution.d.ts.map +1 -1
- package/dest/client/unconstrained_execution.js +3 -3
- package/dest/public/enqueued_call_simulator.d.ts +43 -0
- package/dest/public/enqueued_call_simulator.d.ts.map +1 -0
- package/dest/public/enqueued_call_simulator.js +155 -0
- package/dest/public/enqueued_calls_processor.d.ts +44 -0
- package/dest/public/enqueued_calls_processor.d.ts.map +1 -0
- package/dest/public/enqueued_calls_processor.js +218 -0
- package/dest/public/execution.d.ts +4 -9
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +5 -10
- package/dest/public/executor.d.ts +3 -5
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +5 -9
- package/dest/public/hints_builder.d.ts +4 -3
- package/dest/public/hints_builder.d.ts.map +1 -1
- package/dest/public/hints_builder.js +4 -4
- package/dest/public/index.d.ts +3 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +3 -3
- package/dest/public/public_db_sources.d.ts +17 -24
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +58 -65
- package/dest/public/public_kernel.d.ts +3 -9
- package/dest/public/public_kernel.d.ts.map +1 -1
- package/dest/public/public_kernel.js +13 -40
- package/dest/public/public_kernel_circuit_simulator.d.ts +5 -11
- package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -1
- package/dest/public/public_kernel_tail_simulator.d.ts +19 -0
- package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -0
- package/dest/public/public_kernel_tail_simulator.js +45 -0
- package/dest/public/public_processor.d.ts +8 -5
- package/dest/public/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor.js +31 -48
- package/dest/public/public_processor_metrics.d.ts +3 -3
- package/dest/public/public_processor_metrics.d.ts.map +1 -1
- package/dest/public/public_processor_metrics.js +1 -1
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +4 -8
- package/package.json +9 -9
- package/src/avm/avm_execution_environment.ts +1 -21
- package/src/avm/avm_gas.ts +30 -114
- package/src/avm/avm_machine_state.ts +5 -5
- package/src/avm/avm_memory_types.ts +31 -9
- package/src/avm/avm_simulator.ts +4 -5
- package/src/avm/bytecode_utils.ts +2 -17
- package/src/avm/fixtures/index.ts +12 -44
- package/src/avm/journal/index.ts +0 -1
- package/src/avm/journal/journal.ts +13 -18
- package/src/avm/opcodes/bitwise.ts +14 -10
- package/src/avm/opcodes/commitment.ts +1 -1
- package/src/avm/opcodes/comparators.ts +2 -2
- package/src/avm/opcodes/conversion.ts +19 -10
- package/src/avm/opcodes/environment_getters.ts +72 -99
- package/src/avm/opcodes/hashing.ts +47 -22
- package/src/avm/opcodes/index.ts +3 -0
- package/src/avm/opcodes/instruction_impl.ts +0 -62
- package/src/avm/opcodes/memory.ts +20 -6
- package/src/avm/opcodes/multi_scalar_mul.ts +6 -6
- package/src/avm/serialization/bytecode_serialization.ts +11 -33
- package/src/avm/serialization/instruction_serialization.ts +5 -18
- package/src/avm/test_utils.ts +20 -16
- package/src/client/test_utils.ts +36 -9
- package/src/client/unconstrained_execution.ts +3 -8
- package/src/public/enqueued_call_simulator.ts +336 -0
- package/src/public/enqueued_calls_processor.ts +388 -0
- package/src/public/execution.ts +5 -12
- package/src/public/executor.ts +4 -12
- package/src/public/hints_builder.ts +14 -12
- package/src/public/index.ts +3 -2
- package/src/public/public_db_sources.ts +62 -67
- package/src/public/public_kernel.ts +15 -49
- package/src/public/public_kernel_circuit_simulator.ts +6 -10
- package/src/public/public_kernel_tail_simulator.ts +106 -0
- package/src/public/public_processor.ts +61 -86
- package/src/public/public_processor_metrics.ts +3 -3
- package/src/public/side_effect_trace.ts +5 -12
- package/dest/avm/journal/host_storage.d.ts +0 -13
- package/dest/avm/journal/host_storage.d.ts.map +0 -1
- package/dest/avm/journal/host_storage.js +0 -13
- package/dest/avm/opcodes/context_getters.d.ts +0 -15
- package/dest/avm/opcodes/context_getters.d.ts.map +0 -1
- package/dest/avm/opcodes/context_getters.js +0 -19
- package/dest/public/abstract_phase_manager.d.ts +0 -93
- package/dest/public/abstract_phase_manager.d.ts.map +0 -1
- package/dest/public/abstract_phase_manager.js +0 -285
- package/dest/public/app_logic_phase_manager.d.ts +0 -25
- package/dest/public/app_logic_phase_manager.d.ts.map +0 -1
- package/dest/public/app_logic_phase_manager.js +0 -52
- package/dest/public/phase_manager_factory.d.ts +0 -18
- package/dest/public/phase_manager_factory.d.ts.map +0 -1
- package/dest/public/phase_manager_factory.js +0 -56
- package/dest/public/setup_phase_manager.d.ts +0 -25
- package/dest/public/setup_phase_manager.d.ts.map +0 -1
- package/dest/public/setup_phase_manager.js +0 -39
- package/dest/public/tail_phase_manager.d.ts +0 -23
- package/dest/public/tail_phase_manager.d.ts.map +0 -1
- package/dest/public/tail_phase_manager.js +0 -50
- package/dest/public/teardown_phase_manager.d.ts +0 -27
- package/dest/public/teardown_phase_manager.d.ts.map +0 -1
- package/dest/public/teardown_phase_manager.js +0 -56
- package/src/avm/journal/host_storage.ts +0 -14
- package/src/avm/opcodes/context_getters.ts +0 -23
- package/src/public/abstract_phase_manager.ts +0 -522
- package/src/public/app_logic_phase_manager.ts +0 -81
- package/src/public/phase_manager_factory.ts +0 -126
- package/src/public/setup_phase_manager.ts +0 -61
- package/src/public/tail_phase_manager.ts +0 -127
- package/src/public/teardown_phase_manager.ts +0 -86
package/src/avm/test_utils.ts
CHANGED
|
@@ -4,12 +4,11 @@ import { type ContractInstanceWithAddress } from '@aztec/types/contracts';
|
|
|
4
4
|
import { type jest } from '@jest/globals';
|
|
5
5
|
import { mock } from 'jest-mock-extended';
|
|
6
6
|
|
|
7
|
-
import { type
|
|
7
|
+
import { type WorldStateDB } from '../public/public_db_sources.js';
|
|
8
8
|
import { type PublicSideEffectTraceInterface } from '../public/side_effect_trace_interface.js';
|
|
9
|
-
import { type HostStorage } from './journal/host_storage.js';
|
|
10
9
|
|
|
11
|
-
export function mockGetBytecode(
|
|
12
|
-
(
|
|
10
|
+
export function mockGetBytecode(worldStateDB: WorldStateDB, bytecode: Buffer) {
|
|
11
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).getBytecode.mockResolvedValue(bytecode);
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export function mockTraceFork(trace: PublicSideEffectTraceInterface, nestedTrace?: PublicSideEffectTraceInterface) {
|
|
@@ -18,18 +17,18 @@ export function mockTraceFork(trace: PublicSideEffectTraceInterface, nestedTrace
|
|
|
18
17
|
);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
export function mockStorageRead(
|
|
22
|
-
(
|
|
20
|
+
export function mockStorageRead(worldStateDB: WorldStateDB, value: Fr) {
|
|
21
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).storageRead.mockResolvedValue(value);
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
export function mockStorageReadWithMap(
|
|
26
|
-
(
|
|
24
|
+
export function mockStorageReadWithMap(worldStateDB: WorldStateDB, mockedStorage: Map<bigint, Fr>) {
|
|
25
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).storageRead.mockImplementation((_address, slot) =>
|
|
27
26
|
Promise.resolve(mockedStorage.get(slot.toBigInt()) ?? Fr.ZERO),
|
|
28
27
|
);
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
export function mockNoteHashExists(
|
|
32
|
-
(
|
|
30
|
+
export function mockNoteHashExists(worldStateDB: WorldStateDB, _leafIndex: Fr, value?: Fr) {
|
|
31
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).getCommitmentValue.mockImplementation((index: bigint) => {
|
|
33
32
|
if (index == _leafIndex.toBigInt()) {
|
|
34
33
|
return Promise.resolve(value);
|
|
35
34
|
} else {
|
|
@@ -39,12 +38,17 @@ export function mockNoteHashExists(hs: HostStorage, _leafIndex: Fr, value?: Fr)
|
|
|
39
38
|
});
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
export function mockNullifierExists(
|
|
43
|
-
(
|
|
41
|
+
export function mockNullifierExists(worldStateDB: WorldStateDB, leafIndex: Fr, _value?: Fr) {
|
|
42
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).getNullifierIndex.mockResolvedValue(leafIndex.toBigInt());
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
export function mockL1ToL2MessageExists(
|
|
47
|
-
|
|
45
|
+
export function mockL1ToL2MessageExists(
|
|
46
|
+
worldStateDB: WorldStateDB,
|
|
47
|
+
leafIndex: Fr,
|
|
48
|
+
value: Fr,
|
|
49
|
+
valueAtOtherIndices?: Fr,
|
|
50
|
+
) {
|
|
51
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).getL1ToL2LeafValue.mockImplementation((index: bigint) => {
|
|
48
52
|
if (index == leafIndex.toBigInt()) {
|
|
49
53
|
return Promise.resolve(value);
|
|
50
54
|
} else {
|
|
@@ -55,6 +59,6 @@ export function mockL1ToL2MessageExists(hs: HostStorage, leafIndex: Fr, value: F
|
|
|
55
59
|
});
|
|
56
60
|
}
|
|
57
61
|
|
|
58
|
-
export function mockGetContractInstance(
|
|
59
|
-
(
|
|
62
|
+
export function mockGetContractInstance(worldStateDB: WorldStateDB, contractInstance: ContractInstanceWithAddress) {
|
|
63
|
+
(worldStateDB as jest.Mocked<WorldStateDB>).getContractInstance.mockResolvedValue(contractInstance);
|
|
60
64
|
}
|
package/src/client/test_utils.ts
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
import { Fq, Fr,
|
|
1
|
+
import { Fq, Fr, Point } from '@aztec/circuits.js';
|
|
2
2
|
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
3
|
-
import { pedersenCommit } from '@aztec/foundation/crypto';
|
|
4
3
|
|
|
5
4
|
// Copied over from `noir-projects/aztec-nr/aztec/src/generators.nr`
|
|
5
|
+
const GENERATORS = [
|
|
6
|
+
new Point(
|
|
7
|
+
new Fr(0x30426e64aee30e998c13c8ceecda3a77807dbead52bc2f3bf0eae851b4b710c1n),
|
|
8
|
+
new Fr(0x113156a068f603023240c96b4da5474667db3b8711c521c748212a15bc034ea6n),
|
|
9
|
+
false,
|
|
10
|
+
),
|
|
11
|
+
new Point(
|
|
12
|
+
new Fr(0x2825c79cc6a5cbbeef7d6a8f1b6a12b312aa338440aefeb4396148c89147c049n),
|
|
13
|
+
new Fr(0x129bfd1da54b7062d6b544e7e36b90736350f6fba01228c41c72099509f5701en),
|
|
14
|
+
false,
|
|
15
|
+
),
|
|
16
|
+
new Point(
|
|
17
|
+
new Fr(0x0edb1e293c3ce91bfc04e3ceaa50d2c541fa9d091c72eb403efb1cfa2cb3357fn),
|
|
18
|
+
new Fr(0x1341d675fa030ece3113ad53ca34fd13b19b6e9762046734f414824c4d6ade35n),
|
|
19
|
+
false,
|
|
20
|
+
),
|
|
21
|
+
new Point(
|
|
22
|
+
new Fr(0x0e0dad2250583f2a9f0acb04ededf1701b85b0393cae753fe7e14b88af81cb52n),
|
|
23
|
+
new Fr(0x0973b02c5caac339ee4ad5dab51329920f7bf1b6a07e1dabe5df67040b300962n),
|
|
24
|
+
false,
|
|
25
|
+
),
|
|
26
|
+
new Point(
|
|
27
|
+
new Fr(0x2f3342e900e8c488a28931aae68970738fdc68afde2910de7b320c00c902087dn),
|
|
28
|
+
new Fr(0x1bf958dc63cb09d59230603a0269ae86d6f92494da244910351f1132df20fc08n),
|
|
29
|
+
false,
|
|
30
|
+
),
|
|
31
|
+
];
|
|
32
|
+
|
|
6
33
|
const G_SLOT = new Point(
|
|
7
34
|
new Fr(0x041223147b680850dc82e8a55a952d4df20256fe0593d949a9541ca00f0abf15n),
|
|
8
35
|
new Fr(0x0a8c72e60d0e60f5d804549d48f3044d06140b98ed717a9b532af630c1530791n),
|
|
@@ -16,14 +43,14 @@ const G_SLOT = new Point(
|
|
|
16
43
|
* @returns A note hash.
|
|
17
44
|
*/
|
|
18
45
|
export function computeNoteHash(storageSlot: Fr, noteContent: Fr[]): Fr {
|
|
19
|
-
// TODO(#7771): update this to do only 1 MSM call
|
|
20
|
-
const c = pedersenCommit(
|
|
21
|
-
noteContent.map(f => f.toBuffer()),
|
|
22
|
-
GeneratorIndex.NOTE_HIDING_POINT,
|
|
23
|
-
);
|
|
24
|
-
const noteHidingPointBeforeSlotting = new Point(new Fr(c[0]), new Fr(c[1]), false);
|
|
25
|
-
|
|
26
46
|
const grumpkin = new Grumpkin();
|
|
47
|
+
const noteHidingPointBeforeSlotting = noteContent
|
|
48
|
+
.slice(1)
|
|
49
|
+
.reduce(
|
|
50
|
+
(acc, item, i) => grumpkin.add(acc, grumpkin.mul(GENERATORS[i + 1], new Fq(item.toBigInt()))),
|
|
51
|
+
grumpkin.mul(GENERATORS[0], new Fq(noteContent[0].toBigInt())),
|
|
52
|
+
);
|
|
53
|
+
|
|
27
54
|
const slotPoint = grumpkin.mul(G_SLOT, new Fq(storageSlot.toBigInt()));
|
|
28
55
|
const noteHidingPoint = grumpkin.add(noteHidingPointBeforeSlotting, slotPoint);
|
|
29
56
|
return noteHidingPoint.x;
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DecodedReturn,
|
|
3
|
-
type FunctionArtifact,
|
|
4
|
-
type FunctionSelector,
|
|
5
|
-
decodeReturnValues,
|
|
6
|
-
} from '@aztec/foundation/abi';
|
|
1
|
+
import { type AbiDecoded, type FunctionArtifact, type FunctionSelector, decodeFromAbi } from '@aztec/foundation/abi';
|
|
7
2
|
import { type AztecAddress } from '@aztec/foundation/aztec-address';
|
|
8
3
|
import { type Fr } from '@aztec/foundation/fields';
|
|
9
4
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
@@ -24,7 +19,7 @@ export async function executeUnconstrainedFunction(
|
|
|
24
19
|
functionSelector: FunctionSelector,
|
|
25
20
|
args: Fr[],
|
|
26
21
|
log = createDebugLogger('aztec:simulator:unconstrained_execution'),
|
|
27
|
-
): Promise<
|
|
22
|
+
): Promise<AbiDecoded> {
|
|
28
23
|
log.verbose(`Executing unconstrained function ${contractAddress}:${functionSelector}(${artifact.name})`);
|
|
29
24
|
|
|
30
25
|
const acir = artifact.bytecode;
|
|
@@ -42,6 +37,6 @@ export async function executeUnconstrainedFunction(
|
|
|
42
37
|
});
|
|
43
38
|
|
|
44
39
|
const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
|
|
45
|
-
return
|
|
40
|
+
return decodeFromAbi(artifact.returnTypes, returnWitness);
|
|
46
41
|
}
|
|
47
42
|
// docs:end:execute_unconstrained_function
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AVM_REQUEST,
|
|
3
|
+
type AvmProvingRequest,
|
|
4
|
+
MerkleTreeId,
|
|
5
|
+
NestedProcessReturnValues,
|
|
6
|
+
ProvingRequestType,
|
|
7
|
+
type PublicExecutionRequest,
|
|
8
|
+
PublicKernelPhase,
|
|
9
|
+
type PublicProvingRequest,
|
|
10
|
+
type SimulationError,
|
|
11
|
+
type Tx,
|
|
12
|
+
UnencryptedFunctionL2Logs,
|
|
13
|
+
} from '@aztec/circuit-types';
|
|
14
|
+
import {
|
|
15
|
+
AztecAddress,
|
|
16
|
+
ContractStorageRead,
|
|
17
|
+
ContractStorageUpdateRequest,
|
|
18
|
+
Fr,
|
|
19
|
+
FunctionData,
|
|
20
|
+
Gas,
|
|
21
|
+
type GlobalVariables,
|
|
22
|
+
type Header,
|
|
23
|
+
L2ToL1Message,
|
|
24
|
+
LogHash,
|
|
25
|
+
MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL,
|
|
26
|
+
MAX_L2_TO_L1_MSGS_PER_CALL,
|
|
27
|
+
MAX_NOTE_HASHES_PER_CALL,
|
|
28
|
+
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
|
|
29
|
+
MAX_NULLIFIERS_PER_CALL,
|
|
30
|
+
MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
|
|
31
|
+
MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
|
|
32
|
+
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
|
|
33
|
+
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
|
|
34
|
+
MAX_PUBLIC_DATA_READS_PER_CALL,
|
|
35
|
+
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
|
|
36
|
+
MAX_UNENCRYPTED_LOGS_PER_CALL,
|
|
37
|
+
NESTED_RECURSIVE_PROOF_LENGTH,
|
|
38
|
+
NoteHash,
|
|
39
|
+
Nullifier,
|
|
40
|
+
PublicAccumulatedData,
|
|
41
|
+
PublicAccumulatedDataArrayLengths,
|
|
42
|
+
PublicCallData,
|
|
43
|
+
type PublicCallRequest,
|
|
44
|
+
PublicCallStackItem,
|
|
45
|
+
PublicCircuitPublicInputs,
|
|
46
|
+
PublicInnerCallRequest,
|
|
47
|
+
type PublicKernelCircuitPublicInputs,
|
|
48
|
+
PublicKernelInnerCircuitPrivateInputs,
|
|
49
|
+
PublicKernelInnerData,
|
|
50
|
+
PublicValidationRequestArrayLengths,
|
|
51
|
+
PublicValidationRequests,
|
|
52
|
+
ReadRequest,
|
|
53
|
+
RevertCode,
|
|
54
|
+
TreeLeafReadRequest,
|
|
55
|
+
VMCircuitPublicInputs,
|
|
56
|
+
makeEmptyProof,
|
|
57
|
+
makeEmptyRecursiveProof,
|
|
58
|
+
} from '@aztec/circuits.js';
|
|
59
|
+
import { computeVarArgsHash } from '@aztec/circuits.js/hash';
|
|
60
|
+
import { makeTuple } from '@aztec/foundation/array';
|
|
61
|
+
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
62
|
+
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
|
|
63
|
+
import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types';
|
|
64
|
+
import { type MerkleTreeOperations } from '@aztec/world-state';
|
|
65
|
+
|
|
66
|
+
import { accumulateReturnValues } from '../common/index.js';
|
|
67
|
+
import { type PublicExecutionResult, collectExecutionResults } from './execution.js';
|
|
68
|
+
import { type PublicExecutor } from './executor.js';
|
|
69
|
+
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
|
|
70
|
+
|
|
71
|
+
function makeAvmProvingRequest(
|
|
72
|
+
inputs: PublicKernelInnerCircuitPrivateInputs,
|
|
73
|
+
result: PublicExecutionResult,
|
|
74
|
+
): AvmProvingRequest {
|
|
75
|
+
return {
|
|
76
|
+
type: AVM_REQUEST,
|
|
77
|
+
functionName: result.functionName,
|
|
78
|
+
calldata: result.calldata,
|
|
79
|
+
bytecode: result.bytecode!,
|
|
80
|
+
avmHints: result.avmCircuitHints,
|
|
81
|
+
kernelRequest: {
|
|
82
|
+
type: ProvingRequestType.PUBLIC_KERNEL_INNER,
|
|
83
|
+
inputs,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type EnqueuedCallResult = {
|
|
89
|
+
/** Inputs to be used for proving */
|
|
90
|
+
provingRequests: PublicProvingRequest[];
|
|
91
|
+
/** The public kernel output at the end of the enqueued call */
|
|
92
|
+
kernelOutput: VMCircuitPublicInputs;
|
|
93
|
+
/** Unencrypted logs generated during the execution of this enqueued call */
|
|
94
|
+
newUnencryptedLogs: UnencryptedFunctionL2Logs;
|
|
95
|
+
/** Return values of simulating complete callstack */
|
|
96
|
+
returnValues: NestedProcessReturnValues;
|
|
97
|
+
/** Gas used during the execution this enqueued call */
|
|
98
|
+
gasUsed: Gas;
|
|
99
|
+
/** Revert reason, if any */
|
|
100
|
+
revertReason?: SimulationError;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export class EnqueuedCallSimulator {
|
|
104
|
+
private log: DebugLogger;
|
|
105
|
+
constructor(
|
|
106
|
+
private db: MerkleTreeOperations,
|
|
107
|
+
private publicExecutor: PublicExecutor,
|
|
108
|
+
private publicKernelSimulator: PublicKernelCircuitSimulator,
|
|
109
|
+
private globalVariables: GlobalVariables,
|
|
110
|
+
private historicalHeader: Header,
|
|
111
|
+
) {
|
|
112
|
+
this.log = createDebugLogger(`aztec:sequencer`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async simulate(
|
|
116
|
+
callRequest: PublicCallRequest,
|
|
117
|
+
executionRequest: PublicExecutionRequest,
|
|
118
|
+
tx: Tx,
|
|
119
|
+
previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
|
|
120
|
+
availableGas: Gas,
|
|
121
|
+
transactionFee: Fr,
|
|
122
|
+
phase: PublicKernelPhase,
|
|
123
|
+
): Promise<EnqueuedCallResult> {
|
|
124
|
+
const pendingNullifiers = this.getSiloedPendingNullifiers(previousPublicKernelOutput);
|
|
125
|
+
const startSideEffectCounter = previousPublicKernelOutput.endSideEffectCounter + 1;
|
|
126
|
+
const result = await this.publicExecutor.simulate(
|
|
127
|
+
executionRequest,
|
|
128
|
+
this.globalVariables,
|
|
129
|
+
availableGas,
|
|
130
|
+
tx.data.constants.txContext,
|
|
131
|
+
pendingNullifiers,
|
|
132
|
+
transactionFee,
|
|
133
|
+
startSideEffectCounter,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const callStack = makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, PublicInnerCallRequest.empty);
|
|
137
|
+
callStack[0].item.contractAddress = callRequest.contractAddress;
|
|
138
|
+
callStack[0].item.callContext = callRequest.callContext;
|
|
139
|
+
callStack[0].item.argsHash = callRequest.argsHash;
|
|
140
|
+
const prevAccumulatedData =
|
|
141
|
+
phase === PublicKernelPhase.SETUP
|
|
142
|
+
? previousPublicKernelOutput.endNonRevertibleData
|
|
143
|
+
: previousPublicKernelOutput.end;
|
|
144
|
+
const accumulatedData = PublicAccumulatedData.empty();
|
|
145
|
+
accumulatedData.publicCallStack[0] = callRequest;
|
|
146
|
+
const startVMCircuitOutput = new VMCircuitPublicInputs(
|
|
147
|
+
previousPublicKernelOutput.constants,
|
|
148
|
+
callRequest,
|
|
149
|
+
callStack,
|
|
150
|
+
PublicValidationRequestArrayLengths.new(previousPublicKernelOutput.validationRequests),
|
|
151
|
+
PublicValidationRequests.empty(),
|
|
152
|
+
PublicAccumulatedDataArrayLengths.new(prevAccumulatedData),
|
|
153
|
+
accumulatedData,
|
|
154
|
+
startSideEffectCounter,
|
|
155
|
+
startSideEffectCounter,
|
|
156
|
+
availableGas,
|
|
157
|
+
result.transactionFee,
|
|
158
|
+
result.reverted,
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return await this.combineNestedExecutionResults(result, startVMCircuitOutput);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private async combineNestedExecutionResults(
|
|
165
|
+
topResult: PublicExecutionResult,
|
|
166
|
+
startVMCircuitOutput: VMCircuitPublicInputs,
|
|
167
|
+
): Promise<EnqueuedCallResult> {
|
|
168
|
+
const executionResults = collectExecutionResults(topResult);
|
|
169
|
+
|
|
170
|
+
const provingRequests: AvmProvingRequest[] = [];
|
|
171
|
+
let gasUsed = Gas.empty();
|
|
172
|
+
let revertReason;
|
|
173
|
+
let kernelOutput = startVMCircuitOutput;
|
|
174
|
+
|
|
175
|
+
for (const result of executionResults) {
|
|
176
|
+
// Accumulate gas used in this enqueued call.
|
|
177
|
+
gasUsed = gasUsed.add(Gas.from(result.startGasLeft).sub(Gas.from(result.endGasLeft)));
|
|
178
|
+
|
|
179
|
+
// Sanity check for a current upstream assumption.
|
|
180
|
+
// Consumers of the result seem to expect "reverted <=> revertReason !== undefined".
|
|
181
|
+
const functionSelector = result.executionRequest.callContext.functionSelector.toString();
|
|
182
|
+
if (result.reverted && !result.revertReason) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Simulation of ${result.executionRequest.contractAddress.toString()}:${functionSelector}(${
|
|
185
|
+
result.functionName
|
|
186
|
+
}) reverted with no reason.`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Simulate the public kernel circuit.
|
|
191
|
+
this.log.debug(
|
|
192
|
+
`Running public kernel inner circuit for ${result.executionRequest.contractAddress.toString()}:${functionSelector}(${
|
|
193
|
+
result.functionName
|
|
194
|
+
})`,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const callData = await this.getPublicCallData(result);
|
|
198
|
+
const { inputs, output } = await this.runKernelCircuit(kernelOutput, callData);
|
|
199
|
+
kernelOutput = output;
|
|
200
|
+
|
|
201
|
+
// Capture the inputs for later proving in the AVM and kernel.
|
|
202
|
+
provingRequests.push(makeAvmProvingRequest(inputs, result));
|
|
203
|
+
|
|
204
|
+
// Safely return the revert reason and the kernel output (which has had its revertible side effects dropped)
|
|
205
|
+
// TODO(@leila) we shouldn't drop everything when it reverts. The tail kernel needs the data to prove that it's reverted for the correct reason.
|
|
206
|
+
if (result.reverted) {
|
|
207
|
+
this.log.debug(
|
|
208
|
+
`Reverting on ${result.executionRequest.contractAddress.toString()}:${functionSelector}(${
|
|
209
|
+
result.functionName
|
|
210
|
+
}) with reason: ${result.revertReason}`,
|
|
211
|
+
);
|
|
212
|
+
// TODO(@spalladino): Check gasUsed is correct. The AVM should take care of setting gasLeft to zero upon a revert.
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
provingRequests,
|
|
216
|
+
kernelOutput,
|
|
217
|
+
newUnencryptedLogs: UnencryptedFunctionL2Logs.empty(),
|
|
218
|
+
returnValues: NestedProcessReturnValues.empty(),
|
|
219
|
+
gasUsed,
|
|
220
|
+
revertReason: result.revertReason,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
provingRequests,
|
|
227
|
+
kernelOutput,
|
|
228
|
+
newUnencryptedLogs: topResult.allUnencryptedLogs,
|
|
229
|
+
returnValues: accumulateReturnValues(topResult),
|
|
230
|
+
gasUsed,
|
|
231
|
+
revertReason,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Returns all pending private and public nullifiers. */
|
|
236
|
+
private getSiloedPendingNullifiers(ko: PublicKernelCircuitPublicInputs) {
|
|
237
|
+
return [...ko.end.nullifiers, ...ko.endNonRevertibleData.nullifiers].filter(n => !n.isEmpty());
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private async runKernelCircuit(
|
|
241
|
+
previousOutput: VMCircuitPublicInputs,
|
|
242
|
+
callData: PublicCallData,
|
|
243
|
+
): Promise<{ inputs: PublicKernelInnerCircuitPrivateInputs; output: VMCircuitPublicInputs }> {
|
|
244
|
+
// The proof is not used in simulation
|
|
245
|
+
const proof = makeEmptyRecursiveProof(NESTED_RECURSIVE_PROOF_LENGTH);
|
|
246
|
+
const vk = ProtocolCircuitVks.PublicKernelInnerArtifact;
|
|
247
|
+
const previousKernel = new PublicKernelInnerData(previousOutput, proof, vk);
|
|
248
|
+
const inputs = new PublicKernelInnerCircuitPrivateInputs(previousKernel, callData);
|
|
249
|
+
return { inputs, output: await this.publicKernelSimulator.publicKernelCircuitInner(inputs) };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Calculates the PublicCircuitOutput for this execution result along with its proof,
|
|
254
|
+
* and assembles a PublicCallData object from it.
|
|
255
|
+
* @param result - The execution result.
|
|
256
|
+
* @returns A corresponding PublicCallData object.
|
|
257
|
+
*/
|
|
258
|
+
private async getPublicCallData(result: PublicExecutionResult) {
|
|
259
|
+
const bytecodeHash = await this.getBytecodeHash(result);
|
|
260
|
+
const callStackItem = await this.getPublicCallStackItem(result);
|
|
261
|
+
return new PublicCallData(callStackItem, makeEmptyProof(), bytecodeHash);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private async getPublicCallStackItem(result: PublicExecutionResult) {
|
|
265
|
+
const publicDataTreeInfo = await this.db.getTreeInfo(MerkleTreeId.PUBLIC_DATA_TREE);
|
|
266
|
+
this.historicalHeader.state.partial.publicDataTree.root = Fr.fromBuffer(publicDataTreeInfo.root);
|
|
267
|
+
|
|
268
|
+
const publicCircuitPublicInputs = PublicCircuitPublicInputs.from({
|
|
269
|
+
callContext: result.executionRequest.callContext,
|
|
270
|
+
proverAddress: AztecAddress.ZERO,
|
|
271
|
+
argsHash: computeVarArgsHash(result.executionRequest.args),
|
|
272
|
+
noteHashes: padArrayEnd(result.noteHashes, NoteHash.empty(), MAX_NOTE_HASHES_PER_CALL),
|
|
273
|
+
nullifiers: padArrayEnd(result.nullifiers, Nullifier.empty(), MAX_NULLIFIERS_PER_CALL),
|
|
274
|
+
l2ToL1Msgs: padArrayEnd(result.l2ToL1Messages, L2ToL1Message.empty(), MAX_L2_TO_L1_MSGS_PER_CALL),
|
|
275
|
+
startSideEffectCounter: result.startSideEffectCounter,
|
|
276
|
+
endSideEffectCounter: result.endSideEffectCounter,
|
|
277
|
+
returnsHash: computeVarArgsHash(result.returnValues),
|
|
278
|
+
noteHashReadRequests: padArrayEnd(
|
|
279
|
+
result.noteHashReadRequests,
|
|
280
|
+
TreeLeafReadRequest.empty(),
|
|
281
|
+
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
|
|
282
|
+
),
|
|
283
|
+
nullifierReadRequests: padArrayEnd(
|
|
284
|
+
result.nullifierReadRequests,
|
|
285
|
+
ReadRequest.empty(),
|
|
286
|
+
MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
|
|
287
|
+
),
|
|
288
|
+
nullifierNonExistentReadRequests: padArrayEnd(
|
|
289
|
+
result.nullifierNonExistentReadRequests,
|
|
290
|
+
ReadRequest.empty(),
|
|
291
|
+
MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
|
|
292
|
+
),
|
|
293
|
+
l1ToL2MsgReadRequests: padArrayEnd(
|
|
294
|
+
result.l1ToL2MsgReadRequests,
|
|
295
|
+
TreeLeafReadRequest.empty(),
|
|
296
|
+
MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL,
|
|
297
|
+
),
|
|
298
|
+
contractStorageReads: padArrayEnd(
|
|
299
|
+
result.contractStorageReads,
|
|
300
|
+
ContractStorageRead.empty(),
|
|
301
|
+
MAX_PUBLIC_DATA_READS_PER_CALL,
|
|
302
|
+
),
|
|
303
|
+
contractStorageUpdateRequests: padArrayEnd(
|
|
304
|
+
result.contractStorageUpdateRequests,
|
|
305
|
+
ContractStorageUpdateRequest.empty(),
|
|
306
|
+
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
|
|
307
|
+
),
|
|
308
|
+
publicCallRequests: padArrayEnd(
|
|
309
|
+
result.publicCallRequests,
|
|
310
|
+
PublicInnerCallRequest.empty(),
|
|
311
|
+
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
|
|
312
|
+
),
|
|
313
|
+
unencryptedLogsHashes: padArrayEnd(result.unencryptedLogsHashes, LogHash.empty(), MAX_UNENCRYPTED_LOGS_PER_CALL),
|
|
314
|
+
historicalHeader: this.historicalHeader,
|
|
315
|
+
globalVariables: this.globalVariables,
|
|
316
|
+
startGasLeft: Gas.from(result.startGasLeft),
|
|
317
|
+
endGasLeft: Gas.from(result.endGasLeft),
|
|
318
|
+
transactionFee: result.transactionFee,
|
|
319
|
+
// TODO(@just-mitch): need better mapping from simulator to revert code.
|
|
320
|
+
revertCode: result.reverted ? RevertCode.APP_LOGIC_REVERTED : RevertCode.OK,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
return new PublicCallStackItem(
|
|
324
|
+
result.executionRequest.contractAddress,
|
|
325
|
+
new FunctionData(result.executionRequest.callContext.functionSelector, false),
|
|
326
|
+
publicCircuitPublicInputs,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private getBytecodeHash(_result: PublicExecutionResult) {
|
|
331
|
+
// TODO: Determine how to calculate bytecode hash. Circuits just check it isn't zero for now.
|
|
332
|
+
// See https://github.com/AztecProtocol/aztec3-packages/issues/378
|
|
333
|
+
const bytecodeHash = new Fr(1n);
|
|
334
|
+
return Promise.resolve(bytecodeHash);
|
|
335
|
+
}
|
|
336
|
+
}
|