3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
20using ::testing::ElementsAre;
21using ::testing::Return;
22using ::testing::StrictMock;
28TEST(AvmSimulationRetrievedBytecodesTreeCheck, ContainsNotExists)
31 StrictMock<MockMerkleCheck> merkle_check;
32 StrictMock<MockFieldGreaterThan>
field_gt;
40 ASSERT_EQ(initial_state.get_snapshot().nextAvailableLeafIndex, 1);
41 uint64_t low_leaf_index = 0;
46 auto sibling_path = initial_state.get_sibling_path(0);
47 auto snapshot = initial_state.get_snapshot();
53 EXPECT_CALL(merkle_check, assert_membership(low_leaf_hash, low_leaf_index, _, snapshot.root))
54 .WillRepeatedly(Return());
59 RetrievedBytecodesTreeCheckEvent expect_event = {
61 .prev_snapshot = snapshot,
62 .next_snapshot = snapshot,
64 .low_leaf_hash = low_leaf_hash,
65 .low_leaf_index = low_leaf_index,
67 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
70TEST(AvmSimulationRetrievedBytecodesTree, ContainsExists)
73 StrictMock<MockMerkleCheck> merkle_check;
74 StrictMock<MockFieldGreaterThan>
field_gt;
81 uint64_t low_leaf_index = initial_state.get_snapshot().nextAvailableLeafIndex;
82 initial_state.insert_indexed_leaves({ { ClassIdLeafValue(
class_id) } });
86 std::vector<FF> sibling_path = initial_state.get_sibling_path(low_leaf_index);
87 auto snapshot = initial_state.get_snapshot();
93 EXPECT_CALL(merkle_check, assert_membership(low_leaf_hash, low_leaf_index, _, snapshot.root))
94 .WillRepeatedly(Return());
98 RetrievedBytecodesTreeCheckEvent expect_event = {
100 .prev_snapshot = snapshot,
101 .next_snapshot = snapshot,
103 .low_leaf_hash = low_leaf_hash,
104 .low_leaf_index = low_leaf_index,
106 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
109TEST(AvmSimulationRetrievedBytecodesTree, ReadNotExistsLowPointsToAnotherLeaf)
112 StrictMock<MockMerkleCheck> merkle_check;
113 StrictMock<MockFieldGreaterThan>
field_gt;
115 EventEmitter<RetrievedBytecodesTreeCheckEvent>
event_emitter;
119 initial_state.insert_indexed_leaves({ { ClassIdLeafValue(FF::neg_one()) } });
123 auto low_leaf = initial_state.get_leaf_preimage(0);
126 uint64_t low_leaf_index = 0;
127 std::vector<FF> sibling_path = initial_state.get_sibling_path(low_leaf_index);
128 AppendOnlyTreeSnapshot snapshot = initial_state.get_snapshot();
134 EXPECT_CALL(merkle_check, assert_membership(low_leaf_hash, low_leaf_index, _, snapshot.root))
135 .WillRepeatedly(Return());
141 RetrievedBytecodesTreeCheckEvent expect_event = {
143 .prev_snapshot = snapshot,
144 .next_snapshot = snapshot,
146 .low_leaf_hash = low_leaf_hash,
147 .low_leaf_index = low_leaf_index,
149 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
152TEST(AvmSimulationRetrievedBytecodesTree, InsertExists)
155 StrictMock<MockMerkleCheck> merkle_check;
156 StrictMock<MockFieldGreaterThan>
field_gt;
158 EventEmitter<RetrievedBytecodesTreeCheckEvent>
event_emitter;
163 uint64_t low_leaf_index = initial_state.get_snapshot().nextAvailableLeafIndex;
164 initial_state.insert_indexed_leaves({ { ClassIdLeafValue(
class_id) } });
168 std::vector<FF> sibling_path = initial_state.get_sibling_path(low_leaf_index);
169 AppendOnlyTreeSnapshot snapshot = initial_state.get_snapshot();
175 EXPECT_CALL(merkle_check, assert_membership(low_leaf_hash, low_leaf_index, _, snapshot.root))
176 .WillRepeatedly(Return());
182 RetrievedBytecodesTreeCheckEvent expect_event = {
184 .prev_snapshot = snapshot,
185 .next_snapshot = snapshot,
187 .low_leaf_hash = low_leaf_hash,
188 .low_leaf_index = low_leaf_index,
191 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
194TEST(AvmSimulationRetrievedBytecodesTree, InsertAppend)
197 StrictMock<MockMerkleCheck> merkle_check;
198 StrictMock<MockFieldGreaterThan>
field_gt;
200 EventEmitter<RetrievedBytecodesTreeCheckEvent>
event_emitter;
206 ASSERT_EQ(initial_state.get_snapshot().nextAvailableLeafIndex, 1);
207 uint64_t low_leaf_index = 0;
208 uint64_t new_leaf_index = 1;
215 std::vector<FF> low_leaf_sibling_path = initial_state.get_sibling_path(low_leaf_index);
218 updated_low_leaf.
nextIndex = new_leaf_index;
219 updated_low_leaf.nextKey =
class_id;
223 std::vector<FF> insertion_sibling_path = state_after_insert.get_sibling_path(new_leaf_index);
232 EXPECT_CALL(
poseidon2,
hash(_)).WillRepeatedly([](
const std::vector<FF>& input) {
235 EXPECT_CALL(merkle_check,
236 write(low_leaf_hash, updated_low_leaf_hash, low_leaf_index, _, initial_state.get_snapshot().root))
237 .WillRepeatedly(Return(intermediate_root));
240 EXPECT_CALL(merkle_check,
write(
FF(0), new_leaf_hash, new_leaf_index, _, intermediate_root))
241 .WillRepeatedly(Return(state_after_insert.get_snapshot().root));
247 RetrievedBytecodesTreeCheckEvent expect_event = { .class_id =
class_id,
248 .prev_snapshot = initial_state.get_snapshot(),
249 .next_snapshot = state_after_insert.get_snapshot(),
251 .low_leaf_hash = low_leaf_hash,
252 .low_leaf_index = low_leaf_index,
254 .append_data = RetrievedBytecodeAppendData{
255 .updated_low_leaf_hash = updated_low_leaf_hash,
256 .new_leaf_hash = new_leaf_hash,
257 .intermediate_root = intermediate_root,
260 EXPECT_THAT(
event_emitter.dump_events(), ElementsAre(expect_event));
StrictMock< MockRetrievedBytecodesTreeCheck > retrieved_bytecodes_tree_check
SequentialInsertionResult< LeafType > insert_indexed_leaves(std::span< const LeafType > leaves)
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
EventEmitter< DataCopyEvent > event_emitter
NullifierTreeLeafPreimage low_leaf
void hash(State &state) noexcept
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
IndexedMemoryTree< ClassIdLeafValue, Poseidon2HashPolicy > RetrievedBytecodesTree
TEST(EmitUnencryptedLogTest, Basic)
IndexedLeaf< ClassIdLeafValue > RetrievedBytecodesTreeLeafPreimage
RetrievedBytecodesTree build_retrieved_bytecodes_tree()
FF unconstrained_root_from_path(const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
void write(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)
FieldGreaterThan field_gt
std::vector< fr > get_hash_inputs() const