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.
22 .value() /* We can assume that get_derived_address will not return a
23 nullopt, since we have succesfully fetched the instance.*/
24 : address;
25 address_derivation.assert_derivation(derived_address, instance.value());
26 return instance;
27}
28
30{
31 // Get the contract class from the raw DB.
33 if (!maybe_klass.has_value()) {
34 return std::nullopt;
35 }
36
37 // Get the bytecode commitment for this class.
39 // If the class exists, the bytecode commitment must also exist.
40 assert(maybe_bytecode_commitment.has_value());
41
42 // Perform class ID derivation to verify the class ID is correctly derived from the class data.
43 class_id_derivation.assert_derivation(maybe_klass->with_commitment(maybe_bytecode_commitment.value()));
44
45 return maybe_klass;
46}
47
52
58
59void ContractDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
60{
61 raw_contract_db.add_contracts(contract_deployment_data);
62}
63
64// Merkle DB starts.
65
67{
68 // No event generated.
70 TreeCounters tree_counters = tree_counters_stack.top();
71 return {
72 .note_hash_tree = { .tree = tree_snapshots.note_hash_tree, .counter = tree_counters.note_hash_counter },
73 .nullifier_tree = { .tree = tree_snapshots.nullifier_tree, .counter = tree_counters.nullifier_counter },
74 .l1_to_l2_message_tree = { .tree = tree_snapshots.l1_to_l2_message_tree,
75 .counter = tree_counters.l2_to_l1_msg_counter },
76 .public_data_tree = { .tree = tree_snapshots.public_data_tree, .counter = written_public_data_slots.size() },
77 };
78}
79
81{
82 auto [present, index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::PUBLIC_DATA_TREE,
84 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::PUBLIC_DATA_TREE, index);
86
87 FF value = present ? preimage.leaf.value : 0;
88
91
92 return value;
93}
94
96 const FF& slot,
97 const FF& value,
98 bool is_protocol_write)
99{
102
104
105 auto& low_leaf_hint = hint.low_leaf_witness_data.at(0);
106 auto& insertion_hint = hint.insertion_witness_data.at(0);
107
110 value,
111 low_leaf_hint.leaf,
112 low_leaf_hint.index,
113 low_leaf_hint.path,
114 snapshot_before,
115 insertion_hint.path,
116 is_protocol_write);
117
118 (void)snapshot_after; // Silence unused variable warning when assert is stripped out
119 // Sanity check.
120 assert(snapshot_after == raw_merkle_db.get_tree_roots().public_data_tree);
121 if (!is_protocol_write) {
123 }
124}
125
130
135
137{
138 return nullifier_exists_internal(/*contract_address*/ std::nullopt, nullifier);
139}
140
142{
143 FF siloed_nullifier = nullifier;
144 if (contract_address.has_value()) {
145 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
146 // The siloing will later be constrained in the nullifier tree check gadget.
147 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
148 }
149
150 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
151 auto low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
152 auto low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
153
156 present,
157 low_leaf_preimage,
158 low_leaf_index,
159 low_leaf_path,
161
162 return present;
163}
164
169
171{
172 return nullifier_write_internal(/*contract_address*/ std::nullopt, nullifier);
173}
174
176{
177 uint32_t nullifier_counter = tree_counters_stack.top().nullifier_counter;
178 FF siloed_nullifier = nullifier;
179 if (contract_address.has_value()) {
180 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
181 // The siloing will later be constrained in the nullifier tree check gadget.
182 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
183 }
184
185 auto [present, low_leaf_index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
187
188 SiblingPath low_leaf_path;
189 IndexedLeaf<NullifierLeafValue> low_leaf_preimage;
191
192 if (present) {
193 low_leaf_path = raw_merkle_db.get_sibling_path(MerkleTreeId::NULLIFIER_TREE, low_leaf_index);
194 low_leaf_preimage = raw_merkle_db.get_leaf_preimage_nullifier_tree(low_leaf_index);
195 } else {
196 auto insertion_result = raw_merkle_db.insert_indexed_leaves_nullifier_tree(siloed_nullifier);
197
198 low_leaf_path = insertion_result.low_leaf_witness_data.at(0).path;
199 low_leaf_preimage = insertion_result.low_leaf_witness_data.at(0).leaf;
200 insertion_path = insertion_result.insertion_witness_data.at(0).path;
201 }
202
205 nullifier_counter,
206 low_leaf_preimage,
207 low_leaf_index,
208 low_leaf_path,
209 snapshot_before,
210 insertion_path);
211
212 (void)snapshot_after; // Silence unused variable warning when assert is stripped out
213 // Sanity check.
214 assert(snapshot_after == raw_merkle_db.get_tree_roots().nullifier_tree);
215
216 if (!present) {
217 tree_counters_stack.top().nullifier_counter++;
218 } else {
219 throw NullifierCollisionException(format("Nullifier ", nullifier, " already exists"));
220 }
221}
222
223bool MerkleDB::note_hash_exists(uint64_t leaf_index, const FF& unique_note_hash) const
224{
225 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
226 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
227 return note_hash_tree_check.note_hash_exists(
228 unique_note_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().note_hash_tree);
229}
230
232{
233 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
234
236 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
237 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
239 FF unique_note_hash = unconstrained_make_unique_note_hash(
240 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
241 auto append_result =
242 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash })[0];
243
244 AppendOnlyTreeSnapshot snapshot_after = note_hash_tree_check.append_note_hash(
245 note_hash, contract_address, note_hash_counter, append_result.path, snapshot_before);
246
247 (void)snapshot_after; // Silence unused variable warning when assert is stripped out
248 // Sanity check.
249 assert(snapshot_after == raw_merkle_db.get_tree_roots().note_hash_tree);
250
251 tree_counters_stack.top().note_hash_counter++;
252}
253
254void MerkleDB::siloed_note_hash_write(const FF& siloed_note_hash)
255{
256 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
258 // Unconstrained siloing and uniqueness to fetch the hint, since the hints are keyed by the unique note hash.
259 // The siloing and uniqueness will later be constrained in the note hash tree check gadget.
260 FF unique_note_hash = unconstrained_make_unique_note_hash(
261 siloed_note_hash, note_hash_tree_check.get_first_nullifier(), note_hash_counter);
262 auto hint = raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash })[0];
263
264 AppendOnlyTreeSnapshot snapshot_after =
265 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, hint.path, snapshot_before);
266
267 (void)snapshot_after; // Silence unused variable warning when assert is stripped out
268 // Sanity check.
269 assert(snapshot_after == raw_merkle_db.get_tree_roots().note_hash_tree);
270
271 tree_counters_stack.top().note_hash_counter++;
272}
273
274void MerkleDB::unique_note_hash_write(const FF& unique_note_hash)
275{
276 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
278 auto hint = raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash })[0];
279
280 AppendOnlyTreeSnapshot snapshot_after =
281 note_hash_tree_check.append_unique_note_hash(unique_note_hash, note_hash_counter, hint.path, snapshot_before);
282
283 (void)snapshot_after; // Silence unused variable warning when assert is stripped out
284 // Sanity check.
285 assert(snapshot_after == raw_merkle_db.get_tree_roots().note_hash_tree);
286
287 tree_counters_stack.top().note_hash_counter++;
288}
289
290bool MerkleDB::l1_to_l2_msg_exists(uint64_t leaf_index, const FF& msg_hash) const
291{
292 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
293 auto path = raw_merkle_db.get_sibling_path(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
295 msg_hash, leaf_value, leaf_index, path, raw_merkle_db.get_tree_roots().l1_to_l2_message_tree);
296}
297
299{
300 // The public data tree is not padded.
301 raw_merkle_db.pad_tree(MerkleTreeId::NOTE_HASH_TREE,
302 MAX_NOTE_HASHES_PER_TX - tree_counters_stack.top().note_hash_counter);
303 raw_merkle_db.pad_tree(MerkleTreeId::NULLIFIER_TREE,
304 MAX_NULLIFIERS_PER_TX - tree_counters_stack.top().nullifier_counter);
305}
306
308{
312 for (auto& listener : checkpoint_listeners) {
313 listener->on_checkpoint_created();
314 }
315}
316
318{
321 TreeCounters current_counters = tree_counters_stack.top();
323 tree_counters_stack.top() = current_counters;
324 for (auto& listener : checkpoint_listeners) {
325 listener->on_checkpoint_committed();
326 }
327}
328
330{
334 for (auto& listener : checkpoint_listeners) {
335 listener->on_checkpoint_reverted();
336 }
337}
338
340{
342}
343
344} // namespace bb::avm2::simulation
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 std::vector< AppendLeafResult > append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves)=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 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:22
::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