8#include <botan/bigint.h>
10#include <botan/internal/bit_ops.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/mp_core.h>
14#include <botan/internal/rounding.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));
31#if BOTAN_MP_WORD_BITS == 64
70 bool negative =
false;
72 if(str.length() > 0 && str[0] ==
'-') {
77 if(str.length() > markers + 2 && str[markers] ==
'0' && str[markers + 1] ==
'x') {
99 *
this =
decode(input, length, base);
104 const size_t input_bits = 8 * length;
109 if(input_bits > max_bits) {
110 const size_t bits_to_shift = input_bits - max_bits;
112 bn >>= bits_to_shift;
191 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
198size_t BigInt::Data::calc_sig_words()
const {
199 const size_t sz = m_reg.size();
204 for(
size_t i = 0; i != sz; ++i) {
205 const word w = m_reg[sz - i - 1];
223 if(length == 0 || length > 32) {
227 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
237 const word w0 =
word_at(word_offset);
240 return static_cast<uint32_t
>(w0 >> wshift) & mask;
242 const word w1 =
word_at(word_offset + 1);
243 return static_cast<uint32_t
>((w0 >> wshift) | (w1 << (
BOTAN_MP_WORD_BITS - wshift))) & mask;
255 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
259 for(
size_t i = 0; i != 4; ++i) {
260 out = (out << 8) |
byte_at(3 - i);
273 m_data.set_word_at(which,
word_at(which) & mask);
284 const word top_word =
word_at(words - 1);
285 const size_t bits_used =
high_bit(top_word);
300 return full_words + top_bits;
314 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
319 if(
size() < p_words + 1) {
323 if(ws.size() < p_words + 1) {
324 ws.resize(p_words + 1);
329 size_t reductions = 0;
346 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
349 const size_t mod_words = mod.
sig_words();
353 const size_t sz =
size();
359 for(
size_t i = 0; i != bound; ++i) {
383 const size_t full_words = len /
sizeof(word);
384 const size_t extra_bytes = len %
sizeof(word);
386 for(
size_t i = 0; i != full_words; ++i) {
388 store_be(w, output + (len - (i + 1) *
sizeof(word)));
391 if(extra_bytes > 0) {
392 const word w =
word_at(full_words);
394 for(
size_t i = 0; i != extra_bytes; ++i) {
395 output[extra_bytes - i - 1] =
get_byte_var(
sizeof(word) - i - 1, w);
406 const size_t full_words = length /
sizeof(word);
407 const size_t extra_bytes = length %
sizeof(word);
411 for(
size_t i = 0; i != full_words; ++i) {
412 reg[i] = load_be<word>(buf + length -
sizeof(word) * (i + 1), 0);
415 if(extra_bytes > 0) {
416 for(
size_t i = 0; i != extra_bytes; ++i) {
417 reg[full_words] = (reg[full_words] << 8) | buf[i];
426 throw Invalid_Argument(
"BigInt::ct_cond_add requires both values to be positive");
434 const size_t max_words = std::max(
size(), other.
size());
446 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
448 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
454 const size_t t_words =
size();
455 const size_t o_words = other.
size();
457 if(o_words < t_words) {
461 const size_t r_words = std::max(t_words, o_words);
465 for(
size_t i = 0; i != r_words; ++i) {
466 const word o_word = other.
word_at(i);
467 const word t_word = this->
word_at(i);
471 const bool different_sign =
sign() != other.
sign();
475#if defined(BOTAN_HAS_VALGRIND)
477 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)