aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math/numbertheory/monty.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-05-02 20:28:02 -0400
committerJack Lloyd <[email protected]>2018-05-02 20:34:49 -0400
commit9aafe361e714a0376d5bf4ebfdaae6e0a3aaa915 (patch)
treeeb6ffda52b30fd10df93a49f5bf948064158fd86 /src/lib/math/numbertheory/monty.cpp
parent7badac9c6a32dbb0fe64fc909b7124c044ee1a1d (diff)
Remove needless allocation in Montgomery_Int::mul_by
Diffstat (limited to 'src/lib/math/numbertheory/monty.cpp')
-rw-r--r--src/lib/math/numbertheory/monty.cpp42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/lib/math/numbertheory/monty.cpp b/src/lib/math/numbertheory/monty.cpp
index 1cdc7fa9a..b33fdf34c 100644
--- a/src/lib/math/numbertheory/monty.cpp
+++ b/src/lib/math/numbertheory/monty.cpp
@@ -136,6 +136,32 @@ void Montgomery_Params::mul_by(BigInt& x,
copy_mem(x.mutable_data(), z_data, output_size);
}
+void Montgomery_Params::mul_by(BigInt& x,
+ const BigInt& y,
+ secure_vector<word>& ws) const
+ {
+ const size_t output_size = 2*m_p_words + 2;
+
+ if(ws.size() < 2*output_size)
+ ws.resize(2*output_size);
+
+ word* z_data = &ws[0];
+ word* ws_data = &ws[output_size];
+
+ bigint_mul(z_data, output_size,
+ x.data(), x.size(), x.sig_words(),
+ y.data(), y.size(), y.sig_words(),
+ ws_data, output_size);
+
+ bigint_monty_redc(z_data,
+ m_p.data(), m_p_words, m_p_dash,
+ ws_data, output_size);
+
+ if(x.size() < output_size)
+ x.grow_to(output_size);
+ copy_mem(x.mutable_data(), z_data, output_size);
+ }
+
BigInt Montgomery_Params::sqr(const BigInt& x, secure_vector<word>& ws) const
{
const size_t output_size = 2*m_p_words + 2;
@@ -287,16 +313,19 @@ Montgomery_Int& Montgomery_Int::operator+=(const Montgomery_Int& other)
Montgomery_Int& Montgomery_Int::add(const Montgomery_Int& other, secure_vector<word>& ws)
{
- m_v += other.m_v;
- m_v.reduce_below(m_params->p(), ws);
+ m_v.mod_add(other.m_v, m_params->p(), ws);
return (*this);
}
Montgomery_Int& Montgomery_Int::operator-=(const Montgomery_Int& other)
{
- m_v -= other.m_v;
- if(m_v.is_negative())
- m_v += m_params->p();
+ secure_vector<word> ws;
+ return this->sub(other, ws);
+ }
+
+Montgomery_Int& Montgomery_Int::sub(const Montgomery_Int& other, secure_vector<word>& ws)
+ {
+ m_v.mod_sub(other.m_v, m_params->p(), ws);
return (*this);
}
@@ -315,7 +344,7 @@ Montgomery_Int Montgomery_Int::mul(const Montgomery_Int& other,
Montgomery_Int& Montgomery_Int::mul_by(const Montgomery_Int& other,
secure_vector<word>& ws)
{
- m_v = m_params->mul(m_v, other.m_v, ws);
+ m_params->mul_by(m_v, other.m_v, ws);
return (*this);
}
@@ -323,7 +352,6 @@ Montgomery_Int& Montgomery_Int::mul_by(const secure_vector<word>& other,
secure_vector<word>& ws)
{
m_params->mul_by(m_v, other, ws);
- //m_v = m_params->mul(m_v, other, ws);
return (*this);
}