10#define BOTAN_BIGINT_H_
12#include <botan/exceptn.h>
13#include <botan/secmem.h>
14#include <botan/types.h>
19class RandomNumberGenerator;
29 enum Base { Decimal = 10, Hexadecimal = 16, Binary = 256 };
34 enum Sign { Negative = 0, Positive = 1 };
49 static BigInt one() {
return BigInt::from_word(1); }
55 static BigInt from_u64(uint64_t n);
61 static BigInt from_word(word n);
67 static BigInt from_s32(int32_t n);
88 explicit BigInt(std::string_view str);
95 BigInt(
const uint8_t buf[],
size_t length);
101 template <
typename Alloc>
102 explicit BigInt(
const std::vector<uint8_t, Alloc>& vec) :
BigInt(vec.data(), vec.size()) {}
110 BigInt(
const uint8_t buf[],
size_t length, Base base);
123 static BigInt from_bytes_with_max_bits(
const uint8_t buf[],
size_t length,
size_t max_bits);
140 static BigInt with_capacity(
size_t n);
170 m_data.swap(other.m_data);
171 std::swap(m_signedness, other.m_signedness);
233 word operator%=(word y);
287 static BigInt add2(
const BigInt& x,
const word y[],
size_t y_words, Sign y_sign);
289 BigInt& add(
const word y[],
size_t y_words, Sign sign);
292 return add(y, y_words, sign == Positive ? Negative : Positive);
370 m_data.set_to_zero();
371 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); }
427 bool is_zero()
const {
return (sig_words() == 0); }
433 void set_bit(
size_t n) { conditionally_set_bit(n,
true); }
446 m_data.set_word_at(which, word_at(which) | mask);
453 void clear_bit(
size_t n);
475 uint32_t get_substring(
size_t offset,
size_t length)
const;
491 std::string to_dec_string()
const;
504 std::string to_hex_string()
const;
510 uint8_t byte_at(
size_t n)
const;
517 word
word_at(
size_t n)
const {
return m_data.get_word_at(n); }
521 void set_words(
const word w[],
size_t len) { m_data.set_words(w, len); }
545 if(sign() == Positive) {
561 if(sign == Negative && is_zero()) {
577 size_t size()
const {
return m_data.size(); }
589 size_t bytes()
const;
602 size_t top_bits_free()
const;
614 const word*
data()
const {
return m_data.const_data(); }
630 void grow_to(
size_t n)
const { m_data.grow_to(n); }
632 void resize(
size_t s) { m_data.resize(s); }
651 void binary_encode(uint8_t buf[])
const;
663 void binary_encode(uint8_t buf[],
size_t len)
const;
670 void binary_decode(
const uint8_t buf[],
size_t length);
676 template <
typename Alloc>
678 binary_decode(buf.data(), buf.size());
685 void encode_words(word out[],
size_t size)
const;
691 void ct_cond_assign(
bool predicate,
const BigInt& other);
697 void ct_cond_swap(
bool predicate,
BigInt& other);
702 void ct_cond_add(
bool predicate,
const BigInt& value);
707 void cond_flip_sign(
bool predicate);
709#if defined(BOTAN_HAS_VALGRIND)
710 void const_time_poison()
const;
711 void const_time_unpoison()
const;
743 std::vector<uint8_t> output(n.
bytes());
772 template <
typename Alloc>
784 static BigInt decode(
const uint8_t buf[],
size_t length, Base base);
792 template <
typename Alloc>
797 return BigInt::decode(buf.data(), buf.size(), base);
807 static void encode_1363(std::span<uint8_t> out,
const BigInt& n);
809 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
823 word* mutable_data() {
824 invalidate_sig_words();
828 const word* const_data()
const {
return m_reg.data(); }
830 secure_vector<word>& mutable_vector() {
831 invalidate_sig_words();
835 const secure_vector<word>& const_vector()
const {
return m_reg; }
837 word get_word_at(
size_t n)
const {
838 if(n < m_reg.size()) {
844 void set_word_at(
size_t i, word w) {
845 invalidate_sig_words();
846 if(i >= m_reg.size()) {
855 void set_words(
const word w[],
size_t len) {
856 invalidate_sig_words();
857 m_reg.assign(w, w + len);
861 m_reg.resize(m_reg.capacity());
866 void set_size(
size_t s) {
867 invalidate_sig_words();
869 m_reg.resize(s + (8 - (s % 8)));
872 void mask_bits(
size_t n) {
874 return set_to_zero();
880 if(top_word < size()) {
882 const size_t len = size() - (top_word + 1);
886 m_reg[top_word] &= mask;
887 invalidate_sig_words();
891 void grow_to(
size_t n)
const {
893 if(n <= m_reg.capacity()) {
896 m_reg.resize(n + (8 - (n % 8)));
901 size_t size()
const {
return m_reg.size(); }
903 void shrink_to_fit(
size_t min_size = 0) {
904 const size_t words = std::max(min_size, sig_words());
908 void resize(
size_t s) { m_reg.resize(s); }
910 void swap(Data& other) {
911 m_reg.swap(other.m_reg);
912 std::swap(m_sig_words, other.m_sig_words);
915 void swap(secure_vector<word>& reg) {
917 invalidate_sig_words();
920 void invalidate_sig_words()
const { m_sig_words = sig_words_npos; }
922 size_t sig_words()
const {
923 if(m_sig_words == sig_words_npos) {
924 m_sig_words = calc_sig_words();
932 static const size_t sig_words_npos =
static_cast<size_t>(-1);
934 size_t calc_sig_words()
const;
936 mutable secure_vector<word> m_reg;
937 mutable size_t m_sig_words = sig_words_npos;
941 Sign m_signedness = Positive;
993 return (a.
cmp(b) <= 0);
997 return (a.
cmp(b) >= 0);
1035BOTAN_PUBLIC_API(2, 0) std::ostream& operator<<(std::ostream&, const BigInt&);
#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)
friend void swap(BigInt &x, 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 &)
constexpr auto operator>>=(Strong< T1, Tags... > &a, T2 b)
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)
constexpr auto operator/=(Strong< T1, Tags... > &a, T2 b)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
constexpr auto operator<<=(Strong< T1, Tags... > &a, T2 b)
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)
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)