3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
20 LeafValue(
const FF& key)
24 static bool is_updateable() {
return false; }
26 bool operator==(LeafValue
const& other)
const {
return key == other.key; }
32 std::vector<fr> get_hash_inputs(
fr nextKey,
fr nextIndex)
const
34 return std::vector<fr>({
key, nextKey, nextIndex });
37 static LeafValue empty() {
return {
fr::zero() }; }
39 static LeafValue padding(
index_t i) {
return { i }; }
41 static std::string name() {
return "LeafValue"; }
43 [[maybe_unused]]
friend std::ostream&
operator<<(std::ostream& os,
const LeafValue& v)
45 os <<
"key = " << v.key;
50struct UpdatableLeafValue {
54 UpdatableLeafValue(
const FF& key,
const FF& value)
59 static bool is_updateable() {
return true; }
61 bool operator==(UpdatableLeafValue
const& other)
const {
return key == other.key &&
value == other.value; }
67 std::vector<fr> get_hash_inputs(
fr nextKey,
fr nextIndex)
const
69 return std::vector<fr>({
key,
value, nextKey, nextIndex });
74 static UpdatableLeafValue padding(
index_t i) {
return { i,
fr::zero() }; }
76 static std::string name() {
return "UpdatableLeafValue"; }
78 [[maybe_unused]]
friend std::ostream&
operator<<(std::ostream& os,
const UpdatableLeafValue& v)
80 os <<
"key = " << v.key <<
" : value = " << v.value;
85using Tree = IndexedMemoryTree<LeafValue, Poseidon2HashPolicy>;
86using UpdatableTree = IndexedMemoryTree<UpdatableLeafValue, Poseidon2HashPolicy>;
88TEST(IndexedMemoryTree, Append)
91 auto prev_snapshot = tree.get_snapshot();
94 auto result = tree.insert_indexed_leaves({ { leaf } });
96 auto snapshot_after = tree.get_snapshot();
98 EXPECT_EQ(result.insertion_witness_data.size(), 1);
99 EXPECT_EQ(result.low_leaf_witness_data.size(), 1);
105 EXPECT_EQ(low_leaf_witness_data.
index, 0);
109 EXPECT_EQ(low_leaf_witness_data.
leaf, padding_leaf);
116 padding_leaf.nextIndex = 1;
117 padding_leaf.nextKey = leaf.key;
119 auto intermediate_root =
128 EXPECT_EQ(insertion_witness_data.
leaf, inserted_leaf);
133 EXPECT_EQ(snapshot_after.root, final_root);
134 EXPECT_EQ(snapshot_after.nextAvailableLeafIndex, 2);
137TEST(IndexedMemoryTree, Update)
139 UpdatableTree tree(5, 1);
140 auto prev_snapshot = tree.get_snapshot();
142 UpdatableLeafValue leaf(1, 43);
143 auto result = tree.insert_indexed_leaves({ { leaf } });
145 auto snapshot_after = tree.get_snapshot();
147 EXPECT_EQ(result.insertion_witness_data.size(), 1);
148 EXPECT_EQ(result.low_leaf_witness_data.size(), 1);
154 EXPECT_EQ(low_leaf_witness_data.
index, 0);
158 EXPECT_EQ(low_leaf_witness_data.
leaf, padding_leaf);
165 padding_leaf.leaf.value = leaf.value;
167 auto intermediate_root =
171 EXPECT_EQ(snapshot_after.root, intermediate_root);
172 EXPECT_EQ(snapshot_after.nextAvailableLeafIndex, 1);
175TEST(IndexedMemoryTree, GetLeaves)
182 for (
size_t i = 10; i < 20; i++) {
183 leaves.push_back(LeafValue(i * 10));
186 tree.insert_indexed_leaves(leaves);
201TEST(IndexedMemoryTree, GetSiblingPath)
205 tree.insert_indexed_leaves({ { leaf } });
207 auto path = tree.get_sibling_path(1);
209 EXPECT_EQ(path.size(), 5);
211 tree.get_snapshot().root);
214TEST(IndexedMemoryTree, Full)
217 tree.insert_indexed_leaves({ { LeafValue(100) } });
218 tree.insert_indexed_leaves({ { LeafValue(110) } });
219 tree.insert_indexed_leaves({ { LeafValue(120) } });
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
Implements a parallelized batch insertion indexed tree Accepts template argument of the type of store...
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
TEST(EmitUnencryptedLogTest, Basic)
FF unconstrained_root_from_path(const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
std::ostream & operator<<(std::ostream &os, const WrittenPublicDataSlotLeafValue &v)
bool is_empty(const LeafType &leaf)
bool operator==(ecdsa_signature const &lhs, ecdsa_signature const &rhs)
Key get_key(int64_t keyCount)
std::variant< TreeWithStore< FrTree >, TreeWithStore< NullifierTree >, TreeWithStore< PublicDataTree > > Tree
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
IndexedLeaf< LeafType > leaf
static constexpr field zero()