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 };
64 static BigInt with_capacity(
size_t n);
79 explicit BigInt(
const std::string& str);
86 BigInt(
const uint8_t buf[],
size_t length);
92 template<
typename Alloc>
93 explicit BigInt(
const std::vector<uint8_t, Alloc>& vec) :
BigInt(vec.data(), vec.size()) {}
101 BigInt(
const uint8_t buf[],
size_t length, Base base);
110 BigInt(
const uint8_t buf[],
size_t length,
size_t max_bits);
117 BigInt(
const word words[],
size_t length);
135 BigInt(Sign sign,
size_t n);
169 m_data.swap(other.m_data);
170 std::swap(m_signedness, other.m_signedness);
194 return add(&y, 1, Positive);
212 return sub(&y, 1, Positive);
225 BigInt& operator*=(word y);
243 word operator%=(word y);
249 BigInt& operator<<=(
size_t shift);
255 BigInt& operator>>=(
size_t shift);
287 bool operator !()
const {
return (!is_nonzero()); }
289 static BigInt add2(
const BigInt& x,
const word y[],
size_t y_words, Sign y_sign);
291 BigInt& add(
const word y[],
size_t y_words, Sign sign);
295 return add(y, y_words, sign == Positive ? Negative : Positive);
372 void clear() { m_data.set_to_zero(); m_signedness = Positive; }
381 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
388 bool is_equal(
const BigInt& n)
const;
395 bool is_less_than(
const BigInt& n)
const;
403 int32_t cmp_word(word n)
const;
409 bool is_even()
const {
return (get_bit(0) == 0); }
415 bool is_odd()
const {
return (get_bit(0) == 1); }
429 return (sig_words() == 0);
438 conditionally_set_bit(n,
true);
449 void conditionally_set_bit(
size_t n,
bool set_it);
455 void clear_bit(
size_t n);
473 return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
483 uint32_t get_substring(
size_t offset,
size_t length)
const;
496 std::string to_dec_string()
const;
501 std::string to_hex_string()
const;
507 uint8_t byte_at(
size_t n)
const;
516 return m_data.get_word_at(n);
521 m_data.set_word_at(i, w);
526 m_data.set_words(w, len);
552 if(sign() == Positive)
562 set_sign(reverse_sign());
571 if(sign == Negative && is_zero())
586 size_t size()
const {
return m_data.size(); }
594 return m_data.sig_words();
601 size_t bytes()
const;
614 size_t top_bits_free()
const;
626 const word*
data()
const {
return m_data.const_data(); }
642 void grow_to(
size_t n)
const { m_data.grow_to(n); }
648 void BOTAN_DEPRECATED(
"Use resize if required") shrink_to_fit(
size_t min_size = 0)
650 m_data.shrink_to_fit(min_size);
653 void resize(
size_t s) { m_data.resize(s); }
672 void binary_encode(uint8_t buf[])
const;
684 void binary_encode(uint8_t buf[],
size_t len)
const;
691 void binary_decode(
const uint8_t buf[],
size_t length);
697 template<
typename Alloc>
700 binary_decode(buf.data(), buf.size());
710 BOTAN_DEPRECATED(
"See comments on declaration")
711 size_t encoded_size(Base base = Binary) const;
717 void encode_words(word out[],
size_t size) const;
723 void ct_cond_assign(
bool predicate, const
BigInt& other);
729 void ct_cond_swap(
bool predicate,
BigInt& other);
734 void ct_cond_add(
bool predicate, const
BigInt& value);
740 void ct_shift_left(
size_t shift);
745 void cond_flip_sign(
bool predicate);
747#if defined(BOTAN_HAS_VALGRIND)
748 void const_time_poison()
const;
749 void const_time_unpoison()
const;
784 std::vector<uint8_t> output(n.
bytes());
806 static BOTAN_DEPRECATED(
"Use n.binary_encode") void encode(uint8_t buf[], const
BigInt& n)
808 n.binary_encode(buf);
819 return BigInt(buf, length);
827 template<
typename Alloc>
843 BOTAN_DEPRECATED(
"See comments on declaration")
844 static
std::vector<uint8_t> encode(const
BigInt& n, Base base);
856 BOTAN_DEPRECATED("See comments on declaration")
870 BOTAN_DEPRECATED("See comments on declaration")
871 static
void encode(uint8_t buf[], const
BigInt& n, Base base);
880 static
BigInt decode(const uint8_t buf[],
size_t length,
889 template<typename Alloc>
894 return BigInt::decode(buf.data(), buf.size(), base);
905 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
922 static void BOTAN_DEPRECATED(
"No longer in use") const_time_lookup(
934 invalidate_sig_words();
938 const word* const_data()
const
943 secure_vector<word>& mutable_vector()
945 invalidate_sig_words();
949 const secure_vector<word>& const_vector()
const
954 word get_word_at(
size_t n)
const
961 void set_word_at(
size_t i, word w)
963 invalidate_sig_words();
964 if(i >= m_reg.size())
973 void set_words(
const word w[],
size_t len)
975 invalidate_sig_words();
976 m_reg.assign(w, w + len);
981 m_reg.resize(m_reg.capacity());
986 void set_size(
size_t s)
988 invalidate_sig_words();
990 m_reg.resize(s + (8 - (s % 8)));
993 void mask_bits(
size_t n)
995 if(n == 0) {
return set_to_zero(); }
997 const size_t top_word = n / BOTAN_MP_WORD_BITS;
1000 if(top_word < size())
1002 const word mask = (
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
1003 const size_t len = size() - (top_word + 1);
1008 m_reg[top_word] &= mask;
1009 invalidate_sig_words();
1013 void grow_to(
size_t n)
const
1017 if(n <= m_reg.capacity())
1020 m_reg.resize(n + (8 - (n % 8)));
1024 size_t size()
const {
return m_reg.size(); }
1026 void shrink_to_fit(
size_t min_size = 0)
1028 const size_t words = std::max(min_size, sig_words());
1029 m_reg.resize(words);
1032 void resize(
size_t s)
1037 void swap(Data& other)
1039 m_reg.swap(other.m_reg);
1040 std::swap(m_sig_words, other.m_sig_words);
1043 void swap(secure_vector<word>& reg)
1046 invalidate_sig_words();
1049 void invalidate_sig_words()
const
1051 m_sig_words = sig_words_npos;
1054 size_t sig_words()
const
1056 if(m_sig_words == sig_words_npos)
1058 m_sig_words = calc_sig_words();
1067 static const size_t sig_words_npos =
static_cast<size_t>(-1);
1069 size_t calc_sig_words()
const;
1071 mutable secure_vector<word> m_reg;
1072 mutable size_t m_sig_words = sig_words_npos;
1076 Sign m_signedness = Positive;
1126 {
return (a.
cmp(b) <= 0); }
1128 {
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)
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)