barretenberg
Loading...
Searching...
No Matches
nullifier_leaf.hpp
1#pragma once
2#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp"
3#include "barretenberg/serialize/msgpack.hpp"
4
5namespace proof_system::plonk {
6namespace stdlib {
7namespace merkle_tree {
8
9using namespace barretenberg;
10typedef uint256_t index_t;
11
13 fr value;
14 index_t nextIndex;
15 fr nextValue;
16
17 // For serialization, update with any new fields
18 MSGPACK_FIELDS(value, nextIndex, nextValue);
19 bool operator==(nullifier_leaf const&) const = default;
20
21 std::ostream& operator<<(std::ostream& os)
22 {
23 os << "value = " << value << "\nnextIdx = " << nextIndex << "\nnextVal = " << nextValue;
24 return os;
25 }
26
27 barretenberg::fr hash() const { return stdlib::merkle_tree::hash_native({ value, nextIndex, nextValue }); }
28};
29
35
36 public:
37 // Initialize with a nullifier leaf
39 : data(value)
40 {}
41 // Initialize an empty leaf
43 : data(std::nullopt)
44 {}
45
46 bool operator==(WrappedNullifierLeaf const&) const = default;
47
54 bool has_value() const { return data.has_value(); }
55
61 nullifier_leaf unwrap() const { return data.value(); }
62
68 void set(nullifier_leaf value) { data.emplace(value); }
69
75 barretenberg::fr hash() const { return data.has_value() ? data.value().hash() : barretenberg::fr::zero(); }
76
83
84 private:
85 // Underlying data
86 std::optional<nullifier_leaf> data;
87};
88
89inline std::pair<size_t, bool> find_closest_leaf(std::vector<WrappedNullifierLeaf> const& leaves_, fr const& new_value)
90{
91 std::vector<uint256_t> diff;
92 bool repeated = false;
93 auto new_value_ = uint256_t(new_value);
94
95 for (size_t i = 0; i < leaves_.size(); i++) {
96
97 if (!leaves_[i].has_value()) {
98 diff.push_back(new_value_);
99 continue;
100 }
101
102 auto leaf_value_ = uint256_t(leaves_[i].unwrap().value);
103 if (leaf_value_ > new_value_) {
104 diff.push_back(leaf_value_);
105 } else if (leaf_value_ == new_value_) {
106 repeated = true;
107 return std::make_pair(i, repeated);
108 } else {
109 diff.push_back(new_value_ - leaf_value_);
110 }
111 }
112 auto it = std::min_element(diff.begin(), diff.end());
113 return std::make_pair(static_cast<size_t>(it - diff.begin()), repeated);
114}
115
116} // namespace merkle_tree
117} // namespace stdlib
118} // namespace proof_system::plonk
Definition: uint256.hpp:25
Wrapper for the Nullifier leaf class that allows for 0 values.
Definition: nullifier_leaf.hpp:34
static WrappedNullifierLeaf zero()
Generate a zero leaf (call the constructor with no arguments)
Definition: nullifier_leaf.hpp:82
nullifier_leaf unwrap() const
Return the wrapped nullifier_leaf object.
Definition: nullifier_leaf.hpp:61
bool has_value() const
Pass through the underlying std::optional method.
Definition: nullifier_leaf.hpp:54
void set(nullifier_leaf value)
Set the wrapped nullifier_leaf object value.
Definition: nullifier_leaf.hpp:68
barretenberg::fr hash() const
Return the hash of the wrapped object, other return the zero hash of 0.
Definition: nullifier_leaf.hpp:75
constexpr_utils defines some helper methods that perform some stl-equivalent operations but in a cons...
Definition: constexpr_utils.hpp:16
Definition: widget.bench.cpp:13