123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
- #ifndef ECOIN_BIGNUM_H
- #define ECOIN_BIGNUM_H
- #include <stdexcept>
- #include <vector>
- #include <openssl/bn.h>
- #include "util.h" // for uint64
- /** Errors thrown by the bignum class */
- class bignum_error : public std::runtime_error
- {
- public:
- explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
- };
- /** RAII encapsulated BN_CTX (OpenSSL bignum context) */
- class CAutoBN_CTX
- {
- protected:
- BN_CTX* pctx;
- BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
- public:
- CAutoBN_CTX()
- {
- pctx = BN_CTX_new();
- if (pctx == NULL)
- throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
- }
- ~CAutoBN_CTX()
- {
- if (pctx != NULL)
- BN_CTX_free(pctx);
- }
- operator BN_CTX*() { return pctx; }
- BN_CTX& operator*() { return *pctx; }
- BN_CTX** operator&() { return &pctx; }
- bool operator!() { return (pctx == NULL); }
- };
- /** C++ wrapper for BIGNUM (OpenSSL bignum) */
- class CBigNum
- {
- protected:
- BIGNUM *bn;
- void init()
- {
- bn = BN_new();
- }
- public:
- CBigNum()
- {
- init();
- }
- CBigNum(const CBigNum& b)
- {
- init();
- if (!BN_copy(bn, &b))
- {
- BN_clear_free(bn);
- throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
- }
- }
- CBigNum& operator=(const CBigNum& b)
- {
- if (!BN_copy(bn, &b))
- throw bignum_error("CBigNum::operator= : BN_copy failed");
- return (*this);
- }
- ~CBigNum()
- {
- BN_clear_free(bn);
- }
- BIGNUM *operator &() const
- {
- return bn;
- }
- //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'.
- CBigNum(signed char n) { init(); if (n >= 0) setulong(n); else setint64(n); }
- CBigNum(short n) { init(); if (n >= 0) setulong(n); else setint64(n); }
- CBigNum(int n) { init(); if (n >= 0) setulong(n); else setint64(n); }
- CBigNum(long n) { init(); if (n >= 0) setulong(n); else setint64(n); }
- CBigNum(int64 n) { init(); setint64(n); }
- CBigNum(unsigned char n) { init(); setulong(n); }
- CBigNum(unsigned short n) { init(); setulong(n); }
- CBigNum(unsigned int n) { init(); setulong(n); }
- CBigNum(unsigned long n) { init(); setulong(n); }
- CBigNum(uint64 n) { init(); setuint64(n); }
- explicit CBigNum(uint256 n) { init(); setuint256(n); }
- explicit CBigNum(const std::vector<unsigned char>& vch)
- {
- init();
- setvch(vch);
- }
- /** Generates a cryptographically secure random number between zero and range exclusive
- * i.e. 0 < returned number < range
- * @param range The upper bound on the number.
- * @return
- */
- static CBigNum randBignum(const CBigNum& range) {
- CBigNum ret;
- if(!BN_rand_range(&ret, &range)){
- throw bignum_error("CBigNum:rand element : BN_rand_range failed");
- }
- return ret;
- }
- /** Generates a cryptographically secure random k-bit number
- * @param k The bit length of the number.
- * @return
- */
- static CBigNum RandKBitBigum(const uint32_t k){
- CBigNum ret;
- if(!BN_rand(&ret, k, -1, 0)){
- throw bignum_error("CBigNum:rand element : BN_rand failed");
- }
- return ret;
- }
- /**Returns the size in bits of the underlying bignum.
- *
- * @return the size
- */
- int bitSize() const{
- return BN_num_bits(bn);
- }
- void setulong(unsigned long n)
- {
- if (!BN_set_word(bn, n))
- throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
- }
- unsigned long getulong() const
- {
- return BN_get_word(bn);
- }
- unsigned int getuint() const
- {
- return BN_get_word(bn);
- }
- int getint() const
- {
- unsigned long n = BN_get_word(bn);
- if (!BN_is_negative(bn))
- return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
- else
- return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
- }
- void setint64(int64_t sn)
- {
- unsigned char pch[sizeof(sn) + 6];
- unsigned char* p = pch + 4;
- bool fNegative;
- uint64_t n;
- if (sn < (int64_t)0)
- {
- // Since the minimum signed integer cannot be represented as positive so long as its type is signed, and it's not well-defined what happens if you make it unsigned before negating it, we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
- n = -(sn + 1);
- ++n;
- fNegative = true;
- } else {
- n = sn;
- fNegative = false;
- }
- bool fLeadingZeroes = true;
- for (int i = 0; i < 8; i++)
- {
- unsigned char c = (n >> 56) & 0xff;
- n <<= 8;
- if (fLeadingZeroes)
- {
- if (c == 0)
- continue;
- if (c & 0x80)
- *p++ = (fNegative ? 0x80 : 0);
- else if (fNegative)
- c |= 0x80;
- fLeadingZeroes = false;
- }
- *p++ = c;
- }
- unsigned int nSize = p - (pch + 4);
- pch[0] = (nSize >> 24) & 0xff;
- pch[1] = (nSize >> 16) & 0xff;
- pch[2] = (nSize >> 8) & 0xff;
- pch[3] = (nSize) & 0xff;
- BN_mpi2bn(pch, p - pch, bn);
- }
- uint64 getuint64()
- {
- unsigned int nSize = BN_bn2mpi(bn, NULL);
- if (nSize < 4)
- return 0;
- std::vector<unsigned char> vch(nSize);
- BN_bn2mpi(bn, &vch[0]);
- if (vch.size() > 4)
- vch[4] &= 0x7f;
- uint64 n = 0;
- for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
- ((unsigned char*)&n)[i] = vch[j];
- return n;
- }
- void setuint64(uint64_t n)
- {
- unsigned char pch[sizeof(n) + 6];
- unsigned char* p = pch + 4;
- bool fLeadingZeroes = true;
- for (int i = 0; i < 8; i++)
- {
- unsigned char c = (n >> 56) & 0xff;
- n <<= 8;
- if (fLeadingZeroes)
- {
- if (c == 0)
- continue;
- if (c & 0x80)
- *p++ = 0;
- fLeadingZeroes = false;
- }
- *p++ = c;
- }
- unsigned int nSize = p - (pch + 4);
- pch[0] = (nSize >> 24) & 0xff;
- pch[1] = (nSize >> 16) & 0xff;
- pch[2] = (nSize >> 8) & 0xff;
- pch[3] = (nSize) & 0xff;
- BN_mpi2bn(pch, p - pch, bn);
- }
- void setuint256(uint256 n)
- {
- unsigned char pch[sizeof(n) + 6];
- unsigned char* p = pch + 4;
- bool fLeadingZeroes = true;
- unsigned char* pbegin = (unsigned char*)&n;
- unsigned char* psrc = pbegin + sizeof(n);
- while (psrc != pbegin)
- {
- unsigned char c = *(--psrc);
- if (fLeadingZeroes)
- {
- if (c == 0)
- continue;
- if (c & 0x80)
- *p++ = 0;
- fLeadingZeroes = false;
- }
- *p++ = c;
- }
- unsigned int nSize = p - (pch + 4);
- pch[0] = (nSize >> 24) & 0xff;
- pch[1] = (nSize >> 16) & 0xff;
- pch[2] = (nSize >> 8) & 0xff;
- pch[3] = (nSize >> 0) & 0xff;
- BN_mpi2bn(pch, p - pch, bn);
- }
- uint256 getuint256() const
- {
- unsigned int nSize = BN_bn2mpi(bn, NULL);
- if (nSize < 4)
- return 0;
- std::vector<unsigned char> vch(nSize);
- BN_bn2mpi(bn, &vch[0]);
- if (vch.size() > 4)
- vch[4] &= 0x7f;
- uint256 n = 0;
- for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
- ((unsigned char*)&n)[i] = vch[j];
- return n;
- }
- void setvch(const std::vector<unsigned char>& vch)
- {
- std::vector<unsigned char> vch2(vch.size() + 4);
- unsigned int nSize = vch.size();
- // BIGNUM's byte stream format expects 4 bytes of
- // big endian size data info at the front
- vch2[0] = (nSize >> 24) & 0xff;
- vch2[1] = (nSize >> 16) & 0xff;
- vch2[2] = (nSize >> 8) & 0xff;
- vch2[3] = (nSize >> 0) & 0xff;
- // swap data to big endian
- reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
- BN_mpi2bn(&vch2[0], vch2.size(), bn);
- }
- std::vector<unsigned char> getvch() const
- {
- unsigned int nSize = BN_bn2mpi(bn, NULL);
- if (nSize <= 4)
- return std::vector<unsigned char>();
- std::vector<unsigned char> vch(nSize);
- BN_bn2mpi(bn, &vch[0]);
- vch.erase(vch.begin(), vch.begin() + 4);
- reverse(vch.begin(), vch.end());
- return vch;
- }
- CBigNum& SetCompact(unsigned int nCompact)
- {
- unsigned int nSize = nCompact >> 24;
- /** std::vector<unsigned char> vch(4 + nSize);
- vch[3] = nSize;
- if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
- if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
- if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
- BN_mpi2bn(&vch[0], vch.size(), this);*/
- bool fNegative =(nCompact & 0x00800000) != 0;
- unsigned int nWord = nCompact & 0x007fffff;
- if (nSize <= 3)
- {
- nWord >>= 8*(3-nSize);
- BN_set_word(bn, nWord);
- }
- else
- {
- BN_set_word(bn, nWord);
- BN_lshift(bn, bn, 8*(nSize-3));
- }
- BN_set_negative(bn, fNegative);
- return *this;
- }
- unsigned int GetCompact() const
- {
- /** unsigned int nSize = BN_bn2mpi(this, NULL);
- std::vector<unsigned char> vch(nSize);
- nSize -= 4;
- BN_bn2mpi(this, &vch[0]);
- unsigned int nCompact = nSize << 24;
- if (nSize >= 1) nCompact |= (vch[4] << 16);
- if (nSize >= 2) nCompact |= (vch[5] << 8);
- if (nSize >= 3) nCompact |= (vch[6] << 0);*/
- unsigned int nSize = BN_num_bytes(bn);
- unsigned int nCompact = 0;
- if (nSize <= 3)
- nCompact = BN_get_word(bn) << 8*(3-nSize);
- else
- {
- CBigNum bn1;
- BN_rshift(&bn1, bn, 8*(nSize-3));
- nCompact = BN_get_word(&bn1);
- }
- // The 0x00800000 bit denotes the sign.
- // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
- if (nCompact & 0x00800000)
- {
- nCompact >>= 8;
- nSize++;
- }
- nCompact |= nSize << 24;
- nCompact |= (BN_is_negative(bn) ? 0x00800000 : 0);
- return nCompact;
- }
- void SetHex(const std::string& str)
- {
- // skip 0x
- const char* psz = str.c_str();
- while (isspace(*psz))
- psz++;
- bool fNegative = false;
- if (*psz == '-')
- {
- fNegative = true;
- psz++;
- }
- if (psz[0] == '0' && tolower(psz[1]) == 'x')
- psz += 2;
- while (isspace(*psz))
- psz++;
- // hex string to bignum
- static const signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
- *this = 0;
- while (isxdigit(*psz))
- {
- *this <<= 4;
- int n = phexdigit[(unsigned char)*psz++];
- *this += n;
- }
- if (fNegative)
- *this = 0 - *this;
- }
- bool SetHexBool(const std::string& str)
- {
- // skip 0x
- const char* psz = str.c_str();
- while (isspace(*psz))
- psz++;
- bool fNegative = false;
- if (*psz == '-')
- {
- fNegative = true;
- psz++;
- }
- if (psz[0] == '0' && tolower(psz[1]) == 'x')
- psz += 2;
- while (isspace(*psz))
- psz++;
- // hex string to bignum
- static const signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
- *this = 0;
- while (isxdigit(*psz))
- {
- *this <<= 4;
- int n = phexdigit[(unsigned char)*psz++];
- *this += n;
- }
- if (fNegative)
- *this = 0 - *this;
- return true;
- }
- std::string ToString(int nBase=10) const
- {
- CAutoBN_CTX pctx;
- CBigNum bnBase = nBase;
- CBigNum bn0 = 0;
- std::string str;
- CBigNum bn1 = *this;
- BN_set_negative(&bn1, false);
- CBigNum dv;
- CBigNum rem;
- if (BN_cmp(&bn1, &bn0) == 0)
- return "0";
- while (BN_cmp(&bn1, &bn0) > 0)
- {
- if (!BN_div(&dv, &rem, &bn1, &bnBase, pctx))
- throw bignum_error("CBigNum::ToString() : BN_div failed");
- bn1 = dv;
- unsigned int c = rem.getulong();
- str += "0123456789abcdef"[c];
- }
- if (BN_is_negative(bn))
- str += "-";
- reverse(str.begin(), str.end());
- return str;
- }
- std::string GetHex() const
- {
- return ToString(16);
- }
- unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
- {
- return ::GetSerializeSize(getvch(), nType, nVersion);
- }
- template<typename Stream>
- void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
- {
- ::Serialize(s, getvch(), nType, nVersion);
- }
- template<typename Stream>
- void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
- {
- std::vector<unsigned char> vch;
- ::Unserialize(s, vch, nType, nVersion);
- setvch(vch);
- }
- /**
- * exponentiation with an int. this^e
- * @param e the exponent as an int
- * @return
- */
- CBigNum pow(const int e) const {
- return this->pow(CBigNum(e));
- }
- /**
- * exponentiation this^e
- * @param e the exponent
- * @return
- */
- CBigNum pow(const CBigNum& e) const {
- CAutoBN_CTX pctx;
- CBigNum ret;
- if (!BN_exp(&ret, bn, &e, pctx))
- throw bignum_error("CBigNum::pow : BN_exp failed");
- return ret;
- }
- /**
- * modular multiplication: (this * b) mod m
- * @param b operand
- * @param m modulus
- */
- CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const {
- CAutoBN_CTX pctx;
- CBigNum ret;
- if (!BN_mod_mul(&ret, bn, &b, &m, pctx))
- throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed");
-
- return ret;
- }
- /**
- * modular exponentiation: this^e mod n
- * @param e exponent
- * @param m modulus
- */
- CBigNum pow_mod(const CBigNum& e, const CBigNum& m) const {
- CAutoBN_CTX pctx;
- CBigNum ret;
- if( e < 0){
- // g^-x = (g^-1)^x
- CBigNum inv = this->inverse(m);
- CBigNum posE = e * -1;
- if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx))
- throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent");
- }else
- if (!BN_mod_exp(&ret, bn, &e, &m, pctx))
- throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed");
- return ret;
- }
- /**
- * Calculates the inverse of this element mod m.
- * i.e. i such this*i = 1 mod m
- * @param m the modu
- * @return the inverse
- */
- CBigNum inverse(const CBigNum& m) const {
- CAutoBN_CTX pctx;
- CBigNum ret;
- if (!BN_mod_inverse(&ret, bn, &m, pctx))
- throw bignum_error("CBigNum::inverse*= :BN_mod_inverse");
- return ret;
- }
- /**
- * Generates a random (safe) prime of numBits bits
- * @param numBits the number of bits
- * @param safe true for a safe prime
- * @return the prime
- */
- static CBigNum generatePrime(const unsigned int numBits, bool safe = false) {
- CBigNum ret;
- if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL))
- throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex");
- return ret;
- }
- /**
- * Calculates the greatest common divisor (GCD) of two numbers.
- * @param m the second element
- * @return the GCD
- */
- CBigNum gcd( const CBigNum& b) const{
- CAutoBN_CTX pctx;
- CBigNum ret;
- if (!BN_gcd(&ret, bn, &b, pctx))
- throw bignum_error("CBigNum::gcd*= :BN_gcd");
- return ret;
- }
- /**
- * Miller-Rabin primality test on this element
- * @param checks: optional, the number of Miller-Rabin tests to run
- * default causes error rate of 2^-80.
- * @return true if prime
- */
- bool isPrime(const int checks=BN_prime_checks) const {
- CAutoBN_CTX pctx;
- int ret = BN_is_prime_ex(bn, checks, pctx, NULL);
- if(ret < 0){
- throw bignum_error("CBigNum::isPrime :BN_is_prime");
- }
- return ret;
- }
- bool isOne() const {
- return BN_is_one(bn);
- }
- bool operator!() const
- {
- return BN_is_zero(bn);
- }
- CBigNum& operator+=(const CBigNum& b)
- {
- if (!BN_add(bn, bn, &b))
- throw bignum_error("CBigNum::operator+= : BN_add failed");
- return *this;
- }
- CBigNum& operator-=(const CBigNum& b)
- {
- *this = *this - b;
- return *this;
- }
- CBigNum& operator*=(const CBigNum& b)
- {
- CAutoBN_CTX pctx;
- if (!BN_mul(bn, bn, &b, pctx))
- throw bignum_error("CBigNum::operator*= : BN_mul failed");
- return *this;
- }
- CBigNum& operator/=(const CBigNum& b)
- {
- *this = *this / b;
- return *this;
- }
- CBigNum& operator%=(const CBigNum& b)
- {
- *this = *this % b;
- return *this;
- }
- CBigNum& operator<<=(unsigned int shift)
- {
- if (!BN_lshift(bn, bn, shift))
- throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
- return *this;
- }
- CBigNum& operator>>=(unsigned int shift)
- {
- // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
- // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
- CBigNum a = 1;
- a <<= shift;
- if (BN_cmp(&a, bn) > 0)
- {
- *this = 0;
- return *this;
- }
- if (!BN_rshift(bn, bn, shift))
- throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
- return *this;
- }
- CBigNum& operator++()
- {
- // prefix operator
- if (!BN_add(bn, bn, BN_value_one()))
- throw bignum_error("CBigNum::operator++ : BN_add failed");
- return *this;
- }
- const CBigNum operator++(int)
- {
- // postfix operator
- const CBigNum ret = *this;
- ++(*this);
- return ret;
- }
- CBigNum& operator--()
- {
- // prefix operator
- CBigNum r;
- if (!BN_sub(&r, bn, BN_value_one()))
- throw bignum_error("CBigNum::operator-- : BN_sub failed");
- *this = r;
- return *this;
- }
- const CBigNum operator--(int)
- {
- // postfix operator
- const CBigNum ret = *this;
- --(*this);
- return ret;
- }
- friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
- friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
- friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
- friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b);
- friend inline bool operator<(const CBigNum& a, const CBigNum& b);
- };
- inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
- {
- CBigNum r;
- if (!BN_add(&r, &a, &b))
- throw bignum_error("CBigNum::operator+ : BN_add failed");
- return r;
- }
- inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
- {
- CBigNum r;
- if (!BN_sub(&r, &a, &b))
- throw bignum_error("CBigNum::operator- : BN_sub failed");
- return r;
- }
- inline const CBigNum operator-(const CBigNum& a)
- {
- CBigNum r(a);
- BN_set_negative(&r, !BN_is_negative(&r));
- return r;
- }
- inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
- {
- CAutoBN_CTX pctx;
- CBigNum r;
- if (!BN_mul(&r, &a, &b, pctx))
- throw bignum_error("CBigNum::operator* : BN_mul failed");
- return r;
- }
- inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
- {
- CAutoBN_CTX pctx;
- CBigNum r;
- if (!BN_div(&r, NULL, &a, &b, pctx))
- throw bignum_error("CBigNum::operator/ : BN_div failed");
- return r;
- }
- inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
- {
- CAutoBN_CTX pctx;
- CBigNum r;
- if (!BN_nnmod(&r, &a, &b, pctx))
- throw bignum_error("CBigNum::operator% : BN_div failed");
- return r;
- }
- inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
- {
- CBigNum r;
- if (!BN_lshift(&r, &a, shift))
- throw bignum_error("CBigNum:operator<< : BN_lshift failed");
- return r;
- }
- inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
- {
- CBigNum r = a;
- r >>= shift;
- return r;
- }
- inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
- inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
- inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
- inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
- inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
- inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
- inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); }
- //typedef CBigNum Bignum;
- #endif
|