Skip to content

Commit

Permalink
Some changes related to recent changes in multiprecision. (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
martun authored May 15, 2024
1 parent bfb5a61 commit 00730e2
Show file tree
Hide file tree
Showing 14 changed files with 1,120 additions and 1,086 deletions.
28 changes: 14 additions & 14 deletions include/nil/crypto3/math/algorithms/unity_root.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ namespace nil {
* @return a root of unity.
*/
template<typename Backend,
multiprecision::expression_template_option ExpressionTemplates>
multiprecision::number<Backend, ExpressionTemplates>
unity_root(uint32_t m, const multiprecision::number<Backend, ExpressionTemplates> &modulo) {
using namespace multiprecision;
boost::multiprecision::expression_template_option ExpressionTemplates>
boost::multiprecision::number<Backend, ExpressionTemplates>
unity_root(uint32_t m, const boost::multiprecision::number<Backend, ExpressionTemplates> &modulo) {
using namespace boost::multiprecision;

number<Backend, ExpressionTemplates> M(m);

if ((modulo - number<Backend, ExpressionTemplates>(1) % M) % M != 0) {
return {};
}

number<modular_adaptor<Backend, backends::modular_params_rt<Backend>>, ExpressionTemplates>
gen(find_generator(modulo), modulo), result = multiprecision::pow(gen, (modulo - 1) / M);
if (result == 1) {
number<backends::modular_adaptor<Backend, backends::modular_params_rt<Backend>>, ExpressionTemplates>
gen(find_generator(modulo), modulo), result = boost::multiprecision::pow(gen, (modulo - 1) / M);
if (result == 1u) {
result = unity_root(m, modulo);
}

Expand All @@ -120,20 +120,20 @@ namespace nil {
*
*/

multiprecision::number<Backend, ExpressionTemplates> mu = modulo.ComputeMu();
multiprecision::number<Backend, ExpressionTemplates> x(1);
boost::multiprecision::number<Backend, ExpressionTemplates> mu = modulo.ComputeMu();
boost::multiprecision::number<Backend, ExpressionTemplates> x(1);
x.ModMulEq(result, modulo, mu);
multiprecision::number<Backend, ExpressionTemplates> minRU(x);
multiprecision::number<Backend, ExpressionTemplates> curPowIdx(1);
std::vector<multiprecision::number<Backend, ExpressionTemplates>> coprimes = algebra::totient_list<multiprecision::number<Backend, ExpressionTemplates>>(
boost::multiprecision::number<Backend, ExpressionTemplates> minRU(x);
boost::multiprecision::number<Backend, ExpressionTemplates> curPowIdx(1);
std::vector<boost::multiprecision::number<Backend, ExpressionTemplates>> coprimes = algebra::totient_list<boost::multiprecision::number<Backend, ExpressionTemplates>>(
m);
for (uint32_t i = 0; i < coprimes.size(); i++) {
auto nextPowIdx = coprimes[i];
multiprecision::number<Backend, ExpressionTemplates> diffPow(nextPowIdx - curPowIdx);
boost::multiprecision::number<Backend, ExpressionTemplates> diffPow(nextPowIdx - curPowIdx);
for (std::size_t j = 0; j < diffPow; j++) {
x.ModMulEq(result, modulo, mu);
}
if (x < minRU && x != multiprecision::number<Backend, ExpressionTemplates>(1)) {
if (x < minRU && x != boost::multiprecision::number<Backend, ExpressionTemplates>(1)) {
minRU = x;
}
curPowIdx = nextPowIdx;
Expand Down
4 changes: 2 additions & 2 deletions include/nil/crypto3/math/domains/evaluation_domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <vector>

#include <nil/crypto3/multiprecision/integer.hpp>
#include <boost/multiprecision/integer.hpp>
#include <nil/crypto3/math/polynomial/polynomial.hpp>

namespace nil {
Expand All @@ -55,7 +55,7 @@ namespace nil {
*
* (See the function get_evaluation_domain below.)
*/
evaluation_domain(const std::size_t m) : m(m), log2_size(multiprecision::msb(m)) {};
evaluation_domain(const std::size_t m) : m(m), log2_size(boost::multiprecision::msb(m)) {};

inline std::size_t size() const {
return m;
Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/math/domains/step_radix2_domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace nil {
}

// compute A_prefix
const field_value_type over_two = field_value_type(2).inversed();
const field_value_type over_two = field_value_type(2u).inversed();
for (std::size_t i = 0; i < small_m; ++i) {
a[i] = (U0[i] + U1[i]) * over_two;
}
Expand Down
6 changes: 4 additions & 2 deletions include/nil/crypto3/math/polynomial/basic_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <nil/crypto3/math/algorithms/unity_root.hpp>
#include <nil/crypto3/math/domains/detail/basic_radix2_domain_aux.hpp>
#include <nil/crypto3/math/detail/field_utils.hpp>
#include <nil/crypto3/detail/type_traits.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -94,7 +95,8 @@ namespace nil {
* @param &power is the exponent.
* @return exponentiated polynomial (input^power).
*/
template<typename FieldRange, typename IntegerType>
template<typename FieldRange, typename IntegerType,
typename = typename std::enable_if<nil::crypto3::detail::is_range<FieldRange>::value>::type>
FieldRange power(const FieldRange &input, IntegerType power) {
typedef
typename std::iterator_traits<decltype(std::begin(
Expand Down Expand Up @@ -321,7 +323,7 @@ namespace nil {
[&c](const value_type &value) { return value * c; });
// We will always have no reminder here.
r.resize(1);
r[0] = 0;
r[0] = 0u;
}
// Special case when B = X^N + C.
else if (b.back() == value_type::one() && is_zero(b.begin() + 1, b.end() - 1) && a.size() >= b.size()) {
Expand Down
28 changes: 14 additions & 14 deletions include/nil/crypto3/math/polynomial/polynomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,24 @@ namespace nil {
typedef typename container_type::reverse_iterator reverse_iterator;
typedef typename container_type::const_reverse_iterator const_reverse_iterator;

polynomial() : val(1, 0) {
polynomial() : val(1, FieldValueType::zero()) {
}

explicit polynomial(size_type n) : val(n) {
explicit polynomial(size_type n) : val(n, FieldValueType::zero()) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

explicit polynomial(size_type n, const allocator_type& a) : val(n, a) {
explicit polynomial(size_type n, const allocator_type& a) : val(n, FieldValueType::zero(), a) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

polynomial(size_type n, const value_type& x) : val(n, x) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

Expand All @@ -81,14 +81,14 @@ namespace nil {
template<typename InputIterator>
polynomial(InputIterator first, InputIterator last) : val(first, last) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

template<typename InputIterator>
polynomial(InputIterator first, InputIterator last, const allocator_type& a) : val(first, last, a) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

Expand All @@ -113,19 +113,19 @@ namespace nil {
polynomial(polynomial&& x, const allocator_type& a) : val(x.val, a) {
}

polynomial(const FieldValueType& value, std::size_t power = 0) : val(power + 1, FieldValueType(0)) {
polynomial(const FieldValueType& value, std::size_t power = 0) : val(power + 1, FieldValueType::zero()) {
this->operator[](power) = value;
}

explicit polynomial(const container_type &c) : val(c) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

explicit polynomial(container_type &&c) : val(c) {
if (val.empty()) {
val.push_back(0);
val.push_back(FieldValueType::zero());
}
}

Expand Down Expand Up @@ -363,7 +363,7 @@ namespace nil {
}

FieldValueType evaluate(const FieldValueType& value) const {
FieldValueType result = 0;
FieldValueType result = FieldValueType::zero();
auto end = this->end();
while (end != this->begin()) {
result = result * value + *--end;
Expand All @@ -376,7 +376,7 @@ namespace nil {
*/
bool is_zero() const {
return std::all_of(this->begin(), this->end(),
[](FieldValueType i) { return i == FieldValueType(0); });
[](FieldValueType i) { return i == FieldValueType::zero(); });
}

/**
Expand All @@ -385,7 +385,7 @@ namespace nil {
bool is_one() const {
return (*this->begin() == FieldValueType(1)) &&
std::all_of(++this->begin(), this->end(),
[](FieldValueType i) { return i == FieldValueType(0); });
[](FieldValueType i) { return i == FieldValueType::zero(); });
}

inline static polynomial zero() {
Expand Down
9 changes: 4 additions & 5 deletions include/nil/crypto3/math/polynomial/polynomial_dfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ namespace nil {
typedef typename container_type::const_reverse_iterator const_reverse_iterator;

// Default constructor creates a zero polynomial of degree 0 and size 1.
polynomial_dfs() : val(1, 0) {
_d = 0;
polynomial_dfs() : val(1, FieldValueType::zero()), _d(0) {
}

explicit polynomial_dfs(size_t d, size_type n) : val(n), _d(d) {
Expand Down Expand Up @@ -111,7 +110,7 @@ namespace nil {
polynomial_dfs(const polynomial_dfs& x, const allocator_type& a) : val(x.val, a), _d(x._d) {
}

polynomial_dfs(size_t d, std::initializer_list<value_type> il) : val(il), _d(d) {
polynomial_dfs(std::size_t d, std::initializer_list<value_type> il) : val(il), _d(d) {
}

polynomial_dfs(size_t d, std::initializer_list<value_type> il, const allocator_type& a) :
Expand Down Expand Up @@ -364,7 +363,7 @@ namespace nil {

FieldValueType evaluate(const FieldValueType& value) const {
std::vector<FieldValueType> tmp = this->coefficients();
FieldValueType result = 0;
FieldValueType result = FieldValueType::zero();
auto end = tmp.end();
while (end != tmp.begin()) {
result = result * value + *--end;
Expand Down Expand Up @@ -399,7 +398,7 @@ namespace nil {
}

inline static polynomial_dfs one() {
return polynomial_dfs(0, size_type(1), value_type(1));
return polynomial_dfs(0, size_type(1), value_type::one());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ namespace nil {
tmp.begin(),
std::bind(std::multiplies<value_type>(), sconst, std::placeholders::_1));
size_t r_size = tmp.size();
while (r_size > 0 && tmp[r_size - 1] == FieldValueType(0)) {
while (r_size > 0 && tmp[r_size - 1] == FieldValueType::zero()) {
--r_size;
}
tmp.resize(r_size);
Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/math/polynomial/polynomial_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ namespace nil {
*/
bool is_zero() const {
return std::all_of(this->begin(), this->end(),
[](FieldValueType i) { return i == FieldValueType(0); });
[](FieldValueType i) { return i == FieldValueType::zero(); });
}

/**
Expand Down
24 changes: 14 additions & 10 deletions test/evaluation_domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void test_fft() {
typedef typename FieldType::value_type value_type;

const std::size_t m = 4;
std::vector<value_type> f = {2, 5, 3, 8};
std::vector<value_type> f = {2u, 5u, 3u, 8u};

std::shared_ptr<evaluation_domain<FieldType>> domain = make_evaluation_domain<FieldType>(m);

Expand Down Expand Up @@ -139,7 +139,7 @@ template<typename FieldType>
void test_inverse_fft_of_fft() {
typedef typename FieldType::value_type value_type;
const std::size_t m = 4;
std::vector<value_type> f = {2, 5, 3, 8};
std::vector<value_type> f = {2u, 5u, 3u, 8u};

std::shared_ptr<evaluation_domain<FieldType>> domain = make_evaluation_domain<FieldType>(m);

Expand All @@ -158,7 +158,7 @@ template<typename FieldType>
void test_inverse_coset_ftt_of_coset_fft() {
typedef typename FieldType::value_type value_type;
const std::size_t m = 4;
std::vector<value_type> f = {2, 5, 3, 8};
std::vector<value_type> f = {2u, 5u, 3u, 8u};

value_type coset = value_type(fields::arithmetic_params<FieldType>::multiplicative_generator);

Expand All @@ -182,7 +182,7 @@ void test_lagrange_coefficients() {
typedef typename FieldType::value_type value_type;

const std::size_t m = 8;
value_type t = value_type(10);
value_type t = value_type(10u);

std::shared_ptr<evaluation_domain<FieldType>> domain;

Expand Down Expand Up @@ -210,7 +210,7 @@ void test_compute_z() {
typedef typename FieldType::value_type value_type;

const std::size_t m = 8;
value_type t = value_type(10);
value_type t = value_type(10u);

std::shared_ptr<evaluation_domain<FieldType>> domain;
domain = make_evaluation_domain<FieldType>(m);
Expand Down Expand Up @@ -238,7 +238,9 @@ void test_fft_curve_elements() {
// Make sure the results are reproducible.
std::srand(0);
std::vector<field_value_type> f(m);
std::generate(f.begin(), f.end(), std::rand);
for(std::size_t i = 0; i < m; ++i) {
f[i] = unsigned(std::rand());
}
std::vector<value_type> g(m);
for(std::size_t i = 0; i < m; ++i) {
g[i] = value_type::one() * f[i];
Expand Down Expand Up @@ -274,7 +276,9 @@ void test_inverse_fft_curve_elements() {
// Make sure the results are reproducible.
std::srand(0);
std::vector<field_value_type> f(m);
std::generate(f.begin(), f.end(), std::rand);
for(std::size_t i = 0; i < m; ++i) {
f[i] = unsigned(std::rand());
}
std::vector<value_type> g(m);
for(std::size_t i = 0; i < m; ++i) {
g[i] = value_type::one() * f[i];
Expand Down Expand Up @@ -306,7 +310,7 @@ void test_lagrange_coefficients_from_powers(std::size_t m) {

// Make sure the results are reproducible.
std::srand(0);
field_value_type t = std::rand();
field_value_type t = unsigned(std::rand());
std::vector<field_value_type> t_powers(m);
t_powers[0] = field_value_type::one();
for(std::size_t i = 1; i < m; ++i) {
Expand Down Expand Up @@ -336,7 +340,7 @@ void test_lagrange_coefficients_curve_elements(std::size_t m) {

// Make sure the results are reproducible.
std::srand(0);
field_value_type t = std::rand();
field_value_type t = unsigned(std::rand());
std::vector<value_type> t_powers(m);
t_powers[0] = value_type::one();
for(std::size_t i = 1; i < m; ++i) {
Expand Down Expand Up @@ -367,7 +371,7 @@ void test_get_vanishing_polynomial(std::size_t m) {

// Make sure the results are reproducible.
std::srand(0);
field_value_type t = std::rand();
field_value_type t = unsigned(std::rand());

std::shared_ptr<evaluation_domain<FieldType>> domain;

Expand Down
Loading

0 comments on commit 00730e2

Please sign in to comment.