X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fbignum.h;h=acba29a1513ebdefd5783872e8646be723453a7f;hb=24f4884cdf5b4a68a3996238995ac0846dae9126;hp=1042758d9709a3252ed5f6723c49ebe3b8218b65;hpb=77a43545b4491b9703d803765da9059d2bdd5aaa;p=novacoin.git diff --git a/src/bignum.h b/src/bignum.h index 1042758..acba29a 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -47,50 +47,56 @@ public: /** C++ wrapper for BIGNUM (OpenSSL bignum) */ -class CBigNum : public BIGNUM +class CBigNum { +private: + BIGNUM* bn; public: CBigNum() { - BN_init(this); + bn = BN_new(); } CBigNum(const CBigNum& b) { - BN_init(this); - if (!BN_copy(this, &b)) + BIGNUM *dup = BN_dup(b.bn); + if (!dup) { - BN_clear_free(this); - throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); + throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_dup failed"); } + bn = dup; } CBigNum& operator=(const CBigNum& b) { - if (!BN_copy(this, &b)) - throw bignum_error("CBigNum::operator= : BN_copy failed"); + BIGNUM *dup = BN_dup(b.bn); + if (!dup) + { + throw bignum_error("CBigNum::operator= : BN_dup failed"); + } + bn = dup; return (*this); } ~CBigNum() { - BN_clear_free(this); + BN_clear_free(bn); } - //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'. - CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(unsigned char n) { BN_init(this); setulong(n); } - CBigNum(unsigned short n) { BN_init(this); setulong(n); } - CBigNum(unsigned int n) { BN_init(this); setulong(n); } - CBigNum(unsigned long n) { BN_init(this); setulong(n); } - explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } + CBigNum(int8_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int16_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int32_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int64_t n) { bn = BN_new(); if (n >= 0) setuint64(n); else setint64(n); } + + CBigNum(uint8_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint16_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint32_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint64_t n) { bn = BN_new(); setuint64(n); } - explicit CBigNum(const std::vector& vch) + explicit CBigNum(uint256 n) { bn = BN_new(); setuint256(n); } + explicit CBigNum(const std::vector& vch) { - BN_init(this); + bn = BN_new(); setvch(vch); } @@ -101,7 +107,7 @@ public: */ static CBigNum randBignum(const CBigNum& range) { CBigNum ret; - if(!BN_rand_range(&ret, &range)){ + if(!BN_rand_range(ret.bn, range.bn)){ throw bignum_error("CBigNum:rand element : BN_rand_range failed"); } return ret; @@ -113,7 +119,7 @@ public: */ static CBigNum RandKBitBigum(const uint32_t k){ CBigNum ret; - if(!BN_rand(&ret, k, -1, 0)){ + if(!BN_rand(ret.bn, k, -1, 0)){ throw bignum_error("CBigNum:rand element : BN_rand failed"); } return ret; @@ -124,39 +130,34 @@ public: * @return the size */ int bitSize() const{ - return BN_num_bits(this); + return BN_num_bits(bn); } - void setulong(unsigned long n) - { - if (!BN_set_word(this, n)) - throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); - } - - unsigned long getulong() const + void setuint32(uint32_t n) { - return BN_get_word(this); + if (!BN_set_word(bn, n)) + throw bignum_error("CBigNum conversion from uint32_t : BN_set_word failed"); } - unsigned int getuint() const + uint32_t getuint32() const { - return BN_get_word(this); + return BN_get_word(bn); } - int getint() const + int32_t getint32() const { - unsigned long n = BN_get_word(this); - if (!BN_is_negative(this)) - return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::max() : n); + uint64_t n = BN_get_word(bn); + if (!BN_is_negative(bn)) + return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::max() : (int32_t)n); else - return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); + return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::min() : -(int32_t)n); } void setint64(int64_t sn) { - unsigned char pch[sizeof(sn) + 6]; - unsigned char* p = pch + 4; + uint8_t pch[sizeof(sn) + 6]; + uint8_t* p = pch + 4; bool fNegative; uint64_t n; @@ -174,7 +175,7 @@ public: bool fLeadingZeroes = true; for (int i = 0; i < 8; i++) { - unsigned char c = (n >> 56) & 0xff; + uint8_t c = (n >> 56) & 0xff; n <<= 8; if (fLeadingZeroes) { @@ -188,37 +189,48 @@ public: } *p++ = c; } - unsigned int nSize = p - (pch + 4); + uint32_t nSize = (uint32_t) (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, this); + BN_mpi2bn(pch, (int)(p - pch), bn); } uint64_t getuint64() { - unsigned int nSize = BN_bn2mpi(this, NULL); + size_t nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + std::vector vch(nSize); + BN_bn2mpi(bn, &vch[0]); if (vch.size() > 4) vch[4] &= 0x7f; uint64_t n = 0; - for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) - ((unsigned char*)&n)[i] = vch[j]; + for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + ((uint8_t*)&n)[i] = vch[j]; return n; } + //supress msvc C4127: conditional expression is constant + inline bool check(bool value) {return value;} + void setuint64(uint64_t n) { - unsigned char pch[sizeof(n) + 6]; - unsigned char* p = pch + 4; + // Use BN_set_word if word size is sufficient for uint64_t + if (check(sizeof(n) <= sizeof(BN_ULONG))) + { + if (!BN_set_word(bn, (BN_ULONG)n)) + throw bignum_error("CBigNum conversion from uint64_t : BN_set_word failed"); + return; + } + + uint8_t pch[sizeof(n) + 6]; + uint8_t* p = pch + 4; bool fLeadingZeroes = true; for (int i = 0; i < 8; i++) { - unsigned char c = (n >> 56) & 0xff; + uint8_t c = (n >> 56) & 0xff; n <<= 8; if (fLeadingZeroes) { @@ -230,24 +242,24 @@ public: } *p++ = c; } - unsigned int nSize = p - (pch + 4); + uint32_t nSize = (uint32_t) (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, this); + BN_mpi2bn(pch, (int)(p - pch), bn); } void setuint160(uint160 n) { - unsigned char pch[sizeof(n) + 6]; - unsigned char* p = pch + 4; + uint8_t pch[sizeof(n) + 6]; + uint8_t* p = pch + 4; bool fLeadingZeroes = true; - unsigned char* pbegin = (unsigned char*)&n; - unsigned char* psrc = pbegin + sizeof(n); + uint8_t* pbegin = (uint8_t*)&n; + uint8_t* psrc = pbegin + sizeof(n); while (psrc != pbegin) { - unsigned char c = *(--psrc); + uint8_t c = *(--psrc); if (fLeadingZeroes) { if (c == 0) @@ -258,39 +270,39 @@ public: } *p++ = c; } - unsigned int nSize = p - (pch + 4); + uint32_t nSize = (uint32_t) (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, this); + BN_mpi2bn(pch, (int) (p - pch), bn); } uint160 getuint160() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + std::vector vch(nSize); + BN_bn2mpi(bn, &vch[0]); if (vch.size() > 4) vch[4] &= 0x7f; uint160 n = 0; - for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) - ((unsigned char*)&n)[i] = vch[j]; + for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + ((uint8_t*)&n)[i] = vch[j]; return n; } void setuint256(uint256 n) { - unsigned char pch[sizeof(n) + 6]; - unsigned char* p = pch + 4; + uint8_t pch[sizeof(n) + 6]; + uint8_t* p = pch + 4; bool fLeadingZeroes = true; - unsigned char* pbegin = (unsigned char*)&n; - unsigned char* psrc = pbegin + sizeof(n); + uint8_t* pbegin = (uint8_t*)&n; + uint8_t* psrc = pbegin + sizeof(n); while (psrc != pbegin) { - unsigned char c = *(--psrc); + uint8_t c = *(--psrc); if (fLeadingZeroes) { if (c == 0) @@ -301,41 +313,41 @@ public: } *p++ = c; } - unsigned int nSize = p - (pch + 4); + uint32_t nSize = (uint32_t) (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, this); + BN_mpi2bn(pch, (int) (p - pch), bn); } uint256 getuint256() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + std::vector 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]; + for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + ((uint8_t*)&n)[i] = vch[j]; return n; } - void setBytes(const std::vector& vchBytes) + void setBytes(const std::vector& vchBytes) { - BN_bin2bn(&vchBytes[0], vchBytes.size(), this); + BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), bn); } - std::vector getBytes() const + std::vector getBytes() const { - int nBytes = BN_num_bytes(this); + int nBytes = BN_num_bytes(bn); - std::vector vchBytes(nBytes); + std::vector vchBytes(nBytes); - int n = BN_bn2bin(this, &vchBytes[0]); + int n = BN_bn2bin(bn, &vchBytes[0]); if (n != nBytes) { throw bignum_error("CBigNum::getBytes : BN_bn2bin failed"); } @@ -343,10 +355,10 @@ public: return vchBytes; } - void setvch(const std::vector& vch) + void setvch(const std::vector& vch) { - std::vector vch2(vch.size() + 4); - unsigned int nSize = vch.size(); + std::vector vch2(vch.size() + 4); + uint32_t nSize = (uint32_t) vch.size(); // BIGNUM's byte stream format expects 4 bytes of // big endian size data info at the front vch2[0] = (nSize >> 24) & 0xff; @@ -355,40 +367,40 @@ public: 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(), this); + BN_mpi2bn(&vch2[0], (int) vch2.size(), bn); } - std::vector getvch() const + std::vector getvch() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize <= 4) - return std::vector(); - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + return std::vector(); + std::vector 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) + CBigNum& SetCompact(uint32_t nCompact) { - unsigned int nSize = nCompact >> 24; - std::vector vch(4 + nSize); + uint32_t nSize = nCompact >> 24; + std::vector 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); + BN_mpi2bn(&vch[0], (int) vch.size(), bn); return *this; } - unsigned int GetCompact() const + uint32_t GetCompact() const { - unsigned int nSize = BN_bn2mpi(this, NULL); - std::vector vch(nSize); + uint32_t nSize = BN_bn2mpi(bn, NULL); + std::vector vch(nSize); nSize -= 4; - BN_bn2mpi(this, &vch[0]); - unsigned int nCompact = nSize << 24; + BN_bn2mpi(bn, &vch[0]); + uint32_t nCompact = nSize << 24; if (nSize >= 1) nCompact |= (vch[4] << 16); if (nSize >= 2) nCompact |= (vch[5] << 8); if (nSize >= 3) nCompact |= (vch[6] << 0); @@ -418,7 +430,7 @@ public: while (isxdigit(*psz)) { *this <<= 4; - int n = phexdigit[(unsigned char)*psz++]; + int n = phexdigit[(uint8_t)*psz++]; *this += n; } if (fNegative) @@ -432,20 +444,20 @@ public: CBigNum bn0 = 0; std::string str; CBigNum bn = *this; - BN_set_negative(&bn, false); + BN_set_negative(bn.bn, false); CBigNum dv; CBigNum rem; - if (BN_cmp(&bn, &bn0) == 0) + if (BN_cmp(bn.bn, bn0.bn) == 0) return "0"; - while (BN_cmp(&bn, &bn0) > 0) + while (BN_cmp(bn.bn, bn0.bn) > 0) { - if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) + if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx)) throw bignum_error("CBigNum::ToString() : BN_div failed"); bn = dv; - unsigned int c = rem.getulong(); + uint32_t c = rem.getuint32(); str += "0123456789abcdef"[c]; } - if (BN_is_negative(this)) + if (BN_is_negative(bn.bn)) str += "-"; reverse(str.begin(), str.end()); return str; @@ -456,6 +468,14 @@ public: return ToString(16); } + const BIGNUM* get() const { + return bn; + } + + BIGNUM* get() { + return bn; + } + unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const { return ::GetSerializeSize(getvch(), nType, nVersion); @@ -470,7 +490,7 @@ public: template void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) { - std::vector vch; + std::vector vch; ::Unserialize(s, vch, nType, nVersion); setvch(vch); } @@ -492,7 +512,7 @@ public: CBigNum pow(const CBigNum& e) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_exp(&ret, this, &e, pctx)) + if (!BN_exp(ret.bn, bn, e.bn, pctx)) throw bignum_error("CBigNum::pow : BN_exp failed"); return ret; } @@ -505,9 +525,9 @@ public: CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_mod_mul(&ret, this, &b, &m, pctx)) + if (!BN_mod_mul(ret.bn, bn, b.bn, m.bn, pctx)) throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed"); - + return ret; } @@ -523,10 +543,10 @@ public: // g^-x = (g^-1)^x CBigNum inv = this->inverse(m); CBigNum posE = e * -1; - if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx)) + if (!BN_mod_exp(ret.bn, inv.bn, posE.bn, m.bn, pctx)) throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent"); }else - if (!BN_mod_exp(&ret, this, &e, &m, pctx)) + if (!BN_mod_exp(ret.bn, bn, e.bn, m.bn, pctx)) throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed"); return ret; @@ -541,7 +561,7 @@ public: CBigNum inverse(const CBigNum& m) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_mod_inverse(&ret, this, &m, pctx)) + if (!BN_mod_inverse(ret.bn, bn, m.bn, pctx)) throw bignum_error("CBigNum::inverse*= :BN_mod_inverse"); return ret; } @@ -554,7 +574,7 @@ public: */ static CBigNum generatePrime(const unsigned int numBits, bool safe = false) { CBigNum ret; - if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL)) + if(!BN_generate_prime_ex(ret.bn, numBits, (safe == true), NULL, NULL, NULL)) throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex"); return ret; } @@ -567,7 +587,7 @@ public: CBigNum gcd( const CBigNum& b) const{ CAutoBN_CTX pctx; CBigNum ret; - if (!BN_gcd(&ret, this, &b, pctx)) + if (!BN_gcd(ret.bn, bn, b.bn, pctx)) throw bignum_error("CBigNum::gcd*= :BN_gcd"); return ret; } @@ -580,26 +600,26 @@ public: */ bool isPrime(const int checks=BN_prime_checks) const { CAutoBN_CTX pctx; - int ret = BN_is_prime(this, checks, NULL, pctx, NULL); + int ret = BN_is_prime(bn, checks, NULL, pctx, NULL); if(ret < 0){ throw bignum_error("CBigNum::isPrime :BN_is_prime"); } - return ret; + return ret != 0; } bool isOne() const { - return BN_is_one(this); + return BN_is_one(bn); } bool operator!() const { - return BN_is_zero(this); + return BN_is_zero(bn); } CBigNum& operator+=(const CBigNum& b) { - if (!BN_add(this, this, &b)) + if (!BN_add(bn, bn, b.bn)) throw bignum_error("CBigNum::operator+= : BN_add failed"); return *this; } @@ -613,7 +633,7 @@ public: CBigNum& operator*=(const CBigNum& b) { CAutoBN_CTX pctx; - if (!BN_mul(this, this, &b, pctx)) + if (!BN_mul(bn, bn, b.bn, pctx)) throw bignum_error("CBigNum::operator*= : BN_mul failed"); return *this; } @@ -632,7 +652,7 @@ public: CBigNum& operator<<=(unsigned int shift) { - if (!BN_lshift(this, this, shift)) + if (!BN_lshift(bn, bn, shift)) throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); return *this; } @@ -643,13 +663,13 @@ public: // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL CBigNum a = 1; a <<= shift; - if (BN_cmp(&a, this) > 0) + if (BN_cmp(a.bn, bn) > 0) { *this = 0; return *this; } - if (!BN_rshift(this, this, shift)) + if (!BN_rshift(bn, bn, shift)) throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); return *this; } @@ -658,7 +678,7 @@ public: CBigNum& operator++() { // prefix operator - if (!BN_add(this, this, BN_value_one())) + if (!BN_add(bn, bn, BN_value_one())) throw bignum_error("CBigNum::operator++ : BN_add failed"); return *this; } @@ -675,7 +695,7 @@ public: { // prefix operator CBigNum r; - if (!BN_sub(&r, this, BN_value_one())) + if (!BN_sub(r.bn, bn, BN_value_one())) throw bignum_error("CBigNum::operator-- : BN_sub failed"); *this = r; return *this; @@ -689,12 +709,25 @@ public: 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); + 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); + + friend inline const CBigNum operator-(const CBigNum& a); + friend inline const CBigNum operator<<(const CBigNum& a, unsigned int shift); + + friend inline bool operator==(const CBigNum& a, const CBigNum& b); + friend inline bool operator!=(const CBigNum& a, const CBigNum& b); + friend inline bool operator<=(const CBigNum& a, const CBigNum& b); + friend inline bool operator>=(const CBigNum& a, const CBigNum& b); + friend inline bool operator<(const CBigNum& a, const CBigNum& b); + friend inline bool operator>(const CBigNum& a, const CBigNum& b); + friend inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b); }; @@ -702,7 +735,7 @@ public: inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_add(&r, &a, &b)) + if (!BN_add(r.bn, a.bn, b.bn)) throw bignum_error("CBigNum::operator+ : BN_add failed"); return r; } @@ -710,7 +743,7 @@ inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_sub(&r, &a, &b)) + if (!BN_sub(r.bn, a.bn, b.bn)) throw bignum_error("CBigNum::operator- : BN_sub failed"); return r; } @@ -718,7 +751,7 @@ inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a) { CBigNum r(a); - BN_set_negative(&r, !BN_is_negative(&r)); + BN_set_negative(r.bn, !BN_is_negative(r.bn)); return r; } @@ -726,7 +759,7 @@ inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_mul(&r, &a, &b, pctx)) + if (!BN_mul(r.bn, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator* : BN_mul failed"); return r; } @@ -735,7 +768,7 @@ inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_div(&r, NULL, &a, &b, pctx)) + if (!BN_div(r.bn, NULL, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator/ : BN_div failed"); return r; } @@ -744,7 +777,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_nnmod(&r, &a, &b, pctx)) + if (!BN_nnmod(r.bn, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator% : BN_div failed"); return r; } @@ -752,7 +785,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) { CBigNum r; - if (!BN_lshift(&r, &a, shift)) + if (!BN_lshift(r.bn, a.bn, shift)) throw bignum_error("CBigNum:operator<< : BN_lshift failed"); return r; } @@ -764,12 +797,12 @@ inline const CBigNum operator>>(const CBigNum& a, unsigned int 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 bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) == 0); } +inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) != 0); } +inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) <= 0); } +inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) >= 0); } +inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) < 0); } +inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); } inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); }