3#include <gtest/gtest.h>
17 while (res >= test_fq_mod) {
27 for (
size_t i = 0; i < n; ++i) {
37 if (expected < a_raw) {
38 expected -= test_fq_mod;
41 EXPECT_EQ(result, expected);
48 for (
size_t i = 0; i < n; ++i) {
58 if (expected > a_raw) {
59 expected += test_fq_mod;
62 EXPECT_EQ(result, expected);
66TEST(secp256k1, TestToMontgomeryForm)
69 for (
size_t i = 0; i < n; ++i) {
73#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
82 montgomery_result.
data[0], montgomery_result.
data[1], montgomery_result.
data[2], montgomery_result.
data[3]
84 EXPECT_EQ(result, expected);
88TEST(secp256k1, TestFromMontgomeryForm)
91 for (
size_t i = 0; i < n; ++i) {
102 for (
size_t i = 0; i < n; ++i) {
116 EXPECT_EQ(result, expected);
123 for (
size_t i = 0; i < n; ++i) {
133 EXPECT_EQ(result, expected);
140 for (
size_t i = 0; i < n; ++i) {
142 auto [is_sqr, root] = input.
sqrt();
144 EXPECT_EQ(root_test, input);
158TEST(secp256k1, GeneratorOnCurve)
170TEST(secp256k1, RandomAffineElement)
181 EXPECT_EQ(
a ==
b,
true);
182 EXPECT_EQ(
a ==
a,
true);
184 b.self_set_infinity();
186 EXPECT_EQ(
a ==
b,
false);
189 EXPECT_EQ(
a == c,
false);
191 a.self_set_infinity();
193 EXPECT_EQ(
a ==
b,
true);
196TEST(secp256k1, CheckGroupModulus)
207TEST(secp256k1, AddExceptionTestInfinity)
223 result = lhs + rhs_b;
225 EXPECT_EQ(lhs == result,
true);
230 EXPECT_EQ(rhs == result,
true);
233TEST(secp256k1, AddExceptionTestDbl)
243 expected = lhs.
dbl();
245 EXPECT_EQ(result == expected,
true);
248TEST(secp256k1, AddDblConsistency)
263 dbl_result =
a.
dbl();
265 EXPECT_EQ(add_result == dbl_result,
true);
268TEST(secp256k1, AddDblConsistencyRepeated)
288 EXPECT_EQ(result == expected,
true);
291TEST(secp256k1, MixedAddExceptionTestInfinity)
308 EXPECT_EQ(rhs_c == result,
true);
311TEST(secp256k1, MixedAddExceptionTestDbl)
321 expected = lhs.
dbl();
323 EXPECT_EQ(result == expected,
true);
326TEST(secp256k1, AddMixedAddConsistencyCheck)
335 add_result = lhs + rhs_b;
336 mixed_add_result = lhs + rhs;
338 EXPECT_EQ(add_result == mixed_add_result,
true);
343 for (
size_t i = 0; i < 100; ++i) {
347 EXPECT_EQ(affine_test.
on_curve(),
true);
352 size_t num_points = 2;
355 for (
size_t i = 0; i < num_points; ++i) {
359 normalized[i] = points[i];
361 secp256k1::g1::element::batch_normalize(&normalized[0], num_points);
363 for (
size_t i = 0; i < num_points; ++i) {
368 zz = points[i].z.
sqr();
369 zzz = points[i].z * zz;
370 result_x = normalized[i].x * zz;
371 result_y = normalized[i].y * zzz;
373 EXPECT_EQ((result_x == points[i].x),
true);
374 EXPECT_EQ((result_y == points[i].y),
true);
378TEST(secp256k1, GroupExponentiationZeroAndOne)
389TEST(secp256k1, GroupExponentiationConsistencyCheck)
403 EXPECT_EQ(result == expected,
true);
406TEST(secp256k1, DeriveGenerators)
408 constexpr size_t num_generators = 128;
412 for (
size_t i = 0; i < result.size(); ++i) {
413 if ((i != j) && result[i] == y) {
420 for (
size_t k = 0; k < num_generators; ++k) {
421 EXPECT_EQ(is_unique(result[k], k),
true);
422 EXPECT_EQ(result[k].on_curve(),
true);
426TEST(secp256k1, GetEndomorphismScalars)
428 for (
size_t i = 0; i < 2048; i++) {
459 EXPECT_EQ(k, expected);
466TEST(secp256k1, TestEndomorphismScalars)
496 static const uint256_t secp256k1_const_lambda{
497 0xDF02967C1B23BD72ULL, 0x122E22EA20816678UL, 0xA5261C028812645AULL, 0x5363AD4CC05C30E0ULL
503 EXPECT_EQ(k, expected);
506TEST(secp256k1, NegAndSelfNeg0CmpRegression)
510 EXPECT_EQ((
a == a_neg),
true);
514 EXPECT_EQ((
a == a_neg),
true);
517TEST(secp256k1, MontgomeryMulBigBug)
522 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
constexpr element normalize() 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(MegaCircuitBuilder, CopyConstructor)
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