Skip to content

Commit

Permalink
multiprecision: goldilocks: fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 25, 2024
1 parent e98e87f commit 89e95f3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ namespace nil::crypto3::multiprecision {

// Simple modular big integer type with compile-time modulus. Modulus should be a
// static big_uint constant. Uses barret optimizations.
template<const auto& modulus>
using big_mod = big_mod_ct_impl<modulus, detail::barrett_modular_ops>;
template<const auto& Modulus>
using big_mod = big_mod_ct_impl<Modulus, detail::barrett_modular_ops>;

// Simple modular big integer type with runtime modulus. Uses barret optimizations.
template<std::size_t Bits>
Expand All @@ -307,11 +307,11 @@ namespace nil::crypto3::multiprecision {
// Modular big integer type with compile-time modulus, which automatically uses
// montomery form whenever possible (i.e. for odd moduli). Modulus should be a static
// big_uint constant.
template<const auto& modulus>
template<const auto& Modulus>
using auto_big_mod = std::conditional_t<
modulus == goldilocks_modulus, goldilocks_mod,
std::conditional_t<detail::check_montgomery_constraints(modulus),
montgomery_big_mod<modulus>, big_mod<modulus>>>;
Modulus == goldilocks_modulus, goldilocks_mod,
std::conditional_t<detail::modulus_supports_montgomery(Modulus),
montgomery_big_mod<Modulus>, big_mod<Modulus>>>;
} // namespace nil::crypto3::multiprecision

// std::hash specializations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ namespace nil::crypto3::multiprecision::detail {
public:
using base_type = base_type_;

static_assert(is_integral_v<base_type>);

constexpr common_modular_ops(const base_type &m) : m_mod(m) {}

constexpr bool compare_eq(const common_modular_ops &other) const {
return mod() == other.mod();
}

constexpr void negate_inplace(base_type &raw_base) const {
if (!raw_base.is_zero()) {
if (!is_zero(raw_base)) {
auto initial_raw_base = raw_base;
raw_base = mod();
raw_base -= initial_raw_base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
#include "nil/crypto3/multiprecision/detail/integer_ops_base.hpp"

namespace nil::crypto3::multiprecision::detail {
template<std::size_t Bits>
constexpr bool check_montgomery_constraints(const big_uint<Bits> &m) {
template<typename T>
constexpr bool modulus_supports_montgomery(const T &m) {
static_assert(is_integral_v<T> && !std::numeric_limits<T>::is_signed);
// Check m % 2 == 0
return m.bit_test(0u);
return bit_test(m, 0u);
}

// Montgomery modular operations. Uses Barrett reduction internally and inherits
Expand All @@ -43,7 +44,7 @@ namespace nil::crypto3::multiprecision::detail {
static constexpr std::size_t limb_count = big_uint_t::static_limb_count;

constexpr montgomery_modular_ops(const big_uint_t &m) : barrett_modular_ops<Bits_>(m) {
if (!check_montgomery_constraints(m)) {
if (!modulus_supports_montgomery(m)) {
throw std::invalid_argument("module not usable with montgomery");
}

Expand Down
41 changes: 26 additions & 15 deletions crypto3/libs/multiprecision/test/big_mod_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@

#define BOOST_TEST_MODULE big_mod_basic_test

#include <boost/test/unit_test.hpp>

#include <cstdint>
#include <type_traits>
#include <utility>

#include <boost/test/unit_test.hpp>

#include "nil/crypto3/multiprecision/big_mod.hpp"
#include "nil/crypto3/multiprecision/big_uint.hpp"
#include "nil/crypto3/multiprecision/literals.hpp"

using namespace nil::crypto3::multiprecision;
using namespace nil::crypto3::multiprecision::literals;

NIL_CO3_MP_DEFINE_BIG_UINT_LITERAL(2)
NIL_CO3_MP_DEFINE_BIG_UINT_LITERAL(32)
NIL_CO3_MP_DEFINE_BIG_UINT_LITERAL(36)
NIL_CO3_MP_DEFINE_BIG_UINT_LITERAL(57)
NIL_CO3_MP_DEFINE_BIG_UINT_LITERAL(60)

using namespace nil::crypto3::multiprecision::literals;
constexpr big_uint<64> goldilocks_modulus_big_uint = 0xFFFFFFFF00000001ULL;
static_assert(std::is_same_v<auto_big_mod<goldilocks_modulus_big_uint>, goldilocks_mod>);

constexpr auto odd_mod = 0x123456789ABCDEF_big_uint57;
constexpr auto even_mod = 0x123456789ABCDEE_big_uint57;

static_assert(std::is_same_v<auto_big_mod<odd_mod>, montgomery_big_mod<odd_mod>>);
static_assert(std::is_same_v<auto_big_mod<even_mod>, big_mod<even_mod>>);

constexpr auto mod = 0x123456789ABCDEF_big_uint57;
using montgomery_big_mod_t = montgomery_big_mod<mod>;
using big_mod_t = big_mod<mod>;
using montgomery_big_mod_t = montgomery_big_mod<odd_mod>;
using big_mod_t = big_mod<odd_mod>;

BOOST_AUTO_TEST_SUITE(smoke)

Expand Down Expand Up @@ -196,25 +202,30 @@ BOOST_AUTO_TEST_CASE(multilimb) {
BOOST_AUTO_TEST_CASE(big) {
static constexpr auto mod =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001_big_uint224;
big_mod<mod> a = 0xC5067EE5D80302E0561545A8467C6D5C98BC4D37672EB301C38CE9A9_big_uint224;
big_mod<mod> a =
0xC5067EE5D80302E0561545A8467C6D5C98BC4D37672EB301C38CE9A9_big_uint224;

big_mod<mod> b = 0xE632329C42040E595D127EB6889D22215DBE56F540425C705D6BF83_big_uint224;
big_mod<mod> b =
0xE632329C42040E595D127EB6889D22215DBE56F540425C705D6BF83_big_uint224;

BOOST_CHECK_EQUAL((a * b).base(),
0x107BC09A9F3443A6F6458495ADD98CBA1FCD15F17D0EAB66302FEFA6_big_uint224);
BOOST_CHECK_EQUAL(
(a * b).base(),
0x107BC09A9F3443A6F6458495ADD98CBA1FCD15F17D0EAB66302FEFA6_big_uint224);
}

BOOST_AUTO_TEST_CASE(big_assign) {
static constexpr auto mod =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001_big_uint224;
big_mod<mod> a = 0xC5067EE5D80302E0561545A8467C6D5C98BC4D37672EB301C38CE9A9_big_uint224;
big_mod<mod> a =
0xC5067EE5D80302E0561545A8467C6D5C98BC4D37672EB301C38CE9A9_big_uint224;

big_mod<mod> b = 0xE632329C42040E595D127EB6889D22215DBE56F540425C705D6BF83_big_uint224;
big_mod<mod> b =
0xE632329C42040E595D127EB6889D22215DBE56F540425C705D6BF83_big_uint224;

a *= b;

BOOST_CHECK_EQUAL(a.base(),
0x107BC09A9F3443A6F6458495ADD98CBA1FCD15F17D0EAB66302FEFA6_big_uint224);
BOOST_CHECK_EQUAL(
a.base(), 0x107BC09A9F3443A6F6458495ADD98CBA1FCD15F17D0EAB66302FEFA6_big_uint224);
}

BOOST_AUTO_TEST_SUITE_END()
Expand Down

0 comments on commit 89e95f3

Please sign in to comment.