-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simple sparse commitment (#7488)
Add method for more efficiently committing to sparse polynomials. For polynomials with very few non-zero coefficients, it turns out to be more efficient to first reduce the set of {scalars, points} defining the MSM to only those for which scalar != 0, then perform the MSM. This is implemented in a new method `commit_sparse` in the CommitmentKey. This PR also introduces (1) A more comprehensive set of pippenger benchmarks, (2) Tracking of commit times in the ClientIvc benchmark scripts, and (3) a test suite for the `commit_sparse` functionality. Here are some benchmarks for committing to a polynomials of varying sizes, each with only 5 random nonzero entries. The first uses `commit()` and the second uses the new `commit_sparse()` method: ``` --------------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------------------------------- bench_commit_sparse_random<curve::BN254>/14 5.79 ms 0.371 ms 1000 bench_commit_sparse_random<curve::BN254>/15 10.6 ms 0.508 ms 1000 bench_commit_sparse_random<curve::BN254>/16 12.1 ms 1.86 ms 253 bench_commit_sparse_random<curve::BN254>/17 14.0 ms 1.71 ms 412 bench_commit_sparse_random<curve::BN254>/18 73.1 ms 2.75 ms 100 bench_commit_sparse_random_preprocessed<curve::BN254>/14 0.132 ms 0.076 ms 9216 bench_commit_sparse_random_preprocessed<curve::BN254>/15 0.134 ms 0.077 ms 9114 bench_commit_sparse_random_preprocessed<curve::BN254>/16 0.138 ms 0.079 ms 8856 bench_commit_sparse_random_preprocessed<curve::BN254>/17 0.150 ms 0.082 ms 8579 bench_commit_sparse_random_preprocessed<curve::BN254>/18 0.177 ms 0.108 ms 6396 ``` Below are the relevant highlights from ClientIvc bench (master vs branch). Note the reductions in commitment time from databus (bus columns plus counts/tags), ecc_op_wires, and databus_inverses. Also, note that the time spent on these commitments between the unstructured and structured cases is now almost identical, suggesting that the required zero checking is essentially free. Master - Unstructured: ``` ClientIVCBench/Full/6 14277 ms 10173 ms commit(t) 3613 25.60% COMMIT::wires(t) 850 35.80% COMMIT::z_perm(t) 449 18.92% COMMIT::databus(t) 262 11.04% COMMIT::ecc_op_wires(t) 278 11.71% COMMIT::lookup_inverses(t) 189 7.98% COMMIT::databus_inverses(t) 250 10.54% COMMIT::lookup_counts_tags(t) 95 4.01% ``` Branch - Unstructured: ``` ClientIVCBench/Full/6 13530 ms 10167 ms commit(t) 2833 21.20% COMMIT::wires(t) 849 51.63% COMMIT::z_perm(t) 447 27.15% COMMIT::databus(t) 10 0.63% COMMIT::ecc_op_wires(t) 49 2.97% COMMIT::lookup_inverses(t) 190 11.56% COMMIT::databus_inverses(t) 4 0.24% COMMIT::lookup_counts_tags(t) 96 5.82% ``` Master - STRUCTURED ``` ClientIVCBench/FullStructured/6 20935 ms 14669 ms commit(t) 7091 34.14% COMMIT::wires(t) 1471 25.74% COMMIT::z_perm(t) 1028 17.98% COMMIT::databus(t) 509 8.90% COMMIT::ecc_op_wires(t) 929 16.26% COMMIT::lookup_inverses(t) 356 6.23% COMMIT::databus_inverses(t) 1243 21.75% COMMIT::lookup_counts_tags(t) 179 3.13% ``` Branch - STRUCTURED ``` ClientIVCBench/FullStructured/6 18316 ms 13782 ms commit(t) 4398 24.23% COMMIT::wires(t) 1468 47.53% COMMIT::z_perm(t) 1020 33.04% COMMIT::databus(t) 13 0.44% COMMIT::ecc_op_wires(t) 52 1.68% COMMIT::lookup_inverses(t) 350 11.32% COMMIT::databus_inverses(t) 5 0.17% COMMIT::lookup_counts_tags(t) 180 5.82% ```
- Loading branch information
1 parent
4a2011e
commit df08874
Showing
5 changed files
with
344 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
barretenberg/cpp/src/barretenberg/commitment_schemes/sparse_commitment.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "barretenberg/commitment_schemes/commitment_key.hpp" | ||
#include "barretenberg/polynomials/polynomial.hpp" | ||
#include "barretenberg/srs/factories/file_crs_factory.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace bb { | ||
|
||
template <typename Curve> class CommitmentKeyTest : public ::testing::Test { | ||
using CK = CommitmentKey<Curve>; | ||
|
||
using Fr = typename Curve::ScalarField; | ||
using Commitment = typename Curve::AffineElement; | ||
using Polynomial = bb::Polynomial<Fr>; | ||
|
||
public: | ||
template <class CK> inline std::shared_ptr<CK> create_commitment_key(size_t num_points); | ||
}; | ||
|
||
template <> | ||
template <> | ||
std::shared_ptr<CommitmentKey<curve::BN254>> CommitmentKeyTest<curve::BN254>::create_commitment_key< | ||
CommitmentKey<curve::BN254>>(const size_t num_points) | ||
{ | ||
srs::init_crs_factory("../srs_db/ignition"); | ||
return std::make_shared<CommitmentKey<curve::BN254>>(num_points); | ||
} | ||
|
||
template <> | ||
template <> | ||
std::shared_ptr<CommitmentKey<curve::Grumpkin>> CommitmentKeyTest<curve::Grumpkin>::create_commitment_key< | ||
CommitmentKey<curve::Grumpkin>>(const size_t num_points) | ||
{ | ||
srs::init_grumpkin_crs_factory("../srs_db/grumpkin"); | ||
return std::make_shared<CommitmentKey<curve::Grumpkin>>(num_points); | ||
} | ||
|
||
using Curves = ::testing::Types<curve::BN254, curve::Grumpkin>; | ||
|
||
TYPED_TEST_SUITE(CommitmentKeyTest, Curves); | ||
|
||
// Check that commit and commit_sparse return the same result for a random sparse polynomial | ||
TYPED_TEST(CommitmentKeyTest, CommitSparse) | ||
{ | ||
using Curve = TypeParam; | ||
using CK = CommitmentKey<Curve>; | ||
using G1 = Curve::AffineElement; | ||
using Fr = Curve::ScalarField; | ||
using Polynomial = bb::Polynomial<Fr>; | ||
|
||
const size_t num_points = 1 << 12; // large enough to ensure normal pippenger logic is used | ||
const size_t num_nonzero = 7; | ||
|
||
// Construct a sparse random polynomial | ||
Polynomial poly{ num_points }; | ||
for (size_t i = 0; i < num_nonzero; ++i) { | ||
size_t idx = (i + 1) * (i + 1) % num_points; | ||
poly[idx] = Fr::random_element(); | ||
} | ||
|
||
// Commit to the polynomial using both the conventional commit method and the sparse commitment method | ||
auto key = TestFixture::template create_commitment_key<CK>(num_points); | ||
G1 commit_result = key->commit(poly); | ||
G1 sparse_commit_result = key->commit_sparse(poly); | ||
|
||
EXPECT_EQ(sparse_commit_result, commit_result); | ||
} | ||
|
||
} // namespace bb |
Oops, something went wrong.