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);
63 bool negative =
false;
65 if(str.length() > 0 && str[0] ==
'-')
71 if(str.length() > markers + 2 && str[markers ] ==
'0' &&
72 str[markers + 1] ==
'x')
79 str.length() - markers, base);
95 *
this =
decode(input, length, base);
100 if(8 * length > max_bits)
101 length = (max_bits + 7) / 8;
105 if(8 * length > max_bits)
106 *
this >>= (8 - (max_bits % 8));
119 return get_byte(
sizeof(word) - (n %
sizeof(word)) - 1,
177 this->data(), this->sig_words()).is_set();
189 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
195size_t BigInt::Data::calc_sig_words()
const
197 const size_t sz = m_reg.size();
202 for(
size_t i = 0; i != sz; ++i)
204 const word w = m_reg[sz - i - 1];
223 if(length == 0 || length > 32)
226 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
228 const size_t word_offset = offset / BOTAN_MP_WORD_BITS;
229 const size_t wshift = (offset % BOTAN_MP_WORD_BITS);
236 const word w0 =
word_at(word_offset);
238 if(wshift == 0 || (offset + length) / BOTAN_MP_WORD_BITS == word_offset)
240 return static_cast<uint32_t
>(w0 >> wshift) & mask;
244 const word w1 =
word_at(word_offset + 1);
245 return static_cast<uint32_t
>((w0 >> wshift) | (w1 << (BOTAN_MP_WORD_BITS - wshift))) & mask;
257 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
260 for(
size_t i = 0; i != 4; ++i)
261 out = (out << 8) |
byte_at(3-i);
270 const size_t which = n / BOTAN_MP_WORD_BITS;
271 const word mask =
static_cast<word
>(set_it) << (n % BOTAN_MP_WORD_BITS);
272 m_data.set_word_at(which,
word_at(which) | mask);
280 const size_t which = n / BOTAN_MP_WORD_BITS;
284 const word mask = ~(
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS));
285 m_data.set_word_at(which,
word_at(which) & mask);
298 const word top_word =
word_at(words - 1);
299 const size_t bits_used =
high_bit(top_word);
301 return BOTAN_MP_WORD_BITS - bits_used;
311 const size_t full_words = (words - 1) * BOTAN_MP_WORD_BITS;
312 const size_t top_bits = BOTAN_MP_WORD_BITS -
top_bits_free();
314 return full_words + top_bits;
322 static const double LOG_2_BASE_10 = 0.30102999566;
329 return static_cast<size_t>((
bits() * LOG_2_BASE_10) + 1);
347 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
351 if(
size() < p_words + 1)
354 if(ws.size() < p_words + 1)
355 ws.resize(p_words + 1);
359 size_t reductions = 0;
377 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
379 const size_t mod_words = mod.
sig_words();
383 const size_t sz =
size();
389 for(
size_t i = 0; i != bound; ++i)
417 const size_t full_words = len /
sizeof(word);
418 const size_t extra_bytes = len %
sizeof(word);
420 for(
size_t i = 0; i != full_words; ++i)
423 store_be(w, output + (len - (i+1)*
sizeof(word)));
428 const word w =
word_at(full_words);
430 for(
size_t i = 0; i != extra_bytes; ++i)
432 output[extra_bytes - i - 1] =
get_byte(
sizeof(word) - i - 1, w);
444 const size_t full_words = length /
sizeof(word);
445 const size_t extra_bytes = length %
sizeof(word);
449 for(
size_t i = 0; i != full_words; ++i)
451 reg[i] = load_be<word>(buf + length -
sizeof(word)*(i+1), 0);
456 for(
size_t i = 0; i != extra_bytes; ++i)
457 reg[full_words] = (reg[full_words] << 8) | buf[i];
466 throw Invalid_Argument(
"BigInt::ct_cond_add requires both values to be positive");
476 const size_t max_words = std::max(
size(), other.
size());
488 clear_mem(result.mutable_data() + result.size() - 1, 1);
500 constexpr size_t bits_in_word =
sizeof(word) * 8;
501 const size_t word_shift = shift >>
ceil_log2(bits_in_word);
502 const size_t bit_shift = shift & ((1 <<
ceil_log2(bits_in_word)) - 1);
503 const size_t iterations = std::max(
size(), bits_in_word) - 1;
509 for(
size_t i = 0; i < iterations; ++i) {
512 shl_word(*
this, tmp);
523 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
525 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
532 const size_t t_words =
size();
533 const size_t o_words = other.
size();
535 if(o_words < t_words)
538 const size_t r_words = std::max(t_words, o_words);
542 for(
size_t i = 0; i != r_words; ++i)
544 const word o_word = other.
word_at(i);
545 const word t_word = this->
word_at(i);
549 const bool different_sign =
sign() != other.
sign();
553#if defined(BOTAN_HAS_VALGRIND)
556 CT::poison(m_data.const_data(), m_data.size());
566 const std::vector<BigInt>& vec,
569 const size_t words = output.size();
575 for(
size_t i = 0; i != vec.size(); ++i)
578 "Word size as expected in const_time_lookup");
582 for(
size_t w = 0; w != words; ++w)
584 const word viw = vec[i].word_at(w);
585 output[w] = mask.if_set_return(viw);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#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 ct_shift_left(size_t shift)
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
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_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 bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
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)