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 class_id_derivation.assert_derivation(maybe_klass->with_commitment(maybe_bytecode_commitment.value()));
48
49 return maybe_klass;
50}
51
56
62
63void ContractDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
64{
65 raw_contract_db.add_contracts(contract_deployment_data);
66}
67
68// Merkle DB starts.
69
71{
72 // No event generated.
74 TreeCounters tree_counters = tree_counters_stack.top();
75 return {
76 .note_hash_tree = { .tree = tree_snapshots.note_hash_tree, .counter = tree_counters.note_hash_counter },
77 .nullifier_tree = { .tree = tree_snapshots.nullifier_tree, .counter = tree_counters.nullifier_counter },
78 .l1_to_l2_message_tree = { .tree = tree_snapshots.l1_to_l2_message_tree,
79 .counter = tree_counters.l2_to_l1_msg_counter },
80 .public_data_tree = { .tree = tree_snapshots.public_data_tree, .counter = written_public_data_slots.size() },
81 };
82}
83
85{
86 auto [present, index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::PUBLIC_DATA_TREE,
88 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::PUBLIC_DATA_TREE, index);
90
91 FF value = present ? preimage.leaf.value : 0;
92
95
96 return value;
97}
98
100 const FF& slot,
101 const FF& value,
102 bool is_protocol_write)
103{
106
108
109 auto& low_leaf_hint = hint.low_leaf_witness_data.at(0);
110 auto& insertion_hint = hint.insertion_witness_data.at(0);
111
114 value,
115 low_leaf_hint.leaf,
116 low_leaf_hint.index,
117 low_leaf_hint.path,
118 snapshot_before,
119 insertion_hint.path,
120 is_protocol_write);
121
122 // This will throw an unexpected exception if it fails.
123 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().public_data_tree, "Snapshot after mismatch");
124
125 if (!is_protocol_write) {
127 }
128}
129
134
139
141{
142 return nullifier_exists_internal(/*contract_address*/ std::nullopt, nullifier);
143}
144
146{
147 FF siloed_nullifier = nullifier;
148 if (contract_address.has_value()) {
149 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
150 // The siloing will later be constrained in the nullifier tree check gadget.
151 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
152 }
153
154 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
155 auto low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
156 auto low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
157
160 present,
161 low_leaf_preimage,
162 low_leaf_index,
163 low_leaf_path,
165
166 return present;
167}
168
173
175{
176 return nullifier_write_internal(/*contract_address*/ std::nullopt, nullifier);
177}
178
180{
181 uint32_t nullifier_counter = tree_counters_stack.top().nullifier_counter;
182 FF siloed_nullifier = nullifier;
183 if (contract_address.has_value()) {
184 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
185 // The siloing will later be constrained in the nullifier tree check gadget.
186 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
187 }
188
189 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
191
192 SiblingPath low_leaf_path;
193 IndexedLeaf<NullifierLeafValue> low_leaf_preimage;
195
196 if (present) {
197 low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
198 low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
199 } else {
200 auto insertion_result = raw_merkle_db.insert_indexed_leaves_nullifier_tree(siloed_nullifier);
201
202 low_leaf_path = insertion_result.low_leaf_witness_data.at(0).path;
203 low_leaf_preimage = insertion_result.low_leaf_witness_data.at(0).leaf;
204 insertion_path = insertion_result.insertion_witness_data.at(0).path;
205 }
206
209 nullifier_counter,
210 low_leaf_preimage,
211 low_leaf_index,
212 low_leaf_path,
213 snapshot_before,
214 insertion_path);
215
216 // This will throw an unexpected exception if it fails.
217 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().nullifier_tree, "Snapshot after mismatch");
218
219 if (!present) {
220 tree_counters_stack.top().nullifier_counter++;
221 } else {
222 throw NullifierCollisionException(format("Nullifier ", nullifier, " already exists"));
223 }
224}
225
226bool MerkleDB::note_hash_exists(uint64_t leaf_index, const FF& unique_note_hash) const
227{
228 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
229 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
230 return note_hash_tree_check.note_hash_exists(
231 unique_note_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().note_hash_tree);
232}
233
235{
236 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
237
239 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
240 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
242 FF unique_note_hash = unconstrained_make_unique_note_hash(
243 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
244
245 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
246 SiblingPath path =
247 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
248 AppendOnlyTreeSnapshot snapshot_after =
249 note_hash_tree_check.append_note_hash(note_hash, contract_address, note_hash_counter, path, snapshot_before);
250
251 // This will throw an unexpected exception if it fails.
252 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
253
254 tree_counters_stack.top().note_hash_counter++;
255}
256
257void MerkleDB::siloed_note_hash_write(const FF& siloed_note_hash)
258{
259 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
261 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
262 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
263 FF unique_note_hash = unconstrained_make_unique_note_hash(
264 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
265
266 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
267 SiblingPath path =
268 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
269 AppendOnlyTreeSnapshot snapshot_after =
270 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, path, snapshot_before);
271
272 // This will throw an unexpected exception if it fails.
273 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
274
275 tree_counters_stack.top().note_hash_counter++;
276}
277
278void MerkleDB::unique_note_hash_write(const FF& unique_note_hash)
279{
280 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
282
283 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
284 SiblingPath path =
285 raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, snapshot_before.next_available_leaf_index);
286 AppendOnlyTreeSnapshot snapshot_after =
287 note_hash_tree_check.append_unique_note_hash(unique_note_hash, note_hash_counter, path, snapshot_before);
288
289 // This will throw an unexpected exception if it fails.
290 BB_ASSERT_EQ(snapshot_after, raw_merkle_db.get_tree_roots().note_hash_tree, "Snapshot after mismatch");
291
292 tree_counters_stack.top().note_hash_counter++;
293}
294
295bool MerkleDB::l1_to_l2_msg_exists(uint64_t leaf_index, const FF& msg_hash) const
296{
297 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
298 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
300 msg_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().l1_to_l2_message_tree);
301}
302
304{
305 // The public data tree is not padded.
306 raw_merkle_db.pad_tree(MerkleTreeId::NOTE_HASH_TREE,
307 MAX_NOTE_HASHES_PER_TX - tree_counters_stack.top().note_hash_counter);
308 raw_merkle_db.pad_tree(MerkleTreeId::NULLIFIER_TREE,
309 MAX_NULLIFIERS_PER_TX - tree_counters_stack.top().nullifier_counter);
310}
311
313{
317 for (auto& listener : checkpoint_listeners) {
318 listener->on_checkpoint_created();
319 }
320}
321
323{
326 TreeCounters current_counters = tree_counters_stack.top();
328 tree_counters_stack.top() = current_counters;
329 for (auto& listener : checkpoint_listeners) {
330 listener->on_checkpoint_committed();
331 }
332}
333
335{
339 for (auto& listener : checkpoint_listeners) {
340 listener->on_checkpoint_reverted();
341 }
342}
343
345{
347}
348
349} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
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:24
::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
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
AvmFlavorSettings::FF FF
Definition field.hpp:10
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