14#include "gmock/gmock.h"
17#include <gtest/gtest.h>
22using ::testing::ElementsAreArray;
24using ::testing::Property;
29template <
typename G1>
class TestAffineElement :
public testing::Test {
30 using element =
typename G1::element;
34 static void test_read_write_buffer()
42 std::vector<uint8_t> v(65);
43 uint8_t* ptr = v.data();
63 std::vector<uint8_t> v(64);
64 uint8_t* ptr = v.data();
73 static void test_read_and_write()
80 std::vector<uint8_t> v(
sizeof(R));
81 uint8_t* ptr = v.data();
88 const uint8_t* read_ptr = v.data();
96 static void test_msgpack_serialization()
103 msgpack::sbuffer sbuf;
104 msgpack::pack(sbuf, P);
107 msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());
108 msgpack::object deserialized = oh.get();
111 deserialized.convert(R);
123 msgpack::sbuffer sbuf;
124 msgpack::pack(sbuf, P);
127 msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());
128 msgpack::object deserialized = oh.get();
131 deserialized.convert(R);
138 static void test_point_compression()
140 for (
size_t i = 0; i < 10; i++) {
148 static void test_point_compression_unsafe()
150 for (
size_t i = 0; i < 100; i++) {
157 EXPECT_EQ(P, Q_points[0]);
164 static void test_infinity_regression()
169 ASSERT_FALSE(P == R);
173 static void test_infinity_ordering_regression()
179 EXPECT_NE(P < Q, Q < P);
186 static void test_batch_endomorphism_by_minus_one()
195 EXPECT_EQ(affine_points[i], -result[i]);
203 static void test_fixed_point_at_infinity()
216using TestTypes = testing::Types<bb::g1, grumpkin::g1, secp256k1::g1, secp256r1::g1>;
223 TestFixture::test_read_and_write();
228 TestFixture::test_read_write_buffer();
229 TestFixture::test_msgpack_serialization();
234 if constexpr (TypeParam::Fq::modulus.data[3] >= 0x4000000000000000ULL) {
237 TestFixture::test_point_compression();
243 if constexpr (TypeParam::Fq::modulus.data[3] >= 0x4000000000000000ULL) {
246 TestFixture::test_fixed_point_at_infinity();
252 if constexpr (TypeParam::Fq::modulus.data[3] >= 0x4000000000000000ULL) {
253 TestFixture::test_point_compression_unsafe();
261 TestFixture::test_infinity_ordering_regression();
270 template <
typename Element,
typename Scalar>
275 template <
typename Element,
typename Scalar>
284TYPED_TEST(TestAffineElement, MulWithEndomorphismMatchesMulWithoutEndomorphism)
286 for (
int i = 0; i < 100; i++) {
295TEST(AffineElementFromPublicInputs, Bn254FromPublicInputs)
301 AffineElement point = AffineElement::random_element();
308 auto reconstructed = FrCodec::deserialize_from_fields<AffineElement>(limbs);
310 EXPECT_EQ(reconstructed, point);
313TEST(AffineElementFromPublicInputs, GrumpkinFromPublicInputs)
319 AffineElement point = AffineElement::random_element();
326 auto reconstructed = FrCodec::deserialize_from_fields<AffineElement>(limbs);
328 EXPECT_EQ(reconstructed, point);
333TEST(AffineElement, InfinityMulByScalarIsInfinity)
336 EXPECT_TRUE(result.is_point_at_infinity());
340TEST(AffineElement, BatchMulMatchesNonBatchMul)
342 constexpr size_t num_points = 512;
345 affine_points.push_back(grumpkin::g1::affine_element::infinity());
348 std::transform(affine_points.begin(),
351 [exponent](
const auto& el) { return el * exponent; });
354 grumpkin::g1::element::batch_mul_with_endomorphism(affine_points, exponent);
356 EXPECT_THAT(result, ElementsAreArray(expected));
360TEST(AffineElement, InfinityBatchMulByScalarIsInfinity)
362 constexpr size_t num_points = 1024;
368 EXPECT_THAT(result, Each(Property(&grumpkin::g1::affine_element::is_point_at_infinity, Eq(
true))));
373 if constexpr (TypeParam::USE_ENDOMORPHISM) {
374 TestFixture::test_batch_endomorphism_by_minus_one();
380TEST(AffineElement, HashToCurve)
385 fr(
uint256_t(
"24c4cb9c1206ab5470592f237f1698abe684dadf0ab4d7a132c32b2134e2c12e")),
386 fr(
uint256_t(
"0668b8d61a317fb34ccad55c930b3554f1828a0e5530479ecab4defe6bbc0b2e"))));
390 fr(
uint256_t(
"107f1b633c6113f3222f39f6256f0546b41a4880918c86864b06471afb410454")),
391 fr(
uint256_t(
"050cd3823d0c01590b6a50adcc85d2ee4098668fd28805578aa05a423ea938c6"))));
394 test_vectors.emplace_back(std::vector<uint8_t>{ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 },
396 fr(
uint256_t(
"037c5c229ae495f6e8d1b4bf7723fafb2b198b51e27602feb8a4d1053d685093")),
397 fr(
uint256_t(
"10cf9596c5b2515692d930efa2cf3817607e4796856a79f6af40c949b066969f"))));
400 auto result = grumpkin::g1::affine_element::hash_to_curve(
std::get<0>(test_case), 0);
static std::vector< fr > serialize_to_fields(const T &val)
Conversion from transcript values to bb::frs.
typename Group::affine_element AffineElement
static Element mul_without_endomorphism(const Element &element, const Scalar &scalar)
static Element mul_with_endomorphism(const Element &element, const Scalar &scalar)
static constexpr std::array< affine_element, 2 > from_compressed_unsafe(const uint256_t &compressed) noexcept
Reconstruct a point in affine coordinates from compressed form.
constexpr bool is_point_at_infinity() const noexcept
constexpr uint256_t compress() const noexcept
static affine_element serialize_from_buffer(const uint8_t *buffer, bool write_x_first=false)
Restore point from a buffer.
constexpr void self_set_infinity() noexcept
static constexpr affine_element infinity()
static constexpr affine_element from_compressed(const uint256_t &compressed) noexcept
Reconstruct a point in affine coordinates from compressed form.
constexpr bool on_curve() const noexcept
static constexpr affine_element one() noexcept
static void serialize_to_buffer(const affine_element &value, uint8_t *buffer, bool write_x_first=false)
Serialize the point to the given buffer.
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
element mul_with_endomorphism(const Fr &scalar) const noexcept
element mul_without_endomorphism(const Fr &scalar) const noexcept
group_elements::affine_element< Fq, Fr, Params > affine_element
test_vector test_vectors[]
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
Entry point for Barretenberg command-line interface.
void read(B &it, field2< base_field, Params > &value)
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
field< Bn254FrParams > fr
void write(B &buf, field2< base_field, Params > const &value)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
testing::Types< VKTestParams< UltraFlavor, stdlib::recursion::honk::DefaultIO< UltraCircuitBuilder > >, VKTestParams< UltraFlavor, stdlib::recursion::honk::RollupIO >, VKTestParams< UltraKeccakFlavor, stdlib::recursion::honk::DefaultIO< UltraCircuitBuilder > >, VKTestParams< MegaFlavor, stdlib::recursion::honk::DefaultIO< MegaCircuitBuilder > > > TestTypes
static field random_element(numeric::RNG *engine=nullptr) noexcept
static constexpr field zero()