9 #ifndef BOTAN_BIGINT_H__
10 #define BOTAN_BIGINT_H__
12 #include <botan/rng.h>
13 #include <botan/secmem.h>
14 #include <botan/mp_types.h>
15 #include <botan/loadstor.h>
29 enum Base { Decimal = 10, Hexadecimal = 16, Binary = 256 };
34 enum Sign { Negative = 0, Positive = 1 };
66 BigInt(
const std::string& str);
74 BigInt(
const uint8_t buf[],
size_t length,
Base base = Binary);
124 m_reg.swap(other.m_reg);
125 std::swap(m_signedness, other.m_signedness);
167 word operator%=(word y);
173 BigInt& operator<<=(
size_t shift);
179 BigInt& operator>>=(
size_t shift);
211 bool operator !()
const {
return (!is_nonzero()); }
226 int32_t cmp(
const BigInt& n,
bool check_signs =
true)
const;
232 bool is_even()
const {
return (get_bit(0) == 0); }
238 bool is_odd()
const {
return (get_bit(0) == 1); }
252 const size_t sw = sig_words();
254 for(
size_t i = 0; i != sw; ++i)
264 void set_bit(
size_t n);
270 void clear_bit(
size_t n);
278 if(n == 0) { clear();
return; }
280 const size_t top_word = n / BOTAN_MP_WORD_BITS;
281 const word mask = (
static_cast<word
>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
283 if(top_word < size())
285 const size_t len = size() - (top_word + 1);
290 m_reg[top_word] &= mask;
301 return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
311 uint32_t get_substring(
size_t offset,
size_t length)
const;
326 return get_byte(
sizeof(word) - (n %
sizeof(word)) - 1,
327 word_at(n /
sizeof(word)));
336 {
return ((n < size()) ? m_reg[n] : 0); }
365 Sign reverse_sign()
const;
376 void set_sign(Sign sign);
387 size_t size()
const {
return m_reg.size(); }
395 const word* x = m_reg.data();
396 size_t sig = m_reg.size();
398 while(sig && (x[sig-1] == 0))
407 size_t bytes()
const;
425 const word*
data()
const {
return m_reg.data(); }
434 void grow_to(
size_t n);
453 void binary_encode(uint8_t buf[])
const;
460 void binary_decode(
const uint8_t buf[],
size_t length);
468 binary_decode(buf.data(), buf.size());
475 size_t encoded_size(Base base = Binary)
const;
505 static std::vector<uint8_t>
encode(
const BigInt& n, Base base = Binary);
523 static void encode(uint8_t buf[],
const BigInt& n, Base base = Binary);
532 static BigInt decode(
const uint8_t buf[],
size_t length,
567 static void encode_1363(uint8_t out[],
size_t bytes,
const BigInt& n);
580 Sign m_signedness = Positive;
586 BigInt BOTAN_DLL
operator+(
const BigInt& x,
const BigInt& y);
587 BigInt BOTAN_DLL
operator-(
const BigInt& x,
const BigInt& y);
588 BigInt BOTAN_DLL
operator*(
const BigInt& x,
const BigInt& y);
589 BigInt BOTAN_DLL
operator/(
const BigInt& x,
const BigInt& d);
590 BigInt BOTAN_DLL
operator%(
const BigInt& x,
const BigInt& m);
591 word BOTAN_DLL
operator%(
const BigInt& x, word m);
592 BigInt BOTAN_DLL
operator<<(
const BigInt& x,
size_t n);
593 BigInt BOTAN_DLL
operator>>(
const BigInt& x,
size_t n);
599 {
return (a.
cmp(b) == 0); }
601 {
return (a.
cmp(b) != 0); }
603 {
return (a.
cmp(b) <= 0); }
605 {
return (a.
cmp(b) >= 0); }
607 {
return (a.
cmp(b) < 0); }
609 {
return (a.
cmp(b) > 0); }
614 BOTAN_DLL std::ostream&
operator<<(std::ostream&,
const BigInt&);
615 BOTAN_DLL std::istream&
operator>>(std::istream&, BigInt&);
uint32_t to_u32bit(const std::string &str)
word word_at(size_t n) const
bool operator>=(const X509_Time &t1, const X509_Time &t2)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
bool operator>(const X509_Time &t1, const X509_Time &t2)
std::istream & operator>>(std::istream &in, X509_DN &dn)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
void clear_mem(T *ptr, size_t n)
std::ostream & operator<<(std::ostream &out, const X509_DN &dn)
secure_vector< word > & get_word_vector()
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
static BigInt decode(const std::vector< uint8_t > &buf, Base base=Binary)
void swap_reg(secure_vector< word > ®)
static BigInt decode(const secure_vector< uint8_t > &buf, Base base=Binary)
bool operator<(const OID &a, const OID &b)
OID operator+(const OID &oid, uint32_t component)
BigInt operator%(const BigInt &n, const BigInt &mod)
int32_t cmp(const BigInt &n, bool check_signs=true) const
BigInt abs(const BigInt &n)
std::vector< T, secure_allocator< T >> secure_vector
void binary_decode(const secure_vector< uint8_t > &buf)
const secure_vector< word > & get_word_vector() const
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
const word * data() const
BigInt operator*(const BigInt &x, const BigInt &y)
static BigInt power_of_2(size_t n)
void set_word_at(size_t i, word w)
uint8_t get_byte(size_t byte_num, T input)
BigInt & operator=(BigInt &&other)
bool operator<=(const X509_Time &t1, const X509_Time &t2)
bool get_bit(size_t n) const
BigInt operator-(const BigInt &x, const BigInt &y)
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
uint8_t byte_at(size_t n) const
BigInt operator/(const BigInt &x, const BigInt &y)
void zeroise(std::vector< T, Alloc > &vec)