8 #include <botan/bigint.h>
9 #include <botan/internal/mp_core.h>
10 #include <botan/loadstor.h>
11 #include <botan/parsing.h>
12 #include <botan/internal/rounding.h>
13 #include <botan/internal/bit_ops.h>
25 const size_t limbs_needed =
sizeof(uint64_t) /
sizeof(word);
27 m_reg.resize(4*limbs_needed);
28 for(
size_t i = 0; i != limbs_needed; ++i)
47 m_signedness = other.m_signedness;
57 bool negative =
false;
59 if(str.length() > 0 && str[0] ==
'-')
65 if(str.length() > markers + 2 && str[markers ] ==
'0' &&
66 str[markers + 1] ==
'x')
72 *
this =
decode(reinterpret_cast<const uint8_t*>(str.data()) + markers,
73 str.length() - markers, base);
84 *
this =
decode(input, length, base);
126 for(
size_t i = 0; i != 8; ++i)
128 const uint8_t part =
byte_at((offset / 8) + (7-i));
129 piece = (piece << 8) | part;
132 const uint64_t mask = (
static_cast<uint64_t
>(1) << length) - 1;
133 const size_t shift = (offset % 8);
135 return static_cast<uint32_t
>((piece >> shift) & mask);
146 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
149 for(
size_t i = 0; i != 4; ++i)
150 out = (out << 8) |
byte_at(3-i);
160 const word mask =
static_cast<word
>(1) << (n %
MP_WORD_BITS);
162 m_reg[which] |= mask;
171 const word mask =
static_cast<word
>(1) << (n %
MP_WORD_BITS);
173 m_reg[which] &= ~mask;
191 const size_t full_words = words - 1;
200 static const double LOG_2_BASE_10 = 0.30102999566;
207 return static_cast<size_t>((
bits() * LOG_2_BASE_10) + 1);
272 const size_t sig_bytes =
bytes();
273 for(
size_t i = 0; i != sig_bytes; ++i)
274 output[sig_bytes-i-1] =
byte_at(i);
282 const size_t WORD_BYTES =
sizeof(word);
285 m_reg.resize(
round_up((length / WORD_BYTES) + 1, 8));
287 for(
size_t i = 0; i != length / WORD_BYTES; ++i)
289 const size_t top = length - WORD_BYTES*i;
290 for(
size_t j = WORD_BYTES; j > 0; --j)
291 m_reg[i] = (m_reg[i] << 8) | buf[top - j];
294 for(
size_t i = 0; i != length % WORD_BYTES; ++i)
295 m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i];
size_t encoded_size(Base base=Binary) const
word word_at(size_t n) const
void binary_encode(uint8_t buf[]) const
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
uint32_t to_u32bit() const
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Sign reverse_sign() const
int32_t cmp(const BigInt &n, bool check_signs=true) const
const word * data() const
uint32_t get_substring(size_t offset, size_t length) const
void binary_decode(const uint8_t buf[], size_t length)
size_t round_up(size_t n, size_t align_to)
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
uint8_t byte_at(size_t n) const
const size_t MP_WORD_BITS