Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
note_hash_tree_check.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cmath>
5#include <cstdint>
6
30
31namespace bb::avm2::constraining {
32namespace {
33
34using ::testing::NiceMock;
35
36using simulation::EventEmitter;
37using simulation::MerkleCheck;
38using simulation::MerkleCheckEvent;
39using simulation::MockExecutionIdManager;
40using simulation::MockGreaterThan;
41using simulation::NoopEventEmitter;
42using simulation::NoteHashTreeCheck;
43using simulation::Poseidon2;
44using simulation::Poseidon2HashEvent;
45using simulation::Poseidon2PermutationEvent;
46using simulation::Poseidon2PermutationMemoryEvent;
48
49using tracegen::MerkleCheckTraceBuilder;
50using tracegen::NoteHashTreeCheckTraceBuilder;
51using tracegen::Poseidon2TraceBuilder;
52using tracegen::PrecomputedTraceBuilder;
53using tracegen::PublicInputsTraceBuilder;
54using tracegen::TestTraceContainer;
55
57using C = Column;
58using note_hash_tree_check = bb::avm2::note_hash_tree_check<FF>;
60
61TEST(NoteHashTreeCheckConstrainingTests, PositiveExists)
62{
63 EventEmitter<Poseidon2HashEvent> hash_event_emitter;
64 NoopEventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
65 NoopEventEmitter<Poseidon2PermutationMemoryEvent> perm_mem_event_emitter;
66 NiceMock<MockGreaterThan> mock_gt;
67 NiceMock<MockExecutionIdManager> mock_exec_id_manager;
70
71 EventEmitter<MerkleCheckEvent> merkle_event_emitter;
72 MerkleCheck merkle_check(poseidon2, merkle_event_emitter);
73
74 EventEmitter<simulation::NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
75 NoteHashTreeCheck note_hash_tree_check_simulator(0, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
76
77 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } } });
78 MerkleCheckTraceBuilder merkle_check_builder;
79 NoteHashTreeCheckTraceBuilder note_hash_tree_check_builder;
80
81 FF note_hash = 42;
82
83 uint64_t leaf_index = 30;
84 std::vector<FF> sibling_path;
85 sibling_path.reserve(NOTE_HASH_TREE_HEIGHT);
86 for (size_t i = 0; i < NOTE_HASH_TREE_HEIGHT; ++i) {
87 sibling_path.emplace_back(i);
88 }
89 FF root = unconstrained_root_from_path(note_hash, leaf_index, sibling_path);
90
91 EXPECT_TRUE(note_hash_tree_check_simulator.note_hash_exists(
92 note_hash,
93 note_hash,
94 leaf_index,
95 sibling_path,
96 AppendOnlyTreeSnapshot{ .root = root, .nextAvailableLeafIndex = 128 }));
97
98 note_hash_tree_check_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
99 merkle_check_builder.process(merkle_event_emitter.dump_events(), trace);
100
101 check_relation<note_hash_tree_check>(trace);
102 // Not checking all interactions due to the public inputs interaction, which needs to be checked in an e2e test
103 check_interaction<NoteHashTreeCheckTraceBuilder,
109}
110
111TEST(NoteHashTreeCheckConstrainingTests, PositiveNotExists)
112{
113 EventEmitter<Poseidon2HashEvent> hash_event_emitter;
114 EventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
115 NoopEventEmitter<Poseidon2PermutationMemoryEvent> perm_mem_event_emitter;
116
117 NiceMock<MockExecutionIdManager> mock_exec_id_manager;
118 NiceMock<MockGreaterThan> mock_gt;
120
121 EventEmitter<MerkleCheckEvent> merkle_event_emitter;
122 MerkleCheck merkle_check(poseidon2, merkle_event_emitter);
123
124 EventEmitter<simulation::NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
125 NoteHashTreeCheck note_hash_tree_check_simulator(0, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
126
127 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } } });
128 MerkleCheckTraceBuilder merkle_check_builder;
129 NoteHashTreeCheckTraceBuilder note_hash_tree_check_builder;
130
131 FF requested_note_hash = 42;
132 FF actual_leaf_value = 43;
133
134 uint64_t leaf_index = 30;
135 std::vector<FF> sibling_path;
136 sibling_path.reserve(NOTE_HASH_TREE_HEIGHT);
137 for (size_t i = 0; i < NOTE_HASH_TREE_HEIGHT; ++i) {
138 sibling_path.emplace_back(i);
139 }
140 FF root = unconstrained_root_from_path(actual_leaf_value, leaf_index, sibling_path);
141
142 EXPECT_FALSE(note_hash_tree_check_simulator.note_hash_exists(
143 requested_note_hash,
144 actual_leaf_value,
145 leaf_index,
146 sibling_path,
147 AppendOnlyTreeSnapshot{ .root = root, .nextAvailableLeafIndex = 128 }));
148
149 note_hash_tree_check_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
150 merkle_check_builder.process(merkle_event_emitter.dump_events(), trace);
151
152 check_relation<note_hash_tree_check>(trace);
153 // Not checking all interactions due to the public inputs interaction, which needs to be checked in an e2e test
154 check_interaction<NoteHashTreeCheckTraceBuilder,
160}
161
162TEST(NoteHashTreeCheckConstrainingTests, PositiveWrite)
163{
164 auto test_public_inputs = testing::PublicInputsBuilder().rand_previous_non_revertible_accumulated_data(1).build();
165 EventEmitter<Poseidon2HashEvent> hash_event_emitter;
166 NoopEventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
167 NoopEventEmitter<Poseidon2PermutationMemoryEvent> perm_mem_event_emitter;
168 NiceMock<MockGreaterThan> mock_gt;
169 NiceMock<MockExecutionIdManager> mock_exec_id_manager;
172
173 EventEmitter<MerkleCheckEvent> merkle_event_emitter;
174 MerkleCheck merkle_check(poseidon2, merkle_event_emitter);
175
176 EventEmitter<simulation::NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
177 NoteHashTreeCheck note_hash_tree_check_simulator(
178 test_public_inputs.previousNonRevertibleAccumulatedData.nullifiers[0],
179 poseidon2,
180 merkle_check,
181 note_hash_tree_check_event_emitter);
182
183 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } } });
184
185 FF raw_note_hash = 42;
186
187 std::vector<FF> sibling_path;
188 sibling_path.reserve(NOTE_HASH_TREE_HEIGHT);
189 for (size_t i = 0; i < NOTE_HASH_TREE_HEIGHT; ++i) {
190 sibling_path.emplace_back(i);
191 }
192
193 AppendOnlyTreeSnapshot prev_snapshot{ .root = unconstrained_root_from_path(0, 128, sibling_path),
194 .nextAvailableLeafIndex = 128 };
195
196 note_hash_tree_check_simulator.append_note_hash(raw_note_hash, AztecAddress(7), 10, sibling_path, prev_snapshot);
197
198 NoteHashTreeCheckTraceBuilder note_hash_tree_check_builder;
199 note_hash_tree_check_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
200 Poseidon2TraceBuilder poseidon2_builder;
201 poseidon2_builder.process_hash(hash_event_emitter.dump_events(), trace);
202 MerkleCheckTraceBuilder merkle_check_builder;
203 merkle_check_builder.process(merkle_event_emitter.dump_events(), trace);
204
205 PublicInputsTraceBuilder public_inputs_builder;
206 public_inputs_builder.process_public_inputs(trace, test_public_inputs);
207 public_inputs_builder.process_public_inputs_aux_precomputed(trace);
208
209 PrecomputedTraceBuilder precomputed_builder;
210 precomputed_builder.process_phase_table(trace);
212
213 check_relation<note_hash_tree_check>(trace);
214 // Not checking all interactions due to the public inputs interaction, which needs to be checked in an e2e test
215 check_interaction<NoteHashTreeCheckTraceBuilder,
221}
222
223TEST(NoteHashTreeCheckConstrainingTests, NegativeSiloingOnRead)
224{
225 TestTraceContainer trace({ {
226 { C::note_hash_tree_check_sel, 1 },
227 { C::note_hash_tree_check_write, 0 },
228 { C::note_hash_tree_check_should_silo, 1 },
229 { C::note_hash_tree_check_note_hash, 27 },
230 } });
231
233 check_relation<note_hash_tree_check>(trace, note_hash_tree_check::SR_DISABLE_SILOING_ON_READ),
234 "DISABLE_SILOING_ON_READ");
235}
236
237TEST(NoteHashTreeCheckConstrainingTests, NegativePassthroughSiloing)
238{
239 TestTraceContainer trace({ {
240 { C::note_hash_tree_check_sel, 1 },
241 { C::note_hash_tree_check_write, 0 },
242 { C::note_hash_tree_check_should_silo, 0 },
243 { C::note_hash_tree_check_note_hash, 27 },
244 { C::note_hash_tree_check_siloed_note_hash, 27 },
245 } });
246
247 check_relation<note_hash_tree_check>(trace, note_hash_tree_check::SR_PASSTHROUGH_SILOING);
248
249 trace.set(C::note_hash_tree_check_siloed_note_hash, 0, 28);
250
252 "PASSTHROUGH_SILOING");
253}
254
255TEST(NoteHashTreeCheckConstrainingTests, NegativeUniquenessOnRead)
256{
257 TestTraceContainer trace({ {
258 { C::note_hash_tree_check_sel, 1 },
259 { C::note_hash_tree_check_write, 0 },
260 { C::note_hash_tree_check_should_unique, 1 },
261 { C::note_hash_tree_check_note_hash, 27 },
262 } });
263
265 check_relation<note_hash_tree_check>(trace, note_hash_tree_check::SR_DISABLE_UNIQUENESS_ON_READ),
266 "DISABLE_UNIQUENESS_ON_READ");
267}
268
269TEST(NoteHashTreeCheckConstrainingTests, NegativePassthroughUniqueness)
270{
271 TestTraceContainer trace({ {
272 { C::note_hash_tree_check_sel, 1 },
273 { C::note_hash_tree_check_write, 0 },
274 { C::note_hash_tree_check_should_unique, 0 },
275 { C::note_hash_tree_check_note_hash, 27 },
276 { C::note_hash_tree_check_siloed_note_hash, 27 },
277 { C::note_hash_tree_check_unique_note_hash, 27 },
278 } });
279
280 check_relation<note_hash_tree_check>(trace, note_hash_tree_check::SR_PASSTHROUGH_UNIQUENESS);
281
282 trace.set(C::note_hash_tree_check_unique_note_hash, 0, 28);
283
285 check_relation<note_hash_tree_check>(trace, note_hash_tree_check::SR_PASSTHROUGH_UNIQUENESS),
286 "PASSTHROUGH_UNIQUENESS");
287}
288
289} // namespace
290} // namespace bb::avm2::constraining
#define NOTE_HASH_TREE_HEIGHT
#define AVM_PUBLIC_INPUTS_COLUMNS_MAX_LENGTH
static constexpr size_t SR_DISABLE_SILOING_ON_READ
static constexpr size_t SR_PASSTHROUGH_SILOING
static constexpr size_t SR_PASSTHROUGH_UNIQUENESS
static constexpr size_t SR_DISABLE_UNIQUENESS_ON_READ
void set(Column col, uint32_t row, const FF &value)
Implements a parallelized batch insertion indexed tree Accepts template argument of the type of store...
PrecomputedTraceBuilder precomputed_builder
Definition alu.test.cpp:119
TestTraceContainer trace
NoopEventEmitter< Poseidon2PermutationEvent > perm_event_emitter
NoopEventEmitter< Poseidon2PermutationMemoryEvent > perm_mem_event_emitter
NiceMock< MockGreaterThan > mock_gt
EventEmitter< Poseidon2HashEvent > hash_event_emitter
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:508
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
FF unconstrained_root_from_path(const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
Definition merkle.cpp:12
lookup_settings< lookup_note_hash_tree_check_merkle_check_settings_ > lookup_note_hash_tree_check_merkle_check_settings
lookup_settings< lookup_note_hash_tree_check_read_first_nullifier_settings_ > lookup_note_hash_tree_check_read_first_nullifier_settings
lookup_settings< lookup_note_hash_tree_check_nonce_computation_poseidon2_settings_ > lookup_note_hash_tree_check_nonce_computation_poseidon2_settings
lookup_settings< lookup_note_hash_tree_check_unique_note_hash_poseidon2_settings_ > lookup_note_hash_tree_check_unique_note_hash_poseidon2_settings
lookup_settings< lookup_note_hash_tree_check_silo_poseidon2_settings_ > lookup_note_hash_tree_check_silo_poseidon2_settings
typename Flavor::FF FF