]> Git Repo - VerusCoin.git/blob - src/snark/src/algebra/fields/bigint.hpp
ff00dd5cf4a19e30573c4fb22c441ea7a39d553f
[VerusCoin.git] / src / snark / src / algebra / fields / bigint.hpp
1 /** @file
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  *****************************************************************************/
9
10 #ifndef BIGINT_HPP_
11 #define BIGINT_HPP_
12 #include <cstddef>
13 #include <iostream>
14 #include <gmp.h>
15 #include "common/serialization.hpp"
16
17 namespace libsnark {
18
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>&);
22
23 /**
24  * Wrapper class around GMP's MPZ long integers. It supports arithmetic operations,
25  * serialization and randomization. Serialization is fragile, see common/serialization.hpp.
26  */
27
28 template<mp_size_t n>
29 class bigint {
30 public:
31     static const mp_size_t N = n;
32
33     mp_limb_t data[n] = {0};
34
35     bigint() = default;
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
39
40     void print() const;
41     void print_hex() const;
42     bool operator==(const bigint<n>& other) const;
43     bool operator!=(const bigint<n>& other) const;
44     void clear();
45     bool is_zero() const;
46     size_t max_bits() const { return n * GMP_NUMB_BITS; }
47     size_t num_bits() const;
48
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;
52
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;
58
59     inline void limit(const bigint<n>& q, const char *msg) const;
60     bool operator>(const bigint<n>& other) const;
61
62     bigint& randomize();
63
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);
66 };
67
68 } // libsnark
69 #include "algebra/fields/bigint.tcc"
70 #endif
This page took 0.032273 seconds and 2 git commands to generate.