Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pairing_points.test.cpp
Go to the documentation of this file.
7#include <gtest/gtest.h>
8
9namespace bb::stdlib::recursion {
10
11template <typename Builder> class PairingPointsTests : public testing::Test {
12 public:
14};
15
16using Curves = testing::Types<stdlib::bn254<UltraCircuitBuilder>, stdlib::bn254<MegaCircuitBuilder>>;
18
20{
21 static constexpr size_t NUM_GATES_ADDED = 20;
22
23 typename TypeParam::Builder builder;
24
25 size_t num_gates = builder.num_gates();
27 EXPECT_EQ(NUM_GATES_ADDED, builder.num_gates() - num_gates)
28 << "There has been a change in the number of gates required to set default PairingPoints as public inputs.";
29
30 EXPECT_TRUE(CircuitChecker::check(builder));
31}
32
34{
35 using Builder = TypeParam::Builder;
38
40
41 Group P0(DEFAULT_PAIRING_POINTS_P0_X, DEFAULT_PAIRING_POINTS_P0_Y, /*assert_on_curve=*/false);
42 Group P1(DEFAULT_PAIRING_POINTS_P1_X, DEFAULT_PAIRING_POINTS_P1_Y, /*assert_on_curve=*/false);
43 P0.convert_constant_to_fixed_witness(&builder);
44 P1.convert_constant_to_fixed_witness(&builder);
45 PairingPoints<TypeParam> pp(P0, P1);
46 pp.set_public();
47 EXPECT_TRUE(CircuitChecker::check(builder));
48
49 // Validate default PairingPoints
50 CommitmentKey commitment_key;
51 bb::PairingPoints<curve::BN254> native_pp(P0.get_value(), P1.get_value());
52 EXPECT_TRUE(native_pp.check()) << "Default PairingPoints are not valid pairing points.";
53}
54
55TYPED_TEST(PairingPointsTests, TaggingMechanismWorks)
56{
57 using Curve = TypeParam;
58 using Builder = typename Curve::Builder;
60 using Group = PairingPoints::Group;
61 using Fr = PairingPoints::Fr;
62 using NativeFr = typename Curve::ScalarFieldNative;
63
65
66 Fr scalar_one = Fr::from_witness(&builder, NativeFr::random_element());
67 Fr scalar_two = Fr::from_witness(&builder, NativeFr::random_element());
68 Group P0 = Group::batch_mul({ Group::one(&builder) }, { scalar_one });
69 Group P1 = Group::batch_mul({ Group::one(&builder) }, { scalar_two });
70
71 // Check that no pairing points exist
72 EXPECT_TRUE(builder.pairing_points_tagging.has_single_pairing_point_tag());
73
74 PairingPoints pp_one = { P0, P1 };
75 PairingPoints pp_two = { P0, P1 };
76
77 // Check the tags
78 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_one.tag_index), 0U);
79 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_two.tag_index), 1U);
80
81 // Check that there are two different pairing points in the builder
82 EXPECT_FALSE(builder.pairing_points_tagging.has_single_pairing_point_tag());
83
84 // Merge the tags
85 pp_one.aggregate(pp_two);
86
87 // Check that the tags have been merged
88 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_two.tag_index), 0U);
89 EXPECT_TRUE(builder.pairing_points_tagging.has_single_pairing_point_tag());
90
91 // Create two new pairing points and aggregate with aggregate_multiple
92 PairingPoints pp_three = { P0, P1 };
93 PairingPoints pp_four = { P0, P1 };
94
95 // Check the tags
96 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_three.tag_index), 2U);
97 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_four.tag_index), 3U);
98
99 // Check that there are two different pairing points in the builder
100 EXPECT_FALSE(builder.pairing_points_tagging.has_single_pairing_point_tag());
101
102 // Merge the tags
103 std::vector<PairingPoints> pp_to_be_aggregated = { pp_one, pp_three, pp_four };
104 PairingPoints aggregated_pp = PairingPoints::aggregate_multiple(pp_to_be_aggregated);
105
106 // Check that the tags have been merged
107 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_one.tag_index), 4U);
108 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_two.tag_index), 4U);
109 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_three.tag_index), 4U);
110 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_four.tag_index), 4U);
111 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(aggregated_pp.tag_index), 4U);
112 EXPECT_TRUE(builder.pairing_points_tagging.has_single_pairing_point_tag());
113}
114
115TYPED_TEST(PairingPointsTests, TaggingMechanismFails)
116{
117
118 using Curve = TypeParam;
119 using Builder = typename Curve::Builder;
121 using Group = PairingPoints::Group;
122 using Fr = PairingPoints::Fr;
123 using NativeFr = typename Curve::ScalarFieldNative;
126
128
129 Fr scalar_one = Fr::from_witness(&builder, NativeFr::random_element());
130 Fr scalar_two = Fr::from_witness(&builder, NativeFr::random_element());
131 Group P0 = Group::batch_mul({ Group::one(&builder) }, { scalar_one });
132 Group P1 = Group::batch_mul({ Group::one(&builder) }, { scalar_two });
133
134 PairingPoints pp_one = { P0, P1 };
135 PairingPoints pp_two = { P0, P1 };
136 PairingPoints pp_three = { P0, P1 };
137
138 // Check the tags
139 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_one.tag_index), 0U);
140 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_two.tag_index), 1U);
141 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_three.tag_index), 2U);
142
143 // Check that there are different pairing points in the builder
144 EXPECT_FALSE(builder.pairing_points_tagging.has_single_pairing_point_tag());
145
146 // Merge the tags
147 pp_one.aggregate(pp_two);
148
149 // Check that the tags have not been merged
150 EXPECT_FALSE(builder.pairing_points_tagging.has_single_pairing_point_tag());
151
152 // Create a ProverInstance, expect failure because pairing points have not been aggregated
154 ProverInstance prover_instance(builder),
155 ::testing::HasSubstr(
156 "Pairing points must all be aggregated together. Either no pairing points should be created, or all "
157 "created pairing points must be aggregated into a single pairing point. Found 2 different pairing "
158 "points."));
159
160 // Aggregate pairing points
161 pp_one.aggregate(pp_three);
162
163 // Create a ProverInstance, expect failure because pairing points have not been set to public
165 ProverInstance prover_instance(builder),
166 ::testing::HasSubstr(
167 "Pairing points must be set to public in the circuit before constructing the ProverInstance."));
168
170 inputs.pairing_inputs = pp_one;
172
173 // Construct Prover instance successfully
174 ProverInstance prover_instance(builder);
175}
176
177TYPED_TEST(PairingPointsTests, CopyConstructorWorks)
178{
179 using Curve = TypeParam;
180 using Builder = typename Curve::Builder;
181
183 using Group = PairingPoints::Group;
184 using Fr = Curve::ScalarField;
185 using NativeFr = Curve::ScalarFieldNative;
186
188
189 Fr scalar_one = Fr::from_witness(&builder, NativeFr::random_element());
190 Fr scalar_two = Fr::from_witness(&builder, NativeFr::random_element());
191 Group P0 = Group::batch_mul({ Group::one(&builder) }, { scalar_one });
192 Group P1 = Group::batch_mul({ Group::one(&builder) }, { scalar_two });
193
194 PairingPoints pp_original = { P0, P1 };
195 PairingPoints pp_copy(pp_original);
196
197 // Check that there is only one tag
198 EXPECT_TRUE(builder.pairing_points_tagging.has_single_pairing_point_tag());
199
200 // Check that the tags are the same
201 BB_ASSERT_EQ(builder.pairing_points_tagging.get_tag(pp_original.tag_index),
202 builder.pairing_points_tagging.get_tag(pp_copy.tag_index));
203}
204
205} // namespace bb::stdlib::recursion
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
#define EXPECT_THROW_OR_ABORT(statement, matcher)
Definition assert.hpp:174
CommitmentKey object over a pairing group 𝔾₁.
An object storing two EC points that represent the inputs to a pairing check.
bool check() const
Perform the pairing check.
A ProverInstance is normally constructed from a finalized circuit and it contains all the information...
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
Manages the data that is propagated on the public inputs of an application/function circuit.
AluTraceBuilder builder
Definition alu.test.cpp:124
AvmProvingInputs inputs
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TYPED_TEST(PairingPointsTests, ConstructDefault)
TYPED_TEST_SUITE(PairingPointsTests, Curves)
testing::Types< stdlib::bn254< UltraCircuitBuilder >, stdlib::bn254< MegaCircuitBuilder > > Curves
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
An object storing two EC points that represent the inputs to a pairing check.
static uint32_t set_default_to_public(Builder *builder)
Set the witness indices for the default limbs of the pairing points to public.
void aggregate(PairingPoints const &other)
Compute a linear combination of the present pairing points with an input set of pairing points.
uint32_t set_public()
Set the witness indices for the limbs of the pairing points to public.
static PairingPoints aggregate_multiple(std::vector< PairingPoints > &pairing_points)
Aggregate multiple PairingPoints.