Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
hash_path.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.hpp"
10#include <algorithm>
11#include <vector>
12
14
16using fr_sibling_path = std::vector<fr>;
18
19inline fr_hash_path get_new_hash_path(fr_hash_path const& old_path, uint128_t index, fr const& value)
20{
21 fr_hash_path path = old_path;
22 fr current = value;
23 for (size_t i = 0; i < old_path.size(); ++i) {
24 bool path_bit = static_cast<bool>(index & 0x1);
25 if (path_bit) {
26 path[i].second = current;
27 } else {
28 path[i].first = current;
29 }
30 current = hash_pair_native(path[i].first, path[i].second);
31 index /= 2;
32 }
33 return path;
34}
35
36inline fr_hash_path get_random_hash_path(size_t const& tree_depth)
37{
38 fr_hash_path path;
39 for (size_t i = 0; i < tree_depth; ++i) {
41 }
42 return path;
43}
44
45template <typename Ctx> inline hash_path<Ctx> create_witness_hash_path(Ctx& ctx, fr_hash_path const& input)
46{
47 hash_path<Ctx> result;
48 std::transform(input.begin(), input.end(), std::back_inserter(result), [&](auto const& v) {
49 return std::make_pair(bb::stdlib::field_t(bb::stdlib::witness_t(&ctx, v.first)),
50 bb::stdlib::field_t(bb::stdlib::witness_t(&ctx, v.second)));
51 });
52 return result;
53}
54
56{
57 return hash_pair_native(input[input.size() - 1].first, input[input.size() - 1].second);
58}
59
60inline fr zero_hash_at_height(size_t height)
61{
62 auto current = fr(0);
63 for (size_t i = 0; i < height; ++i) {
64 current = hash_pair_native(current, current);
65 }
66 return current;
67}
68
69} // namespace bb::crypto::merkle_tree
70
71// We add to std namespace as fr_hash_path is actually a std::vector, and this is the only way
72// to achieve effective ADL.
73namespace std {
74template <typename Ctx>
75inline std::ostream& operator<<(std::ostream& os, bb::crypto::merkle_tree::hash_path<Ctx> const& path)
76{
77 os << "[\n";
78 for (size_t i = 0; i < path.size(); ++i) {
79 os << " (" << i << ": " << path[i].first << ", " << path[i].second << ")\n";
80 }
81 os << "]\n";
82 return os;
83}
84
85inline std::ostream& operator<<(std::ostream& os, bb::crypto::merkle_tree::fr_hash_path const& path)
86{
87 os << "[\n";
88 for (size_t i = 0; i < path.size(); ++i) {
89 os << " (" << i << ": " << path[i].first << ", " << path[i].second << ")\n";
90 }
91 os << "]\n";
92 return os;
93}
94} // namespace std
std::vector< std::pair< bb::stdlib::field_t< Ctx >, bb::stdlib::field_t< Ctx > > > hash_path
Definition hash_path.hpp:17
hash_path< Ctx > create_witness_hash_path(Ctx &ctx, fr_hash_path const &input)
Definition hash_path.hpp:45
fr zero_hash_at_height(size_t height)
Definition hash_path.hpp:60
fr_hash_path get_random_hash_path(size_t const &tree_depth)
Definition hash_path.hpp: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
fr get_hash_path_root(fr_hash_path const &input)
Definition hash_path.hpp:55
fr_hash_path get_new_hash_path(fr_hash_path const &old_path, uint128_t index, fr const &value)
Definition hash_path.hpp:19
bb::fr hash_pair_native(bb::fr const &lhs, bb::fr const &rhs)
Definition hash.hpp:40
field< Bn254FrParams > fr
Definition fr.hpp:174
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::ostream & operator<<(std::ostream &os, const T &obj)
Automatically derived stream operator for any object that defines .msgpack() (implicitly defined by M...
Definition streams.hpp:79
unsigned __int128 uint128_t
Definition serialize.hpp:44
static field random_element(numeric::RNG *engine=nullptr) noexcept