From 7a06e4479b56264bcd104fd47463e4b07370ef81 Mon Sep 17 00:00:00 2001 From: div72 Date: Mon, 9 Oct 2023 00:10:25 +0300 Subject: [PATCH] refactor: remove usages of CBigNum --- src/Makefile.am | 1 - src/bignum.h | 731 ---------------------------- src/gridcoin/staking/difficulty.cpp | 5 +- src/gridcoin/staking/kernel.cpp | 8 +- src/main.cpp | 2 +- src/miner.cpp | 6 +- src/test/bignum_tests.cpp | 124 ----- src/validation.cpp | 8 +- 8 files changed, 14 insertions(+), 871 deletions(-) delete mode 100644 src/bignum.h delete mode 100755 src/test/bignum_tests.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 1f6dd98424..441d48e36b 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -81,7 +81,6 @@ GRIDCOIN_CORE_H = \ attributes.h \ banman.h \ base58.h \ - bignum.h \ chainparams.h \ chainparamsbase.h \ checkpoints.h \ diff --git a/src/bignum.h b/src/bignum.h deleted file mode 100644 index d5a62d1782..0000000000 --- a/src/bignum.h +++ /dev/null @@ -1,731 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or https://opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_BIGNUM_H -#define BITCOIN_BIGNUM_H - -#include -#include "serialize.h" -#include "uint256.h" -#include "version.h" - -#include - -#include -#include - -#include - -/** 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 == nullptr) - throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned nullptr"); - } - - ~CAutoBN_CTX() - { - if (pctx != nullptr) - BN_CTX_free(pctx); - } - - operator BN_CTX*() { return pctx; } - BN_CTX& operator*() { return *pctx; } - BN_CTX** operator&() { return &pctx; } - bool operator!() { return (pctx == nullptr); } -}; - -/* RAII wrapper for BIGNUM instance */ -class CBigNumBase -{ -protected: - BIGNUM* pbn; - -public: - CBigNumBase() - : pbn(BN_new()) - { - if (pbn == nullptr) - throw bignum_error("CBigNum : BN_new() returned nullptr"); - } - - ~CBigNumBase() - { - BN_clear_free(pbn); - } -}; - -/** C++ wrapper for BIGNUM (OpenSSL bignum) */ -class CBigNum : public CBigNumBase -{ -public: - CBigNum() - {} - - CBigNum(const CBigNum& b) - { - if (!BN_copy(pbn, &b)) - throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); - } - - CBigNum& operator=(const CBigNum& b) - { - if (!BN_copy(pbn, &b)) - throw bignum_error("CBigNum::operator= : BN_copy failed"); - return (*this); - } - - //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'. - CBigNum(signed char n) { if (n >= 0) setulong(n); else setint64(n); } - CBigNum(short n) { if (n >= 0) setulong(n); else setint64(n); } - CBigNum(int n) { if (n >= 0) setulong(n); else setint64(n); } - CBigNum(long n) { if (n >= 0) setulong(n); else setint64(n); } - CBigNum(long long n) { setint64(n); } - CBigNum(unsigned char n) { setulong(n); } - CBigNum(unsigned short n) { setulong(n); } - CBigNum(unsigned int n) { setulong(n); } - CBigNum(unsigned long n) { setulong(n); } - CBigNum(unsigned long long n) { setuint64(n); } - explicit CBigNum(uint256 n) { setuint256(n); } - - explicit CBigNum(const std::vector& vch) - { - 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(pbn); - } - - - void setulong(unsigned long n) - { - if (!BN_set_word(pbn, n)) - throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); - } - - unsigned long getulong() const - { - return BN_get_word(pbn); - } - - unsigned int getuint() const - { - return BN_get_word(pbn); - } - - int getint() const - { - unsigned long n = BN_get_word(pbn); - if (!BN_is_negative(pbn)) - return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::max() : n); - else - return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::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, pbn); - } - - uint64_t getuint64() - { - unsigned int nSize = BN_bn2mpi(pbn, nullptr); - if (nSize < 4) - return 0; - std::vector vch(nSize); - BN_bn2mpi(pbn, &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]; - 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, pbn); - } - - 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, pbn); - } - - uint256 getuint256() const - { - unsigned int nSize = BN_bn2mpi(pbn, nullptr); - if (nSize < 4) - return uint256(); - std::vector vch(nSize); - BN_bn2mpi(pbn, &vch[0]); - if (vch.size() > 4) - vch[4] &= 0x7f; - uint256 n; - 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& vch) - { - std::vector 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(), pbn); - } - - std::vector getvch() const - { - unsigned int nSize = BN_bn2mpi(pbn, nullptr); - if (nSize <= 4) - return std::vector(); - std::vector vch(nSize); - BN_bn2mpi(pbn, &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 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(), pbn); - return *this; - } - - unsigned int GetCompact() const - { - unsigned int nSize = BN_bn2mpi(pbn, nullptr); - std::vector vch(nSize); - nSize -= 4; - BN_bn2mpi(pbn, &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); - 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((unsigned char)psz[1]) == 'x') - psz += 2; - while (IsSpace(*psz)) - psz++; - - // hex string to bignum - *this = 0; - while (HexDigit(*psz) >= 0) - { - *this <<= 4; - int n = HexDigit((unsigned char)*psz++); - *this += n; - } - if (fNegative) - *this = 0 - *this; - } - - std::string ToString(int nBase=10) const - { - CAutoBN_CTX pctx; - CBigNum bnBase = nBase; - CBigNum bn0 = 0; - std::string str; - CBigNum bn = *this; - BN_set_negative(&bn, false); - CBigNum dv; - CBigNum rem; - if (BN_cmp(&bn, &bn0) == 0) - return "0"; - while (BN_cmp(&bn, &bn0) > 0) - { - if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) - throw bignum_error("CBigNum::ToString() : BN_div failed"); - bn = dv; - unsigned int c = rem.getulong(); - str += "0123456789abcdef"[c]; - } - if (BN_is_negative(pbn)) - str += "-"; - reverse(str.begin(), str.end()); - return str; - } - - std::string GetHex() const - { - return ToString(16); - } - - template - void Serialize(Stream& s) const - { - ::Serialize(s, getvch()); - } - - template - void Unserialize(Stream& s) - { - std::vector vch; - ::Unserialize(s, vch); - 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, pbn, &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, pbn, &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, pbn, &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, pbn, &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, nullptr, nullptr, nullptr)) - 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, pbn, &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(pbn, checks, pctx, nullptr); - if(ret < 0){ - throw bignum_error("CBigNum::isPrime :BN_is_prime_ex"); - } - return ret; - } - - bool isOne() const - { - return BN_is_one(pbn); - } - - - bool operator!() const - { - return BN_is_zero(pbn); - } - - CBigNum& operator+=(const CBigNum& b) - { - if (!BN_add(pbn, pbn, &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(pbn, pbn, &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(pbn, pbn, 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, pbn) > 0) - { - *this = 0; - return *this; - } - - if (!BN_rshift(pbn, pbn, shift)) - throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); - return *this; - } - - - CBigNum& operator++() - { - // prefix operator - if (!BN_add(pbn, pbn, 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, pbn, 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; - } - - BIGNUM* operator&() { return pbn; } - const BIGNUM* operator&() const { return pbn; } - - 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, nullptr, &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 diff --git a/src/gridcoin/staking/difficulty.cpp b/src/gridcoin/staking/difficulty.cpp index 5303197ca4..b2cb8f097a 100644 --- a/src/gridcoin/staking/difficulty.cpp +++ b/src/gridcoin/staking/difficulty.cpp @@ -4,7 +4,6 @@ // file COPYING or https://opensource.org/licenses/mit-license.php. #include "amount.h" -#include "bignum.h" #include "chainparams.h" #include "init.h" #include "gridcoin/staking/difficulty.h" @@ -21,7 +20,7 @@ using namespace GRC; namespace { constexpr int64_t TARGET_TIMESPAN = 16 * 60; // 16 mins in seconds -const CBigNum PROOF_OF_STAKE_LIMIT(ArithToUint256(~arith_uint256() >> 20)); +const arith_uint256 PROOF_OF_STAKE_LIMIT = ~arith_uint256() >> 20; // ppcoin: find last block index up to pindex const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake) @@ -61,7 +60,7 @@ unsigned int GRC::GetNextTargetRequired(const CBlockIndex* pindexLast) // ppcoin: target change every block // ppcoin: retarget with exponential moving toward target spacing - CBigNum bnNew; + arith_uint256 bnNew; bnNew.SetCompact(pindexPrev->nBits); // Gridcoin - Reset Diff to 1 on 12-19-2014 (R Halford) - Diff sticking at diff --git a/src/gridcoin/staking/kernel.cpp b/src/gridcoin/staking/kernel.cpp index 47e436643b..511ef3fe18 100644 --- a/src/gridcoin/staking/kernel.cpp +++ b/src/gridcoin/staking/kernel.cpp @@ -461,7 +461,7 @@ bool GRC::CalculateLegacyV3HashProof( << coinstake.nTime << por_nonce; - out_hash_proof = CBigNum(out.GetHash()).getuint256(); + out_hash_proof = out.GetHash(); return true; } @@ -631,10 +631,10 @@ bool GRC::CheckProofOfStakeV8( //Stake refactoring TomasBrod int64_t Weight = CalculateStakeWeightV8(txPrev, prevout.n); - CBigNum bnHashProof(hashProofOfStake); + arith_uint256 bnHashProof = UintToArith256(hashProofOfStake); // Base target - CBigNum bnTarget; + arith_uint256 bnTarget; bnTarget.SetCompact(Block.nBits); // Weighted target bnTarget *= Weight; @@ -646,7 +646,7 @@ bool GRC::CheckProofOfStakeV8( " Trg %72s", generated_by_me?" Local,":"", (double)header.nTime, (double)txPrev.nTime, (double)tx.nTime, Block.nBits, (double)Weight, - CBigNum(hashProofOfStake).GetHex(), bnTarget.GetHex() + hashProofOfStake.GetHex(), bnTarget.GetHex() ); // Now check if proof-of-stake hash meets target protocol diff --git a/src/main.cpp b/src/main.cpp index 32aad9f3aa..4fbe0e2740 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1675,7 +1675,7 @@ bool LoadBlockIndex(bool fAllowNew) txNew.nTime = 1413033777; txNew.vin.resize(1); txNew.vout.resize(1); - txNew.vin[0].scriptSig = CScript() << 0 << CBigNum(42) << vector((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp)); + txNew.vin[0].scriptSig = CScript() << 0 << CScriptNum(42) << vector((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp)); txNew.vout[0].SetEmpty(); CBlock block; block.vtx.push_back(txNew); diff --git a/src/miner.cpp b/src/miner.cpp index c7c1cde861..59aaf77d08 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -662,7 +662,7 @@ bool CreateCoinStake(CBlock &blocknew, CKey &key, function += ": "; int64_t CoinWeight; - CBigNum StakeKernelHash; + arith_uint256 StakeKernelHash; CTxDB txdb("r"); int64_t StakeWeightSum = 0; double StakeValueSum = 0; @@ -730,9 +730,9 @@ bool CreateCoinStake(CBlock &blocknew, CKey &key, CoinWeight = GRC::CalculateStakeWeightV8(CoinTx, CoinTxN); - StakeKernelHash.setuint256(GRC::CalculateStakeHashV8(block_time, CoinTx, CoinTxN, txnew.nTime, StakeModifier)); + StakeKernelHash = UintToArith256(GRC::CalculateStakeHashV8(block_time, CoinTx, CoinTxN, txnew.nTime, StakeModifier)); - CBigNum StakeTarget; + arith_uint256 StakeTarget; StakeTarget.SetCompact(blocknew.nBits); StakeTarget *= CoinWeight; StakeWeightSum += CoinWeight; diff --git a/src/test/bignum_tests.cpp b/src/test/bignum_tests.cpp deleted file mode 100755 index c3da4a571c..0000000000 --- a/src/test/bignum_tests.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include - -#include "bignum.h" -#include "util.h" - -#include - -BOOST_AUTO_TEST_SUITE(bignum_tests) - -// Unfortunately there's no standard way of preventing a function from being -// inlined, so we define a macro for it. -// -// You should use it like this: -// NOINLINE void function() {...} -#if defined(__GNUC__) -// This also works and will be defined for any compiler implementing GCC -// extensions, such as Clang and ICC. -#define NOINLINE __attribute__((noinline)) -#elif defined(_MSC_VER) -#define NOINLINE __declspec(noinline) -#else -// We give out a warning because it impacts the correctness of one bignum test. -#warning You should define NOINLINE for your compiler. -#define NOINLINE -#endif - -// For the following test case, it is useful to use additional tools. -// -// The simplest one to use is the compiler flag -ftrapv, which detects integer -// overflows and similar errors. However, due to optimizations and compilers -// taking advantage of undefined behavior sometimes it may not actually detect -// anything. -// -// You can also use compiler-based stack protection to possibly detect possible -// stack buffer overruns. -// -// For more accurate diagnostics, you can use an undefined arithmetic operation -// detector such as the clang's undefined behaviour checker. -// See also: https://clang.llvm.org/docs/UsersManual.html#controlling-code-generation -// -// It might also be useful to use Google's AddressSanitizer to detect -// stack buffer overruns, which valgrind can't currently detect. - -// Let's force this code not to be inlined, in order to actually -// test a generic version of the function. This increases the chance -// that -ftrapv will detect overflows. -NOINLINE void mysetint64(CBigNum& num, int64_t n) -{ - num.setint64(n); -} - -// For each number, we do 2 tests: one with inline code, then we reset the -// value to 0, then the second one with a non-inlined function. -BOOST_AUTO_TEST_CASE(bignum_setint64) -{ - int64_t n; - - { - n = 0; - CBigNum num(n); - BOOST_CHECK(num.ToString() == "0"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "0"); - } - { - n = 1; - CBigNum num(n); - BOOST_CHECK(num.ToString() == "1"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "1"); - } - { - n = -1; - CBigNum num(n); - BOOST_CHECK(num.ToString() == "-1"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "-1"); - } - { - n = 5; - CBigNum num(n); - BOOST_CHECK(num.ToString() == "5"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "5"); - } - { - n = -5; - CBigNum num(n); - BOOST_CHECK(num.ToString() == "-5"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "-5"); - } - { - n = std::numeric_limits::min(); - CBigNum num(n); - BOOST_CHECK(num.ToString() == "-9223372036854775808"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "-9223372036854775808"); - } - { - n = std::numeric_limits::max(); - CBigNum num(n); - BOOST_CHECK(num.ToString() == "9223372036854775807"); - num.setulong(0); - BOOST_CHECK(num.ToString() == "0"); - mysetint64(num, n); - BOOST_CHECK(num.ToString() == "9223372036854775807"); - } -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/validation.cpp b/src/validation.cpp index ee78d93bf5..d9b12eb165 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -485,7 +485,7 @@ bool ConnectInputs(CTransaction& tx, CTxDB& txdb, MapPrevTx inputs, std::map