8 #include <botan/bigint.h>
9 #include <botan/internal/mp_core.h>
10 #include <botan/get_byte.h>
11 #include <botan/parsing.h>
12 #include <botan/internal/rounding.h>
26 const size_t limbs_needed =
sizeof(
u64bit) /
sizeof(word);
28 reg.
resize(4*limbs_needed);
29 for(
size_t i = 0; i != limbs_needed; ++i)
38 reg.
resize(round_up<size_t>(size, 8));
51 reg.
resize(round_up<size_t>(b_words, 8));
69 bool negative =
false;
70 if(str.length() > 0 && str[0] ==
'-') { markers += 1; negative =
true; }
72 if(str.length() > markers + 2 && str[markers ] ==
'0' &&
73 str[markers + 1] ==
'x')
75 else if(str.length() > markers + 1 && str[markers] ==
'0')
76 { markers += 1; base =
Octal; }
78 *
this =
decode(reinterpret_cast<const byte*>(str.data()) + markers,
79 str.length() - markers,
base);
91 *
this =
decode(input, length, base);
126 reg.
resize(round_up<size_t>(n, 8));
149 const size_t WORD_BYTES =
sizeof(word);
150 size_t word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES;
151 if(word_num >=
size())
154 return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]);
174 for(
size_t i = 0; i != 8; ++i)
177 piece = (piece << 8) | part;
180 const u64bit mask = (
static_cast<u64bit>(1) << length) - 1;
181 const size_t shift = (offset % 8);
183 return static_cast<u32bit>((piece >> shift) & mask);
194 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
197 for(
u32bit j = 0; j != 4; ++j)
198 out = (out << 8) |
byte_at(3-j);
208 const word mask =
static_cast<word
>(1) << (n %
MP_WORD_BITS);
219 const word mask =
static_cast<word
>(1) << (n %
MP_WORD_BITS);
229 if(n == 0) {
clear();
return; }
230 if(n >=
bits())
return;
233 const word mask = (
static_cast<word
>(1) << (n %
MP_WORD_BITS)) - 1;
235 if(top_word <
size())
236 for(
size_t i = top_word + 1; i !=
size(); ++i)
239 reg[top_word] &= mask;
247 return (
bits() + 7) / 8;
263 while(top_bits && ((top_word & mask) == 0))
264 { mask >>= 1; top_bits--; }
274 static const double LOG_2_BASE_10 = 0.30102999566;
280 else if(base ==
Octal)
281 return ((
bits() + 2) / 3);
283 return static_cast<size_t>((
bits() * LOG_2_BASE_10) + 1);
342 const size_t sig_bytes =
bytes();
343 for(
size_t i = 0; i != sig_bytes; ++i)
344 output[sig_bytes-i-1] =
byte_at(i);
352 const size_t WORD_BYTES =
sizeof(word);
355 reg.
resize(round_up<size_t>((length / WORD_BYTES) + 1, 8));
357 for(
size_t i = 0; i != length / WORD_BYTES; ++i)
359 const size_t top = length - WORD_BYTES*i;
360 for(
size_t j = WORD_BYTES; j > 0; --j)
361 reg[i] = (reg[i] << 8) | buf[top - j];
364 for(
size_t i = 0; i != length % WORD_BYTES; ++i)
365 reg[length / WORD_BYTES] = (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(byte buf[]) const
s32bit cmp(const BigInt &n, bool check_signs=true) const
void binary_decode(const byte buf[], size_t length)
Sign reverse_sign() const
byte byte_at(size_t n) const
std::invalid_argument Invalid_Argument
byte get_byte(size_t byte_num, T input)
void copy(const T in[], size_t n)
unsigned long long u64bit
u32bit get_substring(size_t offset, size_t length) const
RandomNumberGenerator * rng
void randomize(RandomNumberGenerator &rng, size_t bitsize=0)
const word * data() const
void swap(MemoryRegion< T > &other)
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
bool get_bit(size_t n) const
const word MP_WORD_TOP_BIT
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
const size_t MP_WORD_BITS