Skip to content

Commit

Permalink
multiprecision: move operators into class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 16, 2024
1 parent c17aa1d commit f022aa0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,43 @@ namespace nil::crypto3::multiprecision {

constexpr void negate() { ops().negate(m_raw_base); }

constexpr auto& operator++() noexcept {
ops().increment(raw_base());
return *this;
}

constexpr auto operator++(int) noexcept {
auto copy = *this;
++*this;
return copy;
}

constexpr auto operator+() const noexcept { return *this; }

constexpr auto& operator--() noexcept {
ops().decrement(raw_base());
return *this;
}

constexpr auto operator--(int) noexcept {
auto copy = *this;
--*this;
return copy;
}

constexpr auto operator-() const noexcept {
auto result = *this;
result.negate();
return result;
}

// IO

friend std::ostream& operator<<(std::ostream& os, const big_mod_impl& value) {
os << value.str(os.flags());
return os;
}

// Misc ops

constexpr bool is_zero() const noexcept {
Expand Down Expand Up @@ -175,7 +212,7 @@ namespace nil::crypto3::multiprecision {

// Comparison

#define NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(OP_) \
#define NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(OP_) \
template<typename T1, typename T2, \
std::enable_if_t<detail::are_valid_operand_types<T1, T2>(), int> = 0> \
constexpr bool operator OP_(const T1& a, const T2& b) noexcept { \
Expand All @@ -188,19 +225,19 @@ namespace nil::crypto3::multiprecision {
} \
}

NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(==)
NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(!=)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(==)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(!=)
// TODO(ioxid) remove these
NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(<)
NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(<=)
NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(>)
NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL(>=)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(<)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(<=)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(>)
NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL(>=)

#undef NIL_CO3_MP_MODULAR_BIG_UINT_COMPARISON_IMPL
#undef NIL_CO3_MP_BIG_MOD_COMPARISON_IMPL

// Arithmetic operations

#define NIL_CO3_MP_MODULAR_BIG_UINT_OPERATOR_IMPL(OP_, OP_ASSIGN_, METHOD_) \
#define NIL_CO3_MP_BIG_MOD_OPERATOR_IMPL(OP_, OP_ASSIGN_, METHOD_) \
template<typename T1, typename T2, \
std::enable_if_t<detail::are_valid_operand_types<T1, T2>(), int> = 0, \
typename big_mod_t = std::conditional_t<detail::is_big_mod_v<T1>, T1, T2>> \
Expand All @@ -222,53 +259,11 @@ namespace nil::crypto3::multiprecision {
return a; \
}

#define NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE \
template<typename big_mod_t, std::enable_if_t<detail::is_big_mod_v<big_mod_t>, int> = 0>

NIL_CO3_MP_MODULAR_BIG_UINT_OPERATOR_IMPL(+, +=, add)

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr auto& operator++(big_mod_t& a) noexcept {
a.ops().increment(a.raw_base());
return a;
}

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr auto operator++(big_mod_t& a, int) noexcept {
auto copy = a;
++a;
return copy;
}

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr auto operator+(const big_mod_t& a) noexcept { return a; }
NIL_CO3_MP_BIG_MOD_OPERATOR_IMPL(+, +=, add)
NIL_CO3_MP_BIG_MOD_OPERATOR_IMPL(-, -=, subtract)
NIL_CO3_MP_BIG_MOD_OPERATOR_IMPL(*, *=, mul)

NIL_CO3_MP_MODULAR_BIG_UINT_OPERATOR_IMPL(-, -=, subtract)

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr auto& operator--(big_mod_t& a) noexcept {
a.ops().decrement(a.raw_base());
return a;
}

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr auto operator--(big_mod_t& a, int) noexcept {
auto copy = a;
--a;
return copy;
}

NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
constexpr big_mod_t operator-(const big_mod_t& a) noexcept {
big_mod_t result = a;
result.negate();
return result;
}

NIL_CO3_MP_MODULAR_BIG_UINT_OPERATOR_IMPL(*, *=, mul)

#undef NIL_CO3_MP_MODULAR_BIG_UINT_UNARY_TEMPLATE
#undef NIL_CO3_MP_MODULAR_BIG_UINT_OPERATOR_IMPL
#undef NIL_CO3_MP_BIG_MOD_OPERATOR_IMPL

// Hash

Expand All @@ -287,15 +282,7 @@ namespace nil::crypto3::multiprecision {
return result;
}

// IO

template<typename T, std::enable_if_t<detail::is_big_mod_v<T>, int> = 0>
std::ostream& operator<<(std::ostream& os, const T& value) {
os << value.str(os.flags());
return os;
}

// Common ops
// Misc ops

template<typename big_mod_t, std::enable_if_t<detail::is_big_mod_v<big_mod_t>, int> = 0>
constexpr bool is_zero(const big_mod_t& a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,13 @@ namespace nil::crypto3::multiprecision {
return result;
}

// IO

friend std::ostream& operator<<(std::ostream& os, const big_uint& value) {
os << value.str(os.flags());
return os;
}

// Misc ops

NIL_CO3_MP_FORCEINLINE constexpr bool is_zero() const noexcept {
Expand Down Expand Up @@ -1075,15 +1082,7 @@ namespace nil::crypto3::multiprecision {
return result;
}

// IO

template<std::size_t Bits>
std::ostream& operator<<(std::ostream& os, const big_uint<Bits>& value) {
os << value.str(os.flags());
return os;
}

// Common ops
// Misc ops

template<std::size_t Bits>
constexpr std::size_t msb(const big_uint<Bits>& a) {
Expand Down

0 comments on commit f022aa0

Please sign in to comment.