2 *****************************************************************************
3 Declaration of bigint wrapper class around GMP's MPZ long integers.
4 *****************************************************************************
5 * @author This file is part of libsnark, developed by SCIPR Lab
6 * and contributors (see AUTHORS).
7 * @copyright MIT license (see LICENSE file)
8 *****************************************************************************/
15 #include "common/serialization.hpp"
19 template<mp_size_t n> class bigint;
20 template<mp_size_t n> std::ostream& operator<<(std::ostream &, const bigint<n>&);
21 template<mp_size_t n> std::istream& operator>>(std::istream &, bigint<n>&);
24 * Wrapper class around GMP's MPZ long integers. It supports arithmetic operations,
25 * serialization and randomization. Serialization is fragile, see common/serialization.hpp.
31 static const mp_size_t N = n;
33 mp_limb_t data[n] = {0};
36 bigint(const unsigned long x); /// Initalize from a small integer
37 bigint(const char* s); /// Initialize from a string containing an integer in decimal notation
38 bigint(const mpz_t r); /// Initialize from MPZ element
41 void print_hex() const;
42 bool operator==(const bigint<n>& other) const;
43 bool operator!=(const bigint<n>& other) const;
46 size_t max_bits() const { return n * GMP_NUMB_BITS; }
47 size_t num_bits() const;
49 unsigned long as_ulong() const; /* return the last limb of the integer */
50 void to_mpz(mpz_t r) const;
51 bool test_bit(const std::size_t bitno) const;
53 template<mp_size_t m> inline void operator+=(const bigint<m>& other);
54 template<mp_size_t m> inline bigint<n+m> operator*(const bigint<m>& other) const;
55 template<mp_size_t d> static inline void div_qr(bigint<n-d+1>& quotient, bigint<d>& remainder,
56 const bigint<n>& dividend, const bigint<d>& divisor);
57 template<mp_size_t m> inline bigint<m> shorten(const bigint<m>& q, const char *msg) const;
59 inline void limit(const bigint<n>& q, const char *msg) const;
60 bool operator>(const bigint<n>& other) const;
64 friend std::ostream& operator<< <n>(std::ostream &out, const bigint<n> &b);
65 friend std::istream& operator>> <n>(std::istream &in, bigint<n> &b);
69 #include "algebra/fields/bigint.tcc"