8 #include <botan/bigint.h>
9 #include <botan/internal/mp_core.h>
10 #include <botan/internal/rounding.h>
11 #include <botan/internal/bit_ops.h>
12 #include <botan/internal/ct_utils.h>
13 #include <botan/loadstor.h>
19 m_data.set_words(words, length);
29 #if BOTAN_MP_WORD_BITS == 32
30 m_data.set_word_at(0, static_cast<word>(n));
31 m_data.set_word_at(1, static_cast<word>(n >> 32));
33 m_data.set_word_at(0, n);
44 m_data.set_size(size);
55 bool negative =
false;
57 if(str.length() > 0 && str[0] ==
'-')
63 if(str.length() > markers + 2 && str[markers ] ==
'0' &&
64 str[markers + 1] ==
'x')
71 str.length() - markers, base);
87 *
this =
decode(input, length, base);
92 const size_t max_bytes = std::min(length, (max_bits + 7) / 8);
95 const size_t b = this->
bits();
98 *
this >>= (b - max_bits);
112 return get_byte(
sizeof(word) - (n %
sizeof(word)) - 1,
182 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
188 size_t BigInt::Data::calc_sig_words()
const
190 const size_t sz = m_reg.size();
195 for(
size_t i = 0; i != sz; ++i)
197 const word w = m_reg[sz - i - 1];
216 if(length == 0 || length > 32)
219 const size_t byte_offset = offset / 8;
220 const size_t shift = (offset % 8);
221 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
223 const uint8_t b0 =
byte_at(byte_offset);
224 const uint8_t b1 =
byte_at(byte_offset + 1);
225 const uint8_t b2 =
byte_at(byte_offset + 2);
226 const uint8_t b3 =
byte_at(byte_offset + 3);
227 const uint8_t b4 =
byte_at(byte_offset + 4);
228 const uint64_t piece =
make_uint64(0, 0, 0, b4, b3, b2, b1, b0);
230 return static_cast<uint32_t
>((piece >> shift) & mask);
241 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
244 for(
size_t i = 0; i != 4; ++i)
245 out = (out << 8) |
byte_at(3-i);
254 const size_t which = n / BOTAN_MP_WORD_BITS;
255 const word mask =
static_cast<word
>(set_it) << (n % BOTAN_MP_WORD_BITS);
256 m_data.set_word_at(which,
word_at(which) | mask);
264 const size_t which = n / BOTAN_MP_WORD_BITS;
268 const word mask = ~(
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS));
269 m_data.set_word_at(which,
word_at(which) & mask);
282 const word top_word =
word_at(words - 1);
283 const size_t bits_used =
high_bit(top_word);
285 return BOTAN_MP_WORD_BITS - bits_used;
295 const size_t full_words = (words - 1) * BOTAN_MP_WORD_BITS;
296 const size_t top_bits = BOTAN_MP_WORD_BITS -
top_bits_free();
298 return full_words + top_bits;
306 static const double LOG_2_BASE_10 = 0.30102999566;
313 return static_cast<size_t>((
bits() * LOG_2_BASE_10) + 1);
331 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
335 if(
size() < p_words + 1)
338 if(ws.size() < p_words + 1)
339 ws.resize(p_words + 1);
343 size_t reductions = 0;
361 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
363 const size_t mod_words = mod.
sig_words();
367 const size_t sz =
size();
373 for(
size_t i = 0; i != bound; ++i)
401 const size_t full_words = len /
sizeof(word);
402 const size_t extra_bytes = len %
sizeof(word);
404 for(
size_t i = 0; i != full_words; ++i)
407 store_be(w, output + (len - (i+1)*
sizeof(word)));
412 const word w =
word_at(full_words);
414 for(
size_t i = 0; i != extra_bytes; ++i)
416 output[extra_bytes - i - 1] =
get_byte(
sizeof(word) - i - 1, w);
428 const size_t full_words = length /
sizeof(word);
429 const size_t extra_bytes = length %
sizeof(word);
433 for(
size_t i = 0; i != full_words; ++i)
435 reg[i] = load_be<word>(buf + length -
sizeof(word)*(i+1), 0);
440 for(
size_t i = 0; i != extra_bytes; ++i)
441 reg[full_words] = (reg[full_words] << 8) | buf[i];
449 const size_t max_words = std::max(
size(), other.
size());
462 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
464 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
466 set_sign(static_cast<Sign>(new_sign));
471 const size_t t_words =
size();
472 const size_t o_words = other.
size();
474 if(o_words < t_words)
477 const size_t r_words = std::max(t_words, o_words);
481 for(
size_t i = 0; i != r_words; ++i)
483 const word o_word = other.
word_at(i);
484 const word t_word = this->
word_at(i);
494 #if defined(BOTAN_HAS_VALGRIND)
497 CT::poison(m_data.const_data(), m_data.size());
507 const std::vector<BigInt>& vec,
510 const size_t words = output.size();
516 for(
size_t i = 0; i != vec.size(); ++i)
519 "Word size as expected in const_time_lookup");
523 for(
size_t w = 0; w != words; ++w)
525 const word viw = vec[i].word_at(w);
526 output[w] = mask.if_set_return(viw);
size_t encoded_size(Base base=Binary) const
word word_at(size_t n) const
void binary_encode(uint8_t buf[]) const
bool is_equal(const BigInt &n) const
static void const_time_lookup(secure_vector< word > &output, const std::vector< BigInt > &vec, size_t idx)
void store_be(uint16_t in, uint8_t out[2])
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)
void clear_mem(T *ptr, size_t n)
void const_time_unpoison() const
constexpr uint8_t get_byte(size_t byte_num, T input)
const uint8_t * cast_char_ptr_to_uint8(const char *s)
constexpr uint64_t make_uint64(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7)
void swap_reg(secure_vector< word > ®)
void poison(const T *p, size_t n)
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
#define BOTAN_ASSERT(expr, assertion_made)
int32_t cmp(const BigInt &n, bool check_signs=true) const
static Mask< T > expand(T v)
std::vector< T, secure_allocator< T >> secure_vector
size_t top_bits_free() const
void ct_cond_swap(bool predicate, BigInt &other)
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
CT::Mask< word > bigint_ct_is_eq(const word x[], size_t x_size, const word y[], size_t y_size)
const word * data() const
void copy_mem(T *out, const T *in, size_t n)
void conditionally_set_bit(size_t n, bool set_it)
void encode_words(word out[], size_t size) const
void cond_flip_sign(bool predicate)
uint32_t get_substring(size_t offset, size_t length) const
CT::Mask< word > bigint_ct_is_lt(const word x[], size_t x_size, const word y[], size_t y_size, bool lt_or_equal=false)
static BigInt decode(const uint8_t buf[], size_t length)
size_t reduce_below(const BigInt &mod, secure_vector< word > &ws)
void grow_to(size_t n) const
void set_word_at(size_t i, word w)
void binary_decode(const uint8_t buf[], size_t length)
bool is_less_than(const BigInt &n) const
void unpoison(const T *p, size_t n)
void ct_reduce_below(const BigInt &mod, secure_vector< word > &ws, size_t bound)
int32_t cmp_word(word n) const
static Mask< T > is_zero(T x)
size_t round_up(size_t n, size_t align_to)
static Mask< T > is_equal(T x, T y)
uint8_t byte_at(size_t n) const
void ct_cond_assign(bool predicate, const BigInt &other)
void const_time_poison() const