diff options
author | Jack Lloyd <[email protected]> | 2018-06-14 21:20:31 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-06-14 21:20:31 -0400 |
commit | 48f44e423701bcaa5dbdf825c825e3bf53edfaa2 (patch) | |
tree | 0c74258ef5063066a4abaca1ccbcfe8f497d947c /src | |
parent | bee4746c1107876583f152295f34a03cc6f6d025 (diff) |
In Montgomery mul, avoid branching based on sig words of integers
Instead just assume they are the same size as the prime
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/math/numbertheory/monty.cpp | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/lib/math/numbertheory/monty.cpp b/src/lib/math/numbertheory/monty.cpp index b33fdf34c..0560cc59e 100644 --- a/src/lib/math/numbertheory/monty.cpp +++ b/src/lib/math/numbertheory/monty.cpp @@ -76,10 +76,13 @@ BigInt Montgomery_Params::mul(const BigInt& x, const BigInt& y, if(ws.size() < output_size) ws.resize(output_size); + BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); + BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words); + BigInt z(BigInt::Positive, output_size); bigint_mul(z.mutable_data(), z.size(), - x.data(), x.size(), x.sig_words(), - y.data(), y.size(), y.sig_words(), + x.data(), x.size(), std::min(m_p_words, x.size()), + y.data(), y.size(), std::min(m_p_words, y.size()), ws.data(), ws.size()); bigint_monty_redc(z.mutable_data(), @@ -98,9 +101,11 @@ BigInt Montgomery_Params::mul(const BigInt& x, ws.resize(output_size); BigInt z(BigInt::Positive, output_size); + BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); + bigint_mul(z.mutable_data(), z.size(), - x.data(), x.size(), x.sig_words(), - y.data(), y.size(), y.size(), + x.data(), x.size(), std::min(m_p_words, x.size()), + y.data(), y.size(), std::min(m_p_words, y.size()), ws.data(), ws.size()); bigint_monty_redc(z.mutable_data(), @@ -122,9 +127,11 @@ void Montgomery_Params::mul_by(BigInt& x, word* z_data = &ws[0]; word* ws_data = &ws[output_size]; + BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); + bigint_mul(z_data, output_size, - x.data(), x.size(), x.sig_words(), - y.data(), y.size(), y.size(), + x.data(), x.size(), std::min(m_p_words, x.size()), + y.data(), y.size(), std::min(m_p_words, y.size()), ws_data, output_size); bigint_monty_redc(z_data, @@ -148,9 +155,11 @@ void Montgomery_Params::mul_by(BigInt& x, word* z_data = &ws[0]; word* ws_data = &ws[output_size]; + BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); + bigint_mul(z_data, output_size, - x.data(), x.size(), x.sig_words(), - y.data(), y.size(), y.sig_words(), + x.data(), x.size(), std::min(m_p_words, x.size()), + y.data(), y.size(), std::min(m_p_words, y.size()), ws_data, output_size); bigint_monty_redc(z_data, @@ -171,13 +180,10 @@ BigInt Montgomery_Params::sqr(const BigInt& x, secure_vector<word>& ws) const BigInt z(BigInt::Positive, output_size); - // assume x.sig_words() is at most p_words BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); - const size_t x_words = (x.size() >= m_p_words) ? m_p_words : x.sig_words(); - bigint_sqr(z.mutable_data(), z.size(), - x.data(), x.size(), x_words, + x.data(), x.size(), std::min(m_p_words, x.size()), ws.data(), ws.size()); bigint_monty_redc(z.mutable_data(), @@ -198,8 +204,10 @@ void Montgomery_Params::square_this(BigInt& x, word* z_data = &ws[0]; word* ws_data = &ws[output_size]; + BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words); + bigint_sqr(z_data, output_size, - x.data(), x.size(), x.sig_words(), + x.data(), x.size(), std::min(m_p_words, x.size()), ws_data, output_size); bigint_monty_redc(z_data, |