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
7
8namespace bb::avm2::simulation {
9
10// Contracts DB starts.
15
20
25
31
32void PureContractDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
33{
34 raw_contract_db.add_contracts(contract_deployment_data);
35}
36
37// Merkle DB starts.
39{
40 // No event generated.
42 TreeCounters tree_counters = tree_counters_stack.top();
43 return {
44 .note_hash_tree = { .tree = tree_snapshots.note_hash_tree, .counter = tree_counters.note_hash_counter },
45 .nullifier_tree = { .tree = tree_snapshots.nullifier_tree, .counter = tree_counters.nullifier_counter },
46 .l1_to_l2_message_tree = { .tree = tree_snapshots.l1_to_l2_message_tree,
47 .counter = tree_counters.l2_to_l1_msg_counter },
48 .public_data_tree = { .tree = tree_snapshots.public_data_tree, .counter = written_public_data_slots.size() },
49 };
50}
51
53{
54 auto [present, index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::PUBLIC_DATA_TREE,
56 FF value = 0;
57
58 if (present) {
60 value = preimage.leaf.value;
61 }
62
63 return value;
64}
65
67 const FF& slot,
68 const FF& value,
69 bool is_protocol_write)
70{
73
74 if (!is_protocol_write) {
76 }
77}
78
83
88
90{
91 return nullifier_exists_internal(/*contract_address*/ std::nullopt, nullifier);
92}
93
95{
96 FF siloed_nullifier = nullifier;
97 if (contract_address.has_value()) {
98 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
99 }
100
101 auto [present, low_leaf_index_] =
102 raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
103
104 return present;
105}
106
111
116
118{
119 FF siloed_nullifier = nullifier;
120 if (contract_address.has_value()) {
121 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
122 // The siloing will later be constrained in the nullifier tree check gadget.
123 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
124 }
125
126 auto [present, low_leaf_index_] =
127 raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
128
129 if (present) {
131 contract_address.has_value() ? format("Attempted to emit duplicate nullifier ",
132 nullifier,
133 " (contract address: ",
134 contract_address.value(),
135 ").")
136 : format("Attempted to emit duplicate siloed nullifier ", nullifier, "."));
137 }
138
140 tree_counters_stack.top().nullifier_counter++;
141}
142
143bool PureMerkleDB::note_hash_exists(uint64_t leaf_index, const FF& unique_note_hash) const
144{
145 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
146 return (unique_note_hash == leaf_value);
147}
148
150{
151 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
153 FF unique_note_hash = unconstrained_make_unique_note_hash(siloed_note_hash, first_nullifier, note_hash_counter);
154 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
155
156 tree_counters_stack.top().note_hash_counter++;
157}
158
159void PureMerkleDB::siloed_note_hash_write(const FF& siloed_note_hash)
160{
161 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
162 FF unique_note_hash = unconstrained_make_unique_note_hash(siloed_note_hash, first_nullifier, note_hash_counter);
163 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
164
165 tree_counters_stack.top().note_hash_counter++;
166}
167
168void PureMerkleDB::unique_note_hash_write(const FF& unique_note_hash)
169{
170 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
171
172 tree_counters_stack.top().note_hash_counter++;
173}
174
175bool PureMerkleDB::l1_to_l2_msg_exists(uint64_t leaf_index, const FF& msg_hash) const
176{
177 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
178 return (msg_hash == leaf_value);
179}
180
182{
183 // The public data tree is not padded.
184 raw_merkle_db.pad_tree(MerkleTreeId::NOTE_HASH_TREE,
185 MAX_NOTE_HASHES_PER_TX - tree_counters_stack.top().note_hash_counter);
186 raw_merkle_db.pad_tree(MerkleTreeId::NULLIFIER_TREE,
187 MAX_NULLIFIERS_PER_TX - tree_counters_stack.top().nullifier_counter);
188}
189
191{
195 for (auto& listener : checkpoint_listeners) {
196 listener->on_checkpoint_created();
197 }
198}
199
201{
204 TreeCounters current_counters = tree_counters_stack.top();
206 tree_counters_stack.top() = current_counters;
207 for (auto& listener : checkpoint_listeners) {
208 listener->on_checkpoint_committed();
209 }
210}
211
213{
217 for (auto& listener : checkpoint_listeners) {
218 listener->on_checkpoint_reverted();
219 }
220}
221
223{
225}
226
227} // namespace bb::avm2::simulation
#define MAX_NOTE_HASHES_PER_TX
#define MAX_NULLIFIERS_PER_TX
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 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 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 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
std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const override
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
void add_contracts(const ContractDeploymentData &contract_deployment_data) override
ContractDBInterface & raw_contract_db
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
WrittenPublicDataSlotsInterface & written_public_data_slots
void nullifier_write(const AztecAddress &contract_address, const FF &nullifier) override
FF storage_read(const AztecAddress &contract_address, const FF &slot) const override
void storage_write(const AztecAddress &contract_address, const FF &slot, const FF &value, bool is_protocol_write) override
std::stack< TreeCounters > tree_counters_stack
TreeStates get_tree_state() const override
bool nullifier_exists_internal(std::optional< AztecAddress > contract_address, const FF &nullifier) const
void siloed_note_hash_write(const FF &note_hash) override
void siloed_nullifier_write(const FF &nullifier) override
bool note_hash_exists(uint64_t leaf_index, const FF &unique_note_hash) const override
std::vector< CheckpointNotifiable * > checkpoint_listeners
bool nullifier_exists(const AztecAddress &contract_address, const FF &nullifier) const override
void note_hash_write(const AztecAddress &contract_address, const FF &note_hash) override
void nullifier_write_internal(std::optional< AztecAddress > contract_address, const FF &nullifier)
bool l1_to_l2_msg_exists(uint64_t leaf_index, const FF &msg_hash) const override
bool was_storage_written(const AztecAddress &contract_address, const FF &slot) const override
void unique_note_hash_write(const FF &note_hash) override
bool siloed_nullifier_exists(const FF &nullifier) const override
LowLevelMerkleDBInterface & raw_merkle_db
uint32_t get_checkpoint_id() const override
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
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
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