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 =
43 .nullifier_key = affine_one,
44 .incoming_viewing_key = affine_one,
45 .outgoing_viewing_key = affine_one,
46 .tagging_key = affine_one,
47 },
48 };
49}
50
51} // anonymous namespace
52
54{
55 auto default_class = create_default_class(bytecode);
56 auto default_instance = create_default_instance(default_class.id);
58
59 contract_db_->add_contract_class(default_class.id, default_class);
60 contract_db_->add_contract_instance(contract_address, default_instance);
62
63 try {
65 } catch (const std::exception& e) {
66 std::string msg = e.what();
67 // Ignore duplicates, the contract is already registered
68 if (msg.find("is already present") == std::string::npos) {
69 // Re-throw other errors
70 throw e;
71 }
72 }
73 return contract_address;
74}
75
83
85{
86 existing_note_hashes_.assign(note_hashes.begin(), note_hashes.end());
87}
88
90{
91 contract_addresses_.assign(contract_addresses.begin(), contract_addresses.end());
92}
93
100
101} // 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:48
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)
FF ContractClassId
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AffinePoint nullifier_key