@@ -85,7 +85,7 @@ template <class Curve> class CommitmentKey {
85
85
*/
86
86
Commitment commit (PolynomialSpan<const Fr> polynomial)
87
87
{
88
- PROFILE_THIS ( );
88
+ PROFILE_THIS_NAME ( " commit " );
89
89
// We must have a power-of-2 SRS points *after* subtracting by start_index.
90
90
size_t dyadic_poly_size = numeric::round_up_power_2 (polynomial.size ());
91
91
// Because pippenger prefers a power-of-2 size, we must choose a starting index for the points so that we don't
@@ -133,7 +133,7 @@ template <class Curve> class CommitmentKey {
133
133
*/
134
134
Commitment commit_sparse (PolynomialSpan<const Fr> polynomial)
135
135
{
136
- PROFILE_THIS ( );
136
+ PROFILE_THIS_NAME ( " commit_sparse " );
137
137
const size_t poly_size = polynomial.size ();
138
138
ASSERT (polynomial.end_index () <= srs->get_monomial_size ());
139
139
@@ -204,21 +204,24 @@ template <class Curve> class CommitmentKey {
204
204
* @return Commitment
205
205
*/
206
206
Commitment commit_structured (PolynomialSpan<const Fr> polynomial,
207
- const std::vector<std::pair<size_t , size_t >>& active_ranges)
207
+ const std::vector<std::pair<size_t , size_t >>& active_ranges,
208
+ size_t final_active_wire_idx = 0 )
208
209
{
209
- BB_OP_COUNT_TIME ( );
210
+ PROFILE_THIS_NAME ( " commit_structured " );
210
211
ASSERT (polynomial.end_index () <= srs->get_monomial_size ());
211
212
212
213
// Percentage of nonzero coefficients beyond which we resort to the conventional commit method
213
214
constexpr size_t NONZERO_THRESHOLD = 75 ;
214
215
216
+ // Compute the number of non-zero coefficients in the polynomial
215
217
size_t total_num_scalars = 0 ;
216
- for (const auto & range : active_ranges) {
217
- total_num_scalars += range. second - range. first ;
218
+ for (const auto & [first, second] : active_ranges) {
219
+ total_num_scalars += second - first;
218
220
}
219
221
220
222
// Compute "active" percentage of polynomial; resort to standard commit if appropriate
221
- size_t percentage_nonzero = total_num_scalars * 100 / polynomial.size ();
223
+ size_t polynomial_size = final_active_wire_idx != 0 ? final_active_wire_idx : polynomial.size ();
224
+ size_t percentage_nonzero = total_num_scalars * 100 / polynomial_size;
222
225
if (percentage_nonzero > NONZERO_THRESHOLD) {
223
226
return commit (polynomial);
224
227
}
@@ -259,9 +262,10 @@ template <class Curve> class CommitmentKey {
259
262
* @return Commitment
260
263
*/
261
264
Commitment commit_structured_with_nonzero_complement (PolynomialSpan<const Fr> polynomial,
262
- const std::vector<std::pair<size_t , size_t >>& active_ranges)
265
+ const std::vector<std::pair<size_t , size_t >>& active_ranges,
266
+ size_t final_active_wire_idx = 0 )
263
267
{
264
- BB_OP_COUNT_TIME ( );
268
+ PROFILE_THIS_NAME ( " commit_structured_with_nonzero_complement " );
265
269
ASSERT (polynomial.end_index () <= srs->get_monomial_size ());
266
270
267
271
using BatchedAddition = BatchedAffineAddition<Curve>;
@@ -273,20 +277,21 @@ template <class Curve> class CommitmentKey {
273
277
// Note: the range from the end of the last active range to the end of the polynomial is excluded from the
274
278
// complement since the polynomial is assumed to be zero there.
275
279
std::vector<std::pair<size_t , size_t >> active_ranges_complement;
280
+ // Also compute total number of scalars in the constant regions
281
+ size_t total_num_complement_scalars = 0 ;
276
282
for (size_t i = 0 ; i < active_ranges.size () - 1 ; ++i) {
277
283
const size_t start = active_ranges[i].second ;
278
284
const size_t end = active_ranges[i + 1 ].first ;
279
- active_ranges_complement.emplace_back (start, end);
280
- }
281
-
282
- // Compute the total number of scalars in the constant regions
283
- size_t total_num_complement_scalars = 0 ;
284
- for (const auto & range : active_ranges_complement) {
285
- total_num_complement_scalars += range.second - range.first ;
285
+ if (end > start) {
286
+ active_ranges_complement.emplace_back (start, end);
287
+ total_num_complement_scalars += end - start;
288
+ }
286
289
}
287
290
291
+ size_t polynomial_size = final_active_wire_idx != 0 ? final_active_wire_idx : polynomial.size ();
288
292
// Compute percentage of polynomial comprised of constant blocks; resort to standard commit if appropriate
289
- size_t percentage_constant = total_num_complement_scalars * 100 / polynomial.size ();
293
+ size_t percentage_constant = total_num_complement_scalars * 100 / polynomial_size;
294
+
290
295
if (percentage_constant < CONSTANT_THRESHOLD) {
291
296
return commit (polynomial);
292
297
}
@@ -299,12 +304,11 @@ template <class Curve> class CommitmentKey {
299
304
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1131): Peak memory usage could be improved by
300
305
// performing this copy and the subsequent summation as a precomputation prior to constructing the point table.
301
306
std::vector<G1> points;
302
- points.reserve (2 * total_num_complement_scalars);
303
- for (const auto & range : active_ranges_complement) {
304
- const size_t start = 2 * range.first ;
305
- const size_t end = 2 * range.second ;
306
- for (size_t i = start; i < end; i += 2 ) {
307
- points.emplace_back (point_table[i]);
307
+
308
+ points.reserve (total_num_complement_scalars);
309
+ for (const auto & [start, end] : active_ranges_complement) {
310
+ for (size_t i = start; i < end; i++) {
311
+ points.emplace_back (point_table[2 * i]);
308
312
}
309
313
}
310
314
@@ -313,17 +317,16 @@ template <class Curve> class CommitmentKey {
313
317
std::vector<Fr> unique_scalars;
314
318
std::vector<size_t > sequence_counts;
315
319
for (const auto & range : active_ranges_complement) {
316
- if (range.second - range.first > 0 ) { // only ranges with nonzero length
317
- unique_scalars.emplace_back (polynomial.span [range.first ]);
318
- sequence_counts.emplace_back (range.second - range.first );
319
- }
320
+ unique_scalars.emplace_back (polynomial.span [range.first ]);
321
+ sequence_counts.emplace_back (range.second - range.first );
320
322
}
321
323
322
324
// Reduce each sequence to a single point
323
325
auto reduced_points = BatchedAddition::add_in_place (points, sequence_counts);
324
326
325
327
// Compute the full commitment as the sum of the "active" region commitment and the constant region contribution
326
- Commitment result = commit_structured (polynomial, active_ranges);
328
+ Commitment result = commit_structured (polynomial, active_ranges, final_active_wire_idx);
329
+
327
330
for (auto [scalar, point] : zip_view (unique_scalars, reduced_points)) {
328
331
result = result + point * scalar;
329
332
}
0 commit comments