Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include "barretenberg/common/bb_bench.hpp"
#include "barretenberg/common/ref_span.hpp"
#include "barretenberg/constants.hpp"
#include "barretenberg/ecc/batched_affine_addition/batched_affine_addition.hpp"
#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp"
Expand All @@ -25,6 +26,8 @@
#include "barretenberg/srs/global_crs.hpp"

#include <cstddef>
#include <cstdlib>
#include <limits>
#include <memory>
#include <string_view>

Expand Down Expand Up @@ -103,6 +106,82 @@ template <class Curve> class CommitmentKey {
Commitment point(r);
return point;
};
/**
* @brief Batch commitment to multiple polynomials
* @details Uses batch_multi_scalar_mul for more efficient processing when committing to multiple polynomials
*
* @param polynomials vector of polynomial spans to commit to
* @return std::vector<Commitment> vector of commitments, one for each polynomial
*/
std::vector<Commitment> batch_commit(RefSpan<Polynomial<Fr>> polynomials,
size_t max_batch_size = std::numeric_limits<size_t>::max()) const
{
BB_BENCH_NAME("CommitmentKey::batch_commit");

// We can only commit max_batch_size at a time
// This is to prevent excessive memory usage in the pippenger algorithm
// First batch, create the commitments vector
std::vector<Commitment> commitments;

max_batch_size = 1;
for (size_t i = 0; i < polynomials.size();) {
// Note: have to be careful how we compute this to not overlow e.g. max_batch_size + 1 would
size_t batch_size = std::min(max_batch_size, polynomials.size() - i);
size_t batch_end = i + batch_size;

// Prepare spans for batch MSM
std::vector<std::span<const G1>> points_spans;
// Note, we need to const_cast unfortunately as pippenger takes non-const spans
// as it converts back and forth from montgomery form
std::vector<std::span<Fr>> scalar_spans;

for (auto& polynomial : polynomials.subspan(i, batch_end - i)) {
std::span<const G1> point_table = srs->get_monomial_points().subspan(polynomial.start_index());
size_t consumed_srs = polynomial.start_index() + polynomial.size();
if (consumed_srs > srs->get_monomial_size()) {
throw_or_abort(format("Attempting to commit to a polynomial that needs ",
consumed_srs,
" points with an SRS of size ",
srs->get_monomial_size()));
}
scalar_spans.emplace_back(polynomial.coeffs());
points_spans.emplace_back(point_table);
}

// Perform batch MSM
auto results = scalar_multiplication::MSM<Curve>::batch_multi_scalar_mul(points_spans, scalar_spans, false);
for (const auto& result : results) {
commitments.emplace_back(result);
}
i += batch_size;
}
return commitments;
};

// helper builder struct for constructing a batch to commit at once
struct CommitBatch {
CommitmentKey* key;
RefVector<Polynomial<Fr>> wires;
std::vector<std::string> labels;
void commit_and_send_to_verifier(auto transcript, size_t max_batch_size = std::numeric_limits<size_t>::max())
{
std::vector<Commitment> commitments = key->batch_commit(wires, max_batch_size);
for (size_t i = 0; i < commitments.size(); ++i) {
transcript->send_to_verifier(labels[i], commitments[i]);
}
}

void add_to_batch(Polynomial<Fr>& poly, const std::string& label, bool mask)
{
if (mask) {
poly.mask();
}
wires.push_back(poly);
labels.push_back(label);
}
};

CommitBatch start_batch() { return CommitBatch{ this, {}, {} }; }

/**
* @brief Efficiently commit to a polynomial whose nonzero elements are arranged in discrete blocks
Expand Down Expand Up @@ -253,17 +332,14 @@ template <class Curve> class CommitmentKey {
return result;
}

enum class CommitType { Default, Structured, Sparse, StructuredNonZeroComplement };
enum class CommitType { Default, StructuredNonZeroComplement };

Commitment commit_with_type(PolynomialSpan<const Fr> poly,
CommitType type,
const std::vector<std::pair<size_t, size_t>>& active_ranges = {},
size_t final_active_wire_idx = 0)
{
switch (type) {
case CommitType::Structured:
case CommitType::Sparse:
return commit(poly);
case CommitType::StructuredNonZeroComplement:
return commit_structured_with_nonzero_complement(poly, active_ranges, final_active_wire_idx);
case CommitType::Default:
Expand Down
18 changes: 10 additions & 8 deletions barretenberg/cpp/src/barretenberg/common/assert.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "barretenberg/common/compiler_hints.hpp"
#include "barretenberg/common/throw_or_abort.hpp"
#include <cstdint>
#include <sstream>

Expand Down Expand Up @@ -59,7 +61,7 @@ struct AssertGuard {
#else
#define ASSERT_IN_CONSTEXPR(expression, ...) \
do { \
if (!(expression)) { \
if (!(BB_LIKELY(expression))) { \
info("Assertion failed: (" #expression ")"); \
__VA_OPT__(info("Reason : ", __VA_ARGS__);) \
bb::assert_failure(""); \
Expand All @@ -68,7 +70,7 @@ struct AssertGuard {

#define ASSERT(expression, ...) \
do { \
if (!(expression)) { \
if (!(BB_LIKELY(expression))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #expression ")"; \
__VA_OPT__(oss << " | Reason: " << __VA_ARGS__;) \
Expand All @@ -80,7 +82,7 @@ struct AssertGuard {
do { \
auto _actual = (actual); \
auto _expected = (expected); \
if (!(_actual == _expected)) { \
if (!(BB_LIKELY(_actual == _expected))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #actual " == " #expected ")\n"; \
oss << " Actual : " << _actual << "\n"; \
Expand All @@ -94,7 +96,7 @@ struct AssertGuard {
do { \
auto _actual = (actual); \
auto _expected = (expected); \
if (!(_actual != _expected)) { \
if (!(BB_LIKELY(_actual != _expected))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #actual " != " #expected ")\n"; \
oss << " Actual : " << _actual << "\n"; \
Expand All @@ -108,7 +110,7 @@ struct AssertGuard {
do { \
auto _left = (left); \
auto _right = (right); \
if (!(_left > _right)) { \
if (!(BB_LIKELY(_left > _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " > " #right ")\n"; \
oss << " Left : " << _left << "\n"; \
Expand All @@ -122,7 +124,7 @@ struct AssertGuard {
do { \
auto _left = (left); \
auto _right = (right); \
if (!(_left >= _right)) { \
if (!(BB_LIKELY(_left >= _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " >= " #right ")\n"; \
oss << " Left : " << _left << "\n"; \
Expand All @@ -136,7 +138,7 @@ struct AssertGuard {
do { \
auto _left = (left); \
auto _right = (right); \
if (!(_left < _right)) { \
if (!(BB_LIKELY(_left < _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " < " #right ")\n"; \
oss << " Left : " << _left << "\n"; \
Expand All @@ -150,7 +152,7 @@ struct AssertGuard {
do { \
auto _left = (left); \
auto _right = (right); \
if (!(_left <= _right)) { \
if (!(BB_LIKELY(_left <= _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " <= " #right ")\n"; \
oss << " Left : " << _left << "\n"; \
Expand Down
9 changes: 9 additions & 0 deletions barretenberg/cpp/src/barretenberg/common/bb_bench.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern bool use_bb_bench;
// Compile-time string
// See e.g. https://www.reddit.com/r/cpp_questions/comments/pumi9r/does_c20_not_support_string_literals_as_template/
template <std::size_t N> struct OperationLabel {
constexpr static std::size_t size() { return N; }
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
constexpr OperationLabel(const char (&str)[N])
{
Expand All @@ -35,6 +36,14 @@ template <std::size_t N> struct OperationLabel {
char value[N];
};

template <OperationLabel op1, OperationLabel op2> constexpr auto concat()
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
char result_cstr[op1.size() + op2.size() - 1] = {};
std::copy(op1.value, op1.value + op1.size() - 1, result_cstr);
std::copy(op2.value, op2.value + op2.size(), result_cstr + op1.size() - 1);
return OperationLabel{ result_cstr };
}
struct TimeStats;
struct TimeStatsEntry;
using OperationKey = std::string_view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ThreadPool {
do_iterations();

{
BB_BENCH_NAME("spinning main thread");
// BB_BENCH_NAME("spinning main thread");
std::unique_lock<std::mutex> lock(tasks_mutex);
complete_condition_.wait(lock, [this] { return complete_ == num_iterations_; });
}
Expand Down Expand Up @@ -72,7 +72,7 @@ class ThreadPool {
}
iteration = iteration_++;
}
BB_BENCH_NAME("do_iterations()");
// BB_BENCH_NAME("do_iterations()");
task_(iteration);
{
std::unique_lock<std::mutex> lock(tasks_mutex);
Expand Down
Loading
Loading