Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
commitment_key.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
20
21#include <algorithm>
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <cstdlib>
26#include <limits>
27#include <memory>
28#include <string_view>
29
30namespace bb {
39template <class Curve> class CommitmentKey {
40
41 using Fr = typename Curve::ScalarField;
43
44 protected:
46
47 public:
48 size_t srs_size;
49
50 CommitmentKey() = default;
51
57 CommitmentKey(const size_t num_points)
58 : srs(srs::get_crs_factory<Curve>()->get_crs(num_points))
59 , srs_size(num_points)
60 {}
66 bool initialized() const { return srs != nullptr; }
67
68 std::span<Commitment> get_monomial_points() const { return srs->get_monomial_points(); }
69 size_t get_monomial_size() const { return srs->get_monomial_size(); }
70
77 Commitment commit(PolynomialSpan<const Fr> polynomial, bool has_duplicates_hint = false) const
78 {
79 BB_BENCH_NAME("CommitmentKey::commit");
81 size_t consumed_srs = polynomial.start_index + polynomial.size();
82 if (consumed_srs > get_monomial_size()) {
83 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
84 consumed_srs,
85 " points with an SRS of size ",
87 }
88 return scalar_multiplication::pippenger_unsafe<Curve>(polynomial, point_table, has_duplicates_hint);
89 };
100 std::vector<Commitment> batch_commit(RefSpan<Polynomial<Fr>> polynomials,
101 std::span<const uint8_t> has_duplicates_hints = {}) const
102 {
103 BB_BENCH_NAME("CommitmentKey::batch_commit");
104
105 std::vector<PolynomialSpan<Fr>> scalar_spans;
106 scalar_spans.reserve(polynomials.size());
107
108 for (auto& polynomial : polynomials) {
109 const size_t consumed_srs = polynomial.start_index() + polynomial.size();
110 if (consumed_srs > get_monomial_size()) {
111 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
112 consumed_srs,
113 " points with an SRS of size ",
115 }
116 scalar_spans.emplace_back(polynomial.start_index(), polynomial.coeffs());
117 }
118
120 get_monomial_points(), scalar_spans, /*handle_edge_cases=*/false, has_duplicates_hints);
121 return std::vector<Commitment>(results.begin(), results.end());
122 };
123
124 // helper builder struct for constructing a batch to commit at once
125 struct CommitBatch {
128 std::vector<std::string> labels;
129 std::vector<uint8_t> has_duplicates_hints; // per-poly dedup opt-in (parallel to wires)
130
131 std::vector<Commitment> commit_and_send_to_verifier(auto transcript)
132 {
133 std::vector<Commitment> commitments = key->batch_commit(wires, has_duplicates_hints);
134 for (size_t i = 0; i < commitments.size(); ++i) {
135 transcript->send_to_verifier(labels[i], commitments[i]);
136 }
137 return commitments;
138 }
139
140 void add_to_batch(Polynomial<Fr>& poly, const std::string& label, bool has_duplicates_hint = false)
141 {
142 wires.push_back(poly);
143 labels.push_back(label);
144 has_duplicates_hints.push_back(has_duplicates_hint ? uint8_t{ 1 } : uint8_t{ 0 });
145 }
146 };
147
148 CommitBatch start_batch() { return CommitBatch{ this, {}, {} }; }
149};
150
151} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
CommitmentKey()=default
size_t get_monomial_size() const
typename Curve::ScalarField Fr
std::span< Commitment > get_monomial_points() const
typename Curve::AffineElement Commitment
CommitmentKey(const size_t num_points)
Construct a new Kate Commitment Key object from existing SRS.
std::vector< Commitment > batch_commit(RefSpan< Polynomial< Fr > > polynomials, std::span< const uint8_t > has_duplicates_hints={}) const
Batch commitment to multiple polynomials.
bool initialized() const
Checks the commitment key is properly initialized.
std::shared_ptr< srs::factories::Crs< Curve > > srs
CommitBatch start_batch()
Commitment commit(PolynomialSpan< const Fr > polynomial, bool has_duplicates_hint=false) const
Uses the ProverSRS to create a commitment to p(X)
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
static std::vector< AffineElement > batch_multi_scalar_mul(std::span< const AffineElement > points, std::span< PolynomialSpan< ScalarField > > scalars, bool handle_edge_cases=true, std::span< const uint8_t > dedup_hints={}) noexcept
std::string format(Args... args)
Definition log.hpp:23
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< std::string > labels
void add_to_batch(Polynomial< Fr > &poly, const std::string &label, bool has_duplicates_hint=false)
std::vector< uint8_t > has_duplicates_hints
RefVector< Polynomial< Fr > > wires
std::vector< Commitment > commit_and_send_to_verifier(auto transcript)
size_t size() const
void throw_or_abort(std::string const &err)