Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
concrete_dbs.cpp
Go to the documentation of this file.
2
6
7namespace bb::avm2::simulation {
8
9// Contracts DB starts.
11{
13 // If we didn't get a contract instance, we don't prove anything.
14 // It is the responsibility of the caller to prove what the protocol expects.
15 if (!instance.has_value()) {
16 return std::nullopt;
17 }
18 // If we did get a contract instance, we need to prove that the address is derived from the instance.
19 // For protocol contracts the input address is the canonical address, we need to retrieve the derived address.
20 AztecAddress derived_address;
22 auto maybe_derived = get_derived_address(protocol_contracts, address);
23 BB_ASSERT(maybe_derived.has_value(),
24 "Derived address should be found for protocol contract whose instance is found");
25 derived_address = maybe_derived.value();
26 } else {
27 derived_address = address;
28 }
29 address_derivation.assert_derivation(derived_address, instance.value());
30 return instance;
31}
32
34{
35 // Get the contract class from the raw DB.
37 if (!maybe_klass.has_value()) {
38 return std::nullopt;
39 }
40
41 // Get the bytecode commitment for this class.
43 // If the class exists, the bytecode commitment must also exist.
44 BB_ASSERT(maybe_bytecode_commitment.has_value(), "Bytecode commitment not found");
45
46 // Perform class ID derivation to verify the class ID is correctly derived from the class data.
47 // Emits ClassIdDerivationEvent (and corresponding Poseidon2HashEvent and Poseidon2PermutationEvents) if
48 // we have yet to derive this class ID.
49 class_id_derivation.assert_derivation(maybe_klass->with_commitment(maybe_bytecode_commitment.value()));
50
51 return maybe_klass;
52}
53
58
64
65void ContractDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
66{
67 raw_contract_db.add_contracts(contract_deployment_data);
68}
69
70// Merkle DB starts.
71
73{
74 // No event generated.
76 TreeCounters tree_counters = tree_counters_stack.top();
77 return {
78 .note_hash_tree = { .tree = tree_snapshots.note_hash_tree, .counter = tree_counters.note_hash_counter },
79 .nullifier_tree = { .tree = tree_snapshots.nullifier_tree, .counter = tree_counters.nullifier_counter },
80 .l1_to_l2_message_tree = { .tree = tree_snapshots.l1_to_l2_message_tree,
81 .counter = tree_counters.l2_to_l1_msg_counter },
82 .public_data_tree = { .tree = tree_snapshots.public_data_tree, .counter = written_public_data_slots.size() },
83 };
84}
85
87{
88 auto [present, index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::PUBLIC_DATA_TREE,
90 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::PUBLIC_DATA_TREE, index);
92
93 FF value = present ? preimage.leaf.value : 0;
94
97
98 return value;
99}
100
102 const FF& slot,
103 const FF& value,
104 bool is_protocol_write)
105{
108
110
111 auto& low_leaf_hint = hint.low_leaf_witness_data.at(0);
112 auto& insertion_hint = hint.insertion_witness_data.at(0);
113
116 value,
117 low_leaf_hint.leaf,
118 low_leaf_hint.index,
119 low_leaf_hint.path,
120 snapshot_before,
121 insertion_hint.path,
122 is_protocol_write);
123
124 // This will throw an unexpected exception if it fails.
125 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().public_data_tree, "Snapshot after mismatch");
126
127 if (!is_protocol_write) {
129 }
130}
131
136
141
143{
144 return nullifier_exists_internal(/*contract_address*/ std::nullopt, nullifier);
145}
146
148{
149 FF siloed_nullifier = nullifier;
150 if (contract_address.has_value()) {
151 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
152 // The siloing will later be constrained in the nullifier tree check gadget.
153 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
154 }
155
156 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
157 auto low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
158 auto low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
159
162 present,
163 low_leaf_preimage,
164 low_leaf_index,
165 low_leaf_path,
167
168 return present;
169}
170
175
177{
178 return nullifier_write_internal(/*contract_address*/ std::nullopt, nullifier);
179}
180
182{
183 uint32_t nullifier_counter = tree_counters_stack.top().nullifier_counter;
184 FF siloed_nullifier = nullifier;
185 if (contract_address.has_value()) {
186 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
187 // The siloing will later be constrained in the nullifier tree check gadget.
188 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
189 }
190
191 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
193
194 SiblingPath low_leaf_path;
195 IndexedLeaf<NullifierLeafValue> low_leaf_preimage;
197
198 if (present) {
199 low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
200 low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
201 } else {
202 auto insertion_result = raw_merkle_db.insert_indexed_leaves_nullifier_tree(siloed_nullifier);
203
204 low_leaf_path = insertion_result.low_leaf_witness_data.at(0).path;
205 low_leaf_preimage = insertion_result.low_leaf_witness_data.at(0).leaf;
206 insertion_path = insertion_result.insertion_witness_data.at(0).path;
207 }
208
211 nullifier_counter,
212 low_leaf_preimage,
213 low_leaf_index,
214 low_leaf_path,
215 snapshot_before,
216 insertion_path);
217
218 // This will throw an unexpected exception if it fails.
219 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().nullifier_tree, "Snapshot after mismatch");
220
221 if (!present) {
222 tree_counters_stack.top().nullifier_counter++;
223 } else {
224 throw NullifierCollisionException(format("Nullifier ", nullifier, " already exists"));
225 }
226}
227
228bool MerkleDB::note_hash_exists(uint64_t leaf_index, const FF& unique_note_hash) const
229{
230 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
231 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
232 return note_hash_tree_check.note_hash_exists(
233 unique_note_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().note_hash_tree);
234}
235
237{
238 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
239
241 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
242 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
244 FF unique_note_hash = unconstrained_make_unique_note_hash(
245 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
246
247 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
248 SiblingPath path =
249 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
250 AppendOnlyTreeSnapshot snapshot_after =
251 note_hash_tree_check.append_note_hash(note_hash, contract_address, note_hash_counter, path, snapshot_before);
252
253 // This will throw an unexpected exception if it fails.
254 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
255
256 tree_counters_stack.top().note_hash_counter++;
257}
258
259void MerkleDB::siloed_note_hash_write(const FF& siloed_note_hash)
260{
261 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
263 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
264 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
265 FF unique_note_hash = unconstrained_make_unique_note_hash(
266 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
267
268 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
269 SiblingPath path =
270 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
271 AppendOnlyTreeSnapshot snapshot_after =
272 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, path, snapshot_before);
273
274 // This will throw an unexpected exception if it fails.
275 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
276
277 tree_counters_stack.top().note_hash_counter++;
278}
279
280void MerkleDB::unique_note_hash_write(const FF& unique_note_hash)
281{
282 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
284
285 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
286 SiblingPath path =
287 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
288 AppendOnlyTreeSnapshot snapshot_after =
289 note_hash_tree_check.append_unique_note_hash(unique_note_hash, note_hash_counter, path, snapshot_before);
290
291 // This will throw an unexpected exception if it fails.
292 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
293
294 tree_counters_stack.top().note_hash_counter++;
295}
296
297bool MerkleDB::l1_to_l2_msg_exists(uint64_t leaf_index, const FF& msg_hash) const
298{
299 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
300 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
302 msg_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().l1_to_l2_message_tree);
303}
304
306{
307 // The public data tree is not padded.
308 raw_merkle_db.pad_tree(MerkleTreeId::NOTE_HASH_TREE,
309 MAX_NOTE_HASHES_PER_TX - tree_counters_stack.top().note_hash_counter);
310 raw_merkle_db.pad_tree(MerkleTreeId::NULLIFIER_TREE,
311 MAX_NULLIFIERS_PER_TX - tree_counters_stack.top().nullifier_counter);
312}
313
315{
319 for (auto& listener : checkpoint_listeners) {
320 listener->on_checkpoint_created();
321 }
322}
323
325{
328 TreeCounters current_counters = tree_counters_stack.top();
330 tree_counters_stack.top() = current_counters;
331 for (auto& listener : checkpoint_listeners) {
332 listener->on_checkpoint_committed();
333 }
334}
335
337{
341 for (auto& listener : checkpoint_listeners) {
342 listener->on_checkpoint_reverted();
343 }
344}
345
347{
349}
350
351} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
std::shared_ptr< Napi::ThreadSafeFunction > instance
#define MAX_NOTE_HASHES_PER_TX
#define MAX_NULLIFIERS_PER_TX
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
ContractDBInterface & raw_contract_db
std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const override
const ProtocolContracts & protocol_contracts
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
void add_contracts(const ContractDeploymentData &contract_deployment_data) override
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
virtual void add_contracts(const ContractDeploymentData &contract_deployment_data)=0
virtual std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const =0
virtual std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const =0
virtual std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const =0
virtual std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const =0
virtual bool exists(const FF &msg_hash, const FF &leaf_value, uint64_t leaf_index, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &snapshot)=0
virtual IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const =0
virtual TreeSnapshots get_tree_roots() const =0
virtual void pad_tree(MerkleTreeId tree_id, size_t num_leaves)=0
virtual IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const =0
virtual SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value)=0
virtual GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF &value) const =0
virtual SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const =0
virtual uint32_t get_checkpoint_id() const =0
virtual SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value)=0
virtual void append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves)=0
virtual FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const =0
WrittenPublicDataSlotsInterface & written_public_data_slots
void note_hash_write(const AztecAddress &contract_address, const FF &note_hash) override
std::vector< CheckpointNotifiable * > checkpoint_listeners
std::stack< TreeCounters > tree_counters_stack
bool nullifier_exists(const AztecAddress &contract_address, const FF &nullifier) const override
LowLevelMerkleDBInterface & raw_merkle_db
void unique_note_hash_write(const FF &note_hash) override
void nullifier_write_internal(std::optional< AztecAddress > contract_address, const FF &nullifier)
TreeStates get_tree_state() const override
bool was_storage_written(const AztecAddress &contract_address, const FF &slot) const override
NullifierTreeCheckInterface & nullifier_tree_check
uint32_t get_checkpoint_id() const override
bool note_hash_exists(uint64_t leaf_index, const FF &unique_note_hash) const override
void siloed_note_hash_write(const FF &note_hash) override
void siloed_nullifier_write(const FF &nullifier) override
FF storage_read(const AztecAddress &contract_address, const FF &slot) const override
bool siloed_nullifier_exists(const FF &nullifier) const override
PublicDataTreeCheckInterface & public_data_tree_check
void storage_write(const AztecAddress &contract_address, const FF &slot, const FF &value, bool is_protocol_write) override
bool nullifier_exists_internal(std::optional< AztecAddress > contract_address, const FF &nullifier) const
void nullifier_write(const AztecAddress &contract_address, const FF &nullifier) override
L1ToL2MessageTreeCheckInterface & l1_to_l2_msg_tree_check
bool l1_to_l2_msg_exists(uint64_t leaf_index, const FF &msg_hash) const override
virtual AppendOnlyTreeSnapshot write(const FF &nullifier, std::optional< AztecAddress > contract_address, uint64_t nullifier_counter, const NullifierTreeLeafPreimage &low_leaf_preimage, uint64_t low_leaf_index, std::span< const FF > low_leaf_sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot, std::optional< std::span< const FF > > insertion_sibling_path)=0
virtual void assert_read(const FF &nullifier, std::optional< AztecAddress > contract_address, bool exists, const NullifierTreeLeafPreimage &low_leaf_preimage, uint64_t low_leaf_index, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &snapshot)=0
virtual AppendOnlyTreeSnapshot write(const FF &slot, const AztecAddress &contract_address, const FF &value, const PublicDataTreeLeafPreimage &low_leaf_preimage, uint64_t low_leaf_index, std::span< const FF > low_leaf_sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot, std::span< const FF > insertion_sibling_path, bool is_protocol_write)=0
virtual void assert_read(const FF &slot, const AztecAddress &contract_address, const FF &value, const PublicDataTreeLeafPreimage &low_leaf_preimage, uint64_t low_leaf_index, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &snapshot)=0
virtual bool contains(const AztecAddress &contract_address, const FF &slot)=0
virtual void insert(const AztecAddress &contract_address, const FF &slot)=0
std::string format(Args... args)
Definition log.hpp:23
AVM range check gadget for witness generation.
::bb::crypto::merkle_tree::fr_sibling_path SiblingPath
Definition db.hpp:36
FF unconstrained_make_unique_note_hash(const FF &siloed_note_hash, const FF &first_nullifier, uint64_t note_hash_counter)
Definition merkle.cpp:41
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
Definition db.hpp:38
FF unconstrained_compute_leaf_slot(const AztecAddress &contract_address, const FF &slot)
Definition merkle.cpp:26
FF unconstrained_silo_note_hash(const AztecAddress &contract_address, const FF &note_hash)
Definition merkle.cpp:36
FF unconstrained_silo_nullifier(const AztecAddress &contract_address, const FF &nullifier)
Definition merkle.cpp:31
AvmFlavorSettings::FF FF
Definition field.hpp:10
FF ContractClassId
std::optional< AztecAddress > get_derived_address(const ProtocolContracts &protocol_contracts, const AztecAddress &canonical_address)
bool is_protocol_contract_address(const AztecAddress &address)
FF FunctionSelector
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AppendOnlyTreeSnapshot public_data_tree
AppendOnlyTreeSnapshot l1_to_l2_message_tree
AppendOnlyTreeSnapshot nullifier_tree
AppendOnlyTreeSnapshot note_hash_tree