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 };
73 explicit BigInt(
const std::string& str);
80 BigInt(
const uint8_t buf[],
size_t length);
86 template<
typename Alloc>
87 explicit BigInt(
const std::vector<uint8_t, Alloc>& vec) :
BigInt(vec.data(), vec.size()) {}
95 BigInt(
const uint8_t buf[],
size_t length, Base base);
104 BigInt(
const uint8_t buf[],
size_t length,
size_t max_bits);
111 BigInt(
const word words[],
size_t length);
129 BigInt(Sign sign,
size_t n);
163 m_data.swap(other.m_data);
164 std::swap(m_signedness, other.m_signedness);
188 return add(&y, 1, Positive);
206 return sub(&y, 1, Positive);
219 BigInt& operator*=(word y);
237 word operator%=(word y);
243 BigInt& operator<<=(
size_t shift);
249 BigInt& operator>>=(
size_t shift);
281 bool operator !()
const {
return (!is_nonzero()); }
283 static BigInt add2(
const BigInt& x,
const word y[],
size_t y_words, Sign y_sign);
285 BigInt& add(
const word y[],
size_t y_words, Sign sign);
289 return add(y, y_words, sign == Positive ? Negative : Positive);
366 void clear() { m_data.set_to_zero(); m_signedness = Positive; }
375 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
382 bool is_equal(
const BigInt& n)
const;
389 bool is_less_than(
const BigInt& n)
const;
397 int32_t cmp_word(word n)
const;
403 bool is_even()
const {
return (get_bit(0) == 0); }
409 bool is_odd()
const {
return (get_bit(0) == 1); }
423 return (sig_words() == 0);
432 conditionally_set_bit(n,
true);
443 void conditionally_set_bit(
size_t n,
bool set_it);
449 void clear_bit(
size_t n);
467 return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
477 uint32_t get_substring(
size_t offset,
size_t length)
const;
490 std::string to_dec_string()
const;
495 std::string to_hex_string()
const;
501 uint8_t byte_at(
size_t n)
const;
510 return m_data.get_word_at(n);
515 m_data.set_word_at(i, w);
520 m_data.set_words(w, len);
546 if(sign() == Positive)
556 set_sign(reverse_sign());
565 if(sign == Negative && is_zero())
580 size_t size()
const {
return m_data.size(); }
588 return m_data.sig_words();
595 size_t bytes()
const;
608 size_t top_bits_free()
const;
620 const word*
data()
const {
return m_data.const_data(); }
636 void grow_to(
size_t n)
const { m_data.grow_to(n); }
642 void BOTAN_DEPRECATED(
"Use resize if required") shrink_to_fit(
size_t min_size = 0)
644 m_data.shrink_to_fit(min_size);
647 void resize(
size_t s) { m_data.resize(s); }
666 void binary_encode(uint8_t buf[])
const;
678 void binary_encode(uint8_t buf[],
size_t len)
const;
685 void binary_decode(
const uint8_t buf[],
size_t length);
691 template<
typename Alloc>
694 binary_decode(buf.data(), buf.size());
704 BOTAN_DEPRECATED(
"See comments on declaration")
705 size_t encoded_size(Base base = Binary) const;
711 void encode_words(word out[],
size_t size) const;
717 void ct_cond_assign(
bool predicate, const
BigInt& other);
723 void ct_cond_swap(
bool predicate,
BigInt& other);
728 void ct_cond_add(
bool predicate, const
BigInt& value);
733 void cond_flip_sign(
bool predicate);
735#if defined(BOTAN_HAS_VALGRIND)
736 void const_time_poison()
const;
737 void const_time_unpoison()
const;
772 std::vector<uint8_t> output(n.
bytes());
794 static BOTAN_DEPRECATED(
"Use n.binary_encode") void
encode(uint8_t buf[], const
BigInt& n)
796 n.binary_encode(buf);
807 return BigInt(buf, length);
815 template<
typename Alloc>
831 BOTAN_DEPRECATED(
"See comments on declaration")
844 BOTAN_DEPRECATED("See comments on declaration")
858 BOTAN_DEPRECATED("See comments on declaration")
859 static
void encode(uint8_t buf[], const
BigInt& n, Base base);
868 static
BigInt decode(const uint8_t buf[],
size_t length,
877 template<typename Alloc>
893 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
910 static void BOTAN_DEPRECATED(
"No longer in use") const_time_lookup(
922 invalidate_sig_words();
926 const word* const_data()
const
931 secure_vector<word>& mutable_vector()
933 invalidate_sig_words();
937 const secure_vector<word>& const_vector()
const
942 word get_word_at(
size_t n)
const
949 void set_word_at(
size_t i, word w)
951 invalidate_sig_words();
952 if(i >= m_reg.size())
961 void set_words(
const word w[],
size_t len)
963 invalidate_sig_words();
964 m_reg.assign(w, w + len);
969 m_reg.resize(m_reg.capacity());
974 void set_size(
size_t s)
976 invalidate_sig_words();
978 m_reg.resize(s + (8 - (s % 8)));
981 void mask_bits(
size_t n)
983 if(n == 0) {
return set_to_zero(); }
985 const size_t top_word = n / BOTAN_MP_WORD_BITS;
988 if(top_word < size())
990 const word mask = (
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
991 const size_t len = size() - (top_word + 1);
996 m_reg[top_word] &= mask;
997 invalidate_sig_words();
1001 void grow_to(
size_t n)
const
1005 if(n <= m_reg.capacity())
1008 m_reg.resize(n + (8 - (n % 8)));
1012 size_t size()
const {
return m_reg.size(); }
1014 void shrink_to_fit(
size_t min_size = 0)
1016 const size_t words = std::max(min_size, sig_words());
1017 m_reg.resize(words);
1020 void resize(
size_t s)
1025 void swap(Data& other)
1027 m_reg.swap(other.m_reg);
1028 std::swap(m_sig_words, other.m_sig_words);
1031 void swap(secure_vector<word>& reg)
1034 invalidate_sig_words();
1037 void invalidate_sig_words()
const
1039 m_sig_words = sig_words_npos;
1042 size_t sig_words()
const
1044 if(m_sig_words == sig_words_npos)
1046 m_sig_words = calc_sig_words();
1055 static const size_t sig_words_npos =
static_cast<size_t>(-1);
1057 size_t calc_sig_words()
const;
1059 mutable secure_vector<word> m_reg;
1060 mutable size_t m_sig_words = sig_words_npos;
1064 Sign m_signedness = Positive;
1110 {
return a.is_equal(b); }
1114 {
return (a.
cmp(b) <= 0); }
1116 {
return (a.
cmp(b) >= 0); }
#define BOTAN_DEBUG_ASSERT(expr)
static BigInt decode(const std::vector< uint8_t, Alloc > &buf)
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
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)
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
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)
uint32_t to_u32bit(const std::string &str)
std::vector< T, secure_allocator< T > > secure_vector
void clear_mem(T *ptr, size_t n)