4#include <gtest/gtest.h>
10template <
typename Group,
typename Fr>
11typename Group::affine_element naive_scalar_mul(
const typename Group::element& base,
const Fr& scalar)
13 typename Group::element acc = Group::point_at_infinity;
14 typename Group::element runner = base;
16 for (
size_t i = 0; i < 256; ++i) {
17 if (bits.get_bit(i)) {
20 runner = runner.dbl();
22 return typename Group::affine_element(acc);
35 EXPECT_EQ(
b, expected);
43 EXPECT_EQ(one_y, expected);
67 EXPECT_EQ(
a ==
b,
true);
68 EXPECT_EQ(
a ==
a,
true);
70 b.self_set_infinity();
72 EXPECT_EQ(
a ==
b,
false);
75 EXPECT_EQ(
a == c,
false);
77 a.self_set_infinity();
79 EXPECT_EQ(
a ==
b,
true);
82TEST(
g1, MixedAddCheckAgainstConstants)
84 fq a_x{ 0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5 };
85 fq a_y{ 0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad };
86 fq a_z{ 0xfef593c9ce1df132, 0xe0486f801303c27d, 0x9bbd01ab881dc08e, 0x2a589badf38ec0f9 };
87 fq b_x{ 0xa1ec5d1398660db8, 0x6be3e1f6fd5d8ab1, 0x69173397dd272e11, 0x12575bbfe1198886 };
88 fq b_y{ 0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e };
89 fq expected_x{ 0x2a9d0201fccca20, 0x36f969b294f31776, 0xee5534422a6f646, 0x911dbc6b02310b6 };
90 fq expected_y{ 0x14c30aaeb4f135ef, 0x9c27c128ea2017a1, 0xf9b7d80c8315eabf, 0x35e628df8add760 };
91 fq expected_z{ 0xa43fe96673d10eb3, 0x88fbe6351753d410, 0x45c21cc9d99cb7d, 0x3018020aa6e9ede5 };
99 rhs.x = b_x.to_montgomery_form();
100 rhs.y = b_y.to_montgomery_form();
106 EXPECT_EQ(result == expected,
true);
111 fq a_x{ 0x8d1703aa518d827f, 0xd19cc40779f54f63, 0xabc11ce30d02728c, 0x10938940de3cbeec };
112 fq a_y{ 0xcf1798994f1258b4, 0x36307a354ad90a25, 0xcd84adb348c63007, 0x6266b85241aff3f };
113 fq a_z{ 0xe213e18fd2df7044, 0xb2f42355982c5bc8, 0xf65cf5150a3a9da1, 0xc43bde08b03aca2 };
114 fq expected_x{ 0xd5c6473044b2e67c, 0x89b185ea20951f3a, 0x4ac597219cf47467, 0x2d00482f63b12c86 };
115 fq expected_y{ 0x4e7e6c06a87e4314, 0x906a877a71735161, 0xaa7b9893cc370d39, 0x62f206bef795a05 };
116 fq expected_z{ 0x8813bdca7b0b115a, 0x929104dffdfabd22, 0x3fff575136879112, 0x18a299c1f683bdca };
131 EXPECT_EQ(result == expected,
true);
136 fq a_x{ 0x184b38afc6e2e09a, 0x4965cd1c3687f635, 0x334da8e7539e71c4, 0xf708d16cfe6e14 };
137 fq a_y{ 0x2a6ff6ffc739b3b6, 0x70761d618b513b9, 0xbf1645401de26ba1, 0x114a1616c164b980 };
138 fq a_z{ 0x10143ade26bbd57a, 0x98cf4e1f6c214053, 0x6bfdc534f6b00006, 0x1875e5068ababf2c };
139 fq b_x{ 0xafdb8a15c98bf74c, 0xac54df622a8d991a, 0xc6e5ae1f3dad4ec8, 0x1bd3fb4a59e19b52 };
140 fq b_y{ 0x21b3bb529bec20c0, 0xaabd496406ffb8c1, 0xcd3526c26ac5bdcb, 0x187ada6b8693c184 };
141 fq b_z{ 0xffcd440a228ed652, 0x8a795c8f234145f1, 0xd5279cdbabb05b95, 0xbdf19ba16fc607a };
142 fq expected_x{ 0x18764da36aa4cd81, 0xd15388d1fea9f3d3, 0xeb7c437de4bbd748, 0x2f09b712adf6f18f };
143 fq expected_y{ 0x50c5f3cab191498c, 0xe50aa3ce802ea3b5, 0xd9d6125b82ebeff8, 0x27e91ba0686e54fe };
144 fq expected_z{ 0xe4b81ef75fedf95, 0xf608edef14913c75, 0xfd9e178143224c96, 0xa8ae44990c8accd };
153 rhs.x = b_x.to_montgomery_form();
154 rhs.y = b_y.to_montgomery_form();
155 rhs.z = b_z.to_montgomery_form();
162 EXPECT_EQ(result == expected,
true);
181 result = lhs + rhs_b;
183 EXPECT_EQ(lhs == result,
true);
188 EXPECT_EQ(rhs == result,
true);
210 expected = lhs.
dbl();
212 EXPECT_EQ(result == expected,
true);
243 dbl_result =
a.dbl();
245 EXPECT_EQ(add_result == dbl_result,
true);
268 EXPECT_EQ(result == expected,
true);
288 EXPECT_EQ(rhs_c == result,
true);
301 expected = lhs.
dbl();
303 EXPECT_EQ(result == expected,
true);
315 add_result = lhs + rhs_b;
316 mixed_add_result = lhs + rhs;
318 EXPECT_EQ(add_result == mixed_add_result,
true);
323 size_t num_points = 2;
326 for (
size_t i = 0; i < num_points; ++i) {
330 normalized[i] = points[i];
332 g1::element::batch_normalize(&normalized[0], num_points);
334 for (
size_t i = 0; i < num_points; ++i) {
339 zz = points[i].z.
sqr();
340 zzz = points[i].z * zz;
341 result_x = normalized[i].x * zz;
342 result_y = normalized[i].y * zzz;
344 EXPECT_EQ((result_x == points[i].x),
true);
345 EXPECT_EQ((result_y == points[i].y),
true);
349TEST(
g1, GroupExponentiationCheckAgainstConstants)
351 fr a{ 0xb67299b792199cf0, 0xc1da7df1e7e12768, 0x692e427911532edf, 0x13dd85e87dc89978 };
354 fq expected_x{ 0x9bf840faf1b4ba00, 0xe81b7260d068e663, 0x7610c9a658d2c443, 0x278307cd3d0cddb0 };
355 fq expected_y{ 0xf6ed5fb779ebecb, 0x414ca771acbe183c, 0xe3692cb56dfbdb67, 0x3d3c5ed19b080a3 };
358 expected.
x = expected_x.to_montgomery_form();
359 expected.
y = expected_y.to_montgomery_form();
363 EXPECT_EQ(result == expected,
true);
371 fr scalar{ 0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e };
402TEST(
g1, GroupExponentiationConsistencyCheck)
416 EXPECT_EQ(result == expected,
true);
421 constexpr size_t num_generators = 128;
425 for (
size_t i = 0; i < result.size(); ++i) {
426 if ((i != j) && result[i] == y) {
433 for (
size_t k = 0; k < num_generators; ++k) {
434 EXPECT_EQ(is_unique(result[k], k),
true);
435 EXPECT_EQ(result[k].on_curve(),
true);
445 g1::affine_element::serialize_to_buffer(expected, &
buffer[0]);
449 EXPECT_EQ(result == expected,
true);
451template <
class T>
void write(
const T t)
453 FILE* fp = fopen(
"/dev/null",
"wb");
454 static_cast<void>(fwrite(&t,
sizeof(t), 1, fp));
455 static_cast<void>(fclose(fp));
458#if !defined(__wasm__)
462 EXPECT_NO_THROW(write<g1::affine_element>({}));
468 ASSERT_TRUE((bb::check_precomputed_generators<g1, "biggroup table offset generator", 1UL>()));
469 ASSERT_TRUE((bb::check_precomputed_generators<g1, "biggroup offset generator", 1UL>()));
470 ASSERT_TRUE((bb::check_precomputed_generators<g1, "ECCVM_OFFSET_GENERATOR", 1UL>()));
471 ASSERT_TRUE((bb::check_precomputed_generators<g1, "test generators", 2UL>()));
482 {{ 0x01624731e1195570, 0x3ba491482db4da14, 0x59e26bcea0d48bac, 0x0 },
"m=1"},
483 {{ 0x02c48e63c232aadf, 0x774922905b69b428, 0xb3c4d79d41a91758, 0x0 },
"m=2"},
484 {{ 0x0426d595a34c004e, 0xb2edb3d8891e8e3c, 0x0da7436be27da304, 0x1 },
"m=3"},
488 for (
const auto& tc : boundary_cases) {
489 fr base_scalar{ tc.limbs[0], tc.limbs[1], tc.limbs[2], tc.limbs[3] };
494 EXPECT_EQ(naive_result.
on_curve(),
true) << tc.tag;
495 EXPECT_EQ(endo_result.
on_curve(),
true) << tc.tag;
496 EXPECT_EQ(endo_result, naive_result) << tc.tag;
499 for (
size_t i = 0; i < 100; ++i) {
502 fr scalar = base_scalar +
fr(offset_int);
506 EXPECT_EQ(naive_res.
on_curve(),
true) << tc.tag <<
" offset " << i;
507 EXPECT_EQ(endo_res.
on_curve(),
true) << tc.tag <<
" offset " << i;
508 EXPECT_EQ(endo_res, naive_res) << tc.tag <<
" offset " << i;
constexpr bool is_point_at_infinity() const noexcept
constexpr bool on_curve() const noexcept
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
constexpr element dbl() const noexcept
constexpr void self_dbl() noexcept
BB_INLINE constexpr bool on_curve() const noexcept
BB_INLINE constexpr void self_set_infinity() noexcept
BB_INLINE constexpr bool is_point_at_infinity() const noexcept
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
group_elements::affine_element< Fq, Fr, Params > affine_element
static constexpr element one
static constexpr affine_element affine_one
group_elements::element< Fq, Fr, Params > element
static std::vector< affine_element > derive_generators(const std::vector< uint8_t > &domain_separator_bytes, const size_t num_generators, const size_t starting_index=0)
Derives generator points via hash-to-curve.
std::unique_ptr< uint8_t[]> buffer
Entry point for Barretenberg command-line interface.
field< Bn254FqParams > fq
field< Bn254FrParams > fr
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
static constexpr fq one_y
static constexpr field one()
BB_INLINE constexpr field to_montgomery_form() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
static BB_INLINE void __copy(const field &a, field &r) noexcept
BB_INLINE constexpr void self_to_montgomery_form() &noexcept
static constexpr field zero()