Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
memory_tree.test.cpp
Go to the documentation of this file.
1#include "memory_tree.hpp"
2#include <gtest/gtest.h>
3
4using namespace bb;
5using namespace bb::crypto::merkle_tree;
6
8
9static std::vector<fr> VALUES = []() {
10 std::vector<fr> values(4);
11 for (size_t i = 0; i < 4; ++i) {
12 values[i] = fr(i);
13 }
14 return values;
15}();
16
17TEST(crypto_merkle_tree, test_memory_store)
18{
19 fr e00 = 0;
20 fr e01 = VALUES[1];
21 fr e02 = VALUES[2];
22 fr e03 = VALUES[3];
23 fr e10 = HashPolicy::hash_pair(e00, e01);
24 fr e11 = HashPolicy::hash_pair(e02, e03);
25 fr root = HashPolicy::hash_pair(e10, e11);
26
28 for (size_t i = 0; i < 4; ++i) {
29 db.update_element(i, VALUES[i]);
30 }
31 fr_hash_path expected = {
32 std::make_pair(e00, e01),
33 std::make_pair(e10, e11),
34 };
35 EXPECT_EQ(db.get_hash_path(0), expected);
36 EXPECT_EQ(db.get_hash_path(1), expected);
37
38 expected = {
39 std::make_pair(e02, e03),
40 std::make_pair(e10, e11),
41 };
42
43 EXPECT_EQ(db.get_hash_path(2), expected);
44 EXPECT_EQ(db.get_hash_path(3), expected);
45 EXPECT_EQ(db.root(), root);
46}
47
48TEST(crypto_merkle_tree, test_memory_store_sibling_path)
49{
50 fr e00 = 0;
51 fr e01 = VALUES[1];
52 fr e02 = VALUES[2];
53 fr e03 = VALUES[3];
54 fr e10 = HashPolicy::hash_pair(e00, e01);
55 fr e11 = HashPolicy::hash_pair(e02, e03);
56 fr root = HashPolicy::hash_pair(e10, e11);
57
59 for (size_t i = 0; i < 4; ++i) {
60 db.update_element(i, VALUES[i]);
61 }
62
63 // Check correct paths are generated for each layer 0 element
64 fr_sibling_path expected00 = {
65 e01,
66 e11,
67 };
68 fr_sibling_path expected01 = { e00, e11 };
69 fr_sibling_path expected02 = {
70 e03,
71 e10,
72 };
73 fr_sibling_path expected03 = {
74 e02,
75 e10,
76 };
77 EXPECT_EQ(db.get_sibling_path(0), expected00);
78 EXPECT_EQ(db.get_sibling_path(1), expected01);
79 EXPECT_EQ(db.get_sibling_path(2), expected02);
80 EXPECT_EQ(db.get_sibling_path(3), expected03);
81 EXPECT_EQ(db.root(), root);
82}
fr update_element(size_t index, fr const &value)
fr_hash_path get_hash_path(size_t index) const
fr_sibling_path get_sibling_path(size_t index) const
std::vector< std::pair< fr, fr > > fr_hash_path
Definition hash_path.hpp:15
std::vector< fr > fr_sibling_path
Definition hash_path.hpp:16
Entry point for Barretenberg command-line interface.
TEST(MegaCircuitBuilder, CopyConstructor)
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static fr hash_pair(const fr &lhs, const fr &rhs)
Definition hash.hpp:24