From e23d048e916fa12966fe01d1a8c0d3bfb50c2943 Mon Sep 17 00:00:00 2001 From: Facundo Date: Tue, 13 Feb 2024 12:13:54 +0000 Subject: [PATCH] chore(bb): small test improvements (#4568) Dipping toes in BB codebase: * Using GTest matchers. * Removing use of `emplace_back` in favour of `push_back`: this makes no difference unless you are building an elements _in place_ by passing the arguments to the element constructor (which is not happening here). `push_back` is a recommended default given the clearer name. --- .../ecc/groups/affine_element.test.cpp | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp b/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp index 8dc10905f5a..d39cdca31e1 100644 --- a/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp @@ -6,7 +6,17 @@ #include "barretenberg/ecc/curves/secp256r1/secp256r1.hpp" #include "barretenberg/ecc/groups/element.hpp" #include "barretenberg/serialize/test_helper.hpp" + +#include "gmock/gmock.h" +#include #include +#include +#include + +using ::testing::Each; +using ::testing::ElementsAreArray; +using ::testing::Eq; +using ::testing::Property; template class TestAffineElement : public testing::Test { using element = typename G1::element; @@ -173,37 +183,30 @@ TEST(AffineElement, InfinityMulByScalarIsInfinity) TEST(AffineElement, BatchMulMatchesNonBatchMul) { constexpr size_t num_points = 512; - std::vector affine_points; - for (size_t i = 0; i < num_points - 1; ++i) { - affine_points.emplace_back(grumpkin::g1::affine_element::random_element()); - } + std::vector affine_points(num_points - 1, grumpkin::g1::affine_element::infinity()); // Include a point at infinity to test the mixed infinity + non-infinity case - affine_points.emplace_back(grumpkin::g1::affine_element::infinity()); + affine_points.push_back(grumpkin::g1::affine_element::infinity()); grumpkin::fr exponent = grumpkin::fr::random_element(); + std::vector expected; + std::transform(affine_points.begin(), + affine_points.end(), + std::back_inserter(expected), + [exponent](const auto& el) { return el * exponent; }); + std::vector result = grumpkin::g1::element::batch_mul_with_endomorphism(affine_points, exponent); - size_t i = 0; - for (grumpkin::g1::affine_element& el : result) { - EXPECT_EQ(el, affine_points[i] * exponent); - i++; - } + + EXPECT_THAT(result, ElementsAreArray(expected)); } // Batched multiplication of a point at infinity by a scalar should result in points at infinity TEST(AffineElement, InfinityBatchMulByScalarIsInfinity) { constexpr size_t num_points = 1024; - std::vector affine_points; - for (size_t i = 0; i < num_points; ++i) { - affine_points.emplace_back(grumpkin::g1::affine_element::infinity()); - } - grumpkin::fr exponent = grumpkin::fr::random_element(); + std::vector affine_points(num_points, grumpkin::g1::affine_element::infinity()); + std::vector result = - grumpkin::g1::element::batch_mul_with_endomorphism(affine_points, exponent); - for (grumpkin::g1::affine_element& el : result) { - EXPECT_TRUE(el.is_point_at_infinity()); - if (!el.is_point_at_infinity()) { - break; // dont spam with errors - } - } + grumpkin::g1::element::batch_mul_with_endomorphism(affine_points, grumpkin::fr::random_element()); + + EXPECT_THAT(result, Each(Property(&grumpkin::g1::affine_element::is_point_at_infinity, Eq(true)))); } \ No newline at end of file