10#define BOTAN_BIGINT_H_
12#include <botan/types.h>
13#include <botan/secmem.h>
14#include <botan/exceptn.h>
19class RandomNumberGenerator;
30 enum Base { Decimal = 10, Hexadecimal = 16, Binary = 256 };
35 enum Sign { Negative = 0, Positive = 1 };
56 static BigInt from_u64(uint64_t n);
62 static BigInt from_word(word n);
68 static BigInt from_s32(int32_t n);
89 explicit BigInt(
const std::string& str);
96 BigInt(
const uint8_t buf[],
size_t length);
102 template<
typename Alloc>
103 explicit BigInt(
const std::vector<uint8_t, Alloc>& vec) :
BigInt(vec.data(), vec.size()) {}
111 BigInt(
const uint8_t buf[],
size_t length, Base base);
120 static BigInt from_bytes_with_max_bits(
const uint8_t buf[],
size_t length,
138 static BigInt with_capacity(
size_t n);
172 m_data.swap(other.m_data);
173 std::swap(m_signedness, other.m_signedness);
197 return add(&y, 1, Positive);
215 return sub(&y, 1, Positive);
228 BigInt& operator*=(word y);
246 word operator%=(word y);
252 BigInt& operator<<=(
size_t shift);
258 BigInt& operator>>=(
size_t shift);
290 bool operator !()
const {
return (!is_nonzero()); }
292 static BigInt add2(
const BigInt& x,
const word y[],
size_t y_words, Sign y_sign);
294 BigInt& add(
const word y[],
size_t y_words, Sign sign);
298 return add(y, y_words, sign == Positive ? Negative : Positive);
375 void clear() { m_data.set_to_zero(); m_signedness = Positive; }
384 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
391 bool is_equal(
const BigInt& n)
const;
398 bool is_less_than(
const BigInt& n)
const;
406 int32_t cmp_word(word n)
const;
412 bool is_even()
const {
return (get_bit(0) == 0); }
418 bool is_odd()
const {
return (get_bit(0) == 1); }
432 return (sig_words() == 0);
441 conditionally_set_bit(n,
true);
456 m_data.set_word_at(which, word_at(which) | mask);
463 void clear_bit(
size_t n);
491 uint32_t get_substring(
size_t offset,
size_t length)
const;
504 std::string to_dec_string()
const;
509 std::string to_hex_string()
const;
515 uint8_t byte_at(
size_t n)
const;
524 return m_data.get_word_at(n);
529 m_data.set_word_at(i, w);
534 m_data.set_words(w, len);
560 if(sign() == Positive)
570 set_sign(reverse_sign());
579 if(sign == Negative && is_zero())
594 size_t size()
const {
return m_data.size(); }
602 return m_data.sig_words();
609 size_t bytes()
const;
622 size_t top_bits_free()
const;
634 const word*
data()
const {
return m_data.const_data(); }
650 void grow_to(
size_t n)
const { m_data.grow_to(n); }
652 void resize(
size_t s) { m_data.resize(s); }
671 void binary_encode(uint8_t buf[])
const;
683 void binary_encode(uint8_t buf[],
size_t len)
const;
690 void binary_decode(
const uint8_t buf[],
size_t length);
696 template<
typename Alloc>
699 binary_decode(buf.data(), buf.size());
706 void encode_words(word out[],
size_t size)
const;
712 void ct_cond_assign(
bool predicate,
const BigInt& other);
718 void ct_cond_swap(
bool predicate,
BigInt& other);
723 void ct_cond_add(
bool predicate,
const BigInt& value);
728 void cond_flip_sign(
bool predicate);
730#if defined(BOTAN_HAS_VALGRIND)
731 void const_time_poison()
const;
732 void const_time_unpoison()
const;
767 std::vector<uint8_t> output(n.
bytes());
792 return BigInt(buf, length);
800 template<
typename Alloc>
813 static BigInt decode(
const uint8_t buf[],
size_t length,
822 template<
typename Alloc>
838 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
856 invalidate_sig_words();
860 const word* const_data()
const
865 secure_vector<word>& mutable_vector()
867 invalidate_sig_words();
871 const secure_vector<word>& const_vector()
const
876 word get_word_at(
size_t n)
const
883 void set_word_at(
size_t i, word w)
885 invalidate_sig_words();
886 if(i >= m_reg.size())
895 void set_words(
const word w[],
size_t len)
897 invalidate_sig_words();
898 m_reg.assign(w, w + len);
903 m_reg.resize(m_reg.capacity());
908 void set_size(
size_t s)
910 invalidate_sig_words();
912 m_reg.resize(s + (8 - (s % 8)));
915 void mask_bits(
size_t n)
917 if(n == 0) {
return set_to_zero(); }
922 if(top_word < size())
925 const size_t len = size() - (top_word + 1);
930 m_reg[top_word] &= mask;
931 invalidate_sig_words();
935 void grow_to(
size_t n)
const
939 if(n <= m_reg.capacity())
942 m_reg.resize(n + (8 - (n % 8)));
946 size_t size()
const {
return m_reg.size(); }
948 void shrink_to_fit(
size_t min_size = 0)
950 const size_t words = std::max(min_size, sig_words());
954 void resize(
size_t s)
959 void swap(Data& other)
961 m_reg.swap(other.m_reg);
962 std::swap(m_sig_words, other.m_sig_words);
965 void swap(secure_vector<word>& reg)
968 invalidate_sig_words();
971 void invalidate_sig_words()
const
973 m_sig_words = sig_words_npos;
976 size_t sig_words()
const
978 if(m_sig_words == sig_words_npos)
980 m_sig_words = calc_sig_words();
989 static const size_t sig_words_npos =
static_cast<size_t>(-1);
991 size_t calc_sig_words()
const;
993 mutable secure_vector<word> m_reg;
994 mutable size_t m_sig_words = sig_words_npos;
998 Sign m_signedness = Positive;
1044 {
return a.is_equal(
b); }
1048 {
return (a.
cmp(
b) <= 0); }
1050 {
return (a.
cmp(
b) >= 0); }
1054 {
return b.is_less_than(a); }
#define BOTAN_DEBUG_ASSERT(expr)
static BigInt decode(const std::vector< uint8_t, Alloc > &buf)
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)
BigInt & sub(const word y[], size_t y_words, Sign sign)
secure_vector< word > & get_word_vector()
void set_word_at(size_t i, word w)
BigInt(const BigInt &other)=default
void grow_to(size_t n) const
Sign reverse_sign() const
const secure_vector< word > & get_word_vector() const
static BigInt add2(const BigInt &x, const word y[], size_t y_words, Sign y_sign)
void set_words(const word w[], size_t len)
bool is_less_than(const BigInt &n) const
int32_t cmp(const BigInt &n, bool check_signs=true) const
BigInt & operator+=(word y)
const word * data() const
BigInt & operator=(BigInt &&other)
static secure_vector< uint8_t > encode_locked(const BigInt &n)
void binary_encode(uint8_t buf[]) const
word word_at(size_t n) const
BigInt & operator-=(const BigInt &y)
void binary_decode(const std::vector< uint8_t, Alloc > &buf)
int32_t cmp_word(word n) const
static std::vector< uint8_t > encode(const BigInt &n)
static BigInt power_of_2(size_t n)
BigInt & operator-=(word y)
BigInt & operator+=(const BigInt &y)
void const_time_poison() const
BigInt & operator=(const BigInt &)=default
static BigInt from_word(word n)
static BigInt decode(const std::vector< uint8_t, Alloc > &buf, Base base)
void const_time_unpoison() const
BigInt(const std::vector< uint8_t, Alloc > &vec)
bool get_bit(size_t n) const
void swap_reg(secure_vector< word > ®)
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_MP_WORD_BITS
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
bool operator>=(const ASN1_Time &, const ASN1_Time &)
bool operator<=(const ASN1_Time &, const ASN1_Time &)
bool operator<(const OID &a, const OID &b)
BigInt square(const BigInt &x)
bool operator>(const ASN1_Time &, const ASN1_Time &)
BigInt abs(const BigInt &n)
BigInt operator-(const BigInt &x, const BigInt &y)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
OID operator+(const OID &oid, uint32_t new_comp)
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
std::vector< T, secure_allocator< T > > secure_vector
uint32_t to_u32bit(const std::string &str)
constexpr void clear_mem(T *ptr, size_t n)