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 };
50 static BigInt one() {
return BigInt::from_word(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(std::string_view 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);
124 static BigInt from_bytes_with_max_bits(
const uint8_t buf[],
size_t length,
142 static BigInt with_capacity(
size_t n);
176 m_data.swap(other.m_data);
177 std::swap(m_signedness, other.m_signedness);
192 return add(
y.data(),
y.sig_words(),
y.sign());
201 return add(&
y, 1, Positive);
210 return sub(
y.data(),
y.sig_words(),
y.sign());
219 return sub(&
y, 1, Positive);
250 word operator%=(word
y);
256 BigInt& operator<<=(
size_t shift);
262 BigInt& operator>>=(
size_t shift);
294 bool operator !()
const {
return (!is_nonzero()); }
296 static BigInt add2(
const BigInt& x,
const word
y[],
size_t y_words, Sign y_sign);
298 BigInt& add(
const word
y[],
size_t y_words, Sign sign);
302 return add(
y, y_words, sign == Positive ? Negative : Positive);
379 void clear() { m_data.set_to_zero(); m_signedness = Positive; }
388 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
395 bool is_equal(
const BigInt& n)
const;
402 bool is_less_than(
const BigInt& n)
const;
410 int32_t cmp_word(word n)
const;
416 bool is_even()
const {
return (get_bit(0) == 0); }
422 bool is_odd()
const {
return (get_bit(0) == 1); }
436 return (sig_words() == 0);
445 conditionally_set_bit(n,
true);
460 m_data.set_word_at(which, word_at(which) | mask);
467 void clear_bit(
size_t n);
495 uint32_t get_substring(
size_t offset,
size_t length)
const;
511 std::string to_dec_string()
const;
524 std::string to_hex_string()
const;
530 uint8_t byte_at(
size_t n)
const;
539 return m_data.get_word_at(n);
544 m_data.set_word_at(i, w);
549 m_data.set_words(w, len);
575 if(sign() == Positive)
585 set_sign(reverse_sign());
594 if(sign == Negative && is_zero())
609 size_t size()
const {
return m_data.size(); }
617 return m_data.sig_words();
624 size_t bytes()
const;
637 size_t top_bits_free()
const;
649 const word*
data()
const {
return m_data.const_data(); }
665 void grow_to(
size_t n)
const { m_data.grow_to(n); }
667 void resize(
size_t s) { m_data.resize(s); }
686 void binary_encode(uint8_t buf[])
const;
698 void binary_encode(uint8_t buf[],
size_t len)
const;
705 void binary_decode(
const uint8_t buf[],
size_t length);
711 template<
typename Alloc>
714 binary_decode(buf.data(), buf.size());
721 void encode_words(word out[],
size_t size)
const;
727 void ct_cond_assign(
bool predicate,
const BigInt& other);
733 void ct_cond_swap(
bool predicate,
BigInt& other);
738 void ct_cond_add(
bool predicate,
const BigInt& value);
743 void cond_flip_sign(
bool predicate);
745#if defined(BOTAN_HAS_VALGRIND)
746 void const_time_poison()
const;
747 void const_time_unpoison()
const;
782 std::vector<uint8_t> output(n.
bytes());
807 return BigInt(buf, length);
815 template<
typename Alloc>
828 static BigInt decode(
const uint8_t buf[],
size_t length,
837 template<
typename Alloc>
842 return BigInt::decode(buf.data(), buf.size(), base);
853 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
871 invalidate_sig_words();
875 const word* const_data()
const
880 secure_vector<word>& mutable_vector()
882 invalidate_sig_words();
886 const secure_vector<word>& const_vector()
const
891 word get_word_at(
size_t n)
const
898 void set_word_at(
size_t i, word w)
900 invalidate_sig_words();
901 if(i >= m_reg.size())
910 void set_words(
const word w[],
size_t len)
912 invalidate_sig_words();
913 m_reg.assign(w, w + len);
918 m_reg.resize(m_reg.capacity());
923 void set_size(
size_t s)
925 invalidate_sig_words();
927 m_reg.resize(s + (8 - (s % 8)));
930 void mask_bits(
size_t n)
932 if(n == 0) {
return set_to_zero(); }
937 if(top_word < size())
940 const size_t len = size() - (top_word + 1);
945 m_reg[top_word] &= mask;
946 invalidate_sig_words();
950 void grow_to(
size_t n)
const
954 if(n <= m_reg.capacity())
957 m_reg.resize(n + (8 - (n % 8)));
961 size_t size()
const {
return m_reg.size(); }
963 void shrink_to_fit(
size_t min_size = 0)
965 const size_t words = std::max(min_size, sig_words());
969 void resize(
size_t s)
974 void swap(Data& other)
976 m_reg.swap(other.m_reg);
977 std::swap(m_sig_words, other.m_sig_words);
980 void swap(secure_vector<word>& reg)
983 invalidate_sig_words();
986 void invalidate_sig_words()
const
988 m_sig_words = sig_words_npos;
991 size_t sig_words()
const
993 if(m_sig_words == sig_words_npos)
995 m_sig_words = calc_sig_words();
1004 static const size_t sig_words_npos =
static_cast<size_t>(-1);
1006 size_t calc_sig_words()
const;
1008 mutable secure_vector<word> m_reg;
1009 mutable size_t m_sig_words = sig_words_npos;
1013 Sign m_signedness = Positive;
1059 {
return a.is_equal(b); }
1063 {
return (a.
cmp(b) <= 0); }
1065 {
return (a.
cmp(b) >= 0); }
#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 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
uint32_t to_u32bit(std::string_view str_view)
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)
OctetString operator+(const OctetString &k1, const OctetString &k2)
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)
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void clear_mem(T *ptr, size_t n)