Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
note_hash_tree_check.cpp
Go to the documentation of this file.
2
4
5namespace bb::avm2::simulation {
6
23bool NoteHashTreeCheck::note_hash_exists(const FF& unique_note_hash,
24 const FF& leaf_value,
25 uint64_t leaf_index,
26 std::span<const FF> sibling_path,
27 const AppendOnlyTreeSnapshot& snapshot)
28{
29 merkle_check.assert_membership(DOM_SEP__MERKLE_HASH, leaf_value, leaf_index, sibling_path, snapshot.root);
31 .existing_leaf_value = leaf_value,
32 .leaf_index = leaf_index,
33 .prev_snapshot = snapshot });
34 return unique_note_hash == leaf_value;
35}
36
48{
49 return poseidon2.hash({ DOM_SEP__SILOED_NOTE_HASH, contract_address, note_hash });
50}
51
60FF NoteHashTreeCheck::make_nonce(uint64_t note_hash_counter) const
61{
62 return poseidon2.hash({ DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter });
63}
64
74FF NoteHashTreeCheck::make_unique(const FF& siloed_note_hash, const FF& nonce) const
75{
76 return poseidon2.hash({ DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash });
77}
78
94 AztecAddress contract_address,
95 uint64_t note_hash_counter,
96 std::span<const FF> sibling_path,
97 const AppendOnlyTreeSnapshot& prev_snapshot)
98{
100 note_hash, contract_address, /*should_make_unique=*/true, note_hash_counter, sibling_path, prev_snapshot);
101}
102
116 uint64_t note_hash_counter,
117 std::span<const FF> sibling_path,
118 const AppendOnlyTreeSnapshot& prev_snapshot)
119{
120 return append_note_hash_internal(siloed_note_hash,
121 /* contract_address */ std::nullopt,
122 /* should_make_unique */ true,
123 note_hash_counter,
124 sibling_path,
125 prev_snapshot);
126}
127
141 uint64_t note_hash_counter,
142 std::span<const FF> sibling_path,
143 const AppendOnlyTreeSnapshot& prev_snapshot)
144{
145 return append_note_hash_internal(unique_note_hash,
146 /* contract_address */ std::nullopt,
147 /* should_make_unique */ false,
148 note_hash_counter,
149 sibling_path,
150 prev_snapshot);
151}
152
169 std::optional<AztecAddress> contract_address,
170 bool should_make_unique,
171 uint64_t note_hash_counter,
172 std::span<const FF> sibling_path,
173 const AppendOnlyTreeSnapshot& prev_snapshot)
174{
175 FF original_note_hash = note_hash;
176
178 if (contract_address.has_value()) {
179 note_hash = make_siloed(*contract_address, note_hash);
180 siloing_data = NoteHashSiloingData{ .siloed_note_hash = note_hash, .address = *contract_address };
181 }
183 if (should_make_unique) {
184 FF nonce = make_nonce(note_hash_counter);
186 uniqueness_data =
187 NoteHashUniquenessData{ .nonce = nonce, .unique_note_hash = note_hash, .first_nullifier = first_nullifier };
188 }
189
190 FF next_root = merkle_check.write(
191 DOM_SEP__MERKLE_HASH, 0, note_hash, prev_snapshot.next_available_leaf_index, sibling_path, prev_snapshot.root);
193 .root = next_root,
194 .next_available_leaf_index = prev_snapshot.next_available_leaf_index + 1,
195 };
196 events.emit(NoteHashTreeReadWriteEvent{ .note_hash = original_note_hash,
197 .existing_leaf_value = 0,
198 .leaf_index = prev_snapshot.next_available_leaf_index,
199 .prev_snapshot = prev_snapshot,
200 .append_data = NoteHashAppendData{
201 .siloing_data = siloing_data,
202 .uniqueness_data = uniqueness_data,
203 .note_hash_counter = note_hash_counter,
204 .next_snapshot = next_snapshot,
205 } });
206 return next_snapshot;
207}
208
214
220
226
227} // namespace bb::avm2::simulation
#define DOM_SEP__MERKLE_HASH
#define DOM_SEP__SILOED_NOTE_HASH
#define DOM_SEP__UNIQUE_NOTE_HASH
#define DOM_SEP__NOTE_HASH_NONCE
virtual void emit(Event &&event)=0
void on_checkpoint_created() override
Emits a checkpoint creation event for the note hash tree.
AppendOnlyTreeSnapshot append_siloed_note_hash(const FF &siloed_note_hash, uint64_t note_hash_counter, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot) override
Appends a pre-siloed note hash to the tree after making it unique.
EventEmitterInterface< NoteHashTreeCheckEvent > & events
AppendOnlyTreeSnapshot append_note_hash(const FF &note_hash, AztecAddress contract_address, uint64_t note_hash_counter, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot) override
Appends a raw (inner) note hash to the tree after siloing and making it unique.
FF make_nonce(uint64_t note_hash_counter) const
Derives a nonce for a note hash from the transaction's first nullifier and the note hash counter.
FF make_unique(const FF &siloed_note_hash, const FF &nonce) const
Computes a unique note hash by combining a siloed note hash with its nonce.
FF make_siloed(AztecAddress contract_address, const FF &note_hash) const
Computes the siloed note hash by hashing the contract address with the note hash.
void on_checkpoint_committed() override
Emits a checkpoint commit event, finalizing pending note hash tree changes.
void on_checkpoint_reverted() override
Emits a checkpoint revert event, rolling back pending note hash tree changes.
AppendOnlyTreeSnapshot append_note_hash_internal(FF note_hash, std::optional< AztecAddress > contract_address, bool should_make_unique, uint64_t note_hash_counter, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot)
Internal implementation for all note hash append variants.
bool note_hash_exists(const FF &unique_note_hash, const FF &leaf_value, uint64_t leaf_index, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &snapshot) override
Checks whether a note hash exists in the note hash tree via Merkle membership proof.
AppendOnlyTreeSnapshot append_unique_note_hash(const FF &unique_note_hash, uint64_t note_hash_counter, std::span< const FF > sibling_path, const AppendOnlyTreeSnapshot &prev_snapshot) override
Appends a pre-computed unique note hash directly to the tree.
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
AVM range check gadget for witness generation.
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::optional< NoteHashSiloingData > siloing_data