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
3namespace bb::avm2::simulation {
4
21bool NoteHashTreeCheck::note_hash_exists(const FF& unique_note_hash,
22 const FF& leaf_value,
23 uint64_t leaf_index,
24 std::span<const FF> sibling_path,
25 const AppendOnlyTreeSnapshot& snapshot)
26{
27 merkle_check.assert_membership(leaf_value, leaf_index, sibling_path, snapshot.root);
29 .existing_leaf_value = leaf_value,
30 .leaf_index = leaf_index,
31 .prev_snapshot = snapshot });
32 return unique_note_hash == leaf_value;
33}
34
46{
47 return poseidon2.hash({ DOM_SEP__SILOED_NOTE_HASH, contract_address, note_hash });
48}
49
58FF NoteHashTreeCheck::make_nonce(uint64_t note_hash_counter) const
59{
60 return poseidon2.hash({ DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter });
61}
62
72FF NoteHashTreeCheck::make_unique(const FF& siloed_note_hash, const FF& nonce) const
73{
74 return poseidon2.hash({ DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash });
75}
76
92 AztecAddress contract_address,
93 uint64_t note_hash_counter,
94 std::span<const FF> sibling_path,
95 const AppendOnlyTreeSnapshot& prev_snapshot)
96{
98 note_hash, contract_address, /*should_make_unique=*/true, note_hash_counter, sibling_path, prev_snapshot);
99}
100
114 uint64_t note_hash_counter,
115 std::span<const FF> sibling_path,
116 const AppendOnlyTreeSnapshot& prev_snapshot)
117{
118 return append_note_hash_internal(siloed_note_hash,
119 /* contract_address */ std::nullopt,
120 /* should_make_unique */ true,
121 note_hash_counter,
122 sibling_path,
123 prev_snapshot);
124}
125
139 uint64_t note_hash_counter,
140 std::span<const FF> sibling_path,
141 const AppendOnlyTreeSnapshot& prev_snapshot)
142{
143 return append_note_hash_internal(unique_note_hash,
144 /* contract_address */ std::nullopt,
145 /* should_make_unique */ false,
146 note_hash_counter,
147 sibling_path,
148 prev_snapshot);
149}
150
167 std::optional<AztecAddress> contract_address,
168 bool should_make_unique,
169 uint64_t note_hash_counter,
170 std::span<const FF> sibling_path,
171 const AppendOnlyTreeSnapshot& prev_snapshot)
172{
173 FF original_note_hash = note_hash;
174
176 if (contract_address.has_value()) {
177 note_hash = make_siloed(*contract_address, note_hash);
178 siloing_data = NoteHashSiloingData{ .siloed_note_hash = note_hash, .address = *contract_address };
179 }
181 if (should_make_unique) {
182 FF nonce = make_nonce(note_hash_counter);
184 uniqueness_data =
185 NoteHashUniquenessData{ .nonce = nonce, .unique_note_hash = note_hash, .first_nullifier = first_nullifier };
186 }
187
188 FF next_root =
189 merkle_check.write(0, note_hash, prev_snapshot.next_available_leaf_index, sibling_path, prev_snapshot.root);
191 .root = next_root,
192 .next_available_leaf_index = prev_snapshot.next_available_leaf_index + 1,
193 };
194 events.emit(NoteHashTreeReadWriteEvent{ .note_hash = original_note_hash,
195 .existing_leaf_value = 0,
196 .leaf_index = prev_snapshot.next_available_leaf_index,
197 .prev_snapshot = prev_snapshot,
198 .append_data = NoteHashAppendData{
199 .siloing_data = siloing_data,
200 .uniqueness_data = uniqueness_data,
201 .note_hash_counter = note_hash_counter,
202 .next_snapshot = next_snapshot,
203 } });
204 return next_snapshot;
205}
206
212
218
224
225} // namespace bb::avm2::simulation
#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