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
21// Merkle DB starts.
23{
24 // No event generated.
26 TreeCounters tree_counters = tree_counters_stack.top();
27 return {
28 .noteHashTree = { .tree = tree_snapshots.noteHashTree, .counter = tree_counters.note_hash_counter },
29 .nullifierTree = { .tree = tree_snapshots.nullifierTree, .counter = tree_counters.nullifier_counter },
30 .l1ToL2MessageTree = { .tree = tree_snapshots.l1ToL2MessageTree,
31 .counter = tree_counters.l2_to_l1_msg_counter },
32 .publicDataTree = { .tree = tree_snapshots.publicDataTree, .counter = written_public_data_slots.size() },
33 };
34}
35
37{
38 auto [present, index] = raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::PUBLIC_DATA_TREE,
40 FF value = 0;
41
42 if (present) {
44 value = preimage.leaf.value;
45 }
46
47 return value;
48}
49
51 const FF& slot,
52 const FF& value,
53 bool is_protocol_write)
54{
57
58 if (!is_protocol_write) {
60 }
61}
62
67
72
74{
75 return nullifier_exists_internal(/*contract_address*/ std::nullopt, nullifier);
76}
77
79{
80 FF siloed_nullifier = nullifier;
81 if (contract_address.has_value()) {
82 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
83 }
84
85 auto [present, low_leaf_index_] =
86 raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
87
88 return present;
89}
90
95
100
102{
103 FF siloed_nullifier = nullifier;
104 if (contract_address.has_value()) {
105 // Unconstrained siloing to fetch the hint, since the hints are keyed by siloed data.
106 // The siloing will later be constrained in the nullifier tree check gadget.
107 siloed_nullifier = unconstrained_silo_nullifier(contract_address.value(), nullifier);
108 }
109
110 auto [present, low_leaf_index_] =
111 raw_merkle_db.get_low_indexed_leaf(MerkleTreeId::NULLIFIER_TREE, siloed_nullifier);
112
113 if (present) {
114 throw NullifierCollisionException(format("Nullifier ", nullifier, " already exists"));
115 }
116
118 tree_counters_stack.top().nullifier_counter++;
119}
120
121bool PureMerkleDB::note_hash_exists(uint64_t leaf_index, const FF& unique_note_hash) const
122{
123 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::NOTE_HASH_TREE, leaf_index);
124 return (unique_note_hash == leaf_value);
125}
126
128{
129 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
130 FF siloed_note_hash = unconstrained_silo_note_hash(contract_address, note_hash);
131 FF unique_note_hash = unconstrained_make_unique_note_hash(siloed_note_hash, first_nullifier, note_hash_counter);
132 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
133
134 tree_counters_stack.top().note_hash_counter++;
135}
136
137void PureMerkleDB::siloed_note_hash_write(const FF& siloed_note_hash)
138{
139 uint32_t note_hash_counter = tree_counters_stack.top().note_hash_counter;
140 FF unique_note_hash = unconstrained_make_unique_note_hash(siloed_note_hash, first_nullifier, note_hash_counter);
141 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
142
143 tree_counters_stack.top().note_hash_counter++;
144}
145
146void PureMerkleDB::unique_note_hash_write(const FF& unique_note_hash)
147{
148 raw_merkle_db.append_leaves(MerkleTreeId::NOTE_HASH_TREE, std::vector<FF>{ unique_note_hash });
149
150 tree_counters_stack.top().note_hash_counter++;
151}
152
153bool PureMerkleDB::l1_to_l2_msg_exists(uint64_t leaf_index, const FF& msg_hash) const
154{
155 auto leaf_value = raw_merkle_db.get_leaf_value(MerkleTreeId::L1_TO_L2_MESSAGE_TREE, leaf_index);
156 return (msg_hash == leaf_value);
157}
158
160{
161 // The public data tree is not padded.
162 raw_merkle_db.pad_tree(MerkleTreeId::NOTE_HASH_TREE,
163 MAX_NOTE_HASHES_PER_TX - tree_counters_stack.top().note_hash_counter);
164 raw_merkle_db.pad_tree(MerkleTreeId::NULLIFIER_TREE,
165 MAX_NULLIFIERS_PER_TX - tree_counters_stack.top().nullifier_counter);
166}
167
169{
173 for (auto& listener : checkpoint_listeners) {
174 listener->on_checkpoint_created();
175 }
176}
177
179{
182 TreeCounters current_counters = tree_counters_stack.top();
184 tree_counters_stack.top() = current_counters;
185 for (auto& listener : checkpoint_listeners) {
186 listener->on_checkpoint_committed();
187 }
188}
189
191{
195 for (auto& listener : checkpoint_listeners) {
196 listener->on_checkpoint_reverted();
197 }
198}
199
201{
203}
204
205} // namespace bb::avm2::simulation
#define MAX_NOTE_HASHES_PER_TX
#define MAX_NULLIFIERS_PER_TX
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 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 const TreeSnapshots & get_tree_roots() const =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
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:21
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:29
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
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AppendOnlyTreeSnapshot noteHashTree
AppendOnlyTreeSnapshot nullifierTree
AppendOnlyTreeSnapshot l1ToL2MessageTree
AppendOnlyTreeSnapshot publicDataTree