aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math/bigint
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-06-17 15:15:17 -0400
committerJack Lloyd <[email protected]>2018-06-17 15:15:17 -0400
commitcb9a5724aed5293359d3d5b35447d50875756b06 (patch)
treec4534f2d4114ffb7a53e095e4d485227659d5197 /src/lib/math/bigint
parent90fd2927c301805aa9ca6c18ed69a8ed6f099d89 (diff)
Avoid unnecessary realloc in BigInt::mod_sub
Diffstat (limited to 'src/lib/math/bigint')
-rw-r--r--src/lib/math/bigint/big_ops2.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/lib/math/bigint/big_ops2.cpp b/src/lib/math/bigint/big_ops2.cpp
index 39f985566..bd107f33a 100644
--- a/src/lib/math/bigint/big_ops2.cpp
+++ b/src/lib/math/bigint/big_ops2.cpp
@@ -133,6 +133,9 @@ BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>&
if(t_sw > mod_sw || s_sw > mod_sw)
throw Invalid_Argument("BigInt::mod_sub args larger than modulus");
+ BOTAN_DEBUG_ASSERT(*this < mod);
+ BOTAN_DEBUG_ASSERT(s < mod);
+
int32_t relative_size = bigint_cmp(data(), t_sw, s.data(), s_sw);
if(relative_size >= 0)
@@ -144,9 +147,11 @@ BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>&
{
// Otherwise we must sub s and then add p (or add (p - s) as here)
- ws.resize(mod_sw + 1);
+ if(ws.size() < mod_sw)
+ ws.resize(mod_sw);
- bigint_sub3(ws.data(), mod.data(), mod_sw, s.data(), s_sw);
+ word borrow = bigint_sub3(ws.data(), mod.data(), mod_sw, s.data(), s_sw);
+ BOTAN_ASSERT_NOMSG(borrow == 0);
if(m_reg.size() < mod_sw)
grow_to(mod_sw);