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
15#include <vector>
16
18
20 static fr hash(const std::vector<fr>& inputs) { return crypto::pedersen_hash::hash(inputs); }
21
22 static fr hash_pair(const fr& lhs, const fr& rhs) { return hash(std::vector<fr>({ lhs, rhs })); }
23
24 static fr zero_hash() { return fr::zero(); }
25};
26
28 static fr hash(const std::vector<fr>& inputs)
29 {
31 }
32
33 static fr hash_pair(const fr& lhs, const fr& rhs) { return hash(std::vector<fr>({ lhs, rhs })); }
34
35 static fr zero_hash() { return fr::zero(); }
36};
37
38inline bb::fr hash_pair_native(bb::fr const& lhs, bb::fr const& rhs)
39{
40 return crypto::pedersen_hash::hash({ lhs, rhs }); // uses lookup tables
41}
42
44{
45 return crypto::pedersen_hash::hash(inputs); // uses lookup tables
46}
47
55{
56 ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
57 auto layer = input;
58 while (layer.size() > 1) {
59 std::vector<bb::fr> next_layer(layer.size() / 2);
60 for (size_t i = 0; i < next_layer.size(); ++i) {
61 next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
62 }
63 layer = std::move(next_layer);
64 }
65
66 return layer[0];
67}
68
69// TODO write test
71{
72 ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
73 auto layer = input;
74 std::vector<bb::fr> tree(input);
75 while (layer.size() > 1) {
76 std::vector<bb::fr> next_layer(layer.size() / 2);
77 for (size_t i = 0; i < next_layer.size(); ++i) {
78 next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
79 tree.push_back(next_layer[i]);
80 }
81 layer = std::move(next_layer);
82 }
83
84 return tree;
85}
86
87} // namespace bb::crypto::merkle_tree
#define ASSERT(expression,...)
Definition assert.hpp:77
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:70
bb::fr hash_native(std::vector< bb::fr > const &inputs)
Definition hash.hpp:43
bb::fr compute_tree_root_native(std::vector< bb::fr > const &input)
Definition hash.hpp:54
bb::fr hash_pair_native(bb::fr const &lhs, bb::fr const &rhs)
Definition hash.hpp:38
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:22
static fr hash(const std::vector< fr > &inputs)
Definition hash.hpp:20
static fr hash_pair(const fr &lhs, const fr &rhs)
Definition hash.hpp:33
static fr hash(const std::vector< fr > &inputs)
Definition hash.hpp:28
static constexpr field zero()