Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
affine_element.test.cpp
Go to the documentation of this file.
2
13
14#include "gmock/gmock.h"
15#include <algorithm>
16#include <fstream>
17#include <gtest/gtest.h>
18#include <iterator>
19#include <tuple>
20
21using ::testing::Each;
22using ::testing::ElementsAreArray;
23using ::testing::Eq;
24using ::testing::Property;
25
26using namespace bb;
27
28namespace {
29template <typename G1> class TestAffineElement : public testing::Test {
30 using element = typename G1::element;
31 using affine_element = typename G1::affine_element;
32 using Fr = typename G1::Fr;
33
34 public:
35 static void test_read_write_buffer()
36 {
37 // a generic point
38 {
39 affine_element P = affine_element(element::random_element());
40 affine_element R;
41
42 std::vector<uint8_t> v(64);
43 uint8_t* ptr = v.data();
44 affine_element::serialize_to_buffer(P, ptr);
45
46 R = affine_element::serialize_from_buffer(ptr);
47 ASSERT_TRUE(R.on_curve());
48 ASSERT_TRUE(P == R);
49 }
50
51 // point at infinity
52 {
53 affine_element P = affine_element(element::random_element());
54 P.self_set_infinity();
55 affine_element R;
56
57 std::vector<uint8_t> v(64);
58 uint8_t* ptr = v.data();
59 affine_element::serialize_to_buffer(P, ptr);
60
61 R = affine_element::serialize_from_buffer(ptr);
62 ASSERT_TRUE(R.is_point_at_infinity());
63 ASSERT_TRUE(P == R);
64 }
65 }
66
67 // Verify that serialize_from_buffer rejects off-curve bytes by throwing.
68 static void test_deserialize_off_curve_throws()
69 {
70 using Fq = typename G1::Fq;
71 // Take a valid on-curve point and corrupt its y-coordinate.
72 // P.y + 1 satisfies (y+1)^2 != y^2 (i.e. off-curve) unless 2y + 1 = 0 (prob ~1/p).
73 affine_element P = affine_element(element::random_element());
74 affine_element off_curve;
75 off_curve.x = P.x;
76 off_curve.y = P.y + Fq::one();
77
78 std::vector<uint8_t> v(sizeof(affine_element));
79 uint8_t* ptr = v.data();
80 affine_element::serialize_to_buffer(off_curve, ptr);
81
82 if (!off_curve.on_curve()) {
83#ifndef __wasm__
84 EXPECT_THROW_OR_ABORT(affine_element::serialize_from_buffer(ptr), "not on the curve");
85#endif
86 }
87 }
88
89 static void test_read_and_write()
90 {
91 // a generic point
92 {
93 affine_element P = affine_element(element::random_element());
94 [[maybe_unused]] affine_element R;
95
96 std::vector<uint8_t> v(sizeof(R));
97 uint8_t* ptr = v.data();
98 write(ptr, P);
99 ASSERT_TRUE(P.on_curve());
100
101 // // Reset to start?
102 // ptr = v.data();
103
104 const uint8_t* read_ptr = v.data();
105 // good read
106 read(read_ptr, R);
107 ASSERT_TRUE(R.on_curve());
108 ASSERT_TRUE(P == R);
109 }
110 }
111
112 static void test_msgpack_serialization()
113 {
114 // a generic point
115 {
116 affine_element P = affine_element(element::random_element());
117
118 // Serialize using msgpack
119 msgpack::sbuffer sbuf;
120 msgpack::pack(sbuf, P);
121
122 // Deserialize using msgpack
123 msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());
124 msgpack::object deserialized = oh.get();
125
126 affine_element R;
127 deserialized.convert(R);
128
129 ASSERT_TRUE(R.on_curve() && !R.is_point_at_infinity());
130 ASSERT_TRUE(P == R);
131 }
132
133 // point at infinity
134 {
135 affine_element P = affine_element(element::random_element());
136 P.self_set_infinity();
137
138 // Serialize using msgpack
139 msgpack::sbuffer sbuf;
140 msgpack::pack(sbuf, P);
141
142 // Deserialize using msgpack
143 msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());
144 msgpack::object deserialized = oh.get();
145
146 affine_element R;
147 deserialized.convert(R);
148
149 ASSERT_TRUE(R.is_point_at_infinity());
150 ASSERT_TRUE(P == R);
151 }
152 }
153
154 static void test_point_compression()
155 {
156 for (size_t i = 0; i < 10; i++) {
157 affine_element P = affine_element(element::random_element());
158 uint256_t compressed = uint256_t(P.x);
159 if (uint256_t(P.y).get_bit(0)) {
160 compressed.data[3] |= group_elements::UINT256_TOP_LIMB_MSB;
161 }
162 affine_element Q = affine_element::from_compressed(compressed);
163 EXPECT_EQ(P, Q);
164 }
165 }
166
167 static void test_point_compression_unsafe()
168 {
169 for (size_t i = 0; i < 100; i++) {
170 affine_element P = affine_element(element::random_element());
171 uint256_t compressed = uint256_t(P.x);
172
173 // Note that we do not check the point Q_points[1] because its highly unlikely to hit a point P on the curve
174 // such that r < P.x < q.
175 std::array<affine_element, 2> Q_points = affine_element::from_compressed_unsafe(compressed);
176 EXPECT_EQ(P, Q_points[0]);
177 }
178 }
179
180 static void test_add_affine()
181 {
182 element lhs = element::random_element();
183 affine_element lhs_affine(lhs);
184
185 element rhs = element::random_element();
186 affine_element rhs_affine(rhs);
187
188 element expected = lhs + rhs;
189 affine_element result = lhs_affine + rhs_affine;
190 EXPECT_EQ(element(result) == expected, true);
191 }
192
193 // Regression test for the large-modulus mixed-addition path: element +/- affine_element must
194 // detect when the affine operand is the infinity sentinel (x = modulus, y = 0). Previously the
195 // operator only checked whether `*this` was infinity, so adding the infinity sentinel to a
196 // normal point fell through to the arithmetic and produced an off-curve garbage result.
197 // operator-=(affine) inherits the bug via its `to_add{other.x, -other.y}` delegation.
198 static void test_mixed_add_infinity_regression()
199 {
200 const element P = element::random_element();
201 const affine_element Q_inf = affine_element::infinity();
202
203 // P (+/-) infinity == P, both as out-of-place and compound-assignment.
204 EXPECT_EQ(P + Q_inf, P);
205 EXPECT_EQ(P - Q_inf, P);
206 {
207 element acc = P;
208 acc += Q_inf;
209 EXPECT_EQ(acc, P);
210 }
211 {
212 element acc = P;
213 acc -= Q_inf;
214 EXPECT_EQ(acc, P);
215 }
216
217 // infinity (+/-) P == +/-P
218 EXPECT_EQ(Q_inf + P, P);
219 EXPECT_EQ(Q_inf - P, -P);
220
221 // *this = infinity, other = infinity must remain infinity (not become {modulus, 0, 1}).
222 element inf_elem = element::zero();
223 ASSERT_TRUE(inf_elem.is_point_at_infinity());
224 EXPECT_TRUE((inf_elem + Q_inf).is_point_at_infinity());
225 EXPECT_TRUE((inf_elem - Q_inf).is_point_at_infinity());
226
227 // The result of mixing a normal point with the infinity sentinel must remain on-curve.
228 EXPECT_TRUE((P + Q_inf).on_curve());
229 EXPECT_TRUE((P - Q_inf).on_curve());
230 }
231
232 // Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie
233 // on the curve, depending on the y-coordinate.
234 static void test_infinity_regression()
235 {
236 affine_element P;
237 P.self_set_infinity();
238 affine_element R(0, P.y);
239 ASSERT_FALSE(P == R);
240 }
241 static void test_infinity_ordering_regression()
242 {
243 affine_element P(0, 1);
244 affine_element Q(0, 1);
245
246 P.self_set_infinity();
247 EXPECT_NE(P < Q, Q < P);
248 }
249
250 // Verify that from_compressed with an x that has no y on the curve returns the (0,0) sentinel.
251 static void test_point_compression_invalid_x()
252 {
253 using Fq = typename G1::Fq;
254 size_t invalid_count = 0;
255 for (size_t i = 0; i < 20; ++i) {
256 affine_element result = affine_element::from_compressed(uint256_t(Fq::random_element()));
257 if (!result.on_curve()) {
258 ++invalid_count;
259 // from_compressed returns (0, 0) when x has no valid y
260 EXPECT_EQ(result.x, Fq::zero());
261 EXPECT_EQ(result.y, Fq::zero());
262 }
263 }
264 // With 20 trials ~10 should have no valid y, so we almost certainly exercise this path
265 EXPECT_GT(invalid_count, 0U);
266 }
267
272 static void test_batch_endomorphism_by_minus_one()
273 {
274 constexpr size_t num_points = 2;
275 std::vector<affine_element> affine_points(num_points, affine_element::one());
276
278 element::batch_mul_with_endomorphism(affine_points, -affine_element::Fr::one());
279
280 for (size_t i = 0; i < num_points; i++) {
281 EXPECT_EQ(affine_points[i], -result[i]);
282 }
283 }
284
289 static void test_fixed_point_at_infinity()
290 {
291 using Fq = affine_element::Fq;
292 affine_element P = affine_element::infinity();
293 affine_element Q(Fq::zero(), Fq::zero());
294 Q.x.self_set_msb();
295 affine_element R = affine_element(element::random_element());
296 EXPECT_EQ(P, Q);
297 EXPECT_NE(P, R);
298 }
299
300 static void test_infinity_mul_by_scalar_is_infinity()
301 {
302 auto result = affine_element::infinity() * Fr::random_element();
303 EXPECT_TRUE(result.is_point_at_infinity());
304 }
305
306 static void test_batch_mul_matches_non_batch_mul()
307 {
308 constexpr size_t num_points = 512;
309 std::vector<affine_element> affine_points(num_points - 1, affine_element::infinity());
310 affine_points.push_back(affine_element::infinity());
311 Fr exponent = Fr::random_element();
313 std::transform(affine_points.begin(),
314 affine_points.end(),
315 std::back_inserter(expected),
316 [exponent](const auto& el) { return el * exponent; });
317 std::vector<affine_element> result = element::batch_mul_with_endomorphism(affine_points, exponent);
318 EXPECT_THAT(result, ElementsAreArray(expected));
319 }
320
321 static void test_infinity_batch_mul_by_scalar_is_infinity()
322 {
323 constexpr size_t num_points = 1024;
324 std::vector<affine_element> affine_points(num_points, affine_element::infinity());
325 std::vector<affine_element> result = element::batch_mul_with_endomorphism(affine_points, Fr::random_element());
326 EXPECT_THAT(result, Each(Property(&affine_element::is_point_at_infinity, Eq(true))));
327 }
328
329 static void test_batch_mul_endomorphism_even_scalars()
330 {
331 const affine_element P = affine_element::one();
332 const std::vector<affine_element> points(4, P);
333 for (const Fr scalar : { Fr(0), Fr(2), Fr(4), Fr(6), Fr(8) }) {
334 const auto result = element::batch_mul_with_endomorphism(points, scalar);
335 const affine_element expected(element(P) * scalar);
336 for (size_t i = 0; i < points.size(); ++i) {
337 EXPECT_EQ(result[i], expected);
338 }
339 }
340 }
341
342 // === helpers for K2-bit-width coverage of batch_mul_with_endomorphism ===
343
344 // bit_length of the K2 half of the GLV split of `scalar` (0 for zero).
345 static size_t k2_bit_length(const Fr& scalar)
346 {
347 const Fr conv = scalar.from_montgomery_form();
348 if (conv.is_zero()) {
349 return 0;
350 }
351 const auto endo = Fr::split_into_endomorphism_scalars(conv);
352 const auto& k2 = endo.second;
353 if (k2[1] != 0) {
354 return 128 - static_cast<size_t>(__builtin_clzll(k2[1]));
355 }
356 if (k2[0] != 0) {
357 return 64 - static_cast<size_t>(__builtin_clzll(k2[0]));
358 }
359 return 0;
360 }
361
362 // Search random scalars until one decomposes to K2 of exactly `target_bits` bits.
363 // K2 ≤ 127 bits is proven, so any target in [0, 127] is reachable; populations:
364 // 127 bits ≈ 50%, 126 bits ≈ 25%, 125 bits ≈ 12.5% — all easily found.
365 static Fr find_scalar_with_k2_bits(size_t target_bits, size_t max_attempts = 2000)
366 {
367 for (size_t i = 0; i < max_attempts; ++i) {
368 const Fr s = Fr::random_element();
369 if (k2_bit_length(s) == target_bits) {
370 return s;
371 }
372 }
373 throw_or_abort("could not find scalar with desired K2 bit-width");
374 }
375
376 // Run batch_mul_with_endomorphism on `num_points` independent random generators
377 // and assert it matches per-point projective multiplication.
378 static void check_batch_mul_against_naive(size_t num_points, const Fr& scalar)
379 {
381 points.reserve(num_points);
382 for (size_t i = 0; i < num_points; ++i) {
383 points.push_back(affine_element(element::random_element()));
384 }
386 expected.reserve(num_points);
387 for (const auto& p : points) {
388 expected.push_back(affine_element(element(p) * scalar));
389 }
390 const std::vector<affine_element> result = element::batch_mul_with_endomorphism(points, scalar);
391 ASSERT_EQ(result.size(), expected.size());
392 EXPECT_THAT(result, ElementsAreArray(expected));
393 }
394
395 // === 9 coverage tests for batch_mul_with_endomorphism ===
396
397 // (0) scalar = 0 ⇒ every output is the point at infinity.
398 static void test_batch_mul_zero_scalar()
399 {
400 constexpr size_t num_points = 64;
402 points.reserve(num_points);
403 for (size_t i = 0; i < num_points; ++i) {
404 points.push_back(affine_element(element::random_element()));
405 }
406 const std::vector<affine_element> result = element::batch_mul_with_endomorphism(points, Fr(0));
407 ASSERT_EQ(result.size(), num_points);
408 for (const auto& r : result) {
409 EXPECT_TRUE(r.is_point_at_infinity());
410 }
411 }
412
413 // (1) num_points coprime to typical num_threads.
414 static void test_batch_mul_num_points_not_multiple_of_threads()
415 {
416 check_batch_mul_against_naive(17, Fr::random_element());
417 }
418
419 // (2) scalar < 2^127 ⇒ GLV gives k1 = scalar, k2 = 0 (proven: c1=c2=0 when k<r/|b1|).
420 static void test_batch_mul_scalar_under_127_bits()
421 {
422 // Top nibble of the upper 64-bit limb is 0x3 ⇒ bit 127 = 0 and bit_length(scalar) = 126.
423 const Fr scalar(uint256_t{ 0xdeadbeefcafef00dULL, 0x3edcba98765432f1ULL, 0, 0 });
424 ASSERT_EQ(k2_bit_length(scalar), 0U);
425 check_batch_mul_against_naive(64, scalar);
426 }
427
428 // (3) scalar's bottom 127 bits all zero (= 2^127).
429 static void test_batch_mul_scalar_low_127_bits_zero()
430 {
431 const Fr scalar(uint256_t{ 0, 0, 1, 0 });
432 check_batch_mul_against_naive(64, scalar);
433 }
434
435 // (4) K2 = 128 bits — must never occur (K2 < 2^127 proven for BN254/Grumpkin GLV).
436 static void test_batch_mul_k2_128_bits_never_occurs()
437 {
438 for (size_t i = 0; i < 10000; ++i) {
439 const Fr s = Fr::random_element();
440 const size_t bits = k2_bit_length(s);
441 ASSERT_LE(bits, 127U) << "GLV split must produce K2 ≤ 127 bits; got " << bits << " bits on sample " << i;
442 }
443 }
444
445 // (5) K2 = 127 bits — init from pos-126 K2 window (top window magnitude ≥ 1).
446 static void test_batch_mul_k2_127_bits()
447 {
448 const Fr scalar = find_scalar_with_k2_bits(127);
449 ASSERT_EQ(k2_bit_length(scalar), 127U);
450 check_batch_mul_against_naive(64, scalar);
451 }
452
453 // (6) K2 = 126 bits — Booth carry from bit-125 lookback still gives top-window magnitude 1.
454 static void test_batch_mul_k2_126_bits()
455 {
456 const Fr scalar = find_scalar_with_k2_bits(126);
457 ASSERT_EQ(k2_bit_length(scalar), 126U);
458 check_batch_mul_against_naive(64, scalar);
459 }
460
461 // (7) K2 = 125 bits — top K2 window is empty; init falls through to pos-124 K1 window.
462 static void test_batch_mul_k2_125_bits()
463 {
464 const Fr scalar = find_scalar_with_k2_bits(125);
465 ASSERT_EQ(k2_bit_length(scalar), 125U);
466 check_batch_mul_against_naive(64, scalar);
467 }
468
469 // (8) empty points span.
470 static void test_batch_mul_empty_input()
471 {
472 const std::vector<affine_element> points;
473 const std::vector<affine_element> result = element::batch_mul_with_endomorphism(points, Fr::random_element());
474 EXPECT_TRUE(result.empty());
475 }
476
477 // (9) num_points < num_threads.
478 static void test_batch_mul_size_less_than_num_threads()
479 {
480 for (size_t sz : { size_t{ 1 }, size_t{ 2 }, size_t{ 3 } }) {
481 check_batch_mul_against_naive(sz, Fr::random_element());
482 }
483 }
484
485 // (10) Small scalars exercise the predicate-true path. The hoisted edge mask
486 // replaces the run-time `x == x` / `2·A + B == O` probes with a precomputed
487 // uint64; a regression here would either falsely set or falsely clear a bit
488 // and produce a wrong result against naive multiplication. Sweeping scalars
489 // with K2 = 0 hits all (a, b) recurrence states where |b| stays 0 — exactly
490 // the regime where Edge 1 / Edge 2 fire.
491 static void test_batch_mul_small_scalars_edge_predicate()
492 {
493 constexpr size_t num_points = 8;
495 points.reserve(num_points);
496 for (size_t i = 0; i < num_points; ++i) {
497 points.push_back(affine_element(element::random_element()));
498 }
499 // Cover [-32, 32] (Booth digits range ±1..±8 per window, so small scalars
500 // exercise every magnitude-comparison branch of edge_for_combined/_add).
501 for (int64_t s = -32; s <= 32; ++s) {
502 const Fr scalar = (s >= 0) ? Fr(static_cast<uint64_t>(s)) : -Fr(static_cast<uint64_t>(-s));
504 expected.reserve(num_points);
505 for (const auto& p : points) {
506 expected.push_back(affine_element(element(p) * scalar));
507 }
508 const std::vector<affine_element> result = element::batch_mul_with_endomorphism(points, scalar);
509 ASSERT_EQ(result.size(), expected.size());
510 for (size_t i = 0; i < num_points; ++i) {
511 EXPECT_EQ(result[i], expected[i]) << "scalar = " << s << ", point index = " << i;
512 }
513 }
514 }
515
516 static void test_batch_mul_randomized_matches_naive()
517 {
518 for (size_t trial = 0; trial < 24; ++trial) {
519 const size_t num_points = 1 + (trial % 19);
520 Fr scalar = Fr::random_element();
521 if ((trial % 8) == 0) {
522 scalar = Fr(static_cast<uint64_t>(trial + 1));
523 }
524 check_batch_mul_against_naive(num_points, scalar);
525 }
526 }
527
528 static void test_frc_codec_round_trip()
529 {
530 using FrField = FrCodec::DataType;
531 affine_element point = affine_element::random_element();
534 affine_element::PUBLIC_INPUTS_SIZE);
535 auto reconstructed = FrCodec::deserialize_from_fields<affine_element>(limbs);
536 EXPECT_EQ(reconstructed, point);
537 }
538
539 // The point at infinity, the generator, and any scalar multiple of the generator must all be
540 // recognized as members of the prime-order subgroup.
541 static void test_is_in_prime_subgroup_accepts_subgroup_points()
542 {
543 EXPECT_TRUE(affine_element::infinity().is_in_prime_subgroup());
544 EXPECT_TRUE(affine_element::one().is_in_prime_subgroup());
545
546 for (size_t i = 0; i < 8; ++i) {
547 affine_element P = affine_element(element::random_element());
548 EXPECT_TRUE(P.is_in_prime_subgroup());
549 }
550 }
551};
552
553// using TestTypes = testing::Types<bb::g1>;
554using TestTypes = testing::Types<bb::g1, grumpkin::g1, secp256k1::g1, secp256r1::g1>;
555} // namespace
556
557TYPED_TEST_SUITE(TestAffineElement, TestTypes);
558
559TYPED_TEST(TestAffineElement, AddAffine)
560{
561 TestFixture::test_add_affine();
562}
563
564// Regression test for `element +/- affine_element` when the affine operand is the infinity sentinel.
565// Exercises both the large-modulus and small-modulus branches of `element::operator+=(affine)`.
566TYPED_TEST(TestAffineElement, MixedAddInfinityRegression)
567{
568 TestFixture::test_mixed_add_infinity_regression();
569}
570
571TYPED_TEST(TestAffineElement, ReadWrite)
572{
573 TestFixture::test_read_and_write();
574}
575
576TYPED_TEST(TestAffineElement, ReadWriteBuffer)
577{
578 TestFixture::test_read_write_buffer();
579 TestFixture::test_msgpack_serialization();
580}
581
582TYPED_TEST(TestAffineElement, PointCompression)
583{
584 if constexpr (TypeParam::Fq::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
585 GTEST_SKIP();
586 } else {
587 TestFixture::test_point_compression();
588 }
589}
590
591TYPED_TEST(TestAffineElement, FixedInfinityPoint)
592{
593 if constexpr (TypeParam::Fq::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
594 GTEST_SKIP();
595 } else {
596 TestFixture::test_fixed_point_at_infinity();
597 }
598}
599
600TYPED_TEST(TestAffineElement, PointCompressionUnsafe)
601{
602 if constexpr (TypeParam::Fq::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
603 TestFixture::test_point_compression_unsafe();
604 } else {
605 GTEST_SKIP();
606 }
607}
608
609TYPED_TEST(TestAffineElement, InfinityOrderingRegression)
610{
611 TestFixture::test_infinity_ordering_regression();
612}
613
614namespace bb::group_elements {
615// mul_with_endomorphism and mul_without_endomorphism are private in affine_element.
616// We could make those public to test or create other public utilities, but to keep the API intact we
617// instead mark TestElementPrivate as a friend class so that our test functions can have access.
619 public:
620 template <typename Element, typename Scalar>
621 static Element mul_without_endomorphism(const Element& element, const Scalar& scalar)
622 {
623 return element.mul_without_endomorphism(scalar);
624 }
625 template <typename Element, typename Scalar>
626 static Element mul_with_endomorphism(const Element& element, const Scalar& scalar)
627 {
628 return element.mul_with_endomorphism(scalar);
629 }
630};
631} // namespace bb::group_elements
632
633// Endomorphism-specialized multiplication should match generic multiplication on every curve that supports it.
634TYPED_TEST(TestAffineElement, MulWithEndomorphismMatchesMulWithoutEndomorphism)
635{
636 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
637 GTEST_SKIP();
638 } else {
639 using element_t = typename TypeParam::element;
640 using Fr = typename TypeParam::Fr;
641 for (int i = 0; i < 100; i++) {
642 element_t x1(element_t::random_element());
643 Fr f1 = Fr::random_element();
646 EXPECT_EQ(r1, r2);
647 }
648 }
649}
650
651TYPED_TEST(TestAffineElement, MulWithEndomorphismEdgeCasesMatchMulWithoutEndomorphism)
652{
653 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
654 GTEST_SKIP();
655 } else {
656 using element_t = typename TypeParam::element;
657 using Fr = typename TypeParam::Fr;
658
659 const element_t point(element_t::random_element());
660 std::vector<Fr> scalars;
661 scalars.reserve(96);
662 for (uint64_t i = 0; i <= 64; ++i) {
663 scalars.emplace_back(i);
664 }
665 scalars.push_back(-Fr::one());
666 for (const size_t bit : { 125UL, 126UL, 127UL }) {
667 const uint256_t power = uint256_t(1) << bit;
668 for (const uint64_t delta : { 0UL, 1UL, 2UL, 7UL, 8UL, 15UL, 16UL }) {
669 scalars.emplace_back(power + delta);
670 if (delta != 0) {
671 scalars.emplace_back(power - delta);
672 }
673 }
674 }
675
676 for (const Fr& scalar : scalars) {
677 const element_t expected = bb::group_elements::TestElementPrivate::mul_without_endomorphism(point, scalar);
678 EXPECT_EQ(bb::group_elements::TestElementPrivate::mul_with_endomorphism(point, scalar), expected);
679 EXPECT_EQ(point * scalar, expected);
680 EXPECT_EQ(point.mul_const_time(scalar), expected);
681 }
682 }
683}
684
685// mul_const_time must agree with operator* on every input, including edge cases (0, 1, n-1, low and
686// high Hamming weight).
687TYPED_TEST(TestAffineElement, MulConstTimeMatchesOperatorMul)
688{
689 using element_t = typename TypeParam::element;
690 using Fr = typename TypeParam::Fr;
691 element_t G(element_t::random_element());
692
693 // Edge-case scalars
694 for (Fr s : { Fr::zero(), Fr::one(), -Fr::one(), Fr(2), Fr(uint256_t(1) << 128) }) {
695 EXPECT_EQ(G.mul_const_time(s), G * s);
696 }
697 // Random scalars
698 for (int i = 0; i < 50; ++i) {
700 EXPECT_EQ(G.mul_const_time(s), G * s);
701 }
702}
703
704// FrCodec is defined only for BN254 and Grumpkin (the two curves whose points appear in transcripts).
705TYPED_TEST(TestAffineElement, FrCodecRoundTrip)
706{
708 TestFixture::test_frc_codec_round_trip();
709 } else {
710 GTEST_SKIP();
711 }
712}
713
714// Even scalars exercise zero and low-magnitude Booth digits in batch_mul_with_endomorphism.
715// The results should match ordinary point multiplication for every point in the batch.
716TYPED_TEST(TestAffineElement, BatchMulEndomorphismEvenScalars)
717{
718 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
719 GTEST_SKIP();
720 } else {
721 TestFixture::test_batch_mul_endomorphism_even_scalars();
722 }
723}
724
725// Multiplication of a point at infinity by a scalar should be a point at infinity
726TYPED_TEST(TestAffineElement, InfinityMulByScalarIsInfinity)
727{
728 TestFixture::test_infinity_mul_by_scalar_is_infinity();
729}
730
731// Batched multiplication of points should match non-batched multiplication
732TYPED_TEST(TestAffineElement, BatchMulMatchesNonBatchMul)
733{
734 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
735 GTEST_SKIP();
736 } else {
737 TestFixture::test_batch_mul_matches_non_batch_mul();
738 }
739}
740
741// Batched multiplication of a point at infinity by a scalar should result in points at infinity
742TYPED_TEST(TestAffineElement, InfinityBatchMulByScalarIsInfinity)
743{
744 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
745 GTEST_SKIP();
746 } else {
747 TestFixture::test_infinity_batch_mul_by_scalar_is_infinity();
748 }
749}
750
751TYPED_TEST(TestAffineElement, BatchEndomoprhismByMinusOne)
752{
753 if constexpr (TypeParam::USE_ENDOMORPHISM) {
754 TestFixture::test_batch_endomorphism_by_minus_one();
755 } else {
756 GTEST_SKIP();
757 }
758}
759
760// Coverage of batch_mul_with_endomorphism — exercises the K1/K2-interleaved Booth
761// main loop's accumulator-init paths and the standard edge cases (thread-divisor
762// quirks, empty inputs, tiny inputs).
763TYPED_TEST(TestAffineElement, BatchMulZeroScalar)
764{
765 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
766 GTEST_SKIP();
767 } else {
768 TestFixture::test_batch_mul_zero_scalar();
769 }
770}
771
772TYPED_TEST(TestAffineElement, BatchMulNumPointsNotMultipleOfThreads)
773{
774 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
775 GTEST_SKIP();
776 } else {
777 TestFixture::test_batch_mul_num_points_not_multiple_of_threads();
778 }
779}
780
781TYPED_TEST(TestAffineElement, BatchMulScalarUnder127Bits)
782{
783 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
784 GTEST_SKIP();
785 } else {
786 TestFixture::test_batch_mul_scalar_under_127_bits();
787 }
788}
789
790TYPED_TEST(TestAffineElement, BatchMulScalarLow127BitsZero)
791{
792 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
793 GTEST_SKIP();
794 } else {
795 TestFixture::test_batch_mul_scalar_low_127_bits_zero();
796 }
797}
798
799TYPED_TEST(TestAffineElement, BatchMulK2128BitsNeverOccurs)
800{
801 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
802 GTEST_SKIP();
803 } else {
804 TestFixture::test_batch_mul_k2_128_bits_never_occurs();
805 }
806}
807
808TYPED_TEST(TestAffineElement, BatchMulK2127Bits)
809{
810 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
811 GTEST_SKIP();
812 } else {
813 TestFixture::test_batch_mul_k2_127_bits();
814 }
815}
816
817TYPED_TEST(TestAffineElement, BatchMulK2126Bits)
818{
819 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
820 GTEST_SKIP();
821 } else {
822 TestFixture::test_batch_mul_k2_126_bits();
823 }
824}
825
826TYPED_TEST(TestAffineElement, BatchMulK2125Bits)
827{
828 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
829 GTEST_SKIP();
830 } else {
831 TestFixture::test_batch_mul_k2_125_bits();
832 }
833}
834
835TYPED_TEST(TestAffineElement, BatchMulEmptyInput)
836{
837 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
838 GTEST_SKIP();
839 } else {
840 TestFixture::test_batch_mul_empty_input();
841 }
842}
843
844TYPED_TEST(TestAffineElement, BatchMulSizeLessThanNumThreads)
845{
846 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
847 GTEST_SKIP();
848 } else {
849 TestFixture::test_batch_mul_size_less_than_num_threads();
850 }
851}
852
853TYPED_TEST(TestAffineElement, BatchMulRandomizedMatchesNaive)
854{
855 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
856 GTEST_SKIP();
857 } else {
858 TestFixture::test_batch_mul_randomized_matches_naive();
859 }
860}
861
862TYPED_TEST(TestAffineElement, BatchMulSmallScalarsEdgePredicate)
863{
864 if constexpr (!TypeParam::USE_ENDOMORPHISM) {
865 GTEST_SKIP();
866 } else {
867 TestFixture::test_batch_mul_small_scalars_edge_predicate();
868 }
869}
870
871// Verify that serialize_from_buffer rejects off-curve bytes by throwing (tests the invalid-curve attack fix).
872TYPED_TEST(TestAffineElement, DeserializeOffCurveThrows)
873{
874 TestFixture::test_deserialize_off_curve_throws();
875}
876
877// Verify is_in_prime_subgroup accepts known prime-order subgroup points
878TYPED_TEST(TestAffineElement, IsInPrimeSubgroupAcceptsSubgroupPoints)
879{
880 TestFixture::test_is_in_prime_subgroup_accepts_subgroup_points();
881}
882
883// Verify that from_compressed returns the (0,0) sentinel for x values with no valid y.
884TYPED_TEST(TestAffineElement, PointCompressionInvalidX)
885{
886 if constexpr (TypeParam::Fq::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
887 GTEST_SKIP(); // from_compressed is not used on large-modulus curves
888 } else {
889 TestFixture::test_point_compression_invalid_x();
890 }
891}
892
893TEST(AffineElement, HashToCurve)
894{
896 test_vectors.emplace_back(std::vector<uint8_t>(),
898 fr(uint256_t("24c4cb9c1206ab5470592f237f1698abe684dadf0ab4d7a132c32b2134e2c12e")),
899 fr(uint256_t("0668b8d61a317fb34ccad55c930b3554f1828a0e5530479ecab4defe6bbc0b2e"))));
900
901 test_vectors.emplace_back(std::vector<uint8_t>{ 1 },
903 fr(uint256_t("107f1b633c6113f3222f39f6256f0546b41a4880918c86864b06471afb410454")),
904 fr(uint256_t("050cd3823d0c01590b6a50adcc85d2ee4098668fd28805578aa05a423ea938c6"))));
905
906 // "hello world"
907 test_vectors.emplace_back(std::vector<uint8_t>{ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 },
909 fr(uint256_t("037c5c229ae495f6e8d1b4bf7723fafb2b198b51e27602feb8a4d1053d685093")),
910 fr(uint256_t("10cf9596c5b2515692d930efa2cf3817607e4796856a79f6af40c949b066969f"))));
911
912 for (std::tuple<std::vector<uint8_t>, grumpkin::g1::affine_element> test_case : test_vectors) {
913 auto result = grumpkin::g1::affine_element::hash_to_curve(std::get<0>(test_case), 0);
914 auto expected_result = std::get<1>(test_case);
915 std::cout << result << std::endl;
916 EXPECT_TRUE(result == expected_result);
917 }
918}
#define EXPECT_THROW_OR_ABORT(statement, matcher)
Definition assert.hpp:192
static std::vector< fr > serialize_to_fields(const T &val)
Conversion from transcript values to bb::frs.
static Element mul_without_endomorphism(const Element &element, const Scalar &scalar)
static Element mul_with_endomorphism(const Element &element, const Scalar &scalar)
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:35
element mul_with_endomorphism(const Fr &scalar) const noexcept
element mul_without_endomorphism(const Fr &scalar) const noexcept
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:44
constexpr bool get_bit(uint64_t bit_index) const
bool expected_result
#define G(r, i, a, b, c, d)
Definition blake2s.cpp:116
test_vector test_vectors[]
const size_t num_points
AffineElement * rhs
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void read(B &it, field2< base_field, Params > &value)
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
field< Bn254FrParams > fr
Definition fr.hpp:155
void write(B &buf, field2< base_field, Params > const &value)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
testing::Types< VKTestParams< UltraFlavor, stdlib::recursion::honk::DefaultIO< UltraCircuitBuilder > >, VKTestParams< UltraFlavor, stdlib::recursion::honk::RollupIO >, VKTestParams< UltraKeccakFlavor, stdlib::recursion::honk::DefaultIO< UltraCircuitBuilder > >, VKTestParams< MegaFlavor, stdlib::recursion::honk::DefaultIO< MegaCircuitBuilder > > > TestTypes
Curve::Element Element
static constexpr field one()
static void split_into_endomorphism_scalars(const field &k, field &k1, field &k2)
Full-width endomorphism decomposition: k ≡ k1 - k2·λ (mod r). Modifies the field elements k1 and k2.
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr bool is_zero() const noexcept
BB_INLINE constexpr field from_montgomery_form() const noexcept
static constexpr field zero()
void throw_or_abort(std::string const &err)