Skip to content

Commit

Permalink
Fix usage of BOOST_MP_ASSERT. (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
martun authored May 20, 2024
1 parent 643884f commit 37b1ccf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
33 changes: 20 additions & 13 deletions include/nil/crypto3/multiprecision/cpp_int_modular.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@
#define BOOST_MP_DETAIL_ASSERT_HPP
// Using BOOST_STATIC_ASSERT_MSG on the next line results to compilation issues on older compilers like clang-12,
// so commenting it for now. In case we decide to use only fresher compilers, it can be uncommented.
#define BOOST_MP_ASSERT_MSG(expr, msg) \
if (BOOST_MP_IS_CONST_EVALUATED()) { \
/* BOOST_STATIC_ASSERT_MSG(expr, msg); */ \
} else { \
BOOST_ASSERT_MSG(expr, msg); \
}

#define BOOST_MP_ASSERT(expr) \
if (BOOST_MP_IS_CONST_EVALUATED()) { \
/* BOOST_STATIC_ASSERT(expr);*/ \
} else { \
BOOST_ASSERT(expr); \
}
#define BOOST_MP_ASSERT_MSG(expr, msg) \
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION \
if (BOOST_MP_IS_CONST_EVALUATED()) { \
/* BOOST_STATIC_ASSERT_MSG(expr, msg); */ \
} else { \
BOOST_ASSERT_MSG(expr, msg); \
} \
#else \
#endif

#define BOOST_MP_ASSERT(expr) \
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION \
if (BOOST_MP_IS_CONST_EVALUATED()) { \
/* BOOST_STATIC_ASSERT(expr);*/ \
} else { \
BOOST_ASSERT(expr); \
} \
#else \
#endif

#endif

// Sometimes we convert cpp_int_modular_backend to cpp_int_backend of boost for testing and for division.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ namespace boost {

//
// Core subtraction routine for all non-trivial cpp_int's:
// It is the caller's responsibility to make sure that a > b.
// It is the caller's responsibility to make sure that a >= b.
//
template<unsigned Bits>
inline BOOST_MP_CXX14_CONSTEXPR void subtract_unsigned_constexpr(
cpp_int_modular_backend<Bits>& result,
const cpp_int_modular_backend<Bits>& a,
const cpp_int_modular_backend<Bits>& b) noexcept {
BOOST_ASSERT(!eval_lt(a, b));
BOOST_MP_ASSERT(!eval_lt(a, b));

//
// This is the generic, C++ only version of subtraction.
Expand All @@ -90,7 +90,7 @@ namespace boost {
borrow = (borrow >> cpp_int_modular_backend<Bits>::limb_bits) & 1u;
}
// if a > b, then borrow must be 0 at the end.
// BOOST_ASSERT(0 == borrow);
BOOST_MP_ASSERT(0 == borrow);
}

#ifdef CO3_MP_HAS_IMMINTRIN_H
Expand All @@ -116,7 +116,7 @@ namespace boost {
const cpp_int_modular_backend<Bits>& a,
const cpp_int_modular_backend<Bits>& b) noexcept {

#ifndef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(a.size())) {
add_unsigned_constexpr(result, a, b);
} else
Expand Down Expand Up @@ -180,7 +180,7 @@ namespace boost {
cpp_int_modular_backend<Bits>& result,
const cpp_int_modular_backend<Bits>& a,
const cpp_int_modular_backend<Bits>& b) noexcept {
BOOST_ASSERT(!eval_lt(a, b));
BOOST_MP_ASSERT(!eval_lt(a, b));

#ifndef TO3_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(a.size())) {
Expand Down Expand Up @@ -241,7 +241,7 @@ namespace boost {
for (; i < m; ++i)
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i], pb[i], pr + i);

BOOST_ASSERT(0 == borrow);
BOOST_MP_ASSERT(0 == borrow);

} // constepxr.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ namespace boost {
if ((s & limb_shift_mask) == 0) {
left_shift_limb(result, s);
}
#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
else if ((s & byte_shift_mask) == 0)
#else
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
Expand All @@ -214,7 +214,7 @@ namespace boost {
#elif BOOST_ENDIAN_LITTLE_BYTE
BOOST_MP_CXX14_CONSTEXPR const limb_type byte_shift_mask = CHAR_BIT - 1;

#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
if ((s & byte_shift_mask) == 0)
#else
BOOST_MP_CXX14_CONSTEXPR limb_type limb_shift_mask =
Expand Down Expand Up @@ -339,7 +339,7 @@ namespace boost {

if ((s & limb_shift_mask) == 0)
right_shift_limb(result, s);
#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
else if ((s & byte_shift_mask) == 0)
#else
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
Expand All @@ -348,7 +348,7 @@ namespace boost {
#elif BOOST_ENDIAN_LITTLE_BYTE && !defined(TVM)
BOOST_MP_CXX14_CONSTEXPR const limb_type byte_shift_mask = CHAR_BIT - 1;

#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
if ((s & byte_shift_mask) == 0)
#else
BOOST_MP_CXX14_CONSTEXPR limb_type limb_shift_mask =
Expand Down
6 changes: 3 additions & 3 deletions include/nil/crypto3/multiprecision/cpp_int_modular/limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace std {
using ui_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<Bits>,
ExpressionTemplates>;
#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
Expand Down Expand Up @@ -63,7 +63,7 @@ namespace std {
ExpressionTemplates>
get_min() {
// Bounded, unsigned, no allocator (can be constexpr):
#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
Expand All @@ -88,7 +88,7 @@ namespace std {
using ui_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<Bits>,
ExpressionTemplates>;
#ifdef CRYPTO3_MP_NO_CONSTEXPR_DETECTION
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(150)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(151)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(152)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(160)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(161)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(163)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(164)
CRYPTO3_MP_DEFINE_SIZED_CPP_INT_MODULAR_LITERAL(177)
Expand Down

0 comments on commit 37b1ccf

Please sign in to comment.