barretenberg
Loading...
Searching...
No Matches
commitment_key.test.hpp
1#pragma once
2
3#include "barretenberg/commitment_schemes/commitment_key.hpp"
4#include "barretenberg/commitment_schemes/verification_key.hpp"
5#include "barretenberg/ecc/curves/bn254/g1.hpp"
6#include "barretenberg/polynomials/polynomial.hpp"
7#include "barretenberg/srs/factories/file_crs_factory.hpp"
8#include "claim.hpp"
9#include <algorithm>
10#include <concepts>
11#include <memory>
12#include <string_view>
13
14#include <gtest/gtest.h>
15
17
18template <class CK> inline std::shared_ptr<CK> CreateCommitmentKey();
19
20template <> inline std::shared_ptr<CommitmentKey<curve::BN254>> CreateCommitmentKey<CommitmentKey<curve::BN254>>()
21{
22 constexpr size_t n = 4096;
23 std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> crs_factory(
25 return std::make_shared<CommitmentKey<curve::BN254>>(n, crs_factory);
26}
27// For IPA
28template <> inline std::shared_ptr<CommitmentKey<curve::Grumpkin>> CreateCommitmentKey<CommitmentKey<curve::Grumpkin>>()
29{
30 constexpr size_t n = 4096;
31 std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::Grumpkin>> crs_factory(
33 return std::make_shared<CommitmentKey<curve::Grumpkin>>(n, crs_factory);
34}
35
36template <typename CK> inline std::shared_ptr<CK> CreateCommitmentKey()
37// requires std::default_initializable<CK>
38{
39 return std::make_shared<CK>();
40}
41
42template <class VK> inline std::shared_ptr<VK> CreateVerifierCommitmentKey();
43
44template <>
45inline std::shared_ptr<VerifierCommitmentKey<curve::BN254>> CreateVerifierCommitmentKey<
46 VerifierCommitmentKey<curve::BN254>>()
47{
48 constexpr size_t n = 4096;
49 std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> crs_factory(
51 return std::make_shared<VerifierCommitmentKey<curve::BN254>>(n, crs_factory);
52}
53// For IPA
54template <>
55inline std::shared_ptr<VerifierCommitmentKey<curve::Grumpkin>> CreateVerifierCommitmentKey<
56 VerifierCommitmentKey<curve::Grumpkin>>()
57{
58 constexpr size_t n = 4096;
59 std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::Grumpkin>> crs_factory(
61 return std::make_shared<VerifierCommitmentKey<curve::Grumpkin>>(n, crs_factory);
62}
63template <typename VK> inline std::shared_ptr<VK> CreateVerifierCommitmentKey()
64// requires std::default_initializable<VK>
65{
66 return std::make_shared<VK>();
67}
68template <typename Curve> class CommitmentTest : public ::testing::Test {
71
72 using Fr = typename Curve::ScalarField;
73 using Commitment = typename Curve::AffineElement;
75
76 public:
78 : engine{ &numeric::random::get_engine() }
79 {}
80
81 std::shared_ptr<CK> ck() { return commitment_key; }
82 std::shared_ptr<VK> vk() { return verification_key; }
83
84 Commitment commit(const Polynomial& polynomial) { return commitment_key->commit(polynomial); }
85
86 Polynomial random_polynomial(const size_t n)
87 {
88 Polynomial p(n);
89 for (size_t i = 0; i < n; ++i) {
90 p[i] = Fr::random_element(engine);
91 }
92 return p;
93 }
94
95 Fr random_element() { return Fr::random_element(engine); }
96
97 OpeningPair<Curve> random_eval(const Polynomial& polynomial)
98 {
99 Fr x{ random_element() };
100 Fr y{ polynomial.evaluate(x) };
101 return { x, y };
102 }
103
104 std::pair<OpeningClaim<Curve>, Polynomial> random_claim(const size_t n)
105 {
106 auto polynomial = random_polynomial(n);
107 auto opening_pair = random_eval(polynomial);
108 auto commitment = commit(polynomial);
109 auto opening_claim = OpeningClaim<Curve>{ opening_pair, commitment };
110 return { opening_claim, polynomial };
111 };
112
113 std::vector<Fr> random_evaluation_point(const size_t num_variables)
114 {
115 std::vector<Fr> u(num_variables);
116 for (size_t l = 0; l < num_variables; ++l) {
117 u[l] = random_element();
118 }
119 return u;
120 }
121
122 void verify_opening_claim(const OpeningClaim<Curve>& claim, const Polynomial& witness)
123 {
124 auto& commitment = claim.commitment;
125 auto& [x, y] = claim.opening_pair;
126 Fr y_expected = witness.evaluate(x);
127 EXPECT_EQ(y, y_expected) << "OpeningClaim: evaluations mismatch";
128 Commitment commitment_expected = commit(witness);
129 // found it
130 EXPECT_EQ(commitment, commitment_expected) << "OpeningClaim: commitment mismatch";
131 }
132
133 void verify_opening_pair(const OpeningPair<Curve>& opening_pair, const Polynomial& witness)
134 {
135 auto& [x, y] = opening_pair;
136 Fr y_expected = witness.evaluate(x);
137 EXPECT_EQ(y, y_expected) << "OpeningPair: evaluations mismatch";
138 }
139
147 void verify_batch_opening_claim(std::span<const OpeningClaim<Curve>> multi_claims,
148 std::span<const Polynomial> witnesses)
149 {
150 const size_t num_claims = multi_claims.size();
151 ASSERT_EQ(witnesses.size(), num_claims);
152
153 for (size_t j = 0; j < num_claims; ++j) {
154 this->verify_opening_claim(multi_claims[j], witnesses[j]);
155 }
156 }
157
162 void verify_batch_opening_pair(std::span<const OpeningPair<Curve>> opening_pairs,
163 std::span<const Polynomial> witnesses)
164 {
165 const size_t num_pairs = opening_pairs.size();
166 ASSERT_EQ(witnesses.size(), num_pairs);
167
168 for (size_t j = 0; j < num_pairs; ++j) {
169 this->verify_opening_pair(opening_pairs[j], witnesses[j]);
170 }
171 }
172
174
175 // Per-test-suite set-up.
176 // Called before the first test in this test suite.
177 // Can be omitted if not needed.
178 static void SetUpTestSuite()
179 {
180 // Avoid reallocating static objects if called in subclasses of FooTest.
181 if (commitment_key == nullptr) {
182 commitment_key = CreateCommitmentKey<CK>();
183 }
184 if (verification_key == nullptr) {
185 verification_key = CreateVerifierCommitmentKey<VK>();
186 }
187 }
188
189 // Per-test-suite tear-down.
190 // Called after the last test in this test suite.
191 // Can be omitted if not needed.
192 static void TearDownTestSuite() {}
193
194 static typename std::shared_ptr<CK> commitment_key;
195 static typename std::shared_ptr<VK> verification_key;
196};
197
198template <typename Curve>
199typename std::shared_ptr<CommitmentKey<Curve>> CommitmentTest<Curve>::commitment_key = nullptr;
200template <typename Curve>
201typename std::shared_ptr<VerifierCommitmentKey<Curve>> CommitmentTest<Curve>::verification_key = nullptr;
202
203using CommitmentSchemeParams = ::testing::Types<curve::BN254>;
204using IpaCommitmentSchemeParams = ::testing::Types<curve::Grumpkin>;
205// IMPROVEMENT: reinstate typed-tests for multiple field types, i.e.:
206// using CommitmentSchemeParams =
207// ::testing::Types<fake::Params<barretenberg::g1>, fake::Params<grumpkin::g1>, kzg::Params>;
208
209} // namespace proof_system::honk::pcs
Definition: polynomial.hpp:12
Definition: file_crs_factory.hpp:16
Definition: engine.hpp:10
CommitmentKey object over a pairing group 𝔾₁.
Definition: commitment_key.hpp:35
Definition: commitment_key.test.hpp:68
void verify_batch_opening_claim(std::span< const OpeningClaim< Curve > > multi_claims, std::span< const Polynomial > witnesses)
Ensures that a 'BatchOpeningClaim' is correct by checking that.
Definition: commitment_key.test.hpp:147
void verify_batch_opening_pair(std::span< const OpeningPair< Curve > > opening_pairs, std::span< const Polynomial > witnesses)
Ensures that a set of opening pairs is correct by checking that evaluations are correct by recomputin...
Definition: commitment_key.test.hpp:162
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition: claim.hpp:43
Opening pair (r,v) for some witness polynomial p(X) such that p(r) = v.
Definition: claim.hpp:12
Definition: verification_key.hpp:25
Provides interfaces for different 'CommitmentKey' classes.
Definition: claim.hpp:6