X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fbignum.h;h=4bda99d42515d31bb409df2dc89a446a2cc3063b;hb=fe27b2dfe4551bc841b4edb833f13c7f72095faa;hp=e3e60f5e5a1fe4316f51502a47884672d9dd227d;hpb=6f895c2539c4ddefce658bb2ec7083774bbbd5a3;p=novacoin.git diff --git a/src/bignum.h b/src/bignum.h index e3e60f5..4bda99d 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -88,7 +88,7 @@ public: CBigNum(uint64_t n) { BN_init(this); setuint64(n); } explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } - explicit CBigNum(const std::vector& vch) + explicit CBigNum(const std::vector& vch) { BN_init(this); setvch(vch); @@ -143,15 +143,15 @@ public: { uint64_t n = BN_get_word(this); if (!BN_is_negative(this)) - return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::max() : n); + return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::max() : (int32_t)n); else 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; @@ -169,7 +169,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) { @@ -183,37 +183,48 @@ public: } *p++ = c; } - uint32_t 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), this); } uint64_t getuint64() { - unsigned int nSize = BN_bn2mpi(this, NULL); + size_t nSize = BN_bn2mpi(this, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); + std::vector vch(nSize); BN_bn2mpi(this, &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(this, (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) { @@ -225,24 +236,24 @@ public: } *p++ = c; } - uint32_t 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), this); } 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) @@ -253,12 +264,12 @@ public: } *p++ = c; } - uint32_t 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), this); } uint160 getuint160() const @@ -266,26 +277,26 @@ public: unsigned int nSize = BN_bn2mpi(this, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); + std::vector vch(nSize); BN_bn2mpi(this, &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) @@ -296,12 +307,12 @@ public: } *p++ = c; } - uint32_t 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), this); } uint256 getuint256() const @@ -309,26 +320,26 @@ public: unsigned int nSize = BN_bn2mpi(this, NULL); if (nSize < 4) return 0; - std::vector vch(nSize); + std::vector vch(nSize); BN_bn2mpi(this, &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(), this); } - std::vector getBytes() const + std::vector getBytes() const { int nBytes = BN_num_bytes(this); - std::vector vchBytes(nBytes); + std::vector vchBytes(nBytes); int n = BN_bn2bin(this, &vchBytes[0]); if (n != nBytes) { @@ -338,10 +349,10 @@ public: return vchBytes; } - void setvch(const std::vector& vch) + void setvch(const std::vector& vch) { - std::vector vch2(vch.size() + 4); - uint32_t 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; @@ -350,15 +361,15 @@ 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(), this); } - std::vector getvch() const + std::vector getvch() const { unsigned int nSize = BN_bn2mpi(this, NULL); if (nSize <= 4) - return std::vector(); - std::vector vch(nSize); + return std::vector(); + std::vector vch(nSize); BN_bn2mpi(this, &vch[0]); vch.erase(vch.begin(), vch.begin() + 4); reverse(vch.begin(), vch.end()); @@ -368,19 +379,19 @@ public: CBigNum& SetCompact(uint32_t nCompact) { uint32_t nSize = nCompact >> 24; - std::vector vch(4 + nSize); + 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(), this); return *this; } uint32_t GetCompact() const { uint32_t nSize = BN_bn2mpi(this, NULL); - std::vector vch(nSize); + std::vector vch(nSize); nSize -= 4; BN_bn2mpi(this, &vch[0]); uint32_t nCompact = nSize << 24; @@ -413,7 +424,7 @@ public: while (isxdigit(*psz)) { *this <<= 4; - int n = phexdigit[(unsigned char)*psz++]; + int n = phexdigit[(uint8_t)*psz++]; *this += n; } if (fNegative) @@ -437,7 +448,7 @@ public: if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) throw bignum_error("CBigNum::ToString() : BN_div failed"); bn = dv; - unsigned int c = rem.getuint32(); + uint32_t c = rem.getuint32(); str += "0123456789abcdef"[c]; } if (BN_is_negative(this)) @@ -465,7 +476,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); } @@ -502,7 +513,7 @@ public: CBigNum ret; if (!BN_mod_mul(&ret, this, &b, &m, pctx)) throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed"); - + return ret; } @@ -579,7 +590,7 @@ public: if(ret < 0){ throw bignum_error("CBigNum::isPrime :BN_is_prime"); } - return ret; + return ret != 0; } bool isOne() const {