3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
21 LeafValue(
const FF& key)
25 static bool is_updateable() {
return false; }
27 bool operator==(LeafValue
const& other)
const {
return key == other.key; }
33 std::vector<fr> get_hash_inputs(
fr nextKey,
fr nextIndex)
const
35 return std::vector<fr>({
key, nextKey, nextIndex });
38 static LeafValue empty() {
return {
fr::zero() }; }
40 static LeafValue padding(
index_t i) {
return { i }; }
42 static std::string name() {
return "LeafValue"; }
44 [[maybe_unused]]
friend std::ostream&
operator<<(std::ostream& os,
const LeafValue& v)
46 os <<
"key = " << v.key;
51struct UpdatableLeafValue {
55 UpdatableLeafValue(
const FF& key,
const FF& value)
60 static bool is_updateable() {
return true; }
62 bool operator==(UpdatableLeafValue
const& other)
const {
return key == other.key &&
value == other.value; }
68 std::vector<fr> get_hash_inputs(
fr nextKey,
fr nextIndex)
const
70 return std::vector<fr>({
key,
value, nextKey, nextIndex });
75 static UpdatableLeafValue padding(
index_t i) {
return { i,
fr::zero() }; }
77 static std::string name() {
return "UpdatableLeafValue"; }
79 [[maybe_unused]]
friend std::ostream&
operator<<(std::ostream& os,
const UpdatableLeafValue& v)
81 os <<
"key = " << v.key <<
" : value = " << v.value;
86using Tree = IndexedMemoryTree<LeafValue, aztec::NullifierMerkleHashPolicy>;
87using UpdatableTree = IndexedMemoryTree<UpdatableLeafValue, aztec::NullifierMerkleHashPolicy>;
89TEST(IndexedMemoryTree, Append)
92 auto prev_snapshot = tree.get_snapshot();
95 auto result = tree.insert_indexed_leaves({ { leaf } });
97 auto snapshot_after = tree.get_snapshot();
99 EXPECT_EQ(result.insertion_witness_data.size(), 1);
100 EXPECT_EQ(result.low_leaf_witness_data.size(), 1);
106 EXPECT_EQ(low_leaf_witness_data.
index, 0);
110 EXPECT_EQ(low_leaf_witness_data.
leaf, padding_leaf);
118 padding_leaf.nextIndex = 1;
119 padding_leaf.nextKey = leaf.key;
131 EXPECT_EQ(insertion_witness_data.
leaf, inserted_leaf);
136 EXPECT_EQ(snapshot_after.root, final_root);
137 EXPECT_EQ(snapshot_after.next_available_leaf_index, 2);
140TEST(IndexedMemoryTree, Update)
142 UpdatableTree tree(5, 1);
143 auto prev_snapshot = tree.get_snapshot();
145 UpdatableLeafValue leaf(1, 43);
146 auto result = tree.insert_indexed_leaves({ { leaf } });
148 auto snapshot_after = tree.get_snapshot();
150 EXPECT_EQ(result.insertion_witness_data.size(), 1);
151 EXPECT_EQ(result.low_leaf_witness_data.size(), 1);
157 EXPECT_EQ(low_leaf_witness_data.
index, 0);
161 EXPECT_EQ(low_leaf_witness_data.
leaf, padding_leaf);
169 padding_leaf.leaf.value = leaf.value;
175 EXPECT_EQ(snapshot_after.root, intermediate_root);
176 EXPECT_EQ(snapshot_after.next_available_leaf_index, 1);
179TEST(IndexedMemoryTree, GetLeaves)
186 for (
size_t i = 10; i < 20; i++) {
187 leaves.push_back(LeafValue(i * 10));
190 tree.insert_indexed_leaves(leaves);
205TEST(IndexedMemoryTree, GetSiblingPath)
209 tree.insert_indexed_leaves({ { leaf } });
211 auto path = tree.get_sibling_path(1);
213 EXPECT_EQ(path.size(), 5);
216 tree.get_snapshot().root);
219TEST(IndexedMemoryTree, Full)
222 tree.insert_indexed_leaves({ { LeafValue(100) } });
223 tree.insert_indexed_leaves({ { LeafValue(110) } });
224 tree.insert_indexed_leaves({ { LeafValue(120) } });
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
#define DOM_SEP__NULLIFIER_MERKLE
Native Poseidon2 hash function implementation.
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
AVM range check gadget for witness generation.
FF unconstrained_root_from_path(uint64_t domain_separator, const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
std::ostream & operator<<(std::ostream &os, const CoarseTransactionPhase &phase)
bool is_empty(const LeafType &leaf)
bool operator==(schnorr_signature const &lhs, schnorr_signature const &rhs)
Key get_key(int64_t keyCount)
std::variant< TreeWithStore< FrTree >, TreeWithStore< NullifierTree >, TreeWithStore< PublicDataTree > > Tree
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
IndexedLeaf< LeafType > leaf
BB_INLINE constexpr bool is_zero() const noexcept
static constexpr field zero()