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/internal/loadstor.h>
19#if BOTAN_MP_WORD_BITS == 64
20 m_data.set_word_at(0, n);
22 m_data.set_word_at(1,
static_cast<word
>(n >> 32));
23 m_data.set_word_at(0,
static_cast<word
>(n));
32#if BOTAN_MP_WORD_BITS == 64
74 bool negative =
false;
76 if(str.length() > 0 && str[0] ==
'-')
82 if(str.length() > markers + 2 && str[markers ] ==
'0' &&
83 str[markers + 1] ==
'x')
90 str.length() - markers, base);
106 *
this =
decode(input, length, base);
112 const size_t input_bits = 8 * length;
117 if(input_bits > max_bits)
119 const size_t bits_to_shift = input_bits - max_bits;
121 bn >>= bits_to_shift;
137 return get_byte_var(
sizeof(word) - (n %
sizeof(word)) - 1,
195 this->data(), this->sig_words()).is_set();
207 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
213size_t BigInt::Data::calc_sig_words()
const
215 const size_t sz = m_reg.size();
220 for(
size_t i = 0; i != sz; ++i)
222 const word w = m_reg[sz - i - 1];
241 if(length == 0 || length > 32)
244 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
254 const word w0 =
word_at(word_offset);
258 return static_cast<uint32_t
>(w0 >> wshift) & mask;
262 const word w1 =
word_at(word_offset + 1);
263 return static_cast<uint32_t
>((w0 >> wshift) | (w1 << (
BOTAN_MP_WORD_BITS - wshift))) & mask;
275 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
278 for(
size_t i = 0; i != 4; ++i)
279 out = (out << 8) |
byte_at(3-i);
293 m_data.set_word_at(which,
word_at(which) & mask);
306 const word top_word =
word_at(words - 1);
307 const size_t bits_used =
high_bit(top_word);
322 return full_words + top_bits;
338 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
342 if(
size() < p_words + 1)
345 if(ws.size() < p_words + 1)
346 ws.resize(p_words + 1);
350 size_t reductions = 0;
368 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
370 const size_t mod_words = mod.
sig_words();
374 const size_t sz =
size();
380 for(
size_t i = 0; i != bound; ++i)
408 const size_t full_words = len /
sizeof(word);
409 const size_t extra_bytes = len %
sizeof(word);
411 for(
size_t i = 0; i != full_words; ++i)
414 store_be(w, output + (len - (i+1)*
sizeof(word)));
419 const word w =
word_at(full_words);
421 for(
size_t i = 0; i != extra_bytes; ++i)
423 output[extra_bytes - i - 1] =
get_byte_var(
sizeof(word) - i - 1, w);
435 const size_t full_words = length /
sizeof(word);
436 const size_t extra_bytes = length %
sizeof(word);
440 for(
size_t i = 0; i != full_words; ++i)
442 reg[i] = load_be<word>(buf + length -
sizeof(word)*(i+1), 0);
447 for(
size_t i = 0; i != extra_bytes; ++i)
448 reg[full_words] = (reg[full_words] << 8) | buf[i];
457 throw Invalid_Argument(
"BigInt::ct_cond_add requires both values to be positive");
467 const size_t max_words = std::max(
size(), other.
size());
480 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
482 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
489 const size_t t_words =
size();
490 const size_t o_words = other.
size();
492 if(o_words < t_words)
495 const size_t r_words = std::max(t_words, o_words);
499 for(
size_t i = 0; i != r_words; ++i)
501 const word o_word = other.
word_at(i);
502 const word t_word = this->
word_at(i);
506 const bool different_sign =
sign() != other.
sign();
510#if defined(BOTAN_HAS_VALGRIND)
513 CT::poison(m_data.const_data(), m_data.size());
void ct_cond_add(bool predicate, const BigInt &value)
void binary_decode(const uint8_t buf[], size_t length)
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
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
static BigInt from_u64(uint64_t n)
void const_time_poison() const
void encode_words(word out[], size_t size) const
static BigInt from_s32(int32_t n)
void ct_cond_swap(bool predicate, BigInt &other)
static BigInt from_word(word n)
size_t reduce_below(const BigInt &mod, secure_vector< word > &ws)
static BigInt from_bytes_with_max_bits(const uint8_t buf[], size_t length, size_t max_bits)
void const_time_unpoison() const
static BigInt with_capacity(size_t n)
void swap_reg(secure_vector< word > ®)
uint32_t get_substring(size_t offset, size_t length) const
static Mask< T > is_zero(T x)
static Mask< T > expand(T v)
#define BOTAN_MP_WORD_BITS
void poison(const T *p, size_t n)
void unpoison(const T *p, size_t n)
word bigint_cnd_add(word cnd, word x[], word x_size, const word y[], size_t y_size)
constexpr size_t high_bit(T n)
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
constexpr void copy_mem(T *out, const T *in, size_t n)
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)
constexpr void store_be(uint16_t in, uint8_t out[2])
constexpr uint8_t get_byte_var(size_t byte_num, T input)
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
constexpr T ct_is_zero(T x)
std::vector< T, secure_allocator< T > > secure_vector
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
constexpr void clear_mem(T *ptr, size_t n)
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)