diff options
Diffstat (limited to 'src/lib/math/bigint')
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 39 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 31 |
2 files changed, 31 insertions, 39 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index 6acd04f00..b5a8e0dd5 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -1,6 +1,6 @@ /* * BigInt Base -* (C) 1999-2011,2012 Jack Lloyd +* (C) 1999-2011,2012,2014 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -93,15 +93,6 @@ BigInt::BigInt(RandomNumberGenerator& rng, size_t bits) } /* -* Grow the internal storage -*/ -void BigInt::grow_to(size_t n) - { - if(n > size()) - m_reg.resize(round_up<size_t>(n, 8)); - } - -/* * Comparison Function */ s32bit BigInt::cmp(const BigInt& other, bool check_signs) const @@ -155,8 +146,8 @@ u32bit BigInt::to_u32bit() const throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert"); u32bit out = 0; - for(u32bit j = 0; j != 4; ++j) - out = (out << 8) | byte_at(3-j); + for(size_t i = 0; i != 4; ++i) + out = (out << 8) | byte_at(3-i); return out; } @@ -183,30 +174,6 @@ void BigInt::clear_bit(size_t n) } /* -* Clear all but the lowest n bits -*/ -void BigInt::mask_bits(size_t n) - { - if(n == 0) { clear(); return; } - - const size_t top_word = n / MP_WORD_BITS; - const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1; - - if(top_word < size()) - clear_mem(&m_reg[top_word+1], size() - (top_word + 1)); - - m_reg[top_word] &= mask; - } - -/* -* Count how many bytes are being used -*/ -size_t BigInt::bytes() const - { - return (bits() + 7) / 8; - } - -/* * Count how many bits are being used */ size_t BigInt::bits() const diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index 4993f5d0c..269a74259 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -269,7 +269,19 @@ class BOTAN_DLL BigInt * Clear all but the lowest n bits * @param n amount of bits to keep */ - void mask_bits(size_t n); + void mask_bits(size_t n) + { + if(n == 0) { clear(); return; } + + const size_t top_word = n / BOTAN_MP_WORD_BITS; + const word mask = (static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)) - 1; + + if(top_word < size()) + { + clear_mem(&m_reg[top_word+1], size() - (top_word + 1)); + m_reg[top_word] &= mask; + } + } /** * Return bit value at specified position @@ -315,6 +327,12 @@ class BOTAN_DLL BigInt word word_at(size_t n) const { return ((n < size()) ? m_reg[n] : 0); } + void set_word_at(size_t i, word w) + { + grow_to(i + 1); + m_reg[i] = w; + } + /** * Tests if the sign of the integer is negative * @result true, iff the integer has a negative sign @@ -378,7 +396,7 @@ class BOTAN_DLL BigInt * Give byte length of the integer * @result byte length of the represented integer value */ - size_t bytes() const; + size_t bytes() const { return (bits() + 7) / 8; } /** * Get the bit length of the integer @@ -398,11 +416,18 @@ class BOTAN_DLL BigInt */ const word* data() const { return &m_reg[0]; } + secure_vector<word>& get_word_vector() { return m_reg; } + const secure_vector<word>& get_word_vector() const { return m_reg; } + /** * Increase internal register buffer to at least n words * @param n new size of register */ - void grow_to(size_t n); + void grow_to(size_t n) + { + if(n > size()) + m_reg.resize(n + (8 - n % 8)); + } /** * Fill BigInt with a random number with size of bitsize |