Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
secp256k1.test.cpp
Go to the documentation of this file.
1#include "secp256k1.hpp"
4#include <gtest/gtest.h>
5
6using namespace bb;
7
8// Field tests (add, sub, mul, sqr, sqrt, montgomery form) are in:
9// - barretenberg/ecc/fields/field.test.cpp (generic field tests)
10// - barretenberg/ecc/fields/prime_field.test.cpp (prime field specific tests)
11// The tests below are for the secp256k1 elliptic curve group operations.
12TEST(secp256k1, CurveCoefficients)
13{
14 secp256k1::fq expected_a = secp256k1::fq(0);
15 secp256k1::fq expected_b = secp256k1::fq(7);
16
17 EXPECT_EQ(secp256k1::G1Params::a, expected_a);
18 EXPECT_EQ(secp256k1::G1Params::b, expected_b);
19}
20
21TEST(secp256k1, GeneratorOnCurve)
22{
24 secp256k1::fq expected_x = secp256k1::fq("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798");
25 secp256k1::fq expected_y = secp256k1::fq("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8");
26
27 EXPECT_EQ(result.x, expected_x);
28 EXPECT_EQ(result.y, expected_y);
29 EXPECT_EQ(result.on_curve(), true);
30}
31
32TEST(secp256k1, CheckPrecomputedGenerators)
33{
34 ASSERT_TRUE((bb::check_precomputed_generators<secp256k1::g1, "biggroup offset generator", 1UL>()));
35 ASSERT_TRUE((bb::check_precomputed_generators<secp256k1::g1, "biggroup table offset generator", 1UL>()));
36}
37
38TEST(secp256k1, GetEndomorphismScalars)
39{
40 for (size_t i = 0; i < 2048; i++) {
42 secp256k1::fr k1 = 0;
43 secp256k1::fr k2 = 0;
44
46 bool k1_neg = false;
47 bool k2_neg = false;
48
50 k2 = -k2;
51 k2_neg = true;
52 }
53
54 EXPECT_LT(k1.uint256_t_no_montgomery_conversion().get_msb(), 129ULL);
55 EXPECT_LT(k2.uint256_t_no_montgomery_conversion().get_msb(), 129ULL);
56
57 if (k1_neg) {
58 k1 = -k1;
59 }
60 if (k2_neg) {
61 k2 = -k2;
62 }
63
66
68 secp256k1::fr expected = k1 - k2 * beta;
69
71 EXPECT_EQ(k, expected);
72 if (k != expected) {
73 break;
74 }
75 }
76}
77
78TEST(secp256k1, TestEndomorphismScalars)
79{
81 secp256k1::fr k1 = 0;
82 secp256k1::fr k2 = 0;
83
85 bool k1_neg = false;
86 bool k2_neg = false;
87
89 k1 = -k1;
90 k1_neg = true;
91 }
93 k2 = -k2;
94 k2_neg = true;
95 }
96
97 EXPECT_LT(k1.uint256_t_no_montgomery_conversion().get_msb(), 129ULL);
98 EXPECT_LT(k2.uint256_t_no_montgomery_conversion().get_msb(), 129ULL);
99
100 if (k1_neg) {
101 k1 = -k1;
102 }
103 if (k2_neg) {
104 k2 = -k2;
105 }
108 static const uint256_t secp256k1_const_lambda{
109 0xDF02967C1B23BD72ULL, 0x122E22EA20816678UL, 0xA5261C028812645AULL, 0x5363AD4CC05C30E0ULL
110 };
111
112 secp256k1::fr expected = k1 - k2 * secp256k1_const_lambda;
113
114 expected.self_from_montgomery_form();
115 EXPECT_EQ(k, expected);
116}
117
118TEST(secp256k1, NegAndSelfNeg0CmpRegression)
119{
120 secp256k1::fq a = 0;
121 secp256k1::fq a_neg = -a;
122 EXPECT_EQ((a == a_neg), true);
123 a = 0;
124 a_neg = 0;
125 a_neg.self_neg();
126 EXPECT_EQ((a == a_neg), true);
127}
128
129TEST(secp256k1, MontgomeryMulBigBug)
130{
131 secp256k1::fq a(uint256_t{ 0xfffffffe630dc02f, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff });
132 secp256k1::fq a_sqr = a.sqr();
133 secp256k1::fq expected(uint256_t{ 0x60381e557e100000, 0x0, 0x0, 0x0 });
134 EXPECT_EQ((a_sqr == expected), true);
135}
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:35
BB_INLINE constexpr bool on_curve() const noexcept
static constexpr element one
Definition group.hpp:48
constexpr uint64_t get_msb() const
FF a
field< FqParams > fq
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
General class for prime fields see Prime field documentation["field documentation"] for general imple...
static constexpr field cube_root_of_unity()
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.
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
BB_INLINE constexpr void self_from_montgomery_form() &noexcept
BB_INLINE constexpr void self_to_montgomery_form() &noexcept
static constexpr fq b
static constexpr fq a