Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
g1.test.cpp
Go to the documentation of this file.
1#include "g1.hpp"
4#include <gtest/gtest.h>
5
6using namespace bb;
7
8namespace {
9// Double-and-add scalar mul without endomorphism, used as reference for differential testing.
10template <typename Group, typename Fr>
11typename Group::affine_element naive_scalar_mul(const typename Group::element& base, const Fr& scalar)
12{
13 typename Group::element acc = Group::point_at_infinity;
14 typename Group::element runner = base;
15 uint256_t bits(scalar);
16 for (size_t i = 0; i < 256; ++i) {
17 if (bits.get_bit(i)) {
18 acc = acc + runner;
19 }
20 runner = runner.dbl();
21 }
22 return typename Group::affine_element(acc);
23}
24} // namespace
25
26// =========================
27// Parameter-related tests
28// =========================
29
30TEST(g1, BIsCorrect)
31{
33 fq expected = fq(3);
34
35 EXPECT_EQ(b, expected);
36}
37
38TEST(g1, OneYIsCorrect)
39{
41 auto [_, expected] = (Bn254G1Params::b + fq::one()).sqrt();
42
43 EXPECT_EQ(one_y, expected);
44}
45
46// =========================
47// Group-related tests
48// =========================
49
50TEST(g1, MixedAddCheckAgainstConstants)
51{
52 fq a_x{ 0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5 };
53 fq a_y{ 0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad };
54 fq a_z{ 0xfef593c9ce1df132, 0xe0486f801303c27d, 0x9bbd01ab881dc08e, 0x2a589badf38ec0f9 };
55 fq b_x{ 0xa1ec5d1398660db8, 0x6be3e1f6fd5d8ab1, 0x69173397dd272e11, 0x12575bbfe1198886 };
56 fq b_y{ 0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e };
57 fq expected_x{ 0x2a9d0201fccca20, 0x36f969b294f31776, 0xee5534422a6f646, 0x911dbc6b02310b6 };
58 fq expected_y{ 0x14c30aaeb4f135ef, 0x9c27c128ea2017a1, 0xf9b7d80c8315eabf, 0x35e628df8add760 };
59 fq expected_z{ 0xa43fe96673d10eb3, 0x88fbe6351753d410, 0x45c21cc9d99cb7d, 0x3018020aa6e9ede5 };
60 g1::element lhs;
62 g1::element result;
63 g1::element expected;
64 lhs.x = a_x.to_montgomery_form();
65 lhs.y = a_y.to_montgomery_form();
66 lhs.z = a_z.to_montgomery_form();
67 rhs.x = b_x.to_montgomery_form();
68 rhs.y = b_y.to_montgomery_form();
69 expected.x = expected_x.to_montgomery_form();
70 expected.y = expected_y.to_montgomery_form();
71 expected.z = expected_z.to_montgomery_form();
72 result = lhs + rhs;
73
74 EXPECT_EQ(result == expected, true);
75}
76
77TEST(g1, DblCheckAgainstConstants)
78{
79 fq a_x{ 0x8d1703aa518d827f, 0xd19cc40779f54f63, 0xabc11ce30d02728c, 0x10938940de3cbeec };
80 fq a_y{ 0xcf1798994f1258b4, 0x36307a354ad90a25, 0xcd84adb348c63007, 0x6266b85241aff3f };
81 fq a_z{ 0xe213e18fd2df7044, 0xb2f42355982c5bc8, 0xf65cf5150a3a9da1, 0xc43bde08b03aca2 };
82 fq expected_x{ 0xd5c6473044b2e67c, 0x89b185ea20951f3a, 0x4ac597219cf47467, 0x2d00482f63b12c86 };
83 fq expected_y{ 0x4e7e6c06a87e4314, 0x906a877a71735161, 0xaa7b9893cc370d39, 0x62f206bef795a05 };
84 fq expected_z{ 0x8813bdca7b0b115a, 0x929104dffdfabd22, 0x3fff575136879112, 0x18a299c1f683bdca };
85 g1::element lhs;
86 g1::element result;
87 g1::element expected;
88 lhs.x = a_x.to_montgomery_form();
89 lhs.y = a_y.to_montgomery_form();
90 lhs.z = a_z.to_montgomery_form();
91 expected.x = expected_x.to_montgomery_form();
92 expected.y = expected_y.to_montgomery_form();
93 expected.z = expected_z.to_montgomery_form();
94
95 result = lhs.dbl();
96 result.self_dbl();
97 result.self_dbl();
98
99 EXPECT_EQ(result == expected, true);
100}
101
102TEST(g1, AddCheckAgainstConstants)
103{
104 fq a_x{ 0x184b38afc6e2e09a, 0x4965cd1c3687f635, 0x334da8e7539e71c4, 0xf708d16cfe6e14 };
105 fq a_y{ 0x2a6ff6ffc739b3b6, 0x70761d618b513b9, 0xbf1645401de26ba1, 0x114a1616c164b980 };
106 fq a_z{ 0x10143ade26bbd57a, 0x98cf4e1f6c214053, 0x6bfdc534f6b00006, 0x1875e5068ababf2c };
107 fq b_x{ 0xafdb8a15c98bf74c, 0xac54df622a8d991a, 0xc6e5ae1f3dad4ec8, 0x1bd3fb4a59e19b52 };
108 fq b_y{ 0x21b3bb529bec20c0, 0xaabd496406ffb8c1, 0xcd3526c26ac5bdcb, 0x187ada6b8693c184 };
109 fq b_z{ 0xffcd440a228ed652, 0x8a795c8f234145f1, 0xd5279cdbabb05b95, 0xbdf19ba16fc607a };
110 fq expected_x{ 0x18764da36aa4cd81, 0xd15388d1fea9f3d3, 0xeb7c437de4bbd748, 0x2f09b712adf6f18f };
111 fq expected_y{ 0x50c5f3cab191498c, 0xe50aa3ce802ea3b5, 0xd9d6125b82ebeff8, 0x27e91ba0686e54fe };
112 fq expected_z{ 0xe4b81ef75fedf95, 0xf608edef14913c75, 0xfd9e178143224c96, 0xa8ae44990c8accd };
113 g1::element lhs;
114 g1::element rhs;
115 g1::element result;
116 g1::element expected;
117
118 lhs.x = a_x.to_montgomery_form();
119 lhs.y = a_y.to_montgomery_form();
120 lhs.z = a_z.to_montgomery_form();
121 rhs.x = b_x.to_montgomery_form();
122 rhs.y = b_y.to_montgomery_form();
123 rhs.z = b_z.to_montgomery_form();
124 expected.x = expected_x.to_montgomery_form();
125 expected.y = expected_y.to_montgomery_form();
126 expected.z = expected_z.to_montgomery_form();
127
128 result = lhs + rhs;
129
130 EXPECT_EQ(result == expected, true);
131}
132
133TEST(g1, GroupExponentiationCheckAgainstConstants)
134{
135 fr a{ 0xb67299b792199cf0, 0xc1da7df1e7e12768, 0x692e427911532edf, 0x13dd85e87dc89978 };
137
138 fq expected_x{ 0x9bf840faf1b4ba00, 0xe81b7260d068e663, 0x7610c9a658d2c443, 0x278307cd3d0cddb0 };
139 fq expected_y{ 0xf6ed5fb779ebecb, 0x414ca771acbe183c, 0xe3692cb56dfbdb67, 0x3d3c5ed19b080a3 };
140
141 g1::affine_element expected;
142 expected.x = expected_x.to_montgomery_form();
143 expected.y = expected_y.to_montgomery_form();
144
145 g1::affine_element result(g1::one * a);
146
147 EXPECT_EQ(result == expected, true);
148}
149
150TEST(g1, OperatorOrdering)
151{
152 fr scalar{ 0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e };
153
156
157 g1::element c = a + b;
158 g1::element d = b + a;
159 EXPECT_EQ(c, d);
160
161 g1::element e = a * scalar;
162 g1::element f = b * scalar;
163 g1::affine_element g = b * scalar;
164 g1::affine_element h = a * scalar;
165 EXPECT_EQ(e, f);
166 EXPECT_EQ(g, h);
167}
168
169template <class T> void write(const T t)
170{
171 FILE* fp = fopen("/dev/null", "wb");
172 static_cast<void>(fwrite(&t, sizeof(t), 1, fp));
173 static_cast<void>(fclose(fp));
174}
175
176#if !defined(__wasm__)
177TEST(g1, InitializationCheck)
178{
179 // NOLINTNEXTLINE not our fault googletest uses `goto`!
180 EXPECT_NO_THROW(write<g1::affine_element>({}));
181}
182#endif
183
184TEST(g1, CheckPrecomputedGenerators)
185{
186 ASSERT_TRUE((bb::check_precomputed_generators<g1, "biggroup table offset generator", 1UL>()));
187 ASSERT_TRUE((bb::check_precomputed_generators<g1, "biggroup offset generator", 1UL>()));
188 ASSERT_TRUE((bb::check_precomputed_generators<g1, "ECCVM_OFFSET_GENERATOR", 1UL>()));
189 ASSERT_TRUE((bb::check_precomputed_generators<g1, "test generators", 2UL>()));
190}
191
192// Regression: boundary scalars k = ceil(m * 2^256 / endo_g2) (from endomorphism_scalars.py)
193// previously triggered the negative-k2 bug in split_into_endomorphism_scalars, producing wrong
194// scalar multiplication results. We test boundaries and random samples within each band.
195TEST(g1, ScalarMulNegativeK2Regression)
196{
197 // clang-format off
198 struct test_case { std::array<uint64_t, 4> limbs; const char* tag; };
199 const std::array<test_case, 3> boundary_cases = {{
200 {{ 0x01624731e1195570, 0x3ba491482db4da14, 0x59e26bcea0d48bac, 0x0 }, "m=1"},
201 {{ 0x02c48e63c232aadf, 0x774922905b69b428, 0xb3c4d79d41a91758, 0x0 }, "m=2"},
202 {{ 0x0426d595a34c004e, 0xb2edb3d8891e8e3c, 0x0da7436be27da304, 0x1 }, "m=3"},
203 }};
204 // clang-format on
205
206 for (const auto& tc : boundary_cases) {
207 fr base_scalar{ tc.limbs[0], tc.limbs[1], tc.limbs[2], tc.limbs[3] };
208 base_scalar.self_to_montgomery_form();
209
210 g1::affine_element endo_result(g1::one * base_scalar);
211 g1::affine_element naive_result = naive_scalar_mul<g1, fr>(g1::one, base_scalar);
212 EXPECT_EQ(naive_result.on_curve(), true) << tc.tag;
213 EXPECT_EQ(endo_result.on_curve(), true) << tc.tag;
214 EXPECT_EQ(endo_result, naive_result) << tc.tag;
215
216 // Random samples within the formerly-buggy band (~2^123-2^126 wide; 122-bit offsets).
217 for (size_t i = 0; i < 100; ++i) {
218 uint256_t rand_bits(fr::random_element());
219 uint256_t offset_int = (rand_bits & ((uint256_t(1) << 122) - 1)) + 1;
220 fr scalar = base_scalar + fr(offset_int);
221
222 g1::affine_element endo_res(g1::one * scalar);
223 g1::affine_element naive_res = naive_scalar_mul<g1, fr>(g1::one, scalar);
224 EXPECT_EQ(naive_res.on_curve(), true) << tc.tag << " offset " << i;
225 EXPECT_EQ(endo_res.on_curve(), true) << tc.tag << " offset " << i;
226 EXPECT_EQ(endo_res, naive_res) << tc.tag << " offset " << i;
227 }
228 }
229}
constexpr bool on_curve() const noexcept
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:35
constexpr element dbl() const noexcept
constexpr void self_dbl() noexcept
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:38
static constexpr element one
Definition group.hpp:48
FF a
FF b
void write(const T t)
Definition g1.test.cpp:169
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FqParams > fq
Definition fq.hpp:153
field< Bn254FrParams > fr
Definition fr.hpp:155
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr fq b
Definition g1.hpp:30
static constexpr fq one_y
Definition g1.hpp:24
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 void self_to_montgomery_form() &noexcept