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
19
20#include <cstddef>
21#include <cstdlib>
22#include <limits>
23#include <memory>
24#include <string_view>
25
26namespace bb {
35template <class Curve> class CommitmentKey {
36
37 using Fr = typename Curve::ScalarField;
39
40 protected:
42
43 public:
44 size_t srs_size;
45
46 CommitmentKey() = default;
47
53 CommitmentKey(const size_t num_points)
54 : srs(srs::get_crs_factory<Curve>()->get_crs(num_points))
55 , srs_size(num_points)
56 {}
62 bool initialized() const { return srs != nullptr; }
63
64 std::span<Commitment> get_monomial_points() const { return srs->get_monomial_points(); }
65 size_t get_monomial_size() const { return srs->get_monomial_size(); }
66
74 {
75 BB_BENCH_NAME("CommitmentKey::commit");
77 size_t consumed_srs = polynomial.start_index + polynomial.size();
78 if (consumed_srs > get_monomial_size()) {
79 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
80 consumed_srs,
81 " points with an SRS of size ",
83 }
84 return scalar_multiplication::pippenger_unsafe<Curve>(polynomial, point_table);
85 };
94 std::vector<Commitment> batch_commit(RefSpan<Polynomial<Fr>> polynomials,
95 size_t max_batch_size = std::numeric_limits<size_t>::max()) const
96 {
97 BB_BENCH_NAME("CommitmentKey::batch_commit");
98
99 // We can only commit max_batch_size at a time
100 // This is to prevent excessive memory usage in the pippenger algorithm
101 // First batch, create the commitments vector
102 std::vector<Commitment> commitments;
103
104 for (size_t i = 0; i < polynomials.size();) {
105 // Note: have to be careful how we compute this to not overlow e.g. max_batch_size + 1 would
106 size_t batch_size = std::min(max_batch_size, polynomials.size() - i);
107 size_t batch_end = i + batch_size;
108
109 // Prepare spans for batch MSM
111 std::vector<std::span<Fr>> scalar_spans;
112
113 for (auto& polynomial : polynomials.subspan(i, batch_end - i)) {
114 size_t consumed_srs = polynomial.start_index() + polynomial.size();
115 if (consumed_srs > get_monomial_size()) {
116 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
117 consumed_srs,
118 " points with an SRS of size ",
120 }
121 std::span<const Commitment> point_table = get_monomial_points().subspan(polynomial.start_index());
122 scalar_spans.emplace_back(polynomial.coeffs());
123 points_spans.emplace_back(point_table);
124 }
125
126 // Perform batch MSM
127 auto results = scalar_multiplication::MSM<Curve>::batch_multi_scalar_mul(points_spans, scalar_spans, false);
128 for (const auto& result : results) {
129 commitments.emplace_back(result);
130 }
131 i += batch_size;
132 }
133 return commitments;
134 };
135
136 // helper builder struct for constructing a batch to commit at once
137 struct CommitBatch {
140 std::vector<std::string> labels;
141 std::vector<const Polynomial<Fr>*> tail_polys; // optional ZK masking tails (parallel to wires)
142
143 std::vector<Commitment> commit_and_send_to_verifier(auto transcript,
144 size_t max_batch_size = std::numeric_limits<size_t>::max())
145 {
146 std::vector<Commitment> commitments = key->batch_commit(wires, max_batch_size);
147
148 // Adjust commitments for wires with masking tails: C' = C_short + commit(tail)
149 for (size_t i = 0; i < commitments.size(); ++i) {
150 if (i < tail_polys.size() && tail_polys[i] != nullptr && !tail_polys[i]->is_empty()) {
151 commitments[i] = commitments[i] + key->commit(*tail_polys[i]);
152 }
153 transcript->send_to_verifier(labels[i], commitments[i]);
154 }
155
156 return commitments;
157 }
158
159 void add_to_batch(Polynomial<Fr>& poly, const std::string& label, const Polynomial<Fr>* tail = nullptr)
160 {
161 wires.push_back(poly);
162 labels.push_back(label);
163 tail_polys.push_back(tail);
164 }
165 };
166
167 CommitBatch start_batch() { return CommitBatch{ this, {}, {} }; }
168};
169
170} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
CommitmentKey()=default
std::vector< Commitment > batch_commit(RefSpan< Polynomial< Fr > > polynomials, size_t max_batch_size=std::numeric_limits< size_t >::max()) const
Batch commitment to multiple polynomials.
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.
Commitment commit(PolynomialSpan< const Fr > polynomial) const
Uses the ProverSRS to create a commitment to p(X)
bool initialized() const
Checks the commitment key is properly initialized.
std::shared_ptr< srs::factories::Crs< Curve > > srs
CommitBatch start_batch()
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< std::span< const AffineElement > > points, std::span< std::span< ScalarField > > scalars, bool handle_edge_cases=true) noexcept
Compute multiple MSMs in parallel with work balancing.
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
std::vector< Commitment > commit_and_send_to_verifier(auto transcript, size_t max_batch_size=std::numeric_limits< size_t >::max())
void add_to_batch(Polynomial< Fr > &poly, const std::string &label, const Polynomial< Fr > *tail=nullptr)
RefVector< Polynomial< Fr > > wires
std::vector< const Polynomial< Fr > * > tail_polys
size_t size() const
void throw_or_abort(std::string const &err)