diff --git a/barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp b/barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp index 8dddc62058d6..d1b6656bb68a 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp @@ -13,7 +13,7 @@ std::pair AvmAPI::prove(const AvmA { // Simulate. info("Simulating..."); - AvmSimulationHelper simulation_helper(inputs); + AvmSimulationHelper simulation_helper(inputs.hints); auto events = AVM_TRACK_TIME_V("simulation/all", simulation_helper.simulate()); // Generate trace. @@ -34,7 +34,7 @@ bool AvmAPI::check_circuit(const AvmAPI::ProvingInputs& inputs) { // Simulate. info("Simulating..."); - AvmSimulationHelper simulation_helper(inputs); + AvmSimulationHelper simulation_helper(inputs.hints); auto events = AVM_TRACK_TIME_V("simulation/all", simulation_helper.simulate()); // Generate trace. @@ -55,4 +55,4 @@ bool AvmAPI::verify(const AvmProof& proof, const PublicInputs& pi, const AvmVeri return AVM_TRACK_TIME_V("verifing/all", proving_helper.verify(proof, pi, vk_data)); } -} // namespace bb::avm2 \ No newline at end of file +} // namespace bb::avm2 diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp index 0d4c30c57499..aac9abc1c61b 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp @@ -268,6 +268,7 @@ struct AccumulatedData { // That's why I'm not calling it TxHint. We can reconsider if the inner types seem to dirty. struct Tx { std::string hash; + GlobalVariables globalVariables; AccumulatedData nonRevertibleAccumulatedData; AccumulatedData revertibleAccumulatedData; std::vector setupEnqueuedCalls; @@ -277,6 +278,7 @@ struct Tx { bool operator==(const Tx& other) const = default; MSGPACK_FIELDS(hash, + globalVariables, nonRevertibleAccumulatedData, revertibleAccumulatedData, setupEnqueuedCalls, @@ -291,6 +293,7 @@ struct ExecutionHints { std::vector contractClasses; std::vector bytecodeCommitments; // Merkle DB. + TreeSnapshots startingTreeRoots; std::vector getSiblingPathHints; std::vector getPreviousValueIndexHints; std::vector>> @@ -311,6 +314,7 @@ struct ExecutionHints { contractInstances, contractClasses, bytecodeCommitments, + startingTreeRoots, getSiblingPathHints, getPreviousValueIndexHints, getLeafPreimageHintsPublicDataTree, diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin index b39137336c36..2945ee62d727 100644 Binary files a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin and b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin differ diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.cpp index 203d64dbc7f4..c2db39925e10 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.cpp @@ -148,8 +148,8 @@ FF HintedRawContractDB::get_bytecode_commitment(const ContractClassId& class_id) } // Hinted MerkleDB starts. -HintedRawMerkleDB::HintedRawMerkleDB(const ExecutionHints& hints, const TreeSnapshots& tree_roots) - : tree_roots(tree_roots) +HintedRawMerkleDB::HintedRawMerkleDB(const ExecutionHints& hints) + : tree_roots(hints.startingTreeRoots) { vinfo("Initializing HintedRawMerkleDB with...", "\n * get_sibling_path_hints: ", diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.hpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.hpp index 5c42c069ceaf..cb7c64efa66d 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_dbs.hpp @@ -38,7 +38,7 @@ class HintedRawContractDB final : public ContractDBInterface { // This class interacts with the external world, without emiting any simulation events. class HintedRawMerkleDB final : public LowLevelMerkleDBInterface { public: - HintedRawMerkleDB(const ExecutionHints& hints, const TreeSnapshots& tree_roots); + HintedRawMerkleDB(const ExecutionHints& hints); const TreeSnapshots& get_tree_roots() const override { return tree_roots; } diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp index c5b6e4436c3a..3528e90bdb2b 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp @@ -90,7 +90,7 @@ template EventsContainer AvmSimulationHelper::simulate_with_setting typename S::template DefaultEventEmitter public_data_read_emitter; typename S::template DefaultEventEmitter update_check_emitter; - uint32_t current_block_number = static_cast(inputs.publicInputs.globalVariables.blockNumber); + uint32_t current_block_number = static_cast(hints.tx.globalVariables.blockNumber); Poseidon2 poseidon2(poseidon2_hash_emitter, poseidon2_perm_emitter); ToRadix to_radix(to_radix_emitter); @@ -102,8 +102,8 @@ template EventsContainer AvmSimulationHelper::simulate_with_setting AddressDerivation address_derivation(poseidon2, ecc, address_derivation_emitter); ClassIdDerivation class_id_derivation(poseidon2, class_id_derivation_emitter); - HintedRawContractDB raw_contract_db(inputs.hints); - HintedRawMerkleDB raw_merkle_db(inputs.hints, inputs.publicInputs.startTreeSnapshots); + HintedRawContractDB raw_contract_db(hints); + HintedRawMerkleDB raw_merkle_db(hints); ContractDB contract_db(raw_contract_db, address_derivation, class_id_derivation); MerkleDB merkle_db(raw_merkle_db, public_data_tree_check); UpdateCheck update_check(poseidon2, range_check, merkle_db, current_block_number, update_check_emitter); @@ -128,7 +128,7 @@ template EventsContainer AvmSimulationHelper::simulate_with_setting TxExecution tx_execution(execution, merkle_db); Sha256 sha256(sha256_compression_emitter); - tx_execution.simulate(inputs.hints.tx); + tx_execution.simulate(hints.tx); return { execution_emitter.dump_events(), alu_emitter.dump_events(), diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.hpp b/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.hpp index c86b917ec92f..481aa9a289fd 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation_helper.hpp @@ -7,8 +7,8 @@ namespace bb::avm2 { class AvmSimulationHelper { public: - AvmSimulationHelper(AvmProvingInputs inputs) - : inputs(std::move(inputs)) + AvmSimulationHelper(ExecutionHints hints) + : hints(std::move(hints)) {} // Full simulation with event collection. @@ -20,7 +20,7 @@ class AvmSimulationHelper { private: template simulation::EventsContainer simulate_with_settings(); - AvmProvingInputs inputs; + ExecutionHints hints; }; } // namespace bb::avm2 diff --git a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_context.ts b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_context.ts index 7b4f9337206c..e8fc9c2c0794 100644 --- a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_context.ts +++ b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_context.ts @@ -113,6 +113,13 @@ export class PublicTxContext { const hints = new AvmExecutionHints(await AvmTxHint.fromTx(tx)); const hintingContractsDB = new HintingPublicContractsDB(contractsDB, hints); const hintingTreesDB = new HintingPublicTreesDB(treesDB, hints); + const startStateReference = await treesDB.getStateReference(); + hints.startingTreeRoots = new TreeSnapshots( + startStateReference.l1ToL2MessageTree, + startStateReference.partial.noteHashTree, + startStateReference.partial.nullifierTree, + startStateReference.partial.publicDataTree, + ); // Transaction level state manager that will be forked for revertible phases. const txStateManager = PublicPersistableStateManager.create( diff --git a/yarn-project/stdlib/src/avm/avm.ts b/yarn-project/stdlib/src/avm/avm.ts index b1df4fd4b331..af7e3289dc0d 100644 --- a/yarn-project/stdlib/src/avm/avm.ts +++ b/yarn-project/stdlib/src/avm/avm.ts @@ -10,7 +10,7 @@ import { AppendOnlyTreeSnapshot } from '../trees/append_only_tree_snapshot.js'; import { MerkleTreeId } from '../trees/merkle_tree_id.js'; import { NullifierLeafPreimage } from '../trees/nullifier_leaf.js'; import { PublicDataTreeLeafPreimage } from '../trees/public_data_leaf.js'; -import { TreeSnapshots, type Tx } from '../tx/index.js'; +import { GlobalVariables, TreeSnapshots, type Tx } from '../tx/index.js'; import { AvmCircuitPublicInputs } from './avm_circuit_public_inputs.js'; import { serializeWithMessagePack } from './message_pack.js'; @@ -402,6 +402,7 @@ export class AvmEnqueuedCallHint { export class AvmTxHint { constructor( public readonly hash: string, + public readonly globalVariables: GlobalVariables, public readonly nonRevertibleAccumulatedData: { noteHashes: Fr[]; nullifiers: Fr[]; @@ -429,6 +430,7 @@ export class AvmTxHint { return new AvmTxHint( txHash.hash.toString(), + tx.data.constants.historicalHeader.globalVariables, { noteHashes: tx.data.forPublic!.nonRevertibleAccumulatedData.noteHashes.filter(x => !x.isZero()), nullifiers: tx.data.forPublic!.nonRevertibleAccumulatedData.nullifiers.filter(x => !x.isZero()), @@ -467,13 +469,22 @@ export class AvmTxHint { } static empty() { - return new AvmTxHint('', { noteHashes: [], nullifiers: [] }, { noteHashes: [], nullifiers: [] }, [], [], null); + return new AvmTxHint( + '', + GlobalVariables.empty(), + { noteHashes: [], nullifiers: [] }, + { noteHashes: [], nullifiers: [] }, + [], + [], + null, + ); } static get schema() { return z .object({ hash: z.string(), + globalVariables: GlobalVariables.schema, nonRevertibleAccumulatedData: z.object({ noteHashes: schemas.Fr.array(), nullifiers: schemas.Fr.array(), @@ -489,6 +500,7 @@ export class AvmTxHint { .transform( ({ hash, + globalVariables, nonRevertibleAccumulatedData, revertibleAccumulatedData, setupEnqueuedCalls, @@ -497,6 +509,7 @@ export class AvmTxHint { }) => new AvmTxHint( hash, + globalVariables, nonRevertibleAccumulatedData, revertibleAccumulatedData, setupEnqueuedCalls, @@ -515,6 +528,7 @@ export class AvmExecutionHints { public readonly contractClasses: AvmContractClassHint[] = [], public readonly bytecodeCommitments: AvmBytecodeCommitmentHint[] = [], // Merkle DB hints. + public startingTreeRoots: TreeSnapshots = TreeSnapshots.empty(), public readonly getSiblingPathHints: AvmGetSiblingPathHint[] = [], public readonly getPreviousValueIndexHints: AvmGetPreviousValueIndexHint[] = [], public readonly getLeafPreimageHintsPublicDataTree: AvmGetLeafPreimageHintPublicDataTree[] = [], @@ -539,6 +553,7 @@ export class AvmExecutionHints { contractInstances: AvmContractInstanceHint.schema.array(), contractClasses: AvmContractClassHint.schema.array(), bytecodeCommitments: AvmBytecodeCommitmentHint.schema.array(), + startingTreeRoots: TreeSnapshots.schema, getSiblingPathHints: AvmGetSiblingPathHint.schema.array(), getPreviousValueIndexHints: AvmGetPreviousValueIndexHint.schema.array(), getLeafPreimageHintsPublicDataTree: AvmGetLeafPreimageHintPublicDataTree.schema.array(), @@ -557,6 +572,7 @@ export class AvmExecutionHints { contractInstances, contractClasses, bytecodeCommitments, + startingTreeRoots, getSiblingPathHints, getPreviousValueIndexHints, getLeafPreimageHintsPublicDataTree, @@ -574,6 +590,7 @@ export class AvmExecutionHints { contractInstances, contractClasses, bytecodeCommitments, + startingTreeRoots, getSiblingPathHints, getPreviousValueIndexHints, getLeafPreimageHintsPublicDataTree, diff --git a/yarn-project/stdlib/src/tests/factories.ts b/yarn-project/stdlib/src/tests/factories.ts index b5df34a9b512..ba639a621dd4 100644 --- a/yarn-project/stdlib/src/tests/factories.ts +++ b/yarn-project/stdlib/src/tests/factories.ts @@ -1471,6 +1471,7 @@ export function makeAvmEnqueuedCallHint(seed = 0): AvmEnqueuedCallHint { export function makeAvmTxHint(seed = 0): AvmTxHint { return new AvmTxHint( `txhash-${seed}`, + makeGlobalVariables(seed), { noteHashes: makeArray((seed % 20) + 4, i => new Fr(i), seed + 0x1000), nullifiers: makeArray((seed % 20) + 4, i => new Fr(i), seed + 0x2000), @@ -1503,6 +1504,7 @@ export async function makeAvmExecutionHints( contractInstances: makeArray(baseLength + 2, makeAvmContractInstanceHint, seed + 0x4700), contractClasses: makeArray(baseLength + 5, makeAvmContractClassHint, seed + 0x4900), bytecodeCommitments: await makeArrayAsync(baseLength + 5, makeAvmBytecodeCommitmentHint, seed + 0x4900), + startingTreeRoots: makeTreeSnapshots(seed + 0x4900), getSiblingPathHints: makeArray(baseLength + 5, makeAvmGetSiblingPathHint, seed + 0x4b00), getPreviousValueIndexHints: makeArray(baseLength + 5, makeAvmGetPreviousValueIndexHint, seed + 0x4d00), getLeafPreimageHintPublicDataTree: makeArray( @@ -1534,6 +1536,7 @@ export async function makeAvmExecutionHints( fields.contractInstances, fields.contractClasses, fields.bytecodeCommitments, + fields.startingTreeRoots, fields.getSiblingPathHints, fields.getPreviousValueIndexHints, fields.getLeafPreimageHintPublicDataTree,