diff options
author | Jack Lloyd <[email protected]> | 2018-02-26 17:26:40 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-26 17:26:40 -0500 |
commit | a89255d933d02bb388f9a9fa1093b189f389732d (patch) | |
tree | c523d68e41698710f2e82e04a10612fe5145cdd5 /src/lib/math/bigint | |
parent | 72b12e25bfdacc2e9553f64b3d87a48cb46bd682 (diff) |
Optimize P-256 and P-384 reduction
Precompute the multiples of the prime and then subtract directly.
Diffstat (limited to 'src/lib/math/bigint')
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 12 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 6 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index e99ddb50a..ec0df8f2d 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -291,7 +291,12 @@ BigInt BigInt::abs() const void BigInt::grow_to(size_t n) { if(n > size()) - m_reg.resize(round_up(n, 8)); + { + if(n <= m_reg.capacity()) + m_reg.resize(m_reg.capacity()); + else + m_reg.resize(round_up(n, 8)); + } } /* @@ -325,9 +330,10 @@ void BigInt::binary_decode(const uint8_t buf[], size_t length) m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i]; } -void BigInt::shrink_to_fit() +void BigInt::shrink_to_fit(size_t min_size) { - m_reg.resize(sig_words()); + const size_t words = std::max(min_size, sig_words()); + m_reg.resize(words); } void BigInt::const_time_lookup(secure_vector<word>& output, diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index ca35bd07d..6e2f8dbde 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -480,7 +480,11 @@ class BOTAN_PUBLIC_API(2,0) BigInt final */ void grow_to(size_t n); - void shrink_to_fit(); + /** + * Resize the vector to the minimum word size to hold the integer, or + * min_size words, whichever is larger + */ + void shrink_to_fit(size_t min_size = 0); /** * Fill BigInt with a random number with size of bitsize |