Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 19e3323

Browse files
committed
Cleaned up a bit.
1 parent f6da84f commit 19e3323

File tree

4 files changed

+14
-163
lines changed

4 files changed

+14
-163
lines changed

libs/multiprecision/include/nil/crypto3/multiprecision/cpp_int_modular/add_unsigned.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ namespace boost {
6060
else {
6161
limb_type mask = cpp_int_modular_backend<Bits>::upper_limb_mask;
6262
// If we have set any bit above "Bits", then we have a carry.
63-
if (pr[result.size() - 1] & ~mask) {
64-
pr[result.size() - 1] &= mask;
63+
if (result.limbs()[s - 1] & ~mask) {
64+
result.limbs()[s - 1] &= mask;
6565
result.set_carry(true);
6666
}
6767
}
@@ -188,8 +188,8 @@ namespace boost {
188188
else {
189189
limb_type mask = cpp_int_modular_backend<Bits>::upper_limb_mask;
190190
// If we have set any bit above "Bits", then we have a carry.
191-
if (pr[result.size() - 1] & ~mask) {
192-
pr[result.size() - 1] &= mask;
191+
if (result.limbs()[s - 1] & ~mask) {
192+
result.limbs()[s - 1] &= mask;
193193
result.set_carry(true);
194194
}
195195
}

libs/multiprecision/include/nil/crypto3/multiprecision/modular/modular_functions_fixed.hpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ namespace boost {
194194
}
195195

196196
BOOST_MP_CXX14_CONSTEXPR void initialize_montgomery_params() {
197-
if (check_montgomery_constraints(m_mod)) {
198-
find_const_variables();
199-
}
197+
find_const_variables();
200198
}
201199

202200
/*
@@ -225,16 +223,18 @@ namespace boost {
225223
}
226224

227225
BOOST_MP_CXX14_CONSTEXPR void find_const_variables() {
228-
m_montgomery_p_dash = monty_inverse(m_mod.limbs()[0]);
226+
if (check_montgomery_constraints(m_mod)) {
227+
m_montgomery_p_dash = monty_inverse(m_mod.limbs()[0]);
229228

230-
Backend_doubled_padded_limbs r;
231-
eval_bit_set(r, 2 * m_mod.size() * limb_bits);
232-
barrett_reduce(r);
229+
Backend_doubled_padded_limbs r;
230+
eval_bit_set(r, 2 * m_mod.size() * limb_bits);
231+
barrett_reduce(r);
233232

234-
// Here we are intentionally throwing away half of the bits of r, it's correct.
235-
m_montgomery_r2 = static_cast<Backend>(r);
233+
// Here we are intentionally throwing away half of the bits of r, it's correct.
234+
m_montgomery_r2 = static_cast<Backend>(r);
235+
}
236236

237-
// Compute 2^Bits - Modulus.
237+
// Compute 2^Bits - Modulus, no matter if modulus is even or odd.
238238
Backend_padded_limbs compliment = static_cast<limb_type>(1u), modulus = m_mod;
239239
eval_left_shift(compliment, Bits);
240240
eval_subtract(compliment, modulus);

libs/multiprecision/include/nil/crypto3/multiprecision/modular/modular_params.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ namespace boost {
5959
}
6060
}
6161

62-
// Martun: This needs to be called when setting numbers to 0, which takes lots of time when creating
63-
// empty vectors.
64-
6562
/* Conversion from the regular number A into Montgomery form r*A:
6663
Montgomery_reduce((A mod N)*(r^2 mod N)) = Montgomery_reduce(A*r^2 mod N) = A*r mod N,
6764
where result is A and get_mod() is N.

libs/multiprecision/include/nil/crypto3/multiprecision/modular/montgomery_params.hpp

-146
Original file line numberDiff line numberDiff line change
@@ -35,161 +35,15 @@ namespace boost {
3535
*/
3636
template<typename Backend>
3737
class montgomery_params : virtual public base_params<Backend> {
38-
protected:
39-
template<typename Number>
40-
inline void initialize_montgomery_params(const Number& p) {
41-
this->initialize_base_params(p);
42-
find_const_variables(p);
43-
}
44-
45-
inline void initialize_montgomery_params(const montgomery_params<Backend>& p) {
46-
this->initialize_base_params(p);
47-
find_const_variables(p);
48-
}
49-
50-
/*
51-
* Compute -input^-1 mod 2^limb_bits. Throws an exception if input
52-
* is even. If input is odd, then input and 2^n are relatively prime
53-
* and an inverse exists.
54-
*/
55-
limb_type monty_inverse(limb_type a) {
56-
assert(a % 2 == 1);
57-
58-
limb_type b = 1;
59-
limb_type r = 0;
60-
61-
for (size_t i = 0; i != sizeof(limb_type) * CHAR_BIT; ++i) {
62-
const limb_type bi = b % 2;
63-
r >>= 1;
64-
r += bi << (sizeof(limb_type) * CHAR_BIT - 1);
65-
66-
b -= a * bi;
67-
b >>= 1;
68-
}
69-
70-
// Now invert in addition space
71-
r = (~static_cast<limb_type>(0) - r) + 1;
72-
73-
return r;
74-
}
75-
76-
template<typename T>
77-
void find_const_variables(const T& pp) {
78-
Backend p = pp;
79-
if (p <= 0 || !(p % 2)) {
80-
return;
81-
}
82-
83-
m_p_words = this->m_mod.size();
84-
85-
m_p_dash = monty_inverse(this->m_mod.limbs()[0]);
86-
87-
Backend r;
88-
89-
boost::multiprecision::default_ops::eval_bit_set(r, m_p_words * sizeof(limb_type) * CHAR_BIT);
90-
91-
m_r2 = r * r;
92-
barrett_params<Backend> barrettParams(this->m_mod);
93-
barrettParams.barrett_reduce(m_r2);
94-
}
95-
9638
public:
9739
montgomery_params() : base_params<Backend>() {
9840
}
9941

10042
template<typename Number>
10143
explicit montgomery_params(const Number& p) : base_params<Backend>(p) {
102-
initialize_montgomery_params(p);
103-
}
104-
105-
inline const Backend& r2() const {
106-
return m_r2;
107-
}
108-
109-
inline limb_type p_dash() const {
110-
return m_p_dash;
111-
}
112-
113-
inline size_t p_words() const {
114-
return m_p_words;
115-
}
116-
117-
template<class V>
118-
montgomery_params& operator=(const V& v) {
119-
initialize_montgomery_params(v);
120-
return *this;
121-
}
122-
123-
inline void montgomery_reduce(Backend& result) const {
124-
using boost::multiprecision::default_ops::eval_multiply_add;
125-
using boost::multiprecision::default_ops::eval_right_shift;
126-
using boost::multiprecision::default_ops::eval_add;
127-
128-
typedef cpp_int_modular_backend<sizeof(limb_type) * CHAR_BIT * 3>
129-
cpp_three_int_backend;
130-
131-
const size_t p_size = m_p_words;
132-
const limb_type p_dash = m_p_dash;
133-
const size_t z_size = 2 * (p_words() + 1);
134-
135-
boost::container::vector<limb_type> z(
136-
result.size(), 0); // container::vector<limb_type, alloc> z(result.size(), 0);
137-
for (size_t i = 0; i < result.size(); ++i) {
138-
z[i] = result.limbs()[i];
139-
}
140-
141-
if (result.size() < z_size) {
142-
result.resize(z_size, z_size);
143-
z.resize(z_size, 0);
144-
}
145-
146-
cpp_three_int_backend w(z[0]);
147-
148-
result.limbs()[0] = w.limbs()[0] * p_dash;
149-
150-
eval_multiply_add(w, result.limbs()[0], this->m_mod.limbs()[0]);
151-
eval_right_shift(w, sizeof(limb_type) * CHAR_BIT);
152-
153-
for (size_t i = 1; i != p_size; ++i) {
154-
for (size_t j = 0; j < i; ++j) {
155-
eval_multiply_add(w, result.limbs()[j], this->m_mod.limbs()[i - j]);
156-
}
157-
158-
eval_add(w, z[i]);
159-
160-
result.limbs()[i] = w.limbs()[0] * p_dash;
161-
162-
eval_multiply_add(w, result.limbs()[i], this->m_mod.limbs()[0]);
163-
164-
eval_right_shift(w, sizeof(limb_type) * CHAR_BIT);
165-
}
166-
167-
for (size_t i = 0; i != p_size; ++i) {
168-
for (size_t j = i + 1; j != p_size; ++j) {
169-
eval_multiply_add(w, result.limbs()[j], this->m_mod.limbs()[p_size + i - j]);
170-
}
171-
172-
eval_add(w, z[p_size + i]);
173-
174-
result.limbs()[i] = w.limbs()[0];
175-
176-
eval_right_shift(w, sizeof(limb_type) * CHAR_BIT);
177-
}
178-
179-
eval_add(w, z[z_size - 1]);
180-
181-
result.limbs()[p_size] = w.limbs()[0];
182-
result.limbs()[p_size + 1] = w.limbs()[1];
183-
184-
// TODO(mart we cannot resize any more.
185-
if (result.size() != p_size + 1) {
186-
result.resize(p_size + 1, p_size + 1);
187-
}
18844
}
18945

19046
protected:
191-
Backend m_r2;
192-
limb_type m_p_dash;
19347
size_t m_p_words;
19448

19549
};

0 commit comments

Comments
 (0)