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
52 static constexpr size_t round_up_to_even(size_t n) { return (n + 1) & ~size_t{ 1 }; }
53
61 CommitmentKey(const size_t num_points)
62 : srs(srs::get_crs_factory<Curve>()->get_crs(round_up_to_even(num_points)))
63 , srs_size(round_up_to_even(num_points))
64 {}
70 bool initialized() const { return srs != nullptr; }
71
72 std::span<Commitment> get_monomial_points() const { return srs->get_monomial_points(); }
73 size_t get_monomial_size() const { return srs->get_monomial_size(); }
74
81 Commitment commit(PolynomialSpan<const Fr> polynomial, bool has_duplicates_hint = false) const
82 {
83 BB_BENCH_NAME("CommitmentKey::commit");
85 size_t consumed_srs = polynomial.start_index + polynomial.size();
86 if (consumed_srs > get_monomial_size()) {
87 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
88 consumed_srs,
89 " points with an SRS of size ",
91 }
92 return scalar_multiplication::pippenger_unsafe<Curve>(polynomial, point_table, has_duplicates_hint);
93 };
104 std::vector<Commitment> batch_commit(RefSpan<Polynomial<Fr>> polynomials,
105 std::span<const uint32_t> dedup_infos = {}) const
106 {
107 BB_BENCH_NAME("CommitmentKey::batch_commit");
108
109 std::vector<PolynomialSpan<Fr>> scalar_spans;
110 scalar_spans.reserve(polynomials.size());
111
112 for (auto& polynomial : polynomials) {
113 const size_t consumed_srs = polynomial.start_index() + polynomial.size();
114 if (consumed_srs > get_monomial_size()) {
115 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
116 consumed_srs,
117 " points with an SRS of size ",
119 }
120 scalar_spans.emplace_back(polynomial.start_index(), polynomial.coeffs());
121 }
122
124 get_monomial_points(), scalar_spans, /*handle_edge_cases=*/false, dedup_infos);
125 return std::vector<Commitment>(results.begin(), results.end());
126 };
127
128 // helper builder struct for constructing a batch to commit at once
129 struct CommitBatch {
132 std::vector<std::string> labels;
133 // Per-poly dedup channel (parallel to wires): 0 = off, 1 = hinted with no
134 // estimate, k >= 2 = hinted with a measured duplicate count of k.
135 std::vector<uint32_t> dedup_infos;
136
137 std::vector<Commitment> commit_and_send_to_verifier(auto transcript)
138 {
139 std::vector<Commitment> commitments = key->batch_commit(wires, dedup_infos);
140 for (size_t i = 0; i < commitments.size(); ++i) {
141 transcript->send_to_verifier(labels[i], commitments[i]);
142 }
143 return commitments;
144 }
145
147 const std::string& label,
148 bool has_duplicates_hint = false,
149 uint32_t measured_duplicate_count = 0)
150 {
151 wires.push_back(poly);
152 labels.push_back(label);
153 dedup_infos.push_back(has_duplicates_hint ? std::max<uint32_t>(1, measured_duplicate_count)
154 : uint32_t{ 0 });
155 }
156 };
157
158 CommitBatch start_batch() { return CommitBatch{ this, {}, {} }; }
159};
160
161} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
static constexpr size_t round_up_to_even(size_t n)
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.
bool initialized() const
Checks the commitment key is properly initialized.
std::shared_ptr< srs::factories::Crs< Curve > > srs
CommitBatch start_batch()
std::vector< Commitment > batch_commit(RefSpan< Polynomial< Fr > > polynomials, std::span< const uint32_t > dedup_infos={}) const
Batch commitment to multiple polynomials.
Commitment commit(PolynomialSpan< const Fr > polynomial, bool has_duplicates_hint=false) const
Uses the ProverSRS to create a commitment to p(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 uint32_t > dedup_infos={}) noexcept
std::string format(Args... args)
Definition log.hpp:23
std::string label
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
std::vector< uint32_t > dedup_infos
void add_to_batch(Polynomial< Fr > &poly, const std::string &label, bool has_duplicates_hint=false, uint32_t measured_duplicate_count=0)
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)