Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
dbs.cpp
Go to the documentation of this file.
2
3#include <vector>
4
7
8using namespace bb::avm2::simulation;
9using namespace bb::crypto::merkle_tree;
10using namespace bb::world_state;
11
12// TODO(ilyas): implement other methods as needed
14
16{
17 return {
18 .l1_to_l2_message_tree = { .root = FF(0), .next_available_leaf_index = 0 },
19 .note_hash_tree = { .root = FF(0), .next_available_leaf_index = next_available_note_hash_index },
20 .nullifier_tree = { .root = FF(0), .next_available_leaf_index = next_available_nullifier_index },
21 .public_data_tree = { .root = FF(0), .next_available_leaf_index = next_available_public_data_index },
22 };
23}
24
26 [[maybe_unused]] index_t leaf_index) const
27{
28 throw_or_abort("FuzzerLowLevelDB::get_sibling_path not implemented");
29}
30
32 const std::vector<std::pair<FF, index_t>>& value_sorted_leaves, const FF& value) const
33{
34 for (size_t i = 0; i < value_sorted_leaves.size(); ++i) {
35 if (value_sorted_leaves[i].first == value) {
36 return value_sorted_leaves[i];
37 }
38 if (value_sorted_leaves[i].first > value) {
39 return value_sorted_leaves[i - 1];
40 }
41 }
42 // If we reach here, the value is larger than any leaf in the tree, return the last leaf
43 return value_sorted_leaves.back();
44}
45
47{
48 switch (tree_id) {
49 case MerkleTreeId::NULLIFIER_TREE: {
50 auto [low_value, low_index] = get_indexed_low_leaf_helper(nullifier_values, value);
51 return GetLowIndexedLeafResponse(low_value == value, low_index);
52 break;
53 }
54 case MerkleTreeId::PUBLIC_DATA_TREE: {
55 auto [low_value, low_index] = get_indexed_low_leaf_helper(public_data_slots, value);
56 return GetLowIndexedLeafResponse(low_value == value, low_index);
57 break;
58 }
59 default:
60 break;
61 }
62 return GetLowIndexedLeafResponse(false, 0);
63}
65{
66 switch (tree_id) {
67 case MerkleTreeId::NULLIFIER_TREE:
68 return nullifier_leaves.at(leaf_index).nullifier;
69 case MerkleTreeId::PUBLIC_DATA_TREE:
70 return public_data_leaves.at(leaf_index).value;
71 case MerkleTreeId::NOTE_HASH_TREE:
72 return note_hash_leaves.at(leaf_index);
73 default:
74 break;
75 }
76 return FF(0);
77}
79 index_t leaf_index) const
80{
81 PublicDataLeafValue leaf_value = public_data_leaves.at(leaf_index);
82 std::pair<FF, index_t> value_index_pair = { leaf_value.value, leaf_index };
83 // Find index in public_data_slots
84 auto it = std::ranges::find_if(
85 public_data_slots.begin(), public_data_slots.end(), [&value_index_pair](const std::pair<FF, index_t>& pair) {
86 return pair.second == value_index_pair.second;
87 });
88 if (it == public_data_slots.end()) {
89 throw_or_abort("FuzzerLowLevelDB::get_leaf_preimage_public_data_tree: leaf not found in public_data_slots");
90 }
91 it++; // Now iterator is at the next element
92 if (it == public_data_slots.end()) {
93 // If this is the last leaf, return with index 0
94 return simulation::IndexedLeaf<PublicDataLeafValue>(leaf_value, 0, 0);
95 }
96 auto [next_value, next_index] = *it;
97 return bb::crypto::merkle_tree::IndexedLeaf<PublicDataLeafValue>(leaf_value, next_index, next_value);
98}
99
101{
102 auto leaf_value = nullifier_leaves.at(leaf_index);
103 std::pair<FF, index_t> value_index_pair = { leaf_value.nullifier, leaf_index };
104 // Find index in nullifiers_values
105 auto it = std::ranges::find_if(
106 nullifier_values.begin(), nullifier_values.end(), [&value_index_pair](const std::pair<FF, index_t>& pair) {
107 return pair.second == value_index_pair.second;
108 });
109 if (it == nullifier_values.end()) {
110 throw_or_abort("FuzzerLowLevelDB::get_leaf_preimage_nullifier_tree: leaf not found in nullifier_values");
111 }
112
113 it++; // Now iterator is at the next element
114
115 if (it == nullifier_values.end()) {
116 // If this is the last leaf, return with index 0
117 return simulation::IndexedLeaf<NullifierLeafValue>(leaf_value, 0, 0);
118 }
119 auto [next_value, next_index] = *it;
120 return simulation::IndexedLeaf<NullifierLeafValue>(leaf_value, next_index, next_value);
121}
122
124 const PublicDataLeafValue& leaf_value)
125{
126 // Add to map
128 // Add to sorted vector
130 // Sort vector
131 std::ranges::sort(
132 public_data_slots.begin(),
133 public_data_slots.end(),
134 [](const std::pair<FF, index_t>& a, const std::pair<FF, index_t>& b) { return a.first < b.first; });
135
136 // Increment next available index
138 // Don't return any witness data for now, as it's not used for pure calls.
139 return {};
140}
141
143 const NullifierLeafValue& leaf_value)
144{
145 // Add to map
147 // Add to sorted vector
149 // Sort vector
150 std::ranges::sort(
151 nullifier_values.begin(),
152 nullifier_values.end(),
153 [](const std::pair<FF, index_t>& a, const std::pair<FF, index_t>& b) { return a.first < b.first; });
154
155 // Increment next available index
157 // Don't return any witness data for now, as it's not used for pure calls.
158 return {};
159}
160
162 std::span<const FF> leaves)
163{
164 note_hash_leaves.insert(note_hash_leaves.end(), leaves.begin(), leaves.end());
165 next_available_note_hash_index += leaves.size();
166 return {};
167}
168void FuzzerLowLevelDB::pad_tree([[maybe_unused]] MerkleTreeId tree_id, [[maybe_unused]] size_t num_leaves) {}
169
174{
175 return 0;
176}
177
178// Helper to insert a contract address into the nullifier tree
185
190 [[maybe_unused]] const AztecAddress& address) const
191{
193 .salt = FF(0),
194 .deployer = AztecAddress(0),
195 .current_contract_class_id = ContractClassId(0),
196 .original_contract_class_id = ContractClassId(0),
197 .initialization_hash = FF(0),
198 .public_keys = {
199 .nullifier_key = { FF(0), FF(0) },
200 .incoming_viewing_key = { FF(0), FF(0) },
201 .outgoing_viewing_key = { FF(0), FF(0) },
202 .tagging_key = { FF(0), FF(0) },
203 },
204 };
205
206 return instance;
207}
209 [[maybe_unused]] const ContractClassId& class_id) const
210{
211
212 ContractClass klass = {
213 .artifact_hash = FF(0),
214 .private_functions_root = FF(0),
215 .packed_bytecode = bytecode,
216 };
217 return klass;
218}
219
221{
222 return FF(0);
223}
225 [[maybe_unused]] const AztecAddress& address, [[maybe_unused]] const FunctionSelector& selector) const
226{
227 return std::nullopt;
228}
229
230void FuzzerContractDB::add_contracts([[maybe_unused]] const ContractDeploymentData& contract_deployment_data)
231{
232 // This probably needs to store the input contract deployment data for later retrieval.
233 {};
234}
235
239
243
244// Static instance definition
246
248{
250 { simulation::MerkleTreeId::NULLIFIER_TREE, NULLIFIER_TREE_HEIGHT },
251 { simulation::MerkleTreeId::NOTE_HASH_TREE, NOTE_HASH_TREE_HEIGHT },
252 { simulation::MerkleTreeId::PUBLIC_DATA_TREE, PUBLIC_DATA_TREE_HEIGHT },
253 { simulation::MerkleTreeId::L1_TO_L2_MESSAGE_TREE, L1_TO_L2_MSG_TREE_HEIGHT },
254 { simulation::MerkleTreeId::ARCHIVE, ARCHIVE_HEIGHT },
255 };
257 { simulation::MerkleTreeId::NULLIFIER_TREE, 128 },
258 { simulation::MerkleTreeId::PUBLIC_DATA_TREE, 128 },
259 };
260 uint32_t initial_header_generator_point = 28; // GeneratorIndex.BLOCK_HASH
262 /*thread_pool_size=*/4, DATA_DIR, MAP_SIZE_KB, tree_heights, tree_prefill, initial_header_generator_point);
263
264 fork_ids.push(ws->create_fork(std::nullopt));
265}
266
268{
269 return WorldStateRevision{ .forkId = fork_ids.top(), .blockNumber = 0, .includeUncommitted = true };
270}
271
273{
274 auto fork_id = ws->create_fork(std::nullopt);
275 fork_ids.push(fork_id);
276 return WorldStateRevision{ .forkId = fork_id, .blockNumber = 0, .includeUncommitted = true };
277}
279{
280 // We keep the initial fork, so pop until only one remains
281 while (fork_ids.size() != 1) {
282 ws->delete_fork(fork_ids.top());
283 fork_ids.pop();
284 }
285}
287{
288 NullifierLeafValue contract_nullifier =
290 auto fork_id = fork_ids.top();
291 ws->insert_indexed_leaves<NullifierLeafValue>(MerkleTreeId::NULLIFIER_TREE, { contract_nullifier }, fork_id);
292}
293
294} // namespace bb::avm2::fuzzer
std::shared_ptr< Napi::ThreadSafeFunction > instance
#define ARCHIVE_HEIGHT
#define L1_TO_L2_MSG_TREE_HEIGHT
#define NULLIFIER_TREE_HEIGHT
#define CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS
#define NOTE_HASH_TREE_HEIGHT
#define PUBLIC_DATA_TREE_HEIGHT
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
Definition dbs.cpp:208
void add_contracts(const ContractDeploymentData &contract_deployment_data) override
Definition dbs.cpp:230
void revert_checkpoint() override
Definition dbs.cpp:238
std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const override
ContractDBInterface methods.
Definition dbs.cpp:189
void commit_checkpoint() override
Definition dbs.cpp:237
void create_checkpoint() override
Definition dbs.cpp:236
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
Definition dbs.cpp:220
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
Definition dbs.cpp:224
std::vector< uint8_t > bytecode
Definition dbs.hpp:83
simulation::SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value) override
Definition dbs.cpp:142
std::vector< std::pair< FF, index_t > > public_data_slots
Definition dbs.hpp:52
uint32_t get_checkpoint_id() const override
Definition dbs.cpp:173
TreeSnapshots get_tree_roots() const override
Definition dbs.cpp:15
std::unordered_map< index_t, PublicDataLeafValue > public_data_leaves
Definition dbs.hpp:55
std::vector< FF > note_hash_leaves
Definition dbs.hpp:56
void create_checkpoint() override
Definition dbs.cpp:170
uint64_t next_available_public_data_index
Definition dbs.hpp:60
void pad_tree(simulation::MerkleTreeId tree_id, size_t num_leaves) override
Definition dbs.cpp:168
std::vector< simulation::AppendLeafResult > append_leaves(simulation::MerkleTreeId tree_id, std::span< const FF > leaves) override
Definition dbs.cpp:161
simulation::SiblingPath get_sibling_path(simulation::MerkleTreeId tree_id, index_t leaf_index) const override
Definition dbs.cpp:25
std::unordered_map< index_t, NullifierLeafValue > nullifier_leaves
Definition dbs.hpp:54
FF get_leaf_value(simulation::MerkleTreeId tree_id, index_t leaf_index) const override
Definition dbs.cpp:64
std::vector< std::pair< FF, index_t > > nullifier_values
Definition dbs.hpp:51
void revert_checkpoint() override
Definition dbs.cpp:172
void commit_checkpoint() override
Definition dbs.cpp:171
GetLowIndexedLeafResponse get_low_indexed_leaf(simulation::MerkleTreeId tree_id, const FF &value) const override
Definition dbs.cpp:46
simulation::IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const override
Definition dbs.cpp:78
simulation::SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value) override
Definition dbs.cpp:123
void insert_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:179
simulation::IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const override
Definition dbs.cpp:100
std::pair< FF, index_t > get_indexed_low_leaf_helper(const std::vector< std::pair< FF, index_t > > &value_sorted_leaves, const FF &value) const
Definition dbs.cpp:31
static constexpr const char * DATA_DIR
Definition dbs.hpp:96
std::unique_ptr< world_state::WorldState > ws
Definition dbs.hpp:138
world_state::WorldStateRevision fork()
Definition dbs.cpp:272
void register_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:286
static constexpr uint64_t MAP_SIZE_KB
Definition dbs.hpp:97
std::stack< uint64_t > fork_ids
Definition dbs.hpp:139
static FuzzerWorldStateManager * instance
FuzzerWorldStateManager methods.
Definition dbs.hpp:134
world_state::WorldStateRevision get_current_revision() const
Definition dbs.cpp:267
FF a
FF b
::bb::crypto::merkle_tree::fr_sibling_path SiblingPath
Definition db.hpp:36
FF unconstrained_silo_nullifier(const AztecAddress &contract_address, const FF &nullifier)
Definition merkle.cpp:31
::bb::crypto::merkle_tree::index_t index_t
Definition db.hpp:37
FF ContractClassId
FF FunctionSelector
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AppendOnlyTreeSnapshot l1_to_l2_message_tree
void throw_or_abort(std::string const &err)