3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
16using ::testing::AllOf;
17using ::testing::ElementsAre;
18using ::testing::Field;
19using ::testing::Return;
20using ::testing::StrictMock;
26TEST(AvmSimulationNoteHashTree, Exists)
29 StrictMock<MockMerkleCheck> merkle_check;
34 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
35 AppendOnlyTreeSnapshot snapshot = {
37 .nextAvailableLeafIndex = 128,
41 uint64_t leaf_index = 30;
43 EXPECT_CALL(merkle_check, assert_membership(note_hash, leaf_index, _, snapshot.root)).WillRepeatedly(Return());
45 EXPECT_TRUE(note_hash_tree_check.note_hash_exists(note_hash, note_hash, leaf_index, sibling_path, snapshot));
46 EXPECT_FALSE(note_hash_tree_check.note_hash_exists(27, note_hash, leaf_index, sibling_path, snapshot));
49 NoteHashTreeReadWriteEvent{
50 .note_hash = note_hash,
51 .existing_leaf_value = note_hash,
52 .leaf_index = leaf_index,
53 .prev_snapshot = snapshot,
55 NoteHashTreeReadWriteEvent{
57 .existing_leaf_value = note_hash,
58 .leaf_index = leaf_index,
59 .prev_snapshot = snapshot,
63TEST(AvmSimulationNoteHashTree, WriteUnique)
66 StrictMock<MockMerkleCheck> merkle_check;
71 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
72 AppendOnlyTreeSnapshot snapshot = {
74 .nextAvailableLeafIndex = 128,
77 uint64_t note_hash_counter = 10;
78 FF next_root = 234567;
80 EXPECT_CALL(merkle_check,
write(
FF(0), note_hash, snapshot.nextAvailableLeafIndex, _, snapshot.root))
81 .WillOnce(Return(next_root));
83 AppendOnlyTreeSnapshot next_snapshot =
84 note_hash_tree_check.append_unique_note_hash(note_hash, note_hash_counter, sibling_path, snapshot);
86 EXPECT_EQ(next_snapshot.nextAvailableLeafIndex, snapshot.nextAvailableLeafIndex + 1);
87 NoteHashTreeReadWriteEvent expect_event = { .note_hash = note_hash,
88 .leaf_index = snapshot.nextAvailableLeafIndex,
89 .prev_snapshot = snapshot,
90 .append_data = NoteHashAppendData{
91 .note_hash_counter = note_hash_counter,
92 .next_snapshot = next_snapshot,
94 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
97TEST(AvmSimulationNoteHashTree, WriteSiloed)
100 StrictMock<MockMerkleCheck> merkle_check;
103 FF first_nullifier = 1;
106 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
107 AppendOnlyTreeSnapshot snapshot = {
109 .nextAvailableLeafIndex = 128,
111 FF siloed_note_hash = 42;
112 uint64_t note_hash_counter = 10;
114 FF unique_note_hash = 44;
116 FF next_root = 234567;
119 EXPECT_CALL(
poseidon2,
hash(nonce_hash_inputs)).WillOnce(Return(nonce));
122 EXPECT_CALL(
poseidon2,
hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
124 EXPECT_CALL(merkle_check,
write(
FF(0), unique_note_hash, snapshot.nextAvailableLeafIndex, _, snapshot.root))
125 .WillOnce(Return(next_root));
127 AppendOnlyTreeSnapshot next_snapshot =
128 note_hash_tree_check.append_siloed_note_hash(siloed_note_hash, note_hash_counter, sibling_path, snapshot);
130 EXPECT_EQ(next_snapshot.nextAvailableLeafIndex, snapshot.nextAvailableLeafIndex + 1);
131 NoteHashTreeReadWriteEvent expect_event = { .note_hash = siloed_note_hash,
132 .leaf_index = snapshot.nextAvailableLeafIndex,
133 .prev_snapshot = snapshot,
134 .append_data = NoteHashAppendData{
136 NoteHashUniquenessData{
138 .unique_note_hash = unique_note_hash,
139 .first_nullifier = first_nullifier,
141 .note_hash_counter = note_hash_counter,
142 .next_snapshot = next_snapshot,
144 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
147TEST(AvmSimulationNoteHashTree, WriteRaw)
150 StrictMock<MockMerkleCheck> merkle_check;
153 FF first_nullifier = 1;
156 std::vector<FF> sibling_path = { 1, 2, 3, 4, 5 };
157 AppendOnlyTreeSnapshot snapshot = {
159 .nextAvailableLeafIndex = 128,
162 FF raw_note_hash = 37;
165 FF siloed_note_hash = 42;
167 uint64_t note_hash_counter = 10;
169 FF unique_note_hash = 44;
171 FF next_root = 234567;
174 EXPECT_CALL(
poseidon2,
hash(siloed_note_hash_inputs)).WillOnce(Return(siloed_note_hash));
177 EXPECT_CALL(
poseidon2,
hash(nonce_hash_inputs)).WillOnce(Return(nonce));
180 EXPECT_CALL(
poseidon2,
hash(unique_note_hash_inputs)).WillOnce(Return(unique_note_hash));
182 EXPECT_CALL(merkle_check,
write(
FF(0), unique_note_hash, snapshot.nextAvailableLeafIndex, _, snapshot.root))
183 .WillOnce(Return(next_root));
185 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
186 raw_note_hash,
contract_address, note_hash_counter, sibling_path, snapshot);
188 EXPECT_EQ(next_snapshot.nextAvailableLeafIndex, snapshot.nextAvailableLeafIndex + 1);
189 NoteHashTreeReadWriteEvent expect_event = { .note_hash = raw_note_hash,
190 .leaf_index = snapshot.nextAvailableLeafIndex,
191 .prev_snapshot = snapshot,
192 .append_data = NoteHashAppendData{
195 .siloed_note_hash = siloed_note_hash,
199 NoteHashUniquenessData{
201 .unique_note_hash = unique_note_hash,
202 .first_nullifier = first_nullifier,
204 .note_hash_counter = note_hash_counter,
205 .next_snapshot = next_snapshot,
207 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
210TEST(AvmSimulationNoteHashTree, CheckpointListener)
213 StrictMock<MockMerkleCheck> merkle_check;
218 note_hash_tree_check.on_checkpoint_created();
219 note_hash_tree_check.on_checkpoint_committed();
220 note_hash_tree_check.on_checkpoint_reverted();
#define GENERATOR_INDEX__NOTE_HASH_NONCE
#define GENERATOR_INDEX__SILOED_NOTE_HASH
#define GENERATOR_INDEX__UNIQUE_NOTE_HASH
EventEmitter< DataCopyEvent > event_emitter
AztecAddress contract_address
void hash(State &state) noexcept
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
TEST(EmitUnencryptedLogTest, Basic)
void write(B &buf, field2< base_field, Params > const &value)