diff options
author | Jack Lloyd <[email protected]> | 2018-02-26 15:04:39 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-26 15:04:39 -0500 |
commit | 3b84e568bd591a9a76d8d3778d90a8d761c1698b (patch) | |
tree | 87580ffefc5c34d7b831ac84eab4acc88f764b30 /src | |
parent | 50c69e760b0f47e84f5a3c8d2bea6f072f3fd587 (diff) |
Avoid some needless allocations
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/math/bigint/big_ops2.cpp | 38 | ||||
-rw-r--r-- | src/lib/pubkey/ec_group/point_gfp.cpp | 6 |
2 files changed, 31 insertions, 13 deletions
diff --git a/src/lib/math/bigint/big_ops2.cpp b/src/lib/math/bigint/big_ops2.cpp index fc6135c22..97d2aadfe 100644 --- a/src/lib/math/bigint/big_ops2.cpp +++ b/src/lib/math/bigint/big_ops2.cpp @@ -1,6 +1,5 @@ /* -* BigInt Assignment Operators -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2007,2018 Jack Lloyd * 2016 Matthias Gierlings * * Botan is released under the Simplified BSD License (see license.txt) @@ -20,21 +19,25 @@ BigInt& BigInt::operator+=(const BigInt& y) { const size_t x_sw = sig_words(), y_sw = y.sig_words(); - const size_t reg_size = std::max(x_sw, y_sw) + 1; + if(sign() == y.sign()) + { + const size_t reg_size = std::max(x_sw, y_sw) + 1; - if(m_reg.size() < reg_size) - grow_to(reg_size); + if(m_reg.size() < reg_size) + grow_to(reg_size); - if(sign() == y.sign()) bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw); + } else { - int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); + const int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); if(relative_size < 0) { - secure_vector<word> z(reg_size - 1); - bigint_sub3(z.data(), y.data(), reg_size - 1, data(), x_sw); + const size_t reg_size = std::max(x_sw, y_sw); + + secure_vector<word> z(reg_size); + bigint_sub3(z.data(), y.data(), reg_size, data(), x_sw); std::swap(m_reg, z); set_sign(y.sign()); } @@ -44,7 +47,9 @@ BigInt& BigInt::operator+=(const BigInt& y) set_sign(Positive); } else if(relative_size > 0) + { bigint_sub2(mutable_data(), x_sw, y.data(), y_sw); + } } return (*this); @@ -183,7 +188,9 @@ BigInt& BigInt::operator*=(word y) } const size_t x_sw = sig_words(); - grow_to(x_sw + 1); + + if(size() < x_sw + 1) + grow_to(x_sw + 1); bigint_linmul2(mutable_data(), x_sw, y); return (*this); @@ -254,7 +261,16 @@ BigInt& BigInt::operator<<=(size_t shift) shift_bits = shift % MP_WORD_BITS, words = sig_words(); - grow_to(words + shift_words + (shift_bits ? 1 : 0)); + /* + * FIXME - if shift_words == 0 && the top shift_bits of the top word + * are zero then we know that no additional word is needed and can + * skip the allocation. + */ + const size_t needed_size = words + shift_words + (shift_bits ? 1 : 0); + + if(m_reg.size() < needed_size) + grow_to(needed_size); + bigint_shl1(mutable_data(), words, shift_words, shift_bits); } diff --git a/src/lib/pubkey/ec_group/point_gfp.cpp b/src/lib/pubkey/ec_group/point_gfp.cpp index f054c51ff..12a26f50c 100644 --- a/src/lib/pubkey/ec_group/point_gfp.cpp +++ b/src/lib/pubkey/ec_group/point_gfp.cpp @@ -147,7 +147,8 @@ void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn) m_curve.sqr(m_coord_x, r, monty_ws); m_coord_x -= S2; - m_coord_x -= (U2 << 1); + m_coord_x -= U2; + m_coord_x -= U2; while(m_coord_x.is_negative()) m_coord_x += p; @@ -215,7 +216,8 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) M.reduce_below(p, monty_ws); m_curve.sqr(x, M, monty_ws); - x -= (S << 1); + x -= S; + x -= S; while(x.is_negative()) x += p; |