aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-04-08 19:21:35 -0400
committerJack Lloyd <[email protected]>2018-04-08 19:33:34 -0400
commit0ca00cd6184a9c9e4d6a0dfc8a2488746ee22f23 (patch)
tree5374c6eca54779a3855eac05b7f909c58b649c4d /src/lib/math
parentc921a1bff2f267dd94f7e4aa8f30341e83d8d52f (diff)
Add BigInt::square plus a speed test for BigInt multiply
Diffstat (limited to 'src/lib/math')
-rw-r--r--src/lib/math/bigint/big_ops2.cpp16
-rw-r--r--src/lib/math/bigint/bigint.h6
-rw-r--r--src/lib/math/numbertheory/mp_numth.cpp11
3 files changed, 25 insertions, 8 deletions
diff --git a/src/lib/math/bigint/big_ops2.cpp b/src/lib/math/bigint/big_ops2.cpp
index 9277834ba..eea6a8590 100644
--- a/src/lib/math/bigint/big_ops2.cpp
+++ b/src/lib/math/bigint/big_ops2.cpp
@@ -177,6 +177,22 @@ BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws)
return (*this);
}
+BigInt& BigInt::square(secure_vector<word>& ws)
+ {
+ const size_t sw = sig_words();
+
+ secure_vector<word> z(2*sw);
+ ws.resize(z.size());
+
+ bigint_sqr(z.data(), z.size(),
+ data(), size(), sw,
+ ws.data(), ws.size());
+
+ swap_reg(z);
+
+ return (*this);
+ }
+
BigInt& BigInt::operator*=(word y)
{
if(y == 0)
diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h
index cb518e727..44177de96 100644
--- a/src/lib/math/bigint/bigint.h
+++ b/src/lib/math/bigint/bigint.h
@@ -252,6 +252,12 @@ class BOTAN_PUBLIC_API(2,0) BigInt final
BigInt& mul(const BigInt& y, secure_vector<word>& ws);
/**
+ * Square value of *this
+ * @param ws a temp workspace
+ */
+ BigInt& square(secure_vector<word>& ws);
+
+ /**
* Set *this to y - *this
* @param y the BigInt to subtract from as a sequence of words
* @param y_size length of y in words
diff --git a/src/lib/math/numbertheory/mp_numth.cpp b/src/lib/math/numbertheory/mp_numth.cpp
index 5ad72cd47..eef641996 100644
--- a/src/lib/math/numbertheory/mp_numth.cpp
+++ b/src/lib/math/numbertheory/mp_numth.cpp
@@ -18,14 +18,9 @@ namespace Botan {
*/
BigInt square(const BigInt& x)
{
- const size_t x_sw = x.sig_words();
-
- BigInt z(BigInt::Positive, round_up(2*x_sw, 16));
- secure_vector<word> workspace(z.size());
-
- bigint_sqr(z.mutable_data(), z.size(),
- x.data(), x.size(), x_sw,
- workspace.data(), workspace.size());
+ BigInt z = x;
+ secure_vector<word> ws;
+ z.square(ws);
return z;
}