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.empty() && str[0] ==
'-') {
77 if(str.length() > markers + 2 && str[markers] ==
'0' && str[markers + 1] ==
'x') {
97 r.assign_from_bytes(input);
105 *
this =
decode(input, length, base);
110 const size_t input_bits = 8 * length;
114 if(input_bits > max_bits) {
115 const size_t bits_to_shift = input_bits - max_bits;
117 bn >>= bits_to_shift;
196 throw Encoding_Error(
"BigInt::encode_words value too large to encode");
203size_t BigInt::Data::calc_sig_words()
const {
204 const size_t sz = m_reg.size();
209 for(
size_t i = 0; i != sz; ++i) {
210 const word w = m_reg[sz - i - 1];
228 if(length == 0 || length > 32) {
232 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
242 const word w0 =
word_at(word_offset);
245 return static_cast<uint32_t
>(w0 >> wshift) & mask;
247 const word w1 =
word_at(word_offset + 1);
248 return static_cast<uint32_t
>((w0 >> wshift) | (w1 << (
BOTAN_MP_WORD_BITS - wshift))) & mask;
260 throw Encoding_Error(
"BigInt::to_u32bit: Number is too big to convert");
264 for(
size_t i = 0; i != 4; ++i) {
265 out = (out << 8) |
byte_at(3 - i);
278 m_data.set_word_at(which,
word_at(which) & mask);
289 const word top_word =
word_at(words - 1);
305 return full_words + top_bits;
319 throw Invalid_Argument(
"BigInt::reduce_below both values must be positive");
324 if(
size() < p_words + 1) {
328 if(ws.size() < p_words + 1) {
329 ws.resize(p_words + 1);
334 size_t reductions = 0;
351 throw Invalid_Argument(
"BigInt::ct_reduce_below both values must be positive");
354 const size_t mod_words = mod.
sig_words();
358 const size_t sz =
size();
364 for(
size_t i = 0; i != bound; ++i) {
393 const size_t full_words = len /
sizeof(word);
394 const size_t extra_bytes = len %
sizeof(word);
396 for(
size_t i = 0; i != full_words; ++i) {
398 store_be(w, output + (len - (i + 1) *
sizeof(word)));
401 if(extra_bytes > 0) {
402 const word w =
word_at(full_words);
404 for(
size_t i = 0; i != extra_bytes; ++i) {
405 output[extra_bytes - i - 1] =
get_byte_var(
sizeof(word) - i - 1, w);
413void BigInt::assign_from_bytes(std::span<const uint8_t>
bytes) {
416 const size_t length =
bytes.size();
417 const size_t full_words = length /
sizeof(word);
418 const size_t extra_bytes = length %
sizeof(word);
422 for(
size_t i = 0; i != full_words; ++i) {
429 std::array<uint8_t,
sizeof(word)> last_partial_word = {0};
430 copy_mem(std::span{last_partial_word}.last(extra_bytes),
bytes);
439 throw Invalid_Argument(
"BigInt::ct_cond_add requires both values to be positive");
451 clear_mem(result.mutable_data() + result.size() - 1, 1);
463 constexpr size_t bits_in_word =
sizeof(word) * 8;
464 const size_t word_shift = shift >>
ceil_log2(bits_in_word);
465 const size_t bit_shift = shift & ((1 <<
ceil_log2(bits_in_word)) - 1);
466 const size_t iterations = std::max(
size(), bits_in_word) - 1;
472 for(
size_t i = 0; i < iterations; ++i) {
475 shl_word(*
this, tmp);
481 const size_t max_words = std::max(
size(), other.
size());
493 const uint8_t current_sign =
static_cast<uint8_t
>(
sign());
495 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
501 const size_t t_words =
size();
502 const size_t o_words = other.
size();
504 if(o_words < t_words) {
508 const size_t r_words = std::max(t_words, o_words);
512 for(
size_t i = 0; i != r_words; ++i) {
513 const word o_word = other.
word_at(i);
514 const word t_word = this->
word_at(i);
518 const bool different_sign =
sign() != other.
sign();
522#if defined(BOTAN_CT_POISON_ENABLED)
524 CT::poison(m_data.const_data(), m_data.size());
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
constexpr void _const_time_poison() const
void ct_cond_add(bool predicate, const BigInt &value)
bool is_equal(const BigInt &n) const
static BigInt decode(const uint8_t buf[], size_t length)
BigInt & sub(const word y[], size_t y_words, Sign sign)
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
constexpr void _const_time_unpoison() 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)
static BigInt from_string(std::string_view str)
void serialize_to(std::span< uint8_t > out) const
static BigInt from_bytes(std::span< const uint8_t > bytes)
uint8_t byte_at(size_t n) const
static BigInt from_u64(uint64_t n)
const word * _data() 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)
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 constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_zero(T x)
#define BOTAN_MP_WORD_BITS
constexpr T value_barrier(T x)
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
constexpr void bigint_cnd_swap(W cnd, W x[], W y[], size_t size)
constexpr auto bigint_sub3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr size_t high_bit(T n)
constexpr size_t round_up(size_t n, size_t align_to)
constexpr auto bigint_ct_is_eq(const W x[], size_t x_size, const W y[], size_t y_size) -> CT::Mask< W >
constexpr int32_t bigint_cmp(const W x[], size_t x_size, const W y[], size_t y_size)
constexpr void bigint_shl2(W y[], const W x[], size_t x_size, size_t shift)
constexpr W bigint_cnd_add(W cnd, W x[], size_t x_size, const W y[], size_t y_size)
constexpr auto bigint_ct_is_lt(const W x[], size_t x_size, const W y[], size_t y_size, bool lt_or_equal=false) -> CT::Mask< W >
std::vector< T, secure_allocator< T > > secure_vector
constexpr uint8_t get_byte_var(size_t byte_num, T input)
constexpr T ct_is_zero(T x)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_be(ParamTs &&... params)
constexpr void clear_mem(T *ptr, size_t n)
constexpr auto load_be(ParamTs &&... params)
constexpr uint8_t ceil_log2(T x)
const uint8_t * cast_char_ptr_to_uint8(const char *s)