Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fuzzer_context.cpp
Go to the documentation of this file.
2
8
9namespace bb::avm2::fuzzer {
10
11namespace {
12
13// Helper function to create a default contract class from bytecode
14ContractClassWithCommitment create_default_class(const std::vector<uint8_t>& bytecode)
15{
16 // This isn't strictly needed for pure simulation, but if we want to re-use inputs in proving we need valid
17 // commitment
19 auto class_id =
20 simulation::compute_contract_class_id(/*artifact_hash=*/0, /*private_fn_root=*/0, bytecode_commitment);
22 .id = class_id,
23 .artifact_hash = 0,
24 .private_functions_root = 0,
25 .packed_bytecode = bytecode,
26 .public_bytecode_commitment = bytecode_commitment,
27 };
28}
29
30// Helper function to create a default contract instance from a class ID
31ContractInstance create_default_instance(const ContractClassId& class_id)
32{
33 // To avoid Assertion failed: (contract_instance.public_keys.incoming_viewing_key.on_curve())
34 auto affine_one = grumpkin::g1::affine_one;
35 return ContractInstance{
36 .salt = 0,
37 .deployer = MSG_SENDER,
38 .current_contract_class_id = class_id,
39 .original_contract_class_id = class_id,
40 .initialization_hash = 0,
41 .public_keys =
44 .incoming_viewing_key = affine_one,
45 .outgoing_viewing_key_hash = 0,
46 .tagging_key_hash = 0,
47 .message_signing_key_hash = 0,
48 .fallback_key_hash = 0,
49 },
50 };
51}
52
53} // anonymous namespace
54
56{
57 auto default_class = create_default_class(bytecode);
58 auto default_instance = create_default_instance(default_class.id);
59 auto contract_address = simulation::compute_contract_address(default_instance);
60
61 contract_db_->add_contract_class(default_class.id, default_class);
62 contract_db_->add_contract_instance(contract_address, default_instance);
63 contract_addresses_.push_back(contract_address);
64
65 try {
67 } catch (const std::exception& e) {
68 std::string msg = e.what();
69 // Ignore duplicates, the contract is already registered
70 if (msg.find("is already present") == std::string::npos) {
71 // Re-throw other errors
72 throw e;
73 }
74 }
75 return contract_address;
76}
77
85
87{
88 existing_note_hashes_.assign(note_hashes.begin(), note_hashes.end());
89}
90
92{
93 contract_addresses_.assign(contract_addresses.begin(), contract_addresses.end());
94}
95
102
103} // namespace bb::avm2::fuzzer
const FF MSG_SENDER
Definition constants.hpp:33
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
void set_existing_contract_addresses(std::span< const FF > contract_addresses)
FF register_contract_from_bytecode(const std::vector< uint8_t > &bytecode)
Register a contract from its bytecode.
void reset()
Clear all contract addresses and reset the contract DB.
void set_existing_note_hashes(std::span< const std::pair< FF, uint64_t > > note_hashes)
std::optional< std::pair< FF, uint64_t > > get_existing_note_hash(size_t index) const
std::unique_ptr< FuzzerContractDB > contract_db_
std::vector< std::pair< FF, uint64_t > > existing_note_hashes_
static FuzzerWorldStateManager * getInstance()
Definition dbs.hpp:80
void register_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:225
static constexpr affine_element affine_one
Definition group.hpp:50
FF compute_public_bytecode_commitment(std::span< const uint8_t > bytecode)
FF compute_contract_class_id(const FF &artifact_hash, const FF &private_fn_root, const FF &public_bytecode_commitment)
FF compute_contract_address(const ContractInstance &contract_instance)
AvmFlavorSettings::FF FF
Definition field.hpp:10
FF ContractClassId
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13