diff options
author | Simon Warta <[email protected]> | 2015-07-22 00:15:55 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-07-24 10:33:40 +0200 |
commit | e5e8635ab5e21991e890b229c9c563ffeec2db31 (patch) | |
tree | 3f8e34c177b46ac95706958f9d84dfa8cdcd1783 | |
parent | 10883bb5b8fb2804b9af08c7cfe9f869be811d0b (diff) |
Refactor BigInt
-rw-r--r-- | src/lib/math/bigint/big_rand.cpp | 8 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 11 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 10 | ||||
-rw-r--r-- | src/tests/catchy/test_bigint.cpp | 74 |
4 files changed, 94 insertions, 9 deletions
diff --git a/src/lib/math/bigint/big_rand.cpp b/src/lib/math/bigint/big_rand.cpp index ab66c6cdd..074c368e0 100644 --- a/src/lib/math/bigint/big_rand.cpp +++ b/src/lib/math/bigint/big_rand.cpp @@ -7,6 +7,7 @@ #include <botan/bigint.h> #include <botan/parsing.h> +#include <botan/internal/rounding.h> namespace Botan { @@ -19,15 +20,18 @@ void BigInt::randomize(RandomNumberGenerator& rng, set_sign(Positive); if(bitsize == 0) + { clear(); + } else { - secure_vector<byte> array = rng.random_vec((bitsize + 7) / 8); + secure_vector<byte> array = rng.random_vec(round_up(bitsize, 8) / 8); if(bitsize % 8) array[0] &= 0xFF >> (8 - (bitsize % 8)); array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); - binary_decode(array.data(), array.size()); + + binary_decode(array); } } diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index 045117cbd..242a7fe0b 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -173,6 +173,11 @@ void BigInt::clear_bit(size_t n) m_reg[which] &= ~mask; } +size_t BigInt::bytes() const + { + return round_up(bits(), 8) / 8; + } + /* * Count how many bits are being used */ @@ -253,6 +258,12 @@ BigInt BigInt::abs() const return x; } +void BigInt::grow_to(size_t n) + { + if(n > size()) + m_reg.resize(round_up(n, 8)); + } + /* * Encode this number into bytes */ diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index 4a37425f5..d866698b3 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -42,7 +42,7 @@ class BOTAN_DLL BigInt /** * Create empty BigInt */ - BigInt() { m_signedness = Positive; } + BigInt() {} /** * Create BigInt from 64 bit integer @@ -400,7 +400,7 @@ class BOTAN_DLL BigInt * Give byte length of the integer * @result byte length of the represented integer value */ - size_t bytes() const { return (bits() + 7) / 8; } + size_t bytes() const; /** * Get the bit length of the integer @@ -427,11 +427,7 @@ class BOTAN_DLL BigInt * Increase internal register buffer to at least n words * @param n new size of register */ - void grow_to(size_t n) - { - if(n > size()) - m_reg.resize(n + (8 - n % 8)); - } + void grow_to(size_t n); /** * Fill BigInt with a random number with size of bitsize diff --git a/src/tests/catchy/test_bigint.cpp b/src/tests/catchy/test_bigint.cpp new file mode 100644 index 000000000..5f4c960cf --- /dev/null +++ b/src/tests/catchy/test_bigint.cpp @@ -0,0 +1,74 @@ +// (C) 2015 Simon Warta (Kullo GmbH) +// Botan is released under the Simplified BSD License (see license.txt) + +#include "catch.hpp" + +#include <botan/build.h> + +#if defined(BOTAN_HAS_BIGINT) + +#include <botan/bigint.h> + +using namespace Botan; + +TEST_CASE("Bigint basics", "[bigint]") + { + SECTION("in 0-bit border") + { + BigInt a(0u); + CHECK(( a.bits() == 0 )); + CHECK(( a.bytes() == 0 )); + CHECK(( a.to_u32bit() == 0 )); + } + SECTION("above 0-bit border") + { + BigInt a(1u); + CHECK(( a.bits() == 1 )); + CHECK(( a.bytes() == 1 )); + CHECK(( a.to_u32bit() == 1 )); + } + SECTION("in 8-bit border") + { + BigInt a(255u); + CHECK(( a.bits() == 8 )); + CHECK(( a.bytes() == 1 )); + CHECK(( a.to_u32bit() == 255 )); + } + SECTION("above 8-bit border") + { + BigInt a(256u); + CHECK(( a.bits() == 9 )); + CHECK(( a.bytes() == 2 )); + CHECK(( a.to_u32bit() == 256 )); + } + SECTION("in 16-bit border") + { + BigInt a(65535u); + CHECK(( a.bits() == 16 )); + CHECK(( a.bytes() == 2 )); + CHECK(( a.to_u32bit() == 65535 )); + } + SECTION("above 16-bit border") + { + BigInt a(65536u); + CHECK(( a.bits() == 17 )); + CHECK(( a.bytes() == 3 )); + CHECK(( a.to_u32bit() == 65536 )); + } + SECTION("in 32-bit border") + { + BigInt a(4294967295u); + CHECK(( a.bits() == 32 )); + CHECK(( a.bytes() == 4 )); + CHECK(( a.to_u32bit() == 4294967295u )); + } + SECTION("above 32-bit border") + { + BigInt a(4294967296u); + CHECK(( a.bits() == 33 )); + CHECK(( a.bytes() == 5 )); + CHECK_THROWS( a.to_u32bit() ); + } + } + +#endif |