diff options
author | Jack Lloyd <[email protected]> | 2018-02-26 07:56:20 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-26 07:56:20 -0500 |
commit | ac1d24cf06de5e800cbb8a3c7ab392c081aeb783 (patch) | |
tree | 43929f0ffc1926af9dd40ed5ee493bc77d4893c2 /src | |
parent | e479bae1d4b66e0984ce7791370e95aa69c4e3f6 (diff) |
Add BigInt::operator*= taking a word
Avoids memory allocation when multiplying by a small constant.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/math/bigint/big_ops2.cpp | 19 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 6 |
2 files changed, 23 insertions, 2 deletions
diff --git a/src/lib/math/bigint/big_ops2.cpp b/src/lib/math/bigint/big_ops2.cpp index 2f81989c3..7653884c9 100644 --- a/src/lib/math/bigint/big_ops2.cpp +++ b/src/lib/math/bigint/big_ops2.cpp @@ -107,12 +107,12 @@ BigInt& BigInt::operator*=(const BigInt& y) } else if(x_sw == 1 && y_sw) { - grow_to(y_sw + 2); + grow_to(y_sw + 1); bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0)); } else if(y_sw == 1 && x_sw) { - grow_to(x_sw + 2); + grow_to(x_sw + 1); bigint_linmul2(mutable_data(), x_sw, y.word_at(0)); } else @@ -125,6 +125,21 @@ BigInt& BigInt::operator*=(const BigInt& y) return (*this); } +BigInt& BigInt::operator*=(word y) + { + if(y == 0) + { + clear(); + set_sign(Positive); + } + + const size_t x_sw = sig_words(); + grow_to(x_sw + 1); + bigint_linmul2(mutable_data(), x_sw, y); + + return (*this); + } + /* * Division Operator */ diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index 71629b3aa..2c1d5e905 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -154,6 +154,12 @@ class BOTAN_PUBLIC_API(2,0) BigInt final BigInt& operator*=(const BigInt& y); /** + * *= operator + * @param y the word to multiply with this + */ + BigInt& operator*=(word y); + + /** * /= operator * @param y the BigInt to divide this by */ |