Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
memory_tree.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
9#include "hash_path.hpp"
10
12
30template <typename HashingPolicy> class MemoryTree {
31 public:
32 MemoryTree(size_t depth);
33
34 fr_hash_path get_hash_path(size_t index) const;
35
36 fr_sibling_path get_sibling_path(size_t index) const;
37
38 fr update_element(size_t index, fr const& value);
39
40 fr root() const { return root_; }
41
42 fr get_node(uint32_t level, size_t index) const;
43
44 public:
45 size_t depth_;
49};
50
51template <typename HashingPolicy>
53 : depth_(depth)
54{
55
57 BB_ASSERT_LTE(depth, 20U);
58 total_size_ = 1UL << depth_;
59 hashes_.resize(total_size_ * 2 - 2);
60
61 // Build the entire tree.
62 auto current = fr(0);
63 size_t layer_size = total_size_;
64 for (size_t offset = 0; offset < hashes_.size(); offset += layer_size, layer_size /= 2) {
65 for (size_t i = 0; i < layer_size; ++i) {
66 hashes_[offset + i] = current;
67 }
68 current = HashingPolicy::hash_pair(current, current);
69 }
70
71 root_ = current;
72}
73
74template <typename HashingPolicy> fr MemoryTree<HashingPolicy>::get_node(uint32_t level, size_t index) const
75{
76 auto offset = 0UL;
77 for (size_t i = depth_; i > level; i--) {
78 offset += 1UL << i;
79 }
80 return hashes_[offset + index];
81}
82
83template <typename HashingPolicy> fr_hash_path MemoryTree<HashingPolicy>::get_hash_path(size_t index) const
84{
85 fr_hash_path path(depth_);
86 size_t offset = 0;
87 size_t layer_size = total_size_;
88 for (size_t i = 0; i < depth_; ++i) {
89 index -= index & 0x1;
90 path[i] = std::make_pair(hashes_[offset + index], hashes_[offset + index + 1]);
91 offset += layer_size;
92 layer_size >>= 1;
93 index >>= 1;
94 }
95 return path;
96}
97
98template <typename HashingPolicy> fr_sibling_path MemoryTree<HashingPolicy>::get_sibling_path(size_t index) const
99{
100 fr_sibling_path path(depth_);
101 size_t offset = 0;
102 size_t layer_size = total_size_;
103 for (size_t i = 0; i < depth_; i++) {
104 if (index % 2 == 0) {
105 path[i] = hashes_[offset + index + 1];
106 } else {
107 path[i] = hashes_[offset + index - 1];
108 }
109 offset += layer_size;
110 layer_size >>= 1;
111 index >>= 1;
112 }
113 return path;
114}
115
116template <typename HashingPolicy> fr MemoryTree<HashingPolicy>::update_element(size_t index, fr const& value)
117{
118 size_t offset = 0;
119 size_t layer_size = total_size_;
120 fr current = value;
121 for (size_t i = 0; i < depth_; ++i) {
122 hashes_[offset + index] = current;
123 index &= (~0ULL) - 1;
124 current = HashingPolicy::hash_pair(hashes_[offset + index], hashes_[offset + index + 1]);
125 offset += layer_size;
126 layer_size >>= 1;
127 index >>= 1;
128 }
129 root_ = current;
130 return root_;
131}
132
133} // namespace bb::crypto::merkle_tree
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:101
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:129
fr update_element(size_t index, fr const &value)
fr_hash_path get_hash_path(size_t index) const
fr get_node(uint32_t level, size_t index) const
fr_sibling_path get_sibling_path(size_t index) const
ssize_t offset
Definition engine.cpp:36
std::vector< std::pair< fr, fr > > fr_hash_path
Definition hash_path.hpp:15
std::vector< fr > fr_sibling_path
Definition hash_path.hpp:16
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13