aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorSimon Warta <[email protected]>2015-07-22 00:15:55 +0200
committerSimon Warta <[email protected]>2015-07-24 10:33:40 +0200
commite5e8635ab5e21991e890b229c9c563ffeec2db31 (patch)
tree3f8e34c177b46ac95706958f9d84dfa8cdcd1783 /src/lib
parent10883bb5b8fb2804b9af08c7cfe9f869be811d0b (diff)
Refactor BigInt
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/math/bigint/big_rand.cpp8
-rw-r--r--src/lib/math/bigint/bigint.cpp11
-rw-r--r--src/lib/math/bigint/bigint.h10
3 files changed, 20 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