Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
commitment_key.test.cpp
Go to the documentation of this file.
1
4
5#include <gtest/gtest.h>
6
7namespace bb {
8
9template <typename Curve> class CommitmentKeyTest : public ::testing::Test {
10 public:
12 using Fr = typename Curve::ScalarField;
14 using GroupElement = typename Curve::Element;
16
18
19 // Naive MSM computation for testing
20 static Commitment commit_naive(const CK& ck, const Polynomial& poly)
21 {
22 auto srs = ck.get_monomial_points();
23 GroupElement result = srs[poly.start_index()] * poly[poly.start_index()];
24 for (size_t i = poly.start_index() + 1; i < poly.end_index(); ++i) {
25 result += srs[i] * poly[i];
26 }
27 return result.normalize();
28 }
29
31 {
32 constexpr size_t n = 16;
33 CK ck(n);
34
35 Polynomial zero_poly(n);
36 Commitment commitment = ck.commit(zero_poly);
37
38 EXPECT_TRUE(commitment.is_point_at_infinity());
39 }
40
42 {
43 constexpr size_t n = 16;
44 CK ck(n);
45
46 // Polynomial with mostly zero coefficients
47 Polynomial poly(n);
48 poly.at(3) = Fr::random_element();
49
50 Commitment commitment = ck.commit(poly);
51 Commitment expected = commit_naive(ck, poly);
52 EXPECT_EQ(expected, commitment);
53 }
54
56 {
57 constexpr size_t n = 16;
58 CK ck(n);
59
60 auto poly = Polynomial::random(n);
61 Commitment commitment = ck.commit(poly);
62 Commitment expected = commit_naive(ck, poly);
63 EXPECT_EQ(expected, commitment);
64 }
65
67 {
68 // Test various non-power-of-2 sizes
69 for (size_t n : { size_t{ 10 }, size_t{ 100 }, size_t{ 1000 }, size_t{ 1234 } }) {
70 CK ck(n);
71
72 EXPECT_EQ(ck.srs_size, n);
73 // Note: get_monomial_size() may be >= n since it returns the underlying SRS size
74 EXPECT_GE(ck.get_monomial_size(), n);
75
76 auto poly = Polynomial::random(n);
77 Commitment commitment = ck.commit(poly);
78 Commitment expected = commit_naive(ck, poly);
79 EXPECT_EQ(expected, commitment);
80 }
81 }
82
84 {
85 constexpr size_t n = 16;
86 CK ck(n);
87
88 // Create multiple polynomials
90 for (size_t i = 0; i < 5; ++i) {
91 polys.emplace_back(Polynomial::random(n));
92 }
93
94 RefVector<Polynomial> poly_refs(polys);
95 auto batch_commitments = ck.batch_commit(poly_refs);
96
97 EXPECT_EQ(batch_commitments.size(), polys.size());
98
99 // Verify batch commit matches individual commits and naive computation
100 for (size_t i = 0; i < polys.size(); ++i) {
101 Commitment individual = ck.commit(polys[i]);
102 Commitment expected = commit_naive(ck, polys[i]);
103 EXPECT_EQ(batch_commitments[i], individual);
104 EXPECT_EQ(batch_commitments[i], expected);
105 }
106 }
107
109 {
110 constexpr size_t n = 32;
111 CK ck(n);
112
113 // Create polynomial with non-zero start index
114 // Polynomial(size, virtual_size, start_index) requires start_index + size <= virtual_size
115 // Indices must be in range [start_index, start_index + size)
116 constexpr size_t start_index = 8;
117 constexpr size_t poly_size = 16;
118 constexpr size_t virtual_size = start_index + poly_size; // 24
119 Polynomial poly(poly_size, virtual_size, start_index);
120 for (size_t i = 0; i < poly_size; ++i) {
121 poly.at(start_index + i) = Fr::random_element();
122 }
123
124 Commitment commitment = ck.commit(poly);
125 Commitment expected = commit_naive(ck, poly);
126 EXPECT_EQ(expected, commitment);
127 }
128};
129
130using Curves = ::testing::Types<curve::BN254, curve::Grumpkin>;
132
134{
135 TestFixture::test_commit_to_zero_poly();
136}
138{
139 TestFixture::test_commit_sparse_poly();
140}
142{
143 TestFixture::test_commit_random_poly();
144}
146{
147 TestFixture::test_non_dyadic_srs_size();
148}
150{
151 TestFixture::test_batch_commit();
152}
153TYPED_TEST(CommitmentKeyTest, CommitWithStartIndex)
154{
155 TestFixture::test_commit_with_start_index();
156}
157
158} // namespace bb
CommitmentKey object over a pairing group 𝔾₁.
typename Curve::AffineElement Commitment
static Commitment commit_naive(const CK &ck, const Polynomial &poly)
typename Curve::Element GroupElement
typename Curve::ScalarField Fr
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
size_t start_index() const
static Polynomial random(size_t size, size_t start_index=0)
size_t end_index() const
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::element Element
Definition grumpkin.hpp:62
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
CommitmentKey< Curve > ck
::testing::Types< curve::BN254, curve::Grumpkin > Curves
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept