Skip to content

Commit

Permalink
Some changes related to recent changes in multiprecision.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed May 11, 2024
1 parent 933c313 commit be56806
Show file tree
Hide file tree
Showing 8 changed files with 931 additions and 932 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
2 changes: 1 addition & 1 deletion include/nil/crypto3/math/polynomial/basic_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,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
7 changes: 3 additions & 4 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 @@ -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
Loading

0 comments on commit be56806

Please sign in to comment.