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(note_hash, leaf_index, _, snapshot.root)).WillRepeatedly(Return());
41
42 EXPECT_TRUE(note_hash_tree_check.note_hash_exists(note_hash, note_hash, leaf_index, sibling_path, snapshot));
43 EXPECT_FALSE(note_hash_tree_check.note_hash_exists(27, note_hash, leaf_index, sibling_path, snapshot));
44 EXPECT_THAT(event_emitter.dump_events(),
45 ElementsAre(
46 NoteHashTreeReadWriteEvent{
47 .note_hash = note_hash,
48 .existing_leaf_value = note_hash,
49 .leaf_index = leaf_index,
50 .prev_snapshot = snapshot,
51 },
52 NoteHashTreeReadWriteEvent{
53 .note_hash = 27,
54 .existing_leaf_value = note_hash,
55 .leaf_index = leaf_index,
56 .prev_snapshot = snapshot,
57 }));
58}
59
60TEST(AvmSimulationNoteHashTree, WriteUnique)
61{
62 StrictMock<MockPoseidon2> poseidon2;
63 StrictMock<MockMerkleCheck> merkle_check;
64
65 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
66 NoteHashTreeCheck note_hash_tree_check(1, poseidon2, merkle_check, event_emitter);
67
68 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
69 AppendOnlyTreeSnapshot snapshot = {
70 .root = 123456,
71 .next_available_leaf_index = 128,
72 };
73 FF note_hash = 42;
74 uint64_t note_hash_counter = 10;
75 FF next_root = 234567;
76
77 EXPECT_CALL(merkle_check, write(FF(0), note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
78 .WillOnce(Return(next_root));
79
80 AppendOnlyTreeSnapshot next_snapshot =
81 note_hash_tree_check.append_unique_note_hash(note_hash, note_hash_counter, sibling_path, snapshot);
82
83 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
84 NoteHashTreeReadWriteEvent expect_event = { .note_hash = note_hash,
85 .leaf_index = snapshot.next_available_leaf_index,
86 .prev_snapshot = snapshot,
87 .append_data = NoteHashAppendData{
88 .note_hash_counter = note_hash_counter,
89 .next_snapshot = next_snapshot,
90 } };
91 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
92}
93
94TEST(AvmSimulationNoteHashTree, WriteSiloed)
95{
96 StrictMock<MockPoseidon2> poseidon2;
97 StrictMock<MockMerkleCheck> merkle_check;
98
99 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
101 NoteHashTreeCheck note_hash_tree_check(first_nullifier, poseidon2, merkle_check, event_emitter);
102
103 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
104 AppendOnlyTreeSnapshot snapshot = {
105 .root = 123456,
106 .next_available_leaf_index = 128,
107 };
108 FF siloed_note_hash = 42;
109 uint64_t note_hash_counter = 10;
110 FF nonce = 43;
111 FF unique_note_hash = 44;
112
113 FF next_root = 234567;
114
115 std::vector<FF> nonce_hash_inputs = { DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter };
116 EXPECT_CALL(poseidon2, hash(nonce_hash_inputs)).WillOnce(Return(nonce));
117
118 std::vector<FF> unique_note_hash_inputs = { DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash };
119 EXPECT_CALL(poseidon2, hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
120
121 EXPECT_CALL(merkle_check, write(FF(0), unique_note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
122 .WillOnce(Return(next_root));
123
124 AppendOnlyTreeSnapshot next_snapshot =
125 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, sibling_path, snapshot);
126
127 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
128 NoteHashTreeReadWriteEvent expect_event = { .note_hash = siloed_note_hash,
129 .leaf_index = snapshot.next_available_leaf_index,
130 .prev_snapshot = snapshot,
131 .append_data = NoteHashAppendData{
132 .uniqueness_data =
133 NoteHashUniquenessData{
134 .nonce = nonce,
135 .unique_note_hash = unique_note_hash,
136 .first_nullifier = first_nullifier,
137 },
138 .note_hash_counter = note_hash_counter,
139 .next_snapshot = next_snapshot,
140 } };
141 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
142}
143
144TEST(AvmSimulationNoteHashTree, WriteRaw)
145{
146 StrictMock<MockPoseidon2> poseidon2;
147 StrictMock<MockMerkleCheck> merkle_check;
148
149 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
151 NoteHashTreeCheck note_hash_tree_check(first_nullifier, poseidon2, merkle_check, event_emitter);
152
153 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
154 AppendOnlyTreeSnapshot snapshot = {
155 .root = 123456,
156 .next_available_leaf_index = 128,
157 };
158
159 FF raw_note_hash = 37;
160
161 AztecAddress contract_address = AztecAddress(27);
162 FF siloed_note_hash = 42;
163
164 uint64_t note_hash_counter = 10;
165 FF nonce = 43;
166 FF unique_note_hash = 44;
167
168 FF next_root = 234567;
169
170 std::vector<FF> siloed_note_hash_inputs = { DOM_SEP__SILOED_NOTE_HASH, contract_address, raw_note_hash };
171 EXPECT_CALL(poseidon2, hash(siloed_note_hash_inputs)).WillOnce(Return(siloed_note_hash));
172
173 std::vector<FF> nonce_hash_inputs = { DOM_SEP__NOTE_HASH_NONCE, first_nullifier, note_hash_counter };
174 EXPECT_CALL(poseidon2, hash(nonce_hash_inputs)).WillOnce(Return(nonce));
175
176 std::vector<FF> unique_note_hash_inputs = { DOM_SEP__UNIQUE_NOTE_HASH, nonce, siloed_note_hash };
177 EXPECT_CALL(poseidon2, hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
178
179 EXPECT_CALL(merkle_check, write(FF(0), unique_note_hash, snapshot.next_available_leaf_index, _, snapshot.root))
180 .WillOnce(Return(next_root));
181
182 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
183 raw_note_hash, contract_address, note_hash_counter, sibling_path, snapshot);
184
185 EXPECT_EQ(next_snapshot.next_available_leaf_index, snapshot.next_available_leaf_index + 1);
186 NoteHashTreeReadWriteEvent expect_event = { .note_hash = raw_note_hash,
187 .leaf_index = snapshot.next_available_leaf_index,
188 .prev_snapshot = snapshot,
189 .append_data = NoteHashAppendData{
190 .siloing_data =
191 NoteHashSiloingData{
192 .siloed_note_hash = siloed_note_hash,
193 .address = contract_address,
194 },
195 .uniqueness_data =
196 NoteHashUniquenessData{
197 .nonce = nonce,
198 .unique_note_hash = unique_note_hash,
199 .first_nullifier = first_nullifier,
200 },
201 .note_hash_counter = note_hash_counter,
202 .next_snapshot = next_snapshot,
203 } };
204 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
205}
206
207TEST(AvmSimulationNoteHashTree, CheckpointListener)
208{
209 StrictMock<MockPoseidon2> poseidon2;
210 StrictMock<MockMerkleCheck> merkle_check;
211
212 EventEmitter<NoteHashTreeCheckEvent> event_emitter;
213 NoteHashTreeCheck note_hash_tree_check(1, poseidon2, merkle_check, event_emitter);
214
215 note_hash_tree_check.on_checkpoint_created();
216 note_hash_tree_check.on_checkpoint_committed();
217 note_hash_tree_check.on_checkpoint_reverted();
218 EXPECT_THAT(event_emitter.get_events().size(), 3);
219 EXPECT_THAT(event_emitter.dump_events(),
223}
224
225} // namespace
226
227} // namespace bb::avm2::simulation
#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)