Skip to content

Commit

Permalink
fix(math): initialize variable in constexpr function
Browse files Browse the repository at this point in the history
  • Loading branch information
chokobole committed Jun 20, 2024
1 parent bd3c3b6 commit b3190c8
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion tachyon/math/base/arithmetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ALWAYS_INLINE SubResult<uint32_t> SubWithBorrow(uint32_t a, uint32_t b,
ALWAYS_INLINE constexpr MulResult<uint32_t> MulAddWithCarry(
uint32_t a, uint32_t b, uint32_t c, uint32_t carry = 0) {
uint64_t tmp = uint64_t{a} + uint64_t{b} * uint64_t{c} + uint64_t{carry};
MulResult<uint32_t> result;
MulResult<uint32_t> result{};
result.lo = static_cast<uint32_t>(tmp);
result.hi = static_cast<uint32_t>(tmp >> 32);
return result;
Expand Down
24 changes: 12 additions & 12 deletions tachyon/math/base/big_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {

// Returns the maximum representable value for BigInt.
constexpr static BigInt Max() {
BigInt ret;
BigInt ret{};
for (uint64_t& limb : ret.limbs) {
limb = std::numeric_limits<uint64_t>::max();
}
Expand All @@ -112,7 +112,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {

// Generate a random BigInt between [0, |max|).
constexpr static BigInt Random(const BigInt& max = Max()) {
BigInt ret;
BigInt ret{};
for (size_t i = 0; i < N; ++i) {
ret[i] = base::Uniform(base::Range<uint64_t>::All());
}
Expand Down Expand Up @@ -141,7 +141,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
template <size_t BitNums = kBitNums>
constexpr static BigInt FromBitsLE(const std::bitset<BitNums>& bits) {
static_assert(BitNums <= kBitNums);
BigInt ret;
BigInt ret{};
size_t bit_idx = 0;
size_t limb_idx = 0;
std::bitset<kLimbBitNums> limb_bits;
Expand All @@ -167,7 +167,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
template <size_t BitNums = kBitNums>
constexpr static BigInt FromBitsBE(const std::bitset<BitNums>& bits) {
static_assert(BitNums <= kBitNums);
BigInt ret;
BigInt ret{};
std::bitset<kLimbBitNums> limb_bits;
size_t bit_idx = 0;
size_t limb_idx = 0;
Expand Down Expand Up @@ -196,7 +196,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
// ordering.
template <typename ByteContainer>
constexpr static BigInt FromBytesLE(const ByteContainer& bytes) {
BigInt ret;
BigInt ret{};
size_t byte_idx = 0;
size_t limb_idx = 0;
uint64_t limb = 0;
Expand Down Expand Up @@ -224,7 +224,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
// ordering.
template <typename ByteContainer>
constexpr static BigInt FromBytesBE(const ByteContainer& bytes) {
BigInt ret;
BigInt ret{};
size_t byte_idx = 0;
size_t limb_idx = 0;
uint64_t limb = 0;
Expand Down Expand Up @@ -446,7 +446,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt operator&(const BigInt& other) const {
BigInt ret;
BigInt ret{};
if constexpr (N == 1) {
ret[0] = limbs[0] & other[0];
} else if constexpr (N == 2) {
Expand All @@ -471,7 +471,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt operator|(const BigInt& other) const {
BigInt ret;
BigInt ret{};
if constexpr (N == 1) {
ret[0] = limbs[0] | other[0];
} else if constexpr (N == 2) {
Expand All @@ -496,7 +496,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt operator^(const BigInt& other) const {
BigInt ret;
BigInt ret{};
if constexpr (N == 1) {
ret[0] = limbs[0] ^ other[0];
} else if constexpr (N == 2) {
Expand Down Expand Up @@ -548,7 +548,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt Add(const BigInt& other, uint64_t& carry) const {
BigInt ret;
BigInt ret{};
DoAdd(*this, other, carry, ret);
return ret;
}
Expand All @@ -569,7 +569,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt Sub(const BigInt& other, uint64_t& borrow) const {
BigInt ret;
BigInt ret{};
DoSub(*this, other, borrow, ret);
return ret;
}
Expand All @@ -590,7 +590,7 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
}

constexpr BigInt MulBy2(uint64_t& carry) const {
BigInt ret;
BigInt ret{};
DoMulBy2(*this, carry, ret);
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion tachyon/math/elliptic_curves/msm/msm_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct TACHYON_EXPORT MSMCtx {

template <typename ScalarField>
constexpr static MSMCtx CreateDefault(size_t size) {
MSMCtx ctx;
MSMCtx ctx{};
ctx.window_bits = ComputeWindowsBits(size);
ctx.window_count = ComputeWindowsCount<ScalarField>(ctx.window_bits);
ctx.size = size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AffinePoint<

constexpr static std::optional<AffinePoint> CreateFromX(const BaseField& x,
bool pick_odd) {
AffinePoint point;
AffinePoint point{};
if (!Curve::GetPointFromX(x, pick_odd, &point)) return std::nullopt;
return point;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class JacobianPoint<

constexpr static std::optional<JacobianPoint> CreateFromX(const BaseField& x,
bool pick_odd) {
JacobianPoint point;
JacobianPoint point{};
if (!Curve::GetPointFromX(x, pick_odd, &point)) return std::nullopt;
return point;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PointXYZZ<_Curve,

constexpr static std::optional<PointXYZZ> CreateFromX(const BaseField& x,
bool pick_odd) {
PointXYZZ point;
PointXYZZ point{};
if (!Curve::GetPointFromX(x, pick_odd, &point)) return std::nullopt;
return point;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ProjectivePoint<

constexpr static std::optional<ProjectivePoint> CreateFromX(
const BaseField& x, bool pick_odd) {
ProjectivePoint point;
ProjectivePoint point{};
if (!Curve::GetPointFromX(x, pick_odd, &point)) return std::nullopt;
return point;
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/short_weierstrass/sw_curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class SWCurve {
template <typename Point>
constexpr static bool GetPointFromX(const BaseField& x, bool pick_odd,
Point* point) {
BaseField even_y;
BaseField odd_y;
BaseField even_y{};
BaseField odd_y{};
if (!GetYsFromX(x, &even_y, &odd_y)) return false;
if constexpr (std::is_same_v<Point, AffinePoint>) {
*point = AffinePoint(x, pick_odd ? odd_y : even_y);
Expand Down
6 changes: 3 additions & 3 deletions tachyon/math/finite_fields/cubic_extension_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class CubicExtensionField : public CyclotomicMultiplicativeSubgroup<Derived> {

// MultiplicativeSemigroup methods
constexpr Derived Mul(const Derived& other) const {
Derived ret;
Derived ret{};
DoMul(*static_cast<const Derived*>(this), other, ret);
return ret;
}
Expand All @@ -255,7 +255,7 @@ class CubicExtensionField : public CyclotomicMultiplicativeSubgroup<Derived> {
}

constexpr Derived SquareImpl() const {
Derived ret;
Derived ret{};
DoSquareImpl(*static_cast<const Derived*>(this), ret);
return ret;
}
Expand All @@ -268,7 +268,7 @@ class CubicExtensionField : public CyclotomicMultiplicativeSubgroup<Derived> {

// MultiplicativeGroup methods
constexpr std::optional<Derived> Inverse() const {
Derived ret;
Derived ret{};
if (LIKELY(DoInverse(*static_cast<const Derived*>(this), ret))) return ret;
LOG_IF_NOT_GPU(ERROR) << "Inverse of zero attempted";
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final
constexpr static PrimeField Zero() { return PrimeField(); }

constexpr static PrimeField One() {
PrimeField ret;
PrimeField ret{};
ret.value_ = Config::kOne;
return ret;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final
}

constexpr static PrimeField FromMontgomery(const BigInt<N>& big_int) {
PrimeField ret;
PrimeField ret{};
ret.value_ = big_int;
return ret;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final

// AdditiveSemigroup methods
constexpr PrimeField Add(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
%{prefix}_rawAdd(ret.value_.limbs, value_.limbs, other.value_.limbs);
return ret;
}
Expand All @@ -169,7 +169,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final

// AdditiveGroup methods
constexpr PrimeField Sub(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
%{prefix}_rawSub(ret.value_.limbs, value_.limbs, other.value_.limbs);
return ret;
}
Expand All @@ -180,7 +180,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final
}

constexpr PrimeField Negate() const {
PrimeField ret;
PrimeField ret{};
%{prefix}_rawNeg(ret.value_.limbs, value_.limbs);
return ret;
}
Expand All @@ -193,7 +193,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final
// TODO(chokobole): Support bigendian.
// MultiplicativeSemigroup methods
constexpr PrimeField Mul(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
%{prefix}_rawMMul(ret.value_.limbs, value_.limbs, other.value_.limbs);
return ret;
}
Expand All @@ -204,7 +204,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final
}

constexpr PrimeField Square() const {
PrimeField ret;
PrimeField ret{};
%{prefix}_rawMSquare(ret.value_.limbs, value_.limbs);
return ret;
}
Expand All @@ -216,7 +216,7 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{asm_flag}>> final

// MultiplicativeGroup methods
constexpr std::optional<PrimeField> Inverse() const {
PrimeField ret;
PrimeField ret{};
if (LIKELY(value_.template MontgomeryInverse<Config::kModulusHasSpareBit>(
Config::kModulus, Config::kMontgomeryR2, ret.value_))) {
return ret;
Expand Down
18 changes: 9 additions & 9 deletions tachyon/math/finite_fields/prime_field_fallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
constexpr static PrimeField Zero() { return PrimeField(); }

constexpr static PrimeField One() {
PrimeField ret;
PrimeField ret{};
ret.value_ = Config::kOne;
return ret;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
}

constexpr static PrimeField FromMontgomery(const BigInt<N>& mont) {
PrimeField ret;
PrimeField ret{};
ret.value_ = mont;
return ret;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&

// AdditiveSemigroup methods
constexpr PrimeField Add(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
uint64_t carry = 0;
ret.value_ = value_.Add(other.value_, carry);
BigInt<N>::template Clamp<Config::kModulusHasSpareBit>(Config::kModulus,
Expand All @@ -184,7 +184,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
}

constexpr PrimeField DoubleImpl() const {
PrimeField ret;
PrimeField ret{};
uint64_t carry = 0;
ret.value_ = value_.MulBy2(carry);
BigInt<N>::template Clamp<Config::kModulusHasSpareBit>(Config::kModulus,
Expand All @@ -202,7 +202,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&

// AdditiveGroup methods
constexpr PrimeField Sub(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
if (other.value_ > value_) {
ret.value_ = value_.Add(Config::kModulus);
ret.value_.SubInPlace(other.value_);
Expand All @@ -221,7 +221,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
}

constexpr PrimeField Negate() const {
PrimeField ret;
PrimeField ret{};
if (!IsZero()) {
ret.value_ = Config::kModulus;
ret.value_.SubInPlace(value_);
Expand All @@ -241,7 +241,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
// TODO(chokobole): Support bigendian.
// MultiplicativeSemigroup methods
constexpr PrimeField Mul(const PrimeField& other) const {
PrimeField ret;
PrimeField ret{};
if constexpr (Config::kCanUseNoCarryMulOptimization) {
DoFastMul(*this, other, ret);
} else {
Expand All @@ -263,7 +263,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&
if (N == 1) {
return Mul(*this);
}
PrimeField ret;
PrimeField ret{};
DoSquareImpl(*this, ret);
return ret;
}
Expand All @@ -278,7 +278,7 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kUseAsm &&

// MultiplicativeGroup methods
constexpr std::optional<PrimeField> Inverse() const {
PrimeField ret;
PrimeField ret{};
if (LIKELY(value_.template MontgomeryInverse<Config::kModulusHasSpareBit>(
Config::kModulus, Config::kMontgomeryR2, ret.value_))) {
return ret;
Expand Down
Loading

0 comments on commit b3190c8

Please sign in to comment.