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.
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
11
12namespace bb::avm2::simulation {
13
14using ::testing::_;
15using ::testing::ElementsAre;
16using ::testing::Return;
17using ::testing::StrictMock;
18
20
21namespace {
22
23TEST(AvmSimulationNoteHashTree, Exists)
24{
25 StrictMock<MockPoseidon2> poseidon2;
26 StrictMock<MockMerkleCheck> merkle_check;
27
28 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
29 NoteHashTreeCheck note_hash_tree_check(1, poseidon2, merkle_check, event_emitter);
30
31 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
32 AppendOnlyTreeSnapshot snapshot = {
33 .root = 123456,
34 .next_available_leaf_index = 128,
35 };
36
37 FF note_hash = 42;
38 uint64_t leaf_index = 30;
39
40 EXPECT_CALL(merkle_check, assert_membership(DOM_SEP__MERKLE_HASH, note_hash, leaf_index, _, snapshot.root))
41 .WillRepeatedly(Return());
42
43 EXPECT_TRUE(note_hash_tree_check.note_hash_exists(note_hash, note_hash, leaf_index, sibling_path, snapshot));
44 EXPECT_FALSE(note_hash_tree_check.note_hash_exists(27, note_hash, leaf_index, sibling_path, snapshot));
45 EXPECT_THAT(event_emitter.dump_events(),
46 ElementsAre(
47 NoteHashTreeReadWriteEvent{
48 .note_hash = note_hash,
49 .existing_leaf_value = note_hash,
50 .leaf_index = leaf_index,
51 .prev_snapshot = snapshot,
52 },
53 NoteHashTreeReadWriteEvent{
54 .note_hash = 27,
55 .existing_leaf_value = note_hash,
56 .leaf_index = leaf_index,
57 .prev_snapshot = snapshot,
58 }));
59}
60
61TEST(AvmSimulationNoteHashTree, WriteUnique)
62{
63 StrictMock<MockPoseidon2> poseidon2;
64 StrictMock<MockMerkleCheck> merkle_check;
65
66 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
67 NoteHashTreeCheck note_hash_tree_check(1, poseidon2, merkle_check, event_emitter);
68
69 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
70 AppendOnlyTreeSnapshot snapshot = {
71 .root = 123456,
72 .next_available_leaf_index = 128,
73 };
74 FF note_hash = 42;
75 uint64_t note_hash_counter = 10;
76 FF next_root = 234567;
77
78 EXPECT_CALL(merkle_check,
79 write(DOM_SEP__MERKLE_HASH, FF(0), note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
80 .WillOnce(Return(next_root));
81
82 AppendOnlyTreeSnapshot next_snapshot =
83 note_hash_tree_check.append_unique_note_hash(note_hash, note_hash_counter, sibling_path, snapshot);
84
85 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
86 NoteHashTreeReadWriteEvent expect_event = { .note_hash = note_hash,
87 .leaf_index = snapshot.next_available_leaf_index,
88 .prev_snapshot = snapshot,
89 .append_data = NoteHashAppendData{
90 .note_hash_counter = note_hash_counter,
91 .next_snapshot = next_snapshot,
92 } };
93 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
94}
95
96TEST(AvmSimulationNoteHashTree, WriteSiloed)
97{
98 StrictMock<MockPoseidon2> poseidon2;
99 StrictMock<MockMerkleCheck> merkle_check;
100
101 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
103 NoteHashTreeCheck note_hash_tree_check(first_nullifier, poseidon2, merkle_check, event_emitter);
104
105 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
106 AppendOnlyTreeSnapshot snapshot = {
107 .root = 123456,
108 .next_available_leaf_index = 128,
109 };
110 FF siloed_note_hash = 42;
111 uint64_t note_hash_counter = 10;
112 FF nonce = 43;
113 FF unique_note_hash = 44;
114
115 FF next_root = 234567;
116
117 std::vector<FF> nonce_hash_inputs = { DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter };
118 EXPECT_CALL(poseidon2, hash(nonce_hash_inputs)).WillOnce(Return(nonce));
119
120 std::vector<FF> unique_note_hash_inputs = { DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash };
121 EXPECT_CALL(poseidon2, hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
122
123 EXPECT_CALL(
124 merkle_check,
125 write(DOM_SEP__MERKLE_HASH, FF(0), unique_note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
126 .WillOnce(Return(next_root));
127
128 AppendOnlyTreeSnapshot next_snapshot =
129 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, sibling_path, snapshot);
130
131 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
132 NoteHashTreeReadWriteEvent expect_event = { .note_hash = siloed_note_hash,
133 .leaf_index = snapshot.next_available_leaf_index,
134 .prev_snapshot = snapshot,
135 .append_data = NoteHashAppendData{
136 .uniqueness_data =
137 NoteHashUniquenessData{
138 .nonce = nonce,
139 .unique_note_hash = unique_note_hash,
140 .first_nullifier = first_nullifier,
141 },
142 .note_hash_counter = note_hash_counter,
143 .next_snapshot = next_snapshot,
144 } };
145 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
146}
147
148TEST(AvmSimulationNoteHashTree, WriteRaw)
149{
150 StrictMock<MockPoseidon2> poseidon2;
151 StrictMock<MockMerkleCheck> merkle_check;
152
153 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
155 NoteHashTreeCheck note_hash_tree_check(first_nullifier, poseidon2, merkle_check, event_emitter);
156
157 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
158 AppendOnlyTreeSnapshot snapshot = {
159 .root = 123456,
160 .next_available_leaf_index = 128,
161 };
162
163 FF raw_note_hash = 37;
164
165 AztecAddress contract_address = AztecAddress(27);
166 FF siloed_note_hash = 42;
167
168 uint64_t note_hash_counter = 10;
169 FF nonce = 43;
170 FF unique_note_hash = 44;
171
172 FF next_root = 234567;
173
174 std::vector<FF> siloed_note_hash_inputs = { DOM_SEP__SILOED_NOTE_HASH, contract_address, raw_note_hash };
175 EXPECT_CALL(poseidon2, hash(siloed_note_hash_inputs)).WillOnce(Return(siloed_note_hash));
176
177 std::vector<FF> nonce_hash_inputs = { DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter };
178 EXPECT_CALL(poseidon2, hash(nonce_hash_inputs)).WillOnce(Return(nonce));
179
180 std::vector<FF> unique_note_hash_inputs = { DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash };
181 EXPECT_CALL(poseidon2, hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
182
183 EXPECT_CALL(
184 merkle_check,
185 write(DOM_SEP__MERKLE_HASH, FF(0), unique_note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
186 .WillOnce(Return(next_root));
187
188 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
189 raw_note_hash, contract_address, note_hash_counter, sibling_path, snapshot);
190
191 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
192 NoteHashTreeReadWriteEvent expect_event = { .note_hash = raw_note_hash,
193 .leaf_index = snapshot.next_available_leaf_index,
194 .prev_snapshot = snapshot,
195 .append_data = NoteHashAppendData{
196 .siloing_data =
197 NoteHashSiloingData{
198 .siloed_note_hash = siloed_note_hash,
199 .address = contract_address,
200 },
201 .uniqueness_data =
202 NoteHashUniquenessData{
203 .nonce = nonce,
204 .unique_note_hash = unique_note_hash,
205 .first_nullifier = first_nullifier,
206 },
207 .note_hash_counter = note_hash_counter,
208 .next_snapshot = next_snapshot,
209 } };
210 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
211}
212
213TEST(AvmSimulationNoteHashTree, CheckpointListener)
214{
215 StrictMock<MockPoseidon2> poseidon2;
216 StrictMock<MockMerkleCheck> merkle_check;
217
218 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
219 NoteHashTreeCheck note_hash_tree_check(1, poseidon2, merkle_check, event_emitter);
220
221 note_hash_tree_check.on_checkpoint_created();
222 note_hash_tree_check.on_checkpoint_committed();
223 note_hash_tree_check.on_checkpoint_reverted();
224 EXPECT_THAT(event_emitter.get_events().size(), 3);
225 EXPECT_THAT(event_emitter.dump_events(),
229}
230
231} // namespace
232
233} // 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
MerkleCheck merkle_check
EventEmitter< DataCopyEvent > event_emitter
AVM range check gadget for witness generation.
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
AvmFlavorSettings::FF FF
Definition field.hpp:10
void write(B &buf, field2< base_field, Params > const &value)
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)