Skip to content

Commit

Permalink
Allow signed numbers in modular adaptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed May 16, 2024
1 parent f7b49f1 commit 3477c4f
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <nil/crypto3/multiprecision/modular/modular_params_fixed.hpp>
#include <nil/crypto3/multiprecision/traits/is_backend.hpp>

namespace boost {
namespace boost {
namespace multiprecision {
namespace backends {
template<typename Backend, typename StorageType>
Expand Down Expand Up @@ -80,9 +80,15 @@ namespace boost {
template<typename SI,
typename std::enable_if_t<std::is_integral<SI>::value && std::is_signed<SI>::value> const * = nullptr>
BOOST_MP_CXX14_CONSTEXPR modular_adaptor(SI b)
: m_base(b >= 0 ?
static_cast<typename std::tuple_element<0, unsigned_types>::type>(b) :
this->mod_data().get_mod() - static_cast<typename std::tuple_element<0, unsigned_types>::type>(-b) ) {
: m_base(static_cast<typename std::tuple_element<0, unsigned_types>::type>(0)) {

if (b >= 0) {
m_base = static_cast<typename std::tuple_element<0, unsigned_types>::type>(b);
} else {
m_base = this->mod_data().get_mod();
eval_subtract(m_base, static_cast<typename std::tuple_element<0, unsigned_types>::type>(-b) );
}

// This method must be called only for compile time modular params.
// this->set_modular_params(m);
this->mod_data().adjust_modular(m_base);
Expand All @@ -97,6 +103,22 @@ namespace boost {
this->mod_data().adjust_modular(m_base);
}

template<typename SI,
typename std::enable_if_t<std::is_integral<SI>::value && std::is_signed<SI>::value> const * = nullptr>
BOOST_MP_CXX14_CONSTEXPR modular_adaptor(SI b, const modular_type &m)
: m_base(static_cast<typename std::tuple_element<0, unsigned_types>::type>(0)) {

if (b >= 0) {
m_base = static_cast<typename std::tuple_element<0, unsigned_types>::type>(b);
} else {
m_base = this->mod_data().get_mod();
eval_subtract(m_base, static_cast<typename std::tuple_element<0, unsigned_types>::type>(-b));
}

this->set_modular_params(m);
this->mod_data().adjust_modular(m_base);
}

template<typename UI,
typename std::enable_if_t<std::is_integral<UI>::value && std::is_unsigned<UI>::value> const * = nullptr>
BOOST_MP_CXX14_CONSTEXPR modular_adaptor(UI b, const modular_type &m)
Expand Down

0 comments on commit 3477c4f

Please sign in to comment.