63 size_t size = (mpz_sizeinbase (range.
bn, 2) + CHAR_BIT-1) / CHAR_BIT;
64 std::vector<unsigned char> buf(size);
70 mpz_neg(ret.
bn, ret.
bn);
80 return mpz_sizeinbase(
bn, 2);
90 return mpz_get_ui(
bn);
95 return mpz_get_ui(
bn);
102 return (n > (
unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
104 return (n > (
unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(
int)n);
110 mpz_import(
bn, n.
size(), -1, 1, 0, 0, (
unsigned char*)&n);
116 throw std::range_error(
"cannot convert to uint256, bignum longer than 256 bits");
119 mpz_export((
unsigned char*)&n,
nullptr, -1, 1, 0, 0,
bn);
125 std::vector<unsigned char> vch2 = vch;
126 unsigned char sign = 0;
127 if (vch2.size() > 0) {
128 sign = vch2[vch2.size()-1] & 0x80;
129 vch2[vch2.size()-1] = vch2[vch2.size()-1] & 0x7f;
130 mpz_import(
bn, vch2.size(), -1, 1, 0, 0, vch2.data());
142 return std::vector<unsigned char>(0);
144 size_t size = (mpz_sizeinbase (
bn, 2) + CHAR_BIT-1) / CHAR_BIT;
146 return std::vector<unsigned char>();
147 std::vector<unsigned char> v(size + 1);
148 mpz_export(v.data(), &size, -1, 1, 0, 0,
bn);
149 if (v[v.size()-2] & 0x80) {
151 v[v.size()-1] = 0x80;
153 v[v.size()-1] = 0x00;
158 v[v.size()-1] |= 0x80;
166 const char* psz = str.c_str();
167 mpz_set_str(
bn, psz, 10);
172 const char* psz = str.c_str();
173 int ret = 1 + mpz_set_str(
bn, psz, 16);
179 char* c_str = mpz_get_str(
nullptr, nBase,
bn);
180 std::string str(c_str);
182 void (*freefunc)(
void *, size_t);
183 mp_get_memory_functions (
nullptr,
nullptr, &freefunc);
184 freefunc(c_str, strlen(c_str) + 1);
197 long unsigned int ei = mpz_get_ui (e.
bn);
198 mpz_pow_ui(ret.
bn,
bn, ei);
210 mpz_mul (ret.
bn,
bn, b.
bn);
211 mpz_mod (ret.
bn, ret.
bn, m.
bn);
224 mpz_powm_sec (ret.
bn,
bn, e.
bn, m.
bn);
239 mpz_invert(ret.
bn,
bn, m.
bn);
251 mpz_gcd(ret.
bn,
bn, b.
bn);
263 int ret = mpz_probab_prime_p(
bn, checks);
297 mpz_mul_2exp(
bn,
bn, shift);
303 mpz_div_2exp(
bn,
bn, shift);
arith_uint256 UintToArith256(const uint256 &a)
const CBigNum BN_ZERO
constant bignum instances
std::vector< unsigned char > getvch() const
unsigned long getulong() const
std::string ToString(int nBase=10) const
void SetDec(const std::string &str)
CBigNum & operator*=(const CBigNum &b)
CBigNum & operator+=(const CBigNum &b)
CBigNum()
C++ wrapper for BIGNUM (Gmp bignum)
bool SetHexBool(const std::string &str)
CBigNum & operator/=(const CBigNum &b)
CBigNum & operator<<=(unsigned int shift)
CBigNum gcd(const CBigNum &b) const
Calculates the greatest common divisor (GCD) of two numbers.
unsigned int getuint() const
CBigNum & operator%=(const CBigNum &b)
bool isPrime(const int checks=15) const
Miller-Rabin primality test on this element.
std::string GetHex() const
arith_uint256 getuint256() const
CBigNum pow_mod(const CBigNum &e, const CBigNum &m) const
modular exponentiation: this^e mod n
std::string GetDec() const
void SetHex(const std::string &str)
CBigNum & operator=(const CBigNum &b)
static CBigNum randBignum(const CBigNum &range)
Generates a cryptographically secure random number between zero and range exclusive i....
CBigNum pow(const int e) const
exponentiation with an int.
void setvch(const std::vector< unsigned char > &vch)
int bitSize() const
Returns the size in bits of the underlying bignum.
CBigNum mul_mod(const CBigNum &b, const CBigNum &m) const
modular multiplication: (this * b) mod m
CBigNum & operator-=(const CBigNum &b)
void setulong(unsigned long n)
CBigNum inverse(const CBigNum &m) const
Calculates the inverse of this element mod m.
void setuint256(uint256 n)
CBigNum & operator>>=(unsigned int shift)
256-bit unsigned big integer.
unsigned int size() const
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
const uint256 UINT256_ZERO
constant uint256 instances