Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
nullifier_tree.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#include "nullifier_tree.hpp"
8#include "../hash.hpp"
9#include "../memory_store.hpp"
10#include "../merkle_tree.hpp"
16#include <iostream>
17#include <sstream>
18
20
21template <typename Store, typename HashingPolicy>
22NullifierTree<Store, HashingPolicy>::NullifierTree(Store& store, size_t depth, size_t initial_size, uint8_t tree_id)
23 : MerkleTree<Store, HashingPolicy>(store, depth, tree_id)
24{
26 BB_ASSERT_LTE(depth, 256U);
27 BB_ASSERT_GT(initial_size, 0U);
28 zero_hashes_.resize(depth);
29
30 // Create the zero hashes for the tree
31 auto current = fr::zero();
32 for (size_t i = 0; i < depth; ++i) {
33 zero_hashes_[i] = current;
34 current = HashingPolicy::hash_pair(current, current);
35 }
36
37 // Insert the initial leaves
38 for (size_t i = 0; i < initial_size; i++) {
39 auto initial_leaf = WrappedNullifierLeaf<HashingPolicy>(
40 indexed_nullifier_leaf{ .value = i, .nextIndex = i + 1, .nextValue = i + 1 });
41 leaves.push_back(initial_leaf);
42 }
43
45 indexed_nullifier_leaf{ .value = leaves[initial_size - 1].unwrap().value, .nextIndex = 0, .nextValue = 0 });
46
47 for (size_t i = 0; i < initial_size; ++i) {
48 update_element(i, leaves[i].hash());
49 }
50}
51
52template <typename Store, typename HashingPolicy>
56
57template <typename Store, typename HashingPolicy> NullifierTree<Store, HashingPolicy>::~NullifierTree() {}
58
59template <typename Store, typename HashingPolicy>
61{
62 // Find the leaf with the value closest and less than `value`
63 size_t current;
64 bool is_already_present;
65 std::tie(current, is_already_present) = find_closest_leaf(leaves, value);
66
67 indexed_nullifier_leaf current_leaf = leaves[current].unwrap();
69 { .value = value, .nextIndex = current_leaf.nextIndex, .nextValue = current_leaf.nextValue });
70 if (!is_already_present) {
71 // Update the current leaf to point it to the new leaf
72 current_leaf.nextIndex = leaves.size();
73 current_leaf.nextValue = value;
74
75 leaves[current].set(current_leaf);
76
77 // Insert the new leaf with (nextIndex, nextValue) of the current leaf
78 leaves.push_back(new_leaf);
79 }
80
81 // Update the old leaf in the tree
82 auto old_leaf_hash = leaves[current].hash();
83 index_t old_leaf_index = current;
84 auto r = update_element(old_leaf_index, old_leaf_hash);
85
86 // Insert the new leaf in the tree
87 auto new_leaf_hash = new_leaf.hash();
88 index_t new_leaf_index = is_already_present ? old_leaf_index : leaves.size() - 1;
89 r = update_element(new_leaf_index, new_leaf_hash);
90
91 return r;
92}
93
95
96} // namespace bb::crypto::merkle_tree
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:101
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:87
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:129
NullifierTree(Store &store, size_t depth, size_t initial_size=1, uint8_t tree_id=0)
std::vector< WrappedNullifierLeaf< HashingPolicy > > leaves
Wrapper for the Nullifier leaf class that allows for 0 values.
bb::fr hash() const
Return the hash of the wrapped object, other return the zero hash of 0.
ContentAddressedCachedTreeStore< bb::fr > Store
void hash(State &state) noexcept
std::pair< size_t, bool > find_closest_leaf(std::vector< WrappedNullifierLeaf< HashingPolicy > > const &leaves_, fr const &new_value)
STL namespace.
static constexpr field zero()