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 std::span<const Commitment> point_table = get_monomial_points().subspan(polynomial.start_index());
115 size_t consumed_srs = polynomial.start_index() + polynomial.size();
116 if (consumed_srs > get_monomial_size()) {
117 throw_or_abort(format("Attempting to commit to a polynomial that needs ",
118 consumed_srs,
119 " points with an SRS of size ",
121 }
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<Commitment> commit_and_send_to_verifier(auto transcript,
142 size_t max_batch_size = std::numeric_limits<size_t>::max())
143 {
144 std::vector<Commitment> commitments = key->batch_commit(wires, max_batch_size);
145 for (size_t i = 0; i < commitments.size(); ++i) {
146 transcript->send_to_verifier(labels[i], commitments[i]);
147 }
148
149 return commitments;
150 }
151
152 void add_to_batch(Polynomial<Fr>& poly, const std::string& label, bool mask)
153 {
154 if (mask) {
155 poly.mask();
156 }
157 wires.push_back(poly);
158 labels.push_back(label);
159 }
160 };
161
162 CommitBatch start_batch() { return CommitBatch{ this, {}, {} }; }
163};
164
165} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
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 .....
void mask()
Add random values to the coefficients of a polynomial. In practice, this is used for ensuring the com...
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
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 multi-scalar multiplications.
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, bool mask)
RefVector< Polynomial< Fr > > wires
size_t size() const
void throw_or_abort(std::string const &err)