From 0ca00cd6184a9c9e4d6a0dfc8a2488746ee22f23 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sun, 8 Apr 2018 19:21:35 -0400 Subject: Add BigInt::square plus a speed test for BigInt multiply --- src/cli/speed.cpp | 47 ++++++++++++++++++++++++++++++++++ src/lib/math/bigint/big_ops2.cpp | 16 ++++++++++++ src/lib/math/bigint/bigint.h | 6 +++++ src/lib/math/numbertheory/mp_numth.cpp | 11 +++----- 4 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp index afe75decc..b8397363f 100644 --- a/src/cli/speed.cpp +++ b/src/cli/speed.cpp @@ -23,6 +23,10 @@ #include #include +#if defined(BOTAN_HAS_BIGINT) + #include +#endif + #if defined(BOTAN_HAS_BLOCK_CIPHER) #include #endif @@ -895,6 +899,13 @@ class Speed final : public Command } #endif +#if defined(BOTAN_HAS_BIGINT) + else if(algo == "mp_mul") + { + bench_mp_mul(msec); + } +#endif + #if defined(BOTAN_HAS_NUMBERTHEORY) else if(algo == "random_prime") { @@ -1428,6 +1439,42 @@ class Speed final : public Command } #endif +#if defined(BOTAN_HAS_BIGINT) + + void bench_mp_mul(const std::chrono::milliseconds runtime) + { + std::chrono::milliseconds runtime_per_size = runtime / 9; + for(size_t bits : { 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096 }) + { + std::unique_ptr mul_timer = make_timer("BigInt mul " + std::to_string(bits)); + std::unique_ptr sqr_timer = make_timer("BigInt sqr " + std::to_string(bits)); + + const Botan::BigInt y(rng(), bits); + Botan::secure_vector ws; + + while(mul_timer->under(runtime_per_size)) + { + Botan::BigInt x(rng(), bits); + + sqr_timer->start(); + x.square(ws); + sqr_timer->stop(); + + x.mask_bits(bits); + + mul_timer->start(); + x.mul(y, ws); + mul_timer->stop(); + } + + record_result(mul_timer); + record_result(sqr_timer); + } + + } + +#endif + #if defined(BOTAN_HAS_DL_GROUP) void bench_modexp(const std::chrono::milliseconds runtime) 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& ws) return (*this); } +BigInt& BigInt::square(secure_vector& ws) + { + const size_t sw = sig_words(); + + secure_vector 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 @@ -251,6 +251,12 @@ class BOTAN_PUBLIC_API(2,0) BigInt final */ BigInt& mul(const BigInt& y, secure_vector& ws); + /** + * Square value of *this + * @param ws a temp workspace + */ + BigInt& square(secure_vector& ws); + /** * Set *this to y - *this * @param y the BigInt to subtract from as a sequence of 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 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 ws; + z.square(ws); return z; } -- cgit v1.2.3