Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
secp256r1.test.cpp
Go to the documentation of this file.
1#include "secp256r1.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 secp256r1 elliptic curve group operations.
12
13TEST(secp256r1, CurveCoefficients)
14{
15 secp256r1::fq expected_a = secp256r1::fq("0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc");
16 secp256r1::fq expected_b = secp256r1::fq("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
17
18 EXPECT_EQ(secp256r1::G1Params::a, expected_a);
19 EXPECT_EQ(secp256r1::G1Params::b, expected_b);
20}
21
22TEST(secp256r1, GeneratorOnCurve)
23{
25 secp256r1::fq expected_x = secp256r1::fq("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296");
26 secp256r1::fq expected_y = secp256r1::fq("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5");
27
28 EXPECT_EQ(result.x, expected_x);
29 EXPECT_EQ(result.y, expected_y);
30 EXPECT_EQ(result.on_curve(), true);
31}
32
38TEST(secp256r1, AdditionSubtractionRegressionCheck)
39{
40 secp256r1::fq fq1(uint256_t{ 0xfffffe0000000200, 0x200fffff9ff, 0xfffffbfffffffe00, 0xfffffbff00000400 });
41 secp256r1::fq fq2(uint256_t{ 0xfffffe0000000200, 0x200fffff9ff, 0xfffffbfffffffe00, 0xfffffbff00000400 });
42 secp256r1::fq fq3(0);
43 secp256r1::fq fq4(0);
45 fq1 += secp256r1::fq(2);
46
47 fq3 -= fq1;
48 fq4 -= fq2;
49 EXPECT_EQ(fq1 + fq1, fq2 + fq2);
50 EXPECT_EQ(fq3, fq4);
51}
52
53#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
54TEST(secp256r1, MontgomeryMulBigBug)
55{
57 a.data[0] = 0xC5BF4F6AFF993D09;
58 a.data[1] = 0xA3361BDA67E62E0E;
59 a.data[2] = 0xAAAAAAAAAAAAAAAA;
60 a.data[3] = 0xFFFFFFFFE38E38E3;
61 secp256r1::fr a_sqr = a.sqr();
62 secp256r1::fr expected(uint256_t{ 0x57abc6aa0349c084, 0x65b21b232a4cb7a5, 0x5ba781948b0fcd6e, 0xd6e9e0644bda12f7 });
63 EXPECT_EQ((a_sqr == expected), true);
64}
65#endif
66
67TEST(secp256r1, CheckPrecomputedGenerators)
68{
69 ASSERT_TRUE((bb::check_precomputed_generators<secp256r1::g1, "biggroup offset generator", 1UL>()));
70 ASSERT_TRUE((bb::check_precomputed_generators<secp256r1::g1, "biggroup table offset generator", 1UL>()));
71}
72
73// Hacky: wasm does not properly find main() from gmock_main.
74// We only want to run wasm tests specifically for ecc ops as our field handling is different.
75// We need to make sure the hardcoded generators make sense.
76// As this is our narrow focus, we hack this so ecc_tests can run.
77#ifdef __wasm__
78GTEST_API_ int main(int argc, char** argv)
79{
80 testing::InitGoogleTest(&argc, argv);
81 return RUN_ALL_TESTS();
82}
83#endif
int main(int argc, char **argv)
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
FF a
field< FqParams > fq
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field2< fq, Bn254Fq2Params > fq2
Definition fq2.hpp:71
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
General class for prime fields see Prime field documentation["field documentation"] for general imple...
BB_INLINE constexpr field sqr() const noexcept
static constexpr uint256_t modulus_minus_two
static constexpr fq a
static constexpr fq b