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 if(8 * length > max_bits)
93 length = (max_bits + 7) / 8;
97 if(8 * length > max_bits)
98 *
this >>= (8 - (max_bits % 8));
111 return get_byte(
sizeof(word) - (n %
sizeof(word)) - 1,
169 this->data(), this->sig_words()).is_set();
181 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
187size_t BigInt::Data::calc_sig_words()
const
189 const size_t sz = m_reg.size();
194 for(
size_t i = 0; i != sz; ++i)
196 const word w = m_reg[sz - i - 1];
215 if(length == 0 || length > 32)
218 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
220 const size_t word_offset = offset / BOTAN_MP_WORD_BITS;
221 const size_t wshift = (offset % BOTAN_MP_WORD_BITS);
228 const word w0 =
word_at(word_offset);
230 if(wshift == 0 || (offset + length) / BOTAN_MP_WORD_BITS == word_offset)
232 return static_cast<uint32_t
>(w0 >> wshift) & mask;
236 const word w1 =
word_at(word_offset + 1);
237 return static_cast<uint32_t
>((w0 >> wshift) | (w1 << (BOTAN_MP_WORD_BITS - wshift))) & mask;
249 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
252 for(
size_t i = 0; i != 4; ++i)
253 out = (out << 8) |
byte_at(3-i);
262 const size_t which = n / BOTAN_MP_WORD_BITS;
263 const word mask =
static_cast<word
>(set_it) << (n % BOTAN_MP_WORD_BITS);
264 m_data.set_word_at(which,
word_at(which) | mask);
272 const size_t which = n / BOTAN_MP_WORD_BITS;
276 const word mask = ~(
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS));
277 m_data.set_word_at(which,
word_at(which) & mask);
290 const word top_word =
word_at(words - 1);
291 const size_t bits_used =
high_bit(top_word);
293 return BOTAN_MP_WORD_BITS - bits_used;
303 const size_t full_words = (words - 1) * BOTAN_MP_WORD_BITS;
304 const size_t top_bits = BOTAN_MP_WORD_BITS -
top_bits_free();
306 return full_words + top_bits;
314 static const double LOG_2_BASE_10 = 0.30102999566;
321 return static_cast<size_t>((
bits() * LOG_2_BASE_10) + 1);
339 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
343 if(
size() < p_words + 1)
346 if(ws.size() < p_words + 1)
347 ws.resize(p_words + 1);
351 size_t reductions = 0;
369 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
371 const size_t mod_words = mod.
sig_words();
375 const size_t sz =
size();
381 for(
size_t i = 0; i != bound; ++i)
409 const size_t full_words = len /
sizeof(word);
410 const size_t extra_bytes = len %
sizeof(word);
412 for(
size_t i = 0; i != full_words; ++i)
415 store_be(w, output + (len - (i+1)*
sizeof(word)));
420 const word w =
word_at(full_words);
422 for(
size_t i = 0; i != extra_bytes; ++i)
424 output[extra_bytes - i - 1] =
get_byte(
sizeof(word) - i - 1, w);
436 const size_t full_words = length /
sizeof(word);
437 const size_t extra_bytes = length %
sizeof(word);
441 for(
size_t i = 0; i != full_words; ++i)
443 reg[i] = load_be<word>(buf + length -
sizeof(word)*(i+1), 0);
448 for(
size_t i = 0; i != extra_bytes; ++i)
449 reg[full_words] = (reg[full_words] << 8) | buf[i];
458 throw Invalid_Argument(
"BigInt::ct_cond_add requires both values to be positive");
468 const size_t max_words = std::max(
size(), other.
size());
481 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
483 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
490 const size_t t_words =
size();
491 const size_t o_words = other.
size();
493 if(o_words < t_words)
496 const size_t r_words = std::max(t_words, o_words);
500 for(
size_t i = 0; i != r_words; ++i)
502 const word o_word = other.
word_at(i);
503 const word t_word = this->
word_at(i);
507 const bool different_sign =
sign() != other.
sign();
511#if defined(BOTAN_HAS_VALGRIND)
514 CT::poison(m_data.const_data(), m_data.size());
524 const std::vector<BigInt>& vec,
527 const size_t words = output.size();
533 for(
size_t i = 0; i != vec.size(); ++i)
536 "Word size as expected in const_time_lookup");
540 for(
size_t w = 0; w != words; ++w)
542 const word viw = vec[i].word_at(w);
543 output[w] = mask.if_set_return(viw);
#define BOTAN_ASSERT(expr, assertion_made)
void ct_cond_add(bool predicate, const BigInt &value)
void binary_decode(const uint8_t buf[], size_t length)
void conditionally_set_bit(size_t n, bool set_it)
bool is_equal(const BigInt &n) const
static BigInt decode(const uint8_t buf[], size_t length)
void set_word_at(size_t i, word w)
void ct_cond_assign(bool predicate, const BigInt &other)
void grow_to(size_t n) const
static void const_time_lookup(secure_vector< word > &output, const std::vector< BigInt > &vec, size_t idx)
uint32_t to_u32bit() const
size_t top_bits_free() const
void ct_reduce_below(const BigInt &mod, secure_vector< word > &ws, size_t bound)
bool is_less_than(const BigInt &n) const
int32_t cmp(const BigInt &n, bool check_signs=true) const
const word * data() const
void binary_encode(uint8_t buf[]) const
word word_at(size_t n) const
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
int32_t cmp_word(word n) const
void cond_flip_sign(bool predicate)
uint8_t byte_at(size_t n) const
void const_time_poison() const
void encode_words(word out[], size_t size) const
void ct_cond_swap(bool predicate, BigInt &other)
size_t encoded_size(Base base=Binary) const
size_t reduce_below(const BigInt &mod, secure_vector< word > &ws)
void const_time_unpoison() const
void swap_reg(secure_vector< word > ®)
uint32_t get_substring(size_t offset, size_t length) const
static Mask< T > is_equal(T x, T y)
static Mask< T > is_zero(T x)
static Mask< T > expand(T v)
void poison(const T *p, size_t n)
void unpoison(const T *p, size_t n)
void store_be(uint16_t in, uint8_t out[2])
word bigint_cnd_add(word cnd, word x[], word x_size, const word y[], size_t y_size)
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
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)
void copy_mem(T *out, const T *in, size_t n)
constexpr uint8_t get_byte(size_t byte_num, T input)
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
std::vector< T, secure_allocator< T > > secure_vector
void clear_mem(T *ptr, size_t n)
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
CT::Mask< word > bigint_ct_is_eq(const word x[], size_t x_size, const word y[], size_t y_size)
size_t round_up(size_t n, size_t align_to)
const uint8_t * cast_char_ptr_to_uint8(const char *s)