4#include <gtest/gtest.h>
18 while (res >= test_fq_mod) {
28 for (
size_t i = 0; i < n; ++i) {
38 if (expected < a_raw) {
39 expected -= test_fq_mod;
42 EXPECT_EQ(result, expected);
49 for (
size_t i = 0; i < n; ++i) {
59 if (expected > a_raw) {
60 expected += test_fq_mod;
63 EXPECT_EQ(result, expected);
67TEST(secp256k1, TestToMontgomeryForm)
70 for (
size_t i = 0; i < n; ++i) {
74#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
83 montgomery_result.
data[0], montgomery_result.
data[1], montgomery_result.
data[2], montgomery_result.
data[3]
85 EXPECT_EQ(result, expected);
89TEST(secp256k1, TestFromMontgomeryForm)
92 for (
size_t i = 0; i < n; ++i) {
103 for (
size_t i = 0; i < n; ++i) {
117 EXPECT_EQ(result, expected);
124 for (
size_t i = 0; i < n; ++i) {
134 EXPECT_EQ(result, expected);
141 for (
size_t i = 0; i < n; ++i) {
143 auto [is_sqr, root] = input.
sqrt();
145 EXPECT_EQ(root_test, input);
159TEST(secp256k1, GeneratorOnCurve)
171TEST(secp256k1, RandomAffineElement)
182 EXPECT_EQ(
a ==
b,
true);
183 EXPECT_EQ(
a ==
a,
true);
185 b.self_set_infinity();
187 EXPECT_EQ(
a ==
b,
false);
190 EXPECT_EQ(
a == c,
false);
192 a.self_set_infinity();
194 EXPECT_EQ(
a ==
b,
true);
197TEST(secp256k1, CheckGroupModulus)
208TEST(secp256k1, AddExceptionTestInfinity)
224 result = lhs + rhs_b;
226 EXPECT_EQ(lhs == result,
true);
231 EXPECT_EQ(rhs == result,
true);
234TEST(secp256k1, AddExceptionTestDbl)
244 expected = lhs.
dbl();
246 EXPECT_EQ(result == expected,
true);
249TEST(secp256k1, AddDblConsistency)
264 dbl_result =
a.dbl();
266 EXPECT_EQ(add_result == dbl_result,
true);
269TEST(secp256k1, AddDblConsistencyRepeated)
289 EXPECT_EQ(result == expected,
true);
292TEST(secp256k1, MixedAddExceptionTestInfinity)
309 EXPECT_EQ(rhs_c == result,
true);
312TEST(secp256k1, MixedAddExceptionTestDbl)
322 expected = lhs.
dbl();
324 EXPECT_EQ(result == expected,
true);
327TEST(secp256k1, AddMixedAddConsistencyCheck)
336 add_result = lhs + rhs_b;
337 mixed_add_result = lhs + rhs;
339 EXPECT_EQ(add_result == mixed_add_result,
true);
344 for (
size_t i = 0; i < 100; ++i) {
348 EXPECT_EQ(affine_test.
on_curve(),
true);
353 size_t num_points = 2;
356 for (
size_t i = 0; i < num_points; ++i) {
360 normalized[i] = points[i];
362 secp256k1::g1::element::batch_normalize(&normalized[0], num_points);
364 for (
size_t i = 0; i < num_points; ++i) {
369 zz = points[i].z.
sqr();
370 zzz = points[i].z * zz;
371 result_x = normalized[i].x * zz;
372 result_y = normalized[i].y * zzz;
374 EXPECT_EQ((result_x == points[i].x),
true);
375 EXPECT_EQ((result_y == points[i].y),
true);
379TEST(secp256k1, GroupExponentiationZeroAndOne)
390TEST(secp256k1, GroupExponentiationConsistencyCheck)
404 EXPECT_EQ(result == expected,
true);
407TEST(secp256k1, DeriveGenerators)
409 constexpr size_t num_generators = 128;
413 for (
size_t i = 0; i < result.size(); ++i) {
414 if ((i != j) && result[i] == y) {
421 for (
size_t k = 0; k < num_generators; ++k) {
422 EXPECT_EQ(is_unique(result[k], k),
true);
423 EXPECT_EQ(result[k].on_curve(),
true);
427TEST(secp256k1, CheckPrecomputedGenerators)
429 ASSERT_TRUE((bb::check_precomputed_generators<secp256k1::g1, "biggroup offset generator", 1UL>()));
430 ASSERT_TRUE((bb::check_precomputed_generators<secp256k1::g1, "biggroup table offset generator", 1UL>()));
433TEST(secp256k1, GetEndomorphismScalars)
435 for (
size_t i = 0; i < 2048; i++) {
466 EXPECT_EQ(k, expected);
473TEST(secp256k1, TestEndomorphismScalars)
503 static const uint256_t secp256k1_const_lambda{
504 0xDF02967C1B23BD72ULL, 0x122E22EA20816678UL, 0xA5261C028812645AULL, 0x5363AD4CC05C30E0ULL
510 EXPECT_EQ(k, expected);
513TEST(secp256k1, NegAndSelfNeg0CmpRegression)
517 EXPECT_EQ((
a == a_neg),
true);
521 EXPECT_EQ((
a == a_neg),
true);
524TEST(secp256k1, MontgomeryMulBigBug)
529 EXPECT_EQ((a_sqr == expected),
true);
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
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
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.
virtual uint256_t get_random_uint256()=0
constexpr uint64_t get_msb() const
uintx< uint256_t > uint512_t
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
uintx< uint512_t > uint1024_t
Entry point for Barretenberg command-line interface.
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
General class for prime fields see Prime field documentation["field documentation"] for general imple...
static constexpr field cube_root_of_unity()
static constexpr field one()
static void split_into_endomorphism_scalars(const field &k, field &k1, field &k2)
BB_INLINE constexpr void self_neg() &noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept
constexpr std::pair< bool, field > sqrt() const noexcept
Compute square root of the field element.
static BB_INLINE void __copy(const field &a, field &r) noexcept
BB_INLINE constexpr void self_from_montgomery_form() &noexcept
BB_INLINE constexpr void self_to_montgomery_form() &noexcept
static constexpr field zero()
static constexpr uint64_t modulus_0
static constexpr uint64_t modulus_1
static constexpr uint64_t modulus_2
static constexpr uint64_t modulus_3