Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
nullifier_tree.test.cpp
Go to the documentation of this file.
1#include "nullifier_tree.hpp"
2#include "../memory_store.hpp"
7
8using namespace bb;
9using namespace bb::crypto::merkle_tree;
10
11namespace {
14} // namespace
15
16const size_t NUM_VALUES = 1024;
17static std::vector<fr> VALUES = []() {
18 std::vector<fr> values(NUM_VALUES);
19 for (size_t i = 0; i < NUM_VALUES; ++i) {
20 values[i] = fr(random_engine.get_random_uint256());
21 }
22 return values;
23}();
24
25inline void print_tree(const size_t depth, std::vector<fr> hashes, std::string const& msg)
26{
27 info("\n", msg);
28 size_t offset = 0;
29 for (size_t i = 0; i < depth; i++) {
30 info("i = ", i);
31 size_t layer_size = (1UL << (depth - i));
32 for (size_t j = 0; j < layer_size; j++) {
33 info("j = ", j, ": ", hashes[offset + j]);
34 }
35 offset += layer_size;
36 }
37}
38
39TEST(stdlib_nullifier_tree, test_kv_memory_vs_memory_consistency)
40{
41 constexpr size_t depth = 2;
42 constexpr size_t prefill = 2;
43 NullifierMemoryTree<Poseidon2HashPolicy> memdb(depth, prefill);
44
45 MemoryStore store;
47
48 EXPECT_EQ(db.root(), memdb.root());
49
50 std::vector<size_t> indicies((1 << depth) - prefill);
51 std::iota(indicies.begin(), indicies.end(), 0);
53 std::mt19937 g(rd());
54 std::shuffle(indicies.begin(), indicies.end(), g);
55
56 for (size_t i = 0; i < indicies.size(); ++i) {
57 size_t idx = indicies[i];
58 memdb.update_element(VALUES[idx]);
59 db.update_element(VALUES[idx]);
60 }
61
62 for (size_t i = 0; i < indicies.size(); ++i) {
63 size_t idx = indicies[i];
64 EXPECT_EQ(db.get_hash_path(idx), memdb.get_hash_path(idx));
65 }
66
67 EXPECT_EQ(db.root(), memdb.root());
68}
69
70TEST(stdlib_nullifier_tree, test_size)
71{
72 MemoryStore store;
74
75 // We assume that the first leaf is already filled with (0, 0, 0).
76 EXPECT_EQ(db.size(), 1ULL);
77
78 // Add a new non-zero leaf at index 1.
79 db.update_element(30);
80 EXPECT_EQ(db.size(), 2ULL);
81
82 // Add third.
83 db.update_element(10);
84 EXPECT_EQ(db.size(), 3ULL);
85
86 // Add forth.
87 db.update_element(20);
88 EXPECT_EQ(db.size(), 4ULL);
89
90 // Add forth but with same value.
91 db.update_element(20);
92 EXPECT_EQ(db.size(), 4ULL);
93}
94
95TEST(stdlib_nullifier_tree, test_get_hash_path)
96{
98
99 MemoryStore store;
101
102 EXPECT_EQ(memdb.get_hash_path(512), db.get_hash_path(512));
103
104 memdb.update_element(VALUES[512]);
105 db.update_element(VALUES[512]);
106
107 EXPECT_EQ(db.get_hash_path(512), memdb.get_hash_path(512));
108
109 for (size_t i = 0; i < 512; ++i) {
110 memdb.update_element(VALUES[i]);
111 db.update_element(VALUES[i]);
112 }
113
114 EXPECT_EQ(db.get_hash_path(512), memdb.get_hash_path(512));
115}
116
117TEST(stdlib_nullifier_tree, test_get_hash_path_layers)
118{
119 {
120 MemoryStore store;
122
123 auto before = db.get_hash_path(1);
124 db.update_element(VALUES[1]);
125 auto after = db.get_hash_path(1);
126
127 EXPECT_NE(before[0], after[0]);
128 EXPECT_NE(before[1], after[1]);
129 EXPECT_NE(before[2], after[2]);
130 }
131
132 {
133 MemoryStore store;
135
136 auto before = db.get_hash_path(7);
137 db.update_element(VALUES[1]);
138 auto after = db.get_hash_path(7);
139
140 EXPECT_EQ(before[0], after[0]);
141 EXPECT_EQ(before[1], after[1]);
142 EXPECT_NE(before[2], after[2]);
143 }
144}
fr_hash_path get_hash_path(size_t index) const
fr_hash_path get_hash_path(index_t index)
void info(Args... args)
Definition log.hpp:70
ssize_t offset
Definition engine.cpp:36
const uint32_t NUM_VALUES
Definition fixtures.hpp:21
void print_tree(const uint32_t depth, std::vector< fr > hashes, std::string const &msg)
Definition fixtures.hpp:49
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
RNG & get_randomness()
Definition engine.cpp:203
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