10#define BOTAN_BIGINT_H_
12#include <botan/exceptn.h>
13#include <botan/mem_ops.h>
14#include <botan/secmem.h>
15#include <botan/types.h>
21class RandomNumberGenerator;
40 enum Sign { Negative = 0, Positive = 1 };
61 static BigInt from_u64(uint64_t n);
68 static BigInt from_word(word n);
74 BOTAN_DEPRECATED(
"Use BigInt::from_u64 plus negation if required instead") static
BigInt from_s32(int32_t n);
98 explicit
BigInt(std::string_view str);
111 static
BigInt from_string(std::string_view str);
118 BigInt(const uint8_t buf[],
size_t length) { assign_from_bytes(std::span{buf, length}); }
124 explicit BigInt(std::span<const uint8_t> bytes) { assign_from_bytes(bytes); }
132 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);
255 word operator%=(word y);
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);
317 return add(y, y_words, sign == Positive ? Negative : Positive);
400 m_data.set_to_zero();
401 m_signedness = Positive;
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;
439 bool is_even()
const {
return (get_bit(0) == 0); }
445 bool is_odd()
const {
return (get_bit(0) == 1); }
457 bool is_zero()
const {
return (sig_words() == 0); }
463 void set_bit(
size_t n) { conditionally_set_bit(n,
true); }
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;
512 uint32_t to_u32bit() const;
521 std::
string to_dec_string() const;
534 std::
string to_hex_string() const;
540 uint8_t byte_at(
size_t n) const;
547 word word_at(
size_t n)
const {
return m_data.get_word_at(n); }
549 BOTAN_DEPRECATED(
"Deprecated no replacement") void set_word_at(
size_t i, word w) { m_data.set_word_at(i, w); }
552 m_data.set_words(w, len);
577 if(sign() == Positive) {
593 if(sign == Negative && is_zero()) {
609 size_t size()
const {
return m_data.size(); }
621 size_t bytes()
const;
640 BOTAN_DEPRECATED("Deprecated no replacement") word* mutable_data() {
return m_data.mutable_data(); }
646 BOTAN_DEPRECATED(
"Deprecated no replacement") const word* data()
const {
return m_data.const_data(); }
652 return m_data.mutable_vector();
659 return m_data.const_vector();
666 BOTAN_DEPRECATED(
"Deprecated no replacement") void grow_to(
size_t n)
const { m_data.grow_to(n); }
701 void serialize_to(std::span<uint8_t> out)
const;
710 template <
typename T = std::vector<u
int8_t>>
716 this->serialize_to(out);
723 template <
typename T = std::vector<u
int8_t>>
725 return serialize<T>(this->bytes());
733 this->serialize_to(std::span{buf, this->bytes()});
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});
765 BOTAN_DEPRECATED(
"Use BigInt::from_bytes") void binary_decode(std::span<const uint8_t> buf) {
766 this->assign_from_bytes(buf);
773 BOTAN_DEPRECATED(
"Deprecated no replacement") void encode_words(word out[],
size_t size) const;
779 void ct_cond_assign(
bool predicate, const
BigInt& other);
785 void ct_cond_swap(
bool predicate,
BigInt& other);
790 void ct_cond_add(
bool predicate, const
BigInt& value);
796 void ct_shift_left(
size_t shift);
801 void cond_flip_sign(
bool predicate);
803#if defined(BOTAN_HAS_VALGRIND)
804 void const_time_poison()
const;
805 void const_time_unpoison()
const;
837 return n.
serialize<std::vector<uint8_t>>(n.bytes());
854 static BigInt from_bytes(std::span<const uint8_t> bytes);
863 return BigInt::from_bytes(std::span{buf, length});
872 return BigInt::from_bytes(buf);
883 static
BigInt decode(const uint8_t buf[],
size_t length, Base base);
891 BOTAN_DEPRECATED("For decimal/hex use from_string") static
BigInt decode(std::span<const uint8_t> buf, Base base);
909 static
void encode_1363(uint8_t out[],
size_t bytes, const
BigInt& n) {
910 n.serialize_to(std::span{out, bytes});
931 const word* _data()
const {
return m_data.const_data(); }
949 void assign_from_bytes(std::span<const uint8_t> bytes);
953 word* mutable_data() {
954 invalidate_sig_words();
958 const word* const_data()
const {
return m_reg.data(); }
960 secure_vector<word>& mutable_vector() {
961 invalidate_sig_words();
965 const secure_vector<word>& const_vector()
const {
return m_reg; }
967 word get_word_at(
size_t n)
const {
968 if(n < m_reg.size()) {
974 void set_word_at(
size_t i, word w) {
975 invalidate_sig_words();
976 if(i >= m_reg.size()) {
985 void set_words(
const word w[],
size_t len) {
986 invalidate_sig_words();
987 m_reg.assign(w, w + len);
991 m_reg.resize(m_reg.capacity());
996 void set_size(
size_t s) {
997 invalidate_sig_words();
999 m_reg.resize(s + (8 - (s % 8)));
1002 void mask_bits(
size_t n) {
1004 return set_to_zero();
1010 if(top_word < size()) {
1012 const size_t len = size() - (top_word + 1);
1016 m_reg[top_word] &= mask;
1017 invalidate_sig_words();
1021 void grow_to(
size_t n)
const {
1023 if(n <= m_reg.capacity()) {
1026 m_reg.resize(n + (8 - (n % 8)));
1031 size_t size()
const {
return m_reg.size(); }
1033 void shrink_to_fit(
size_t min_size = 0) {
1034 const size_t words = std::max(min_size, sig_words());
1035 m_reg.resize(words);
1038 void resize(
size_t s) { m_reg.resize(s); }
1040 void swap(Data& other) {
1041 m_reg.swap(other.m_reg);
1042 std::swap(m_sig_words, other.m_sig_words);
1045 void swap(secure_vector<word>& reg) {
1047 invalidate_sig_words();
1050 void invalidate_sig_words()
const { m_sig_words = sig_words_npos; }
1052 size_t sig_words()
const {
1053 if(m_sig_words == sig_words_npos) {
1054 m_sig_words = calc_sig_words();
1062 static const size_t sig_words_npos =
static_cast<size_t>(-1);
1064 size_t calc_sig_words()
const;
1066 mutable secure_vector<word> m_reg;
1067 mutable size_t m_sig_words = sig_words_npos;
1071 Sign m_signedness = Positive;
1123 return (a.
cmp(b) <= 0);
1127 return (a.
cmp(b) >= 0);
1166BOTAN_PUBLIC_API(2, 0) std::ostream& operator<<(std::ostream&, const BigInt&);
#define BOTAN_DEBUG_ASSERT(expr)
void conditionally_set_bit(size_t n, bool set_it)
bool is_equal(const BigInt &n) const
BigInt & sub(const word y[], size_t y_words, Sign sign)
Sign reverse_sign() const
static BigInt add2(const BigInt &x, const word y[], size_t y_words, Sign y_sign)
bool is_less_than(const BigInt &n) const
int32_t cmp(const BigInt &n, bool check_signs=true) const
BigInt & operator+=(word y)
BigInt & operator=(BigInt &&other)
BigInt & operator-=(const BigInt &y)
friend void swap(BigInt &x, BigInt &y)
int32_t cmp_word(word n) const
static BigInt power_of_2(size_t n)
BigInt & operator-=(word y)
BigInt & operator+=(const BigInt &y)
const word * _data() const
void const_time_poison() const
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
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_DEPRECATED(msg)
#define BOTAN_MP_WORD_BITS
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)
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)