From ac1d24cf06de5e800cbb8a3c7ab392c081aeb783 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Mon, 26 Feb 2018 07:56:20 -0500 Subject: Add BigInt::operator*= taking a word Avoids memory allocation when multiplying by a small constant. --- src/lib/math/bigint/big_ops2.cpp | 19 +++++++++++++++++-- src/lib/math/bigint/bigint.h | 6 ++++++ 2 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src') 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 @@ -153,6 +153,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 -- cgit v1.2.3