Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std::pair<AvmAPI::AvmProof, AvmAPI::AvmVerificationKey> 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.
Expand All @@ -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.
Expand All @@ -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
} // namespace bb::avm2
4 changes: 4 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnqueuedCallHint> setupEnqueuedCalls;
Expand All @@ -277,6 +278,7 @@ struct Tx {
bool operator==(const Tx& other) const = default;

MSGPACK_FIELDS(hash,
globalVariables,
nonRevertibleAccumulatedData,
revertibleAccumulatedData,
setupEnqueuedCalls,
Expand All @@ -291,6 +293,7 @@ struct ExecutionHints {
std::vector<ContractClassHint> contractClasses;
std::vector<BytecodeCommitmentHint> bytecodeCommitments;
// Merkle DB.
TreeSnapshots startingTreeRoots;
std::vector<GetSiblingPathHint> getSiblingPathHints;
std::vector<GetPreviousValueIndexHint> getPreviousValueIndexHints;
std::vector<GetLeafPreimageHint<crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::PublicDataLeafValue>>>
Expand All @@ -311,6 +314,7 @@ struct ExecutionHints {
contractInstances,
contractClasses,
bytecodeCommitments,
startingTreeRoots,
getSiblingPathHints,
getPreviousValueIndexHints,
getLeafPreimageHintsPublicDataTree,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
8 changes: 4 additions & 4 deletions barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ template <typename S> EventsContainer AvmSimulationHelper::simulate_with_setting
typename S::template DefaultEventEmitter<PublicDataTreeReadEvent> public_data_read_emitter;
typename S::template DefaultEventEmitter<UpdateCheckEvent> update_check_emitter;

uint32_t current_block_number = static_cast<uint32_t>(inputs.publicInputs.globalVariables.blockNumber);
uint32_t current_block_number = static_cast<uint32_t>(hints.tx.globalVariables.blockNumber);

Poseidon2 poseidon2(poseidon2_hash_emitter, poseidon2_perm_emitter);
ToRadix to_radix(to_radix_emitter);
Expand All @@ -102,8 +102,8 @@ template <typename S> 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);
Expand All @@ -128,7 +128,7 @@ template <typename S> 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(),
Expand Down
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm2/simulation_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,7 +20,7 @@ class AvmSimulationHelper {
private:
template <typename S> simulation::EventsContainer simulate_with_settings();

AvmProvingInputs inputs;
ExecutionHints hints;
};

} // namespace bb::avm2
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 19 additions & 2 deletions yarn-project/stdlib/src/avm/avm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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(),
Expand All @@ -489,6 +500,7 @@ export class AvmTxHint {
.transform(
({
hash,
globalVariables,
nonRevertibleAccumulatedData,
revertibleAccumulatedData,
setupEnqueuedCalls,
Expand All @@ -497,6 +509,7 @@ export class AvmTxHint {
}) =>
new AvmTxHint(
hash,
globalVariables,
nonRevertibleAccumulatedData,
revertibleAccumulatedData,
setupEnqueuedCalls,
Expand All @@ -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[] = [],
Expand All @@ -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(),
Expand All @@ -557,6 +572,7 @@ export class AvmExecutionHints {
contractInstances,
contractClasses,
bytecodeCommitments,
startingTreeRoots,
getSiblingPathHints,
getPreviousValueIndexHints,
getLeafPreimageHintsPublicDataTree,
Expand All @@ -574,6 +590,7 @@ export class AvmExecutionHints {
contractInstances,
contractClasses,
bytecodeCommitments,
startingTreeRoots,
getSiblingPathHints,
getPreviousValueIndexHints,
getLeafPreimageHintsPublicDataTree,
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/stdlib/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1534,6 +1536,7 @@ export async function makeAvmExecutionHints(
fields.contractInstances,
fields.contractClasses,
fields.bytecodeCommitments,
fields.startingTreeRoots,
fields.getSiblingPathHints,
fields.getPreviousValueIndexHints,
fields.getLeafPreimageHintPublicDataTree,
Expand Down
Loading