Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
nullifier_leaf.hpp
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#pragma once
12
14
19
20 // For serialization, update with any new fields
22 bool operator==(indexed_nullifier_leaf const&) const = default;
23
24 std::ostream& operator<<(std::ostream& os)
25 {
26 os << "value = " << value << "\nnextIdx = " << nextIndex << "\nnextVal = " << nextValue;
27 return os;
28 }
29
30 std::vector<fr> get_hash_inputs() const { return std::vector<fr>{ value, nextValue, nextIndex }; }
31
33 {
34 return indexed_nullifier_leaf{ .value = 0, .nextIndex = 0, .nextValue = 0 };
35 }
36};
37
42template <typename HashingPolicy> class WrappedNullifierLeaf {
43
44 public:
45 // Initialize with a nullifier leaf
49 // Initialize an empty leaf
51 : data(std::nullopt)
52 {}
53
54 bool operator==(WrappedNullifierLeaf const&) const = default;
55
62 bool has_value() const { return data.has_value(); }
63
70
77
83 bb::fr hash() const
84 {
85 return data.has_value() ? HashingPolicy::hash(data.value().get_hash_inputs()) : bb::fr::zero();
86 }
87
94
95 private:
96 // Underlying data
98};
99
100template <typename HashingPolicy>
102 fr const& new_value)
103{
105 bool repeated = false;
106 auto new_value_ = uint256_t(new_value);
107
108 for (size_t i = 0; i < leaves_.size(); i++) {
109
110 if (!leaves_[i].has_value()) {
111 diff.push_back(new_value_);
112 continue;
113 }
114
115 auto leaf_value_ = uint256_t(leaves_[i].unwrap().value);
116 if (leaf_value_ > new_value_) {
117 diff.push_back(leaf_value_);
118 } else if (leaf_value_ == new_value_) {
119 repeated = true;
120 return std::make_pair(i, repeated);
121 } else {
122 diff.push_back(new_value_ - leaf_value_);
123 }
124 }
125 auto it = std::min_element(diff.begin(), diff.end());
126 return std::make_pair(static_cast<size_t>(it - diff.begin()), repeated);
127}
128
129} // namespace bb::crypto::merkle_tree
Wrapper for the Nullifier leaf class that allows for 0 values.
bool has_value() const
Pass through the underlying std::optional method.
bool operator==(WrappedNullifierLeaf const &) const =default
indexed_nullifier_leaf unwrap() const
Return the wrapped nullifier_leaf object.
WrappedNullifierLeaf(indexed_nullifier_leaf value)
std::optional< indexed_nullifier_leaf > data
static WrappedNullifierLeaf< HashingPolicy > zero()
Generate a zero leaf (call the constructor with no arguments)
void set(indexed_nullifier_leaf value)
Set the wrapped nullifier_leaf object value.
bb::fr hash() const
Return the hash of the wrapped object, other return the zero hash of 0.
std::pair< size_t, bool > find_closest_leaf(std::vector< WrappedNullifierLeaf< HashingPolicy > > const &leaves_, fr const &new_value)
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
MSGPACK_FIELDS(value, nextIndex, nextValue)
std::ostream & operator<<(std::ostream &os)
bool operator==(indexed_nullifier_leaf const &) const =default
static constexpr field zero()