1 /** @file
2  *****************************************************************************
3  Declaration of bigint wrapper class around GMP's MPZ long integers.
4  *****************************************************************************
5  * @author     This file is part of libff, developed by SCIPR Lab
6  *             and contributors (see AUTHORS).
7  * @copyright  MIT license (see LICENSE file)
8  *****************************************************************************/
9 
10 #ifndef BIGINT_HPP_
11 #define BIGINT_HPP_
12 #include <cstddef>
13 #include <iostream>
14 
15 #include <gmp.h>
16 
17 #include <libff/common/serialization.hpp>
18 
19 namespace libff {
20 
21 template<mp_size_t n> class bigint;
22 template<mp_size_t n> std::ostream& operator<<(std::ostream &, const bigint<n>&);
23 template<mp_size_t n> std::istream& operator>>(std::istream &, bigint<n>&);
24 
25 /**
26  * Wrapper class around GMP's MPZ long integers. It supports arithmetic operations,
27  * serialization and randomization. Serialization is fragile, see common/serialization.hpp.
28  */
29 
30 template<mp_size_t n>
31 class bigint {
32 public:
33     static const mp_size_t N = n;
34 
35     mp_limb_t data[n] = {0};
36 
37     bigint() = default;
38     bigint(const unsigned long x); /// Initalize from a small integer
39     bigint(const char* s); /// Initialize from a string containing an integer in decimal notation
40     bigint(const mpz_t r); /// Initialize from MPZ element
41 
42     void print() const;
43     void print_hex() const;
44     bool operator==(const bigint<n>& other) const;
45     bool operator!=(const bigint<n>& other) const;
46     void clear();
47     bool is_zero() const;
max_bits() const48     size_t max_bits() const { return n * GMP_NUMB_BITS; } /// Returns the number of bits representable by this bigint type
49     size_t num_bits() const; /// Returns the number of bits in this specific bigint value, i.e., position of the most-significant 1
50 
51     unsigned long as_ulong() const; /// Return the last limb of the integer
52     void to_mpz(mpz_t r) const;
53     bool test_bit(const std::size_t bitno) const;
54 
55     bigint& randomize();
56 
57     friend std::ostream& operator<< <n>(std::ostream &out, const bigint<n> &b);
58     friend std::istream& operator>> <n>(std::istream &in, bigint<n> &b);
59 };
60 
61 } // libff
62 #include <libff/algebra/fields/bigint.tcc>
63 #endif
64