Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac: rename group internal operations #391

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tachyon/c/math/elliptic_curves/generator/ext_field.cc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tachyon_%{type}_fq%{degree} tachyon_%{type}_fq%{degree}_neg(const tachyon_%{type
using NativeType =
typename TypeTraits<tachyon_%{type}_fq%{degree}>::NativeType;
NativeType native_a = native_cast(*a);
return c_cast(native_a.NegInPlace());
return c_cast(native_a.NegateInPlace());
}

tachyon_%{type}_fq%{degree} tachyon_%{type}_fq%{degree}_sqr(const tachyon_%{type}_fq%{degree}* a) {
Expand Down
8 changes: 4 additions & 4 deletions tachyon/c/math/elliptic_curves/generator/point.cc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ tachyon_%{type}_%{g1_or_g2}_xyzz tachyon_%{type}_%{g1_or_g2}_xyzz_sub_mixed(cons

tachyon_%{type}_%{g1_or_g2}_affine tachyon_%{type}_%{g1_or_g2}_affine_neg(const tachyon_%{type}_%{g1_or_g2}_affine* a) {
using namespace tachyon::c::math;
return ToCAffinePoint(ToAffinePoint(*a).NegInPlace());
return ToCAffinePoint(ToAffinePoint(*a).NegateInPlace());
}

tachyon_%{type}_%{g1_or_g2}_projective tachyon_%{type}_%{g1_or_g2}_projective_neg(const tachyon_%{type}_%{g1_or_g2}_projective* a) {
using namespace tachyon::c::math;
return ToCProjectivePoint(ToProjectivePoint(*a).NegInPlace());
return ToCProjectivePoint(ToProjectivePoint(*a).NegateInPlace());
}

tachyon_%{type}_%{g1_or_g2}_jacobian tachyon_%{type}_%{g1_or_g2}_jacobian_neg(const tachyon_%{type}_%{g1_or_g2}_jacobian* a) {
using namespace tachyon::c::math;
return ToCJacobianPoint(ToJacobianPoint(*a).NegInPlace());
return ToCJacobianPoint(ToJacobianPoint(*a).NegateInPlace());
}

tachyon_%{type}_%{g1_or_g2}_xyzz tachyon_%{type}_%{g1_or_g2}_xyzz_neg(const tachyon_%{type}_%{g1_or_g2}_xyzz* a) {
using namespace tachyon::c::math;
return ToCPointXYZZ(ToPointXYZZ(*a).NegInPlace());
return ToCPointXYZZ(ToPointXYZZ(*a).NegateInPlace());
}

tachyon_%{type}_%{g1_or_g2}_jacobian tachyon_%{type}_%{g1_or_g2}_affine_dbl(const tachyon_%{type}_%{g1_or_g2}_affine* a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tachyon_%{type}_%{suffix} tachyon_%{type}_%{suffix}_neg(const tachyon_%{type}_%{
using namespace tachyon::c::base;
using NativeType = typename TypeTraits<tachyon_%{type}_%{suffix}>::NativeType;
NativeType native_a = native_cast(*a);
return c_cast(native_a.NegInPlace());
return c_cast(native_a.NegateInPlace());
}

tachyon_%{type}_%{suffix} tachyon_%{type}_%{suffix}_dbl(const tachyon_%{type}_%{suffix}* a) {
Expand Down
2 changes: 1 addition & 1 deletion tachyon/cc/math/finite_fields/prime_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TEST_F(PrimeFieldTest, Div) {
EXPECT_EQ(c::base::native_cast((cc_a_ /= cc_b_).value()), a_ /= b_);
}

TEST_F(PrimeFieldTest, Negative) {
TEST_F(PrimeFieldTest, Negate) {
EXPECT_EQ(c::base::native_cast((-cc_a_).value()), -a_);
}

Expand Down
2 changes: 1 addition & 1 deletion tachyon/math/base/groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class AdditiveGroup : public AdditiveSemigroup<G> {
// Negation: -a
constexpr auto operator-() const {
const G* g = static_cast<const G*>(this);
return g->Negative();
return g->Negate();
}
};

Expand Down
20 changes: 10 additions & 10 deletions tachyon/math/base/groups_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ TEST(GroupsTest, Sub) {
Int(const Int& other) : value_(other.value_) {}

MOCK_METHOD(Int, Add, (const Int& other), (const));
MOCK_METHOD(Int, Negative, (), (const));
MOCK_METHOD(Int, Negate, (), (const));

bool operator==(const Int& other) const { return value_ == other.value_; }

Expand All @@ -111,7 +111,7 @@ TEST(GroupsTest, Sub) {
Int a(3);
Int b(4);
EXPECT_CALL(a, Add(testing::_)).Times(testing::Exactly(1));
EXPECT_CALL(b, Negative()).Times(testing::Exactly(1));
EXPECT_CALL(b, Negate()).Times(testing::Exactly(1));

Int c = a - b;
static_cast<void>(c);
Expand Down Expand Up @@ -144,14 +144,14 @@ TEST(GroupsTest, SubOverAdd) {
static_cast<void>(c);
}

TEST(GroupsTest, NegativeOverride) {
class IntNegative {
TEST(GroupsTest, NegateOverride) {
class IntNegate {
public:
IntNegative() = default;
explicit IntNegative(int value) : value_(value) {}
IntNegative(const IntNegative& other) : value_(other.value_) {}
IntNegate() = default;
explicit IntNegate(int value) : value_(value) {}
IntNegate(const IntNegate& other) : value_(other.value_) {}

bool operator==(const IntNegative& other) const {
bool operator==(const IntNegate& other) const {
return value_ == other.value_;
}

Expand All @@ -165,7 +165,7 @@ TEST(GroupsTest, NegativeOverride) {
explicit Int(int value) : value_(value) {}
Int(const Int& other) : value_(other.value_) {}

IntNegative Negative() const { return IntNegative(-value_); }
IntNegate Negate() const { return IntNegate(-value_); }

bool operator==(const Int& other) const { return value_ == other.value_; }

Expand All @@ -174,7 +174,7 @@ TEST(GroupsTest, NegativeOverride) {
};

Int a(3);
EXPECT_EQ(-a, IntNegative(-3));
EXPECT_EQ(-a, IntNegate(-3));
}

} // namespace tachyon::math
16 changes: 7 additions & 9 deletions tachyon/math/base/rational_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ class RationalField : public Field<RationalField<F>> {
return *this;
}

constexpr RationalField DoDouble() const {
constexpr RationalField DoubleImpl() const {
return {numerator_.Double(), denominator_};
}

constexpr RationalField& DoDoubleInPlace() {
constexpr RationalField& DoubleImplInPlace() {
numerator_.DoubleInPlace();
return *this;
}
Expand All @@ -137,12 +137,10 @@ class RationalField : public Field<RationalField<F>> {
return *this;
}

constexpr RationalField Negative() const {
return {-numerator_, denominator_};
}
constexpr RationalField Negate() const { return {-numerator_, denominator_}; }

constexpr RationalField& NegInPlace() {
numerator_.NegInPlace();
constexpr RationalField& NegateInPlace() {
numerator_.NegateInPlace();
return *this;
}

Expand All @@ -157,11 +155,11 @@ class RationalField : public Field<RationalField<F>> {
return *this;
}

constexpr RationalField Square() const {
constexpr RationalField SquareImpl() const {
return {numerator_.Square(), denominator_.Square()};
}

constexpr RationalField& DoSquareInPlace() {
constexpr RationalField& SquareImplInPlace() {
numerator_.SquareInPlace();
denominator_.SquareInPlace();
return *this;
Expand Down
2 changes: 1 addition & 1 deletion tachyon/math/base/rational_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TEST_F(RationalFieldTest, AdditiveGroupOperators) {
R r(GF7(3), GF7(2));
R neg_expected(GF7(4), GF7(2));
EXPECT_EQ(-r, neg_expected);
r.NegInPlace();
r.NegateInPlace();
EXPECT_EQ(r, neg_expected);

r = R(GF7(3), GF7(2));
Expand Down
24 changes: 12 additions & 12 deletions tachyon/math/base/semigroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ namespace tachyon::math {
namespace internal {

SUPPORTS_BINARY_OPERATOR(Mul);
SUPPORTS_UNARY_OPERATOR(DoSquare);
SUPPORTS_UNARY_OPERATOR(SquareImpl);
SUPPORTS_BINARY_OPERATOR(Add);
SUPPORTS_UNARY_OPERATOR(DoDouble);
SUPPORTS_UNARY_OPERATOR(DoubleImpl);

template <typename T, typename = void>
struct SupportsToBigInt : std::false_type {};
Expand Down Expand Up @@ -117,19 +117,19 @@ class MultiplicativeSemigroup {

// a.Square(): a²
[[nodiscard]] constexpr auto Square() const {
if constexpr (internal::SupportsDoSquare<G>::value) {
if constexpr (internal::SupportsSquareImpl<G>::value) {
const G* g = static_cast<const G*>(this);
return g->DoSquare();
return g->SquareImpl();
} else {
return operator*(static_cast<const G&>(*this));
}
}

// a.SquareInPlace(): a = a²
constexpr G& SquareInPlace() {
if constexpr (internal::SupportsDoSquareInPlace<G>::value) {
if constexpr (internal::SupportsSquareImplInPlace<G>::value) {
G* g = static_cast<G*>(this);
return g->DoSquareInPlace();
return g->SquareImplInPlace();
} else if constexpr (internal::SupportsMulInPlace<G, G>::value) {
return operator*=(static_cast<const G&>(*this));
} else {
Expand Down Expand Up @@ -201,7 +201,7 @@ class MultiplicativeSemigroup {
auto it = BitIteratorBE<BigInt<N>>::begin(&exponent, true);
auto end = BitIteratorBE<BigInt<N>>::end(&exponent);
while (it != end) {
if constexpr (internal::SupportsDoSquareInPlace<G>::value ||
if constexpr (internal::SupportsSquareImplInPlace<G>::value ||
internal::SupportsMulInPlace<G, G>::value) {
ret.SquareInPlace();
} else {
Expand Down Expand Up @@ -245,19 +245,19 @@ class AdditiveSemigroup {

// a.Double(): 2a
[[nodiscard]] constexpr auto Double() const {
if constexpr (internal::SupportsDoDouble<G>::value) {
if constexpr (internal::SupportsDoubleImpl<G>::value) {
const G* g = static_cast<const G*>(this);
return g->DoDouble();
return g->DoubleImpl();
} else {
return operator+(static_cast<const G&>(*this));
}
}

// a.DoubleInPlace(): a = 2a
constexpr G& DoubleInPlace() {
if constexpr (internal::SupportsDoDoubleInPlace<G>::value) {
if constexpr (internal::SupportsDoubleImplInPlace<G>::value) {
G* g = static_cast<G*>(this);
return g->DoDoubleInPlace();
return g->DoubleImplInPlace();
} else if constexpr (internal::SupportsAddInPlace<G, G>::value) {
return operator+=(static_cast<const G&>(*this));
} else {
Expand Down Expand Up @@ -416,7 +416,7 @@ class AdditiveSemigroup {
auto it = BitIteratorBE<BigInt<N>>::begin(&scalar, true);
auto end = BitIteratorBE<BigInt<N>>::end(&scalar);
while (it != end) {
if constexpr (internal::SupportsDoDoubleInPlace<G>::value ||
if constexpr (internal::SupportsDoubleImplInPlace<G>::value ||
internal::SupportsAddInPlace<G, G>::value) {
ret.DoubleInPlace();
} else {
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/bn/g2_prepared.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class G2Prepared : public G2PreparedBase<BNCurveConfig> {

G2AffinePoint q1 = MulByCharacteristic(q);
G2AffinePoint q2 = MulByCharacteristic(q1);
q2.NegInPlace();
q2.NegateInPlace();

if constexpr (Config::kXIsNegative) {
r.NegInPlace();
r.NegateInPlace();
}

ell_coeffs.push_back(r.AddInPlace(q1));
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/msm/glv.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class GLV {
Point b2 = Endomorphism(p);

if (result.k1.sign == Sign::kNegative) {
b1.NegInPlace();
b1.NegateInPlace();
}
if (result.k2.sign == Sign::kNegative) {
b2.NegInPlace();
b2.NegateInPlace();
}

RetPoint b1b2 = b1 + b2;
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/msm/glv_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ TYPED_TEST(GLVTest, Decompose) {
ScalarField k1 = ScalarField::FromMpzClass(result.k1.abs_value);
ScalarField k2 = ScalarField::FromMpzClass(result.k2.abs_value);
if (result.k1.sign == Sign::kNegative) {
k1.NegInPlace();
k1.NegateInPlace();
}
if (result.k2.sign == Sign::kNegative) {
k2.NegInPlace();
k2.NegateInPlace();
}
EXPECT_EQ(scalar, k1 + Point::Curve::Config::kLambda * k2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ __global__ void AggregateBucketsKernel(
auto base =
Load<AffinePoint<Curve>, CacheOperator::kNone>(&bases[base_index]);
if (sign) {
base.NegInPlace();
base.NegateInPlace();
}
bucket = base.ToXYZZ();
} else {
Expand Down Expand Up @@ -246,7 +246,7 @@ __global__ void AggregateBucketsKernel(
auto base =
Load<AffinePoint<Curve>, CacheOperator::kNone>(&bases[base_index]);
if (sign) {
base.NegInPlace();
base.NegateInPlace();
}
bucket += base;
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/pairing/g2_projective.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class G2Projective {
}
}

G2Projective& NegInPlace() {
y_.NegInPlace();
G2Projective& NegateInPlace() {
y_.NegateInPlace();
return *this;
}

Expand Down
6 changes: 3 additions & 3 deletions tachyon/math/elliptic_curves/short_weierstrass/affine_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ class AffinePoint<
return ToXYZZ() + other;
}

constexpr AffinePoint Negative() const { return {x_, -y_, infinity_}; }
constexpr AffinePoint Negate() const { return {x_, -y_, infinity_}; }

constexpr AffinePoint& NegInPlace() {
y_.NegInPlace();
constexpr AffinePoint& NegateInPlace() {
y_.NegateInPlace();
return *this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DEFINE_LAUNCH_FIELD_BINARY_OP(Add)

DEFINE_LAUNCH_UNARY_OP(kThreadNum, Double, bn254::G1AffinePointGpu,
bn254::G1JacobianPointGpu)
DEFINE_LAUNCH_UNARY_OP(kThreadNum, Negative, bn254::G1AffinePointGpu,
DEFINE_LAUNCH_UNARY_OP(kThreadNum, Negate, bn254::G1AffinePointGpu,
bn254::G1AffinePointGpu)

#undef DEFINE_LAUNCH_FIELD_BINARY_OP
Expand Down Expand Up @@ -131,8 +131,8 @@ TEST_F(AffinePointCorrectnessGpuTest, Double) {
}
}

TEST_F(AffinePointCorrectnessGpuTest, Negative) {
GPU_MUST_SUCCESS(LaunchNegative(xs_.get(), affine_results_.get(), N), "");
TEST_F(AffinePointCorrectnessGpuTest, Negate) {
GPU_MUST_SUCCESS(LaunchNegate(xs_.get(), affine_results_.get(), N), "");
for (size_t i = 0; i < N; ++i) {
SCOPED_TRACE(absl::Substitute("a: $0", xs_[i].ToString()));
auto result = ConvertPoint<bn254::G1AffinePoint>(affine_results_[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ TEST_F(AffinePointTest, AdditiveGroupOperators) {
EXPECT_EQ(-ap, test::AffinePoint(GF7(5), GF7(2)));
{
test::AffinePoint ap_tmp = ap;
ap_tmp.NegInPlace();
ap_tmp.NegateInPlace();
EXPECT_EQ(ap_tmp, test::AffinePoint(GF7(5), GF7(2)));
}

Expand Down
10 changes: 5 additions & 5 deletions tachyon/math/elliptic_curves/short_weierstrass/jacobian_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ class JacobianPoint<
constexpr JacobianPoint& AddInPlace(const JacobianPoint& other);
constexpr JacobianPoint Add(const AffinePoint<Curve>& other) const;
constexpr JacobianPoint& AddInPlace(const AffinePoint<Curve>& other);
constexpr JacobianPoint DoDouble() const;
constexpr JacobianPoint& DoDoubleInPlace();
constexpr JacobianPoint DoubleImpl() const;
constexpr JacobianPoint& DoubleImplInPlace();

// AdditiveGroup methods
constexpr JacobianPoint Negative() const { return {x_, -y_, z_}; }
constexpr JacobianPoint Negate() const { return {x_, -y_, z_}; }

constexpr JacobianPoint& NegInPlace() {
y_.NegInPlace();
constexpr JacobianPoint& NegateInPlace() {
y_.NegateInPlace();
return *this;
}

Expand Down
Loading
Loading