Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
merkle_tree.test.cpp
Go to the documentation of this file.
1#include "merkle_tree.hpp"
5#include "memory_store.hpp"
6#include "memory_tree.hpp"
7
8using namespace bb;
9using namespace bb::stdlib;
10using namespace bb::crypto::merkle_tree;
11
13
16namespace {
19} // namespace
20
21static std::vector<fr> VALUES = []() {
22 std::vector<fr> values(1024);
23 for (size_t i = 0; i < 1024; ++i) {
24 values[i] = i;
25 }
26 return values;
27}();
28
29TEST(crypto_merkle_tree, test_kv_memory_vs_memory_consistency)
30{
31 constexpr size_t depth = 10;
33
34 MemoryStore store;
36
37 std::vector<size_t> indicies(1 << depth);
38 std::iota(indicies.begin(), indicies.end(), 0);
40 std::mt19937 g(rd());
41 std::shuffle(indicies.begin(), indicies.end(), g);
42
43 for (size_t i = 0; i < indicies.size(); ++i) {
44 size_t idx = indicies[i];
45 memdb.update_element(idx, VALUES[idx]);
46 db.update_element(idx, VALUES[idx]);
47 }
48
49 for (size_t i = 0; i < indicies.size(); ++i) {
50 size_t idx = indicies[i];
51 EXPECT_EQ(db.get_hash_path(idx), memdb.get_hash_path(idx));
52 }
53
54 EXPECT_EQ(db.root(), memdb.root());
55}
56
57TEST(crypto_merkle_tree, test_size)
58{
59 MemoryStore store;
61
62 EXPECT_EQ(db.size(), 0ULL);
63
64 // Add first.
65 db.update_element(0, VALUES[1]);
66 EXPECT_EQ(db.size(), 1ULL);
67
68 // Add second.
69 db.update_element(1, VALUES[2]);
70 EXPECT_EQ(db.size(), 2ULL);
71
72 // Set second to same value.
73 db.update_element(1, VALUES[2]);
74 EXPECT_EQ(db.size(), 2ULL);
75
76 // Set second to new value.
77 db.update_element(1, VALUES[3]);
78 EXPECT_EQ(db.size(), 2ULL);
79
80 // Set third to new value.
81 db.update_element(2, VALUES[4]);
82 EXPECT_EQ(db.size(), 3ULL);
83}
84
85TEST(crypto_merkle_tree, test_get_hash_path)
86{
88
89 MemoryStore store;
91
92 EXPECT_EQ(memdb.get_hash_path(512), db.get_hash_path(512));
93
94 memdb.update_element(512, VALUES[512]);
95 db.update_element(512, VALUES[512]);
96
97 EXPECT_EQ(db.get_hash_path(512), memdb.get_hash_path(512));
98
99 for (size_t i = 0; i < 1024; ++i) {
100 memdb.update_element(i, VALUES[i]);
101 db.update_element(i, VALUES[i]);
102 }
103
104 EXPECT_EQ(db.get_hash_path(512), memdb.get_hash_path(512));
105}
106
107TEST(crypto_merkle_tree, test_get_sibling_path)
108{
110
111 MemoryStore store;
113
114 EXPECT_EQ(memdb.get_sibling_path(512), db.get_sibling_path(512));
115
116 memdb.update_element(512, VALUES[512]);
117 db.update_element(512, VALUES[512]);
118
119 EXPECT_EQ(db.get_sibling_path(512), memdb.get_sibling_path(512));
120
121 for (size_t i = 0; i < 1024; ++i) {
122 memdb.update_element(i, VALUES[i]);
123 db.update_element(i, VALUES[i]);
124 }
125
126 EXPECT_EQ(db.get_sibling_path(512), memdb.get_sibling_path(512));
127}
128
129TEST(crypto_merkle_tree, test_get_hash_path_layers)
130{
131 {
132 MemoryStore store;
134
135 auto before = db.get_hash_path(1);
136 db.update_element(0, VALUES[1]);
137 auto after = db.get_hash_path(1);
138
139 EXPECT_NE(before[0], after[0]);
140 EXPECT_NE(before[1], after[1]);
141 EXPECT_NE(before[2], after[2]);
142 }
143
144 {
145 MemoryStore store;
147
148 auto before = db.get_hash_path(7);
149 db.update_element(0x0, VALUES[1]);
150 auto after = db.get_hash_path(7);
151
152 EXPECT_EQ(before[0], after[0]);
153 EXPECT_EQ(before[1], after[1]);
154 EXPECT_NE(before[2], after[2]);
155 }
156}
157
158TEST(crypto_merkle_tree, test_get_sibling_path_layers)
159{
160 {
161 MemoryStore store;
163
164 auto before = db.get_sibling_path(1);
165 db.update_element(0, VALUES[1]);
166 auto after = db.get_sibling_path(1);
167
168 EXPECT_NE(before[0], after[0]);
169 EXPECT_EQ(before[1], after[1]);
170 EXPECT_EQ(before[2], after[2]);
171 }
172
173 {
174 MemoryStore store;
176
177 auto before = db.get_sibling_path(7);
178 db.update_element(0x0, VALUES[1]);
179 auto after = db.get_sibling_path(7);
180
181 EXPECT_EQ(before[0], after[0]);
182 EXPECT_EQ(before[1], after[1]);
183 EXPECT_NE(before[2], after[2]);
184 }
185}
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
fr_hash_path get_hash_path(index_t index)
fr update_element(index_t index, fr const &value)
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
RNG & get_randomness()
Definition engine.cpp:203
void g(field_t< Builder > state[BLAKE_STATE_SIZE], size_t a, size_t b, size_t c, size_t d, field_t< Builder > x, field_t< Builder > y, const bool last_update=false)
Entry point for Barretenberg command-line interface.
TEST(MegaCircuitBuilder, CopyConstructor)
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13