Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pedersen.cpp
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#include "pedersen.hpp"
10namespace bb::stdlib {
11
12using namespace bb;
13
14template <typename C>
16{
18 using Curve = EmbeddedCurve;
19
20 const auto base_points = context.generators->get(inputs.size(), context.offset, context.domain_separator);
21
24 scalars.emplace_back(cycle_scalar::create_from_bn254_scalar(field_ct(inputs.size())));
26 for (size_t i = 0; i < inputs.size(); ++i) {
27 scalars.emplace_back(cycle_scalar::create_from_bn254_scalar(inputs[i]));
28 // constructs constant cycle_group objects (non-witness)
29 points.emplace_back(base_points[i]);
30 }
31
32 auto result = cycle_group::batch_mul(points, scalars);
33 return result.x;
34}
35
36template <typename C>
39{
41 using Curve = EmbeddedCurve;
42
43 const auto base_points = context.generators->get(inputs.size(), context.offset, context.domain_separator);
44
47 scalars.emplace_back(cycle_scalar::create_from_bn254_scalar(field_ct(inputs.size())));
49 for (size_t i = 0; i < inputs.size(); ++i) {
50 // `true` param = skip primality test when performing a scalar mul
51 scalars.emplace_back(cycle_scalar::create_from_bn254_scalar(inputs[i], true));
52 // constructs constant cycle_group objects (non-witness)
53 points.emplace_back(base_points[i]);
54 }
55
56 auto result = cycle_group::batch_mul(points, scalars);
57 return result.x;
58}
59
66template <typename C>
68{
69 const size_t num_bytes = input.size();
70 const size_t bytes_per_element = 31;
71 size_t num_elements = static_cast<size_t>(num_bytes % bytes_per_element != 0) + (num_bytes / bytes_per_element);
72
73 std::vector<field_ct> elements;
74 for (size_t i = 0; i < num_elements; ++i) {
75 size_t bytes_to_slice = 0;
76 if (i == num_elements - 1) {
77 bytes_to_slice = num_bytes - (i * bytes_per_element);
78 } else {
79 bytes_to_slice = bytes_per_element;
80 }
81 auto element = static_cast<field_ct>(input.slice(i * bytes_per_element, bytes_to_slice));
82 elements.emplace_back(element);
83 }
84 field_ct hashed;
85 if (elements.size() < 2) {
86 hashed = hash(elements, context);
87 } else {
88 hashed = hash({ elements[0], elements[1] }, context);
89 for (size_t i = 2; i < elements.size(); ++i) {
90 hashed = hash({ hashed, elements[i] }, context);
91 }
92 }
93 return hashed;
94}
97
98} // namespace bb::stdlib
Performs pedersen hashes!
Definition pedersen.hpp:30
Represents a dynamic array of bytes in-circuit.
byte_array slice(size_t offset) const
Slice bytes from the byte array starting at offset. Does not add any constraints.
size_t size() const
::bb::stdlib::cycle_scalar< Builder > cycle_scalar
static cycle_group batch_mul(const std::vector< cycle_group > &base_points, const std::vector< BigScalarField > &scalars, GeneratorContext context={})
cycle_scalar represents a member of the cycle curve SCALAR FIELD. This is NOT the native circuit fiel...
static cycle_scalar create_from_bn254_scalar(const field_t &_in, bool skip_primality_test=false)
Use when we want to multiply a group element by a string of bits of known size. N....
stdlib class that evaluates in-circuit pedersen hashes, consistent with behavior in crypto::pedersen_...
Definition pedersen.hpp:23
static field_ct hash_buffer(const stdlib::byte_array< Builder > &input, GeneratorContext context={})
Hash a byte_array.
Definition pedersen.cpp:67
static field_ct hash_skip_field_validation(const std::vector< field_ct > &in, GeneratorContext context={})
Definition pedersen.cpp:37
typename cycle_group< Builder >::Curve EmbeddedCurve
Definition pedersen.hpp:28
static field_ct hash(const std::vector< field_ct > &in, GeneratorContext context={})
Definition pedersen.cpp:15
StrictMock< MockContext > context
stdlib::field_t< Builder > field_ct
void hash(State &state) noexcept
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13