Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
hash.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
17#include <vector>
18
20
22 static fr hash(const std::vector<fr>& inputs) { return crypto::pedersen_hash::hash(inputs); }
23
24 static fr hash_pair(const fr& lhs, const fr& rhs) { return hash(std::vector<fr>({ lhs, rhs })); }
25
26 static fr zero_hash() { return fr::zero(); }
27};
28
30 static fr hash(const std::vector<fr>& inputs)
31 {
33 }
34
35 static fr hash_pair(const fr& lhs, const fr& rhs) { return hash(std::vector<fr>({ lhs, rhs })); }
36
37 static fr zero_hash() { return fr::zero(); }
38};
39
40inline bb::fr hash_pair_native(bb::fr const& lhs, bb::fr const& rhs)
41{
42 return crypto::pedersen_hash::hash({ lhs, rhs }); // uses lookup tables
43}
44
46{
47 return crypto::pedersen_hash::hash(inputs); // uses lookup tables
48}
49
57{
58 ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
59 auto layer = input;
60 while (layer.size() > 1) {
61 std::vector<bb::fr> next_layer(layer.size() / 2);
62 for (size_t i = 0; i < next_layer.size(); ++i) {
63 next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
64 }
65 layer = std::move(next_layer);
66 }
67
68 return layer[0];
69}
70
71// TODO write test
73{
74 ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
75 auto layer = input;
76 std::vector<bb::fr> tree(input);
77 while (layer.size() > 1) {
78 std::vector<bb::fr> next_layer(layer.size() / 2);
79 for (size_t i = 0; i < next_layer.size(); ++i) {
80 next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
81 tree.push_back(next_layer[i]);
82 }
83 layer = std::move(next_layer);
84 }
85
86 return tree;
87}
88
89} // namespace bb::crypto::merkle_tree
#define ASSERT(expression,...)
Definition assert.hpp:49
static Fq hash(const std::vector< Fq > &inputs, GeneratorContext context={})
Given a vector of fields, generate a pedersen hash using generators from context.
Definition pedersen.cpp:78
std::vector< bb::fr > compute_tree_native(std::vector< bb::fr > const &input)
Definition hash.hpp:72
bb::fr hash_native(std::vector< bb::fr > const &inputs)
Definition hash.hpp:45
bb::fr compute_tree_root_native(std::vector< bb::fr > const &input)
Definition hash.hpp:56
bb::fr hash_pair_native(bb::fr const &lhs, bb::fr const &rhs)
Definition hash.hpp:40
constexpr bool is_power_of_two(uint64_t x)
Definition pow.hpp:35
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static fr hash_pair(const fr &lhs, const fr &rhs)
Definition hash.hpp:24
static fr hash(const std::vector< fr > &inputs)
Definition hash.hpp:22
static fr hash_pair(const fr &lhs, const fr &rhs)
Definition hash.hpp:35
static fr hash(const std::vector< fr > &inputs)
Definition hash.hpp:30
static constexpr field zero()