10#define BOTAN_BIGINT_H_
12#include <botan/exceptn.h>
13#include <botan/secmem.h>
14#include <botan/types.h>
60 static BigInt from_u64(uint64_t n);
73 BOTAN_DEPRECATED(
"Use BigInt::from_u64 plus negation if required instead") static
BigInt from_s32(int32_t n);
97 explicit
BigInt(std::string_view str);
117 BigInt(const uint8_t buf[],
size_t length) { assign_from_bytes(std::span{buf, length}); }
131 BOTAN_DEPRECATED(
"For hex/decimal use from_string")
BigInt(const uint8_t buf[],
size_t length, Base base);
145 static
BigInt from_bytes_with_max_bits(const uint8_t buf[],
size_t length,
size_t max_bits);
192 m_data.swap(other.m_data);
193 std::swap(m_signedness, other.m_signedness);
310 static BigInt add2(
const BigInt& x,
const word y[],
size_t y_words, Sign y_sign);
313 BigInt& add(
const word y[],
size_t y_words, Sign sign);
400 m_data.set_to_zero();
411 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
418 bool is_equal(
const BigInt& n)
const;
425 bool is_less_than(
const BigInt& n)
const;
433 int32_t cmp_word(
word n)
const;
474 const size_t which = n / (
sizeof(
word) * 8);
475 const word mask =
static_cast<word>(set_it) << (n % (
sizeof(
word) * 8));
476 m_data.set_word_at(which,
word_at(which) | mask);
483 void clear_bit(
size_t n);
505 BOTAN_DEPRECATED(
"Deprecated no replacement") uint32_t get_substring(
size_t offset,
size_t length) const;
521 std::
string to_dec_string() const;
534 std::
string to_hex_string() const;
540 uint8_t byte_at(
size_t n) const;
552 m_data.set_words(w, len);
609 size_t size()
const {
return m_data.size(); }
621 size_t bytes()
const;
652 return m_data.mutable_vector();
659 return m_data.const_vector();
701 void serialize_to(std::span<uint8_t> out)
const;
710 template <
typename T = std::vector<u
int8_t>>
723 template <
typename T = std::vector<u
int8_t>>
750 BOTAN_DEPRECATED(
"Use BigInt::serialize_to") void binary_encode(uint8_t buf[],
size_t len) const;
758 this->assign_from_bytes(std::span{buf, length});
766 this->assign_from_bytes(buf);
796 BOTAN_DEPRECATED(
"Deprecated no replacement")
void ct_shift_left(
size_t shift);
801 void cond_flip_sign(
bool predicate);
832 return n.serialize<std::vector<uint8_t>>(n.bytes());
849 static BigInt from_bytes(std::span<const uint8_t> bytes);
878 static
BigInt decode(const uint8_t buf[],
size_t length, Base base);
886 BOTAN_DEPRECATED(
"For decimal/hex use from_string") static
BigInt decode(std::span<const uint8_t> buf, Base base);
905 n.serialize_to(std::span{out,
bytes});
936 const word*
_data()
const {
return m_data.const_data(); }
957 bn.m_data.swap(words);
967 void _const_time_poison()
const;
975 void _const_time_unpoison()
const;
982 void assign_from_bytes(std::span<const uint8_t> bytes);
986 word* mutable_data() {
987 invalidate_sig_words();
991 const word* const_data()
const {
return m_reg.data(); }
993 std::span<const word> const_span()
const {
return std::span{m_reg}; }
995 secure_vector<word>& mutable_vector() {
996 invalidate_sig_words();
1000 const secure_vector<word>& const_vector()
const {
return m_reg; }
1002 word get_word_at(
size_t n)
const {
1003 if(n < m_reg.size()) {
1009 void set_word_at(
size_t i, word w) {
1010 invalidate_sig_words();
1011 if(i >= m_reg.size()) {
1020 void set_words(
const word w[],
size_t len) {
1021 invalidate_sig_words();
1022 m_reg.assign(w, w + len);
1027 void mask_bits(
size_t n);
1029 void grow_to(
size_t n)
const {
1031 if(n <= m_reg.capacity()) {
1034 m_reg.resize(n + (8 - (n % 8)));
1039 size_t size()
const {
return m_reg.size(); }
1041 void shrink_to_fit(
size_t min_size = 0) {
1042 const size_t words = std::max(min_size, sig_words());
1043 m_reg.resize(words);
1046 void resize(
size_t s) { m_reg.resize(s); }
1048 void swap(Data& other) {
1049 m_reg.swap(other.m_reg);
1050 std::swap(m_sig_words, other.m_sig_words);
1053 void swap(secure_vector<word>& reg) {
1055 invalidate_sig_words();
1058 void invalidate_sig_words()
const { m_sig_words = sig_words_npos; }
1060 size_t sig_words()
const {
1061 if(m_sig_words == sig_words_npos) {
1062 m_sig_words = calc_sig_words();
1068 static const size_t sig_words_npos =
static_cast<size_t>(-1);
1070 size_t calc_sig_words()
const;
1072 mutable secure_vector<word> m_reg;
1073 mutable size_t m_sig_words = sig_words_npos;
1077 Sign m_signedness = Positive;
1121 return a.is_equal(b);
1129 return (a.
cmp(b) <= 0);
1133 return (a.
cmp(b) >= 0);
1172BOTAN_PUBLIC_API(2, 0) std::ostream& operator<<(std::ostream&, const BigInt&);
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_DEPRECATED(msg)
void binary_decode(const uint8_t buf[], size_t length)
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)
void grow_to(size_t n) const
Sign reverse_sign() 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
static BigInt _from_words(secure_vector< word > &&words)
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)
int32_t cmp_word(word n) const
static BigInt from_string(std::string_view str)
void _const_time_unpoison() const
void serialize_to(std::span< uint8_t > out) const
static BigInt from_bytes(std::span< const uint8_t > bytes)
static std::vector< uint8_t > encode(const BigInt &n)
void _const_time_poison() const
static BigInt power_of_2(size_t n)
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
BigInt & operator-=(word y)
BigInt & operator+=(const BigInt &y)
static BigInt from_u64(uint64_t n)
const word * _data() const
void const_time_poison() const
BigInt & add(const word y[], size_t y_words, Sign sign)
BigInt & operator=(const BigInt &)=default
void _assign_from_bytes(std::span< const uint8_t > bytes)
BigInt(std::span< const uint8_t > bytes)
void const_time_unpoison() const
bool get_bit(size_t n) const
T serialize(size_t len) const
void swap_reg(secure_vector< word > ®)
std::span< const word > _as_span() const
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
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)