Browse Source

Use arith_uint256 where necessary

Also add conversion from/to uint256 where needed.
0.13
Wladimir J. van der Laan 10 years ago
parent
commit
734f85c4f0
  1. 5
      src/chain.h
  2. 4
      src/chainparams.cpp
  3. 6
      src/chainparams.h
  4. 3
      src/key.cpp
  5. 9
      src/main.cpp
  6. 4
      src/miner.cpp
  7. 15
      src/pow.cpp
  8. 3
      src/pow.h
  9. 4
      src/rpcmining.cpp
  10. 12
      src/test/pmt_tests.cpp
  11. 12
      src/test/skiplist_tests.cpp

5
src/chain.h

@ -6,6 +6,7 @@
#ifndef BITCOIN_CHAIN_H #ifndef BITCOIN_CHAIN_H
#define BITCOIN_CHAIN_H #define BITCOIN_CHAIN_H
#include "arith_uint256.h"
#include "primitives/block.h" #include "primitives/block.h"
#include "pow.h" #include "pow.h"
#include "tinyformat.h" #include "tinyformat.h"
@ -117,7 +118,7 @@ public:
unsigned int nUndoPos; unsigned int nUndoPos;
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
uint256 nChainWork; arith_uint256 nChainWork;
//! Number of transactions in this block. //! Number of transactions in this block.
//! Note: in a potential headers-first mode, this number cannot be relied upon //! Note: in a potential headers-first mode, this number cannot be relied upon
@ -150,7 +151,7 @@ public:
nFile = 0; nFile = 0;
nDataPos = 0; nDataPos = 0;
nUndoPos = 0; nUndoPos = 0;
nChainWork = uint256(); nChainWork = arith_uint256();
nTx = 0; nTx = 0;
nChainTx = 0; nChainTx = 0;
nStatus = 0; nStatus = 0;

4
src/chainparams.cpp

@ -114,7 +114,7 @@ public:
pchMessageStart[3] = 0xd9; pchMessageStart[3] = 0xd9;
vAlertPubKey = ParseHex("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284"); vAlertPubKey = ParseHex("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284");
nDefaultPort = 8333; nDefaultPort = 8333;
bnProofOfWorkLimit = ~uint256(0) >> 32; bnProofOfWorkLimit = ~arith_uint256(0) >> 32;
nSubsidyHalvingInterval = 210000; nSubsidyHalvingInterval = 210000;
nEnforceBlockUpgradeMajority = 750; nEnforceBlockUpgradeMajority = 750;
nRejectBlockOutdatedMajority = 950; nRejectBlockOutdatedMajority = 950;
@ -259,7 +259,7 @@ public:
nMinerThreads = 1; nMinerThreads = 1;
nTargetTimespan = 14 * 24 * 60 * 60; //! two weeks nTargetTimespan = 14 * 24 * 60 * 60; //! two weeks
nTargetSpacing = 10 * 60; nTargetSpacing = 10 * 60;
bnProofOfWorkLimit = ~uint256(0) >> 1; bnProofOfWorkLimit = ~arith_uint256(0) >> 1;
genesis.nTime = 1296688602; genesis.nTime = 1296688602;
genesis.nBits = 0x207fffff; genesis.nBits = 0x207fffff;
genesis.nNonce = 2; genesis.nNonce = 2;

6
src/chainparams.h

@ -10,7 +10,7 @@
#include "checkpoints.h" #include "checkpoints.h"
#include "primitives/block.h" #include "primitives/block.h"
#include "protocol.h" #include "protocol.h"
#include "uint256.h" #include "arith_uint256.h"
#include <vector> #include <vector>
@ -45,7 +45,7 @@ public:
const MessageStartChars& MessageStart() const { return pchMessageStart; } const MessageStartChars& MessageStart() const { return pchMessageStart; }
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; } const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; } int GetDefaultPort() const { return nDefaultPort; }
const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; } const arith_uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; }
int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; } int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; }
/** Used to check majorities for block version upgrade */ /** Used to check majorities for block version upgrade */
int EnforceBlockUpgradeMajority() const { return nEnforceBlockUpgradeMajority; } int EnforceBlockUpgradeMajority() const { return nEnforceBlockUpgradeMajority; }
@ -87,7 +87,7 @@ protected:
//! Raw pub key bytes for the broadcast alert signing key. //! Raw pub key bytes for the broadcast alert signing key.
std::vector<unsigned char> vAlertPubKey; std::vector<unsigned char> vAlertPubKey;
int nDefaultPort; int nDefaultPort;
uint256 bnProofOfWorkLimit; arith_uint256 bnProofOfWorkLimit;
int nSubsidyHalvingInterval; int nSubsidyHalvingInterval;
int nEnforceBlockUpgradeMajority; int nEnforceBlockUpgradeMajority;
int nRejectBlockOutdatedMajority; int nRejectBlockOutdatedMajority;

3
src/key.cpp

@ -4,6 +4,7 @@
#include "key.h" #include "key.h"
#include "arith_uint256.h"
#include "crypto/hmac_sha512.h" #include "crypto/hmac_sha512.h"
#include "crypto/rfc6979_hmac_sha256.h" #include "crypto/rfc6979_hmac_sha256.h"
#include "eccryptoverify.h" #include "eccryptoverify.h"
@ -81,7 +82,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
do { do {
uint256 nonce; uint256 nonce;
prng.Generate((unsigned char*)&nonce, 32); prng.Generate((unsigned char*)&nonce, 32);
nonce += test_case; nonce = ArithToUint256(UintToArith256(nonce) + test_case);
int nSigLen = 72; int nSigLen = 72;
int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce); int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
nonce = uint256(); nonce = uint256();

9
src/main.cpp

@ -5,6 +5,7 @@
#include "main.h" #include "main.h"
#include "arith_uint256.h"
#include "addrman.h" #include "addrman.h"
#include "alert.h" #include "alert.h"
#include "chainparams.h" #include "chainparams.h"
@ -3607,7 +3608,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (hashSalt.IsNull()) if (hashSalt.IsNull())
hashSalt = GetRandHash(); hashSalt = GetRandHash();
uint64_t hashAddr = addr.GetHash(); uint64_t hashAddr = addr.GetHash();
uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)); uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
hashRand = Hash(BEGIN(hashRand), END(hashRand)); hashRand = Hash(BEGIN(hashRand), END(hashRand));
multimap<uint256, CNode*> mapMix; multimap<uint256, CNode*> mapMix;
BOOST_FOREACH(CNode* pnode, vNodes) BOOST_FOREACH(CNode* pnode, vNodes)
@ -3616,7 +3617,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
continue; continue;
unsigned int nPointer; unsigned int nPointer;
memcpy(&nPointer, &pnode, sizeof(nPointer)); memcpy(&nPointer, &pnode, sizeof(nPointer));
uint256 hashKey = hashRand ^ nPointer; uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
hashKey = Hash(BEGIN(hashKey), END(hashKey)); hashKey = Hash(BEGIN(hashKey), END(hashKey));
mapMix.insert(make_pair(hashKey, pnode)); mapMix.insert(make_pair(hashKey, pnode));
} }
@ -4485,9 +4486,9 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
static uint256 hashSalt; static uint256 hashSalt;
if (hashSalt.IsNull()) if (hashSalt.IsNull())
hashSalt = GetRandHash(); hashSalt = GetRandHash();
uint256 hashRand = inv.hash ^ hashSalt; uint256 hashRand = ArithToUint256(UintToArith256(inv.hash) ^ UintToArith256(hashSalt));
hashRand = Hash(BEGIN(hashRand), END(hashRand)); hashRand = Hash(BEGIN(hashRand), END(hashRand));
bool fTrickleWait = ((hashRand & 3) != 0); bool fTrickleWait = ((UintToArith256(hashRand) & 3) != 0);
if (fTrickleWait) if (fTrickleWait)
{ {

4
src/miner.cpp

@ -481,7 +481,7 @@ void static BitcoinMiner(CWallet *pwallet)
// Search // Search
// //
int64_t nStart = GetTime(); int64_t nStart = GetTime();
uint256 hashTarget = uint256().SetCompact(pblock->nBits); arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
uint256 hash; uint256 hash;
uint32_t nNonce = 0; uint32_t nNonce = 0;
uint32_t nOldNonce = 0; uint32_t nOldNonce = 0;
@ -493,7 +493,7 @@ void static BitcoinMiner(CWallet *pwallet)
// Check if something found // Check if something found
if (fFound) if (fFound)
{ {
if (hash <= hashTarget) if (UintToArith256(hash) <= hashTarget)
{ {
// Found a solution // Found a solution
pblock->nNonce = nNonce; pblock->nNonce = nNonce;

15
src/pow.cpp

@ -5,6 +5,7 @@
#include "pow.h" #include "pow.h"
#include "arith_uint256.h"
#include "chain.h" #include "chain.h"
#include "chainparams.h" #include "chainparams.h"
#include "primitives/block.h" #include "primitives/block.h"
@ -56,8 +57,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
nActualTimespan = Params().TargetTimespan()*4; nActualTimespan = Params().TargetTimespan()*4;
// Retarget // Retarget
uint256 bnNew; arith_uint256 bnNew;
uint256 bnOld; arith_uint256 bnOld;
bnNew.SetCompact(pindexLast->nBits); bnNew.SetCompact(pindexLast->nBits);
bnOld = bnNew; bnOld = bnNew;
bnNew *= nActualTimespan; bnNew *= nActualTimespan;
@ -79,7 +80,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
{ {
bool fNegative; bool fNegative;
bool fOverflow; bool fOverflow;
uint256 bnTarget; arith_uint256 bnTarget;
if (Params().SkipProofOfWorkCheck()) if (Params().SkipProofOfWorkCheck())
return true; return true;
@ -91,22 +92,22 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
return error("CheckProofOfWork() : nBits below minimum work"); return error("CheckProofOfWork() : nBits below minimum work");
// Check proof of work matches claimed amount // Check proof of work matches claimed amount
if (hash > bnTarget) if (UintToArith256(hash) > bnTarget)
return error("CheckProofOfWork() : hash doesn't match nBits"); return error("CheckProofOfWork() : hash doesn't match nBits");
return true; return true;
} }
uint256 GetBlockProof(const CBlockIndex& block) arith_uint256 GetBlockProof(const CBlockIndex& block)
{ {
uint256 bnTarget; arith_uint256 bnTarget;
bool fNegative; bool fNegative;
bool fOverflow; bool fOverflow;
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
if (fNegative || fOverflow || bnTarget == 0) if (fNegative || fOverflow || bnTarget == 0)
return 0; return 0;
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
// as it's too large for a uint256. However, as 2**256 is at least as large // as it's too large for a arith_uint256. However, as 2**256 is at least as large
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
// or ~bnTarget / (nTarget+1) + 1. // or ~bnTarget / (nTarget+1) + 1.
return (~bnTarget / (bnTarget + 1)) + 1; return (~bnTarget / (bnTarget + 1)) + 1;

3
src/pow.h

@ -11,11 +11,12 @@
class CBlockHeader; class CBlockHeader;
class CBlockIndex; class CBlockIndex;
class uint256; class uint256;
class arith_uint256;
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock); unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */ /** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
bool CheckProofOfWork(uint256 hash, unsigned int nBits); bool CheckProofOfWork(uint256 hash, unsigned int nBits);
uint256 GetBlockProof(const CBlockIndex& block); arith_uint256 GetBlockProof(const CBlockIndex& block);
#endif // BITCOIN_POW_H #endif // BITCOIN_POW_H

4
src/rpcmining.cpp

@ -64,7 +64,7 @@ Value GetNetworkHashPS(int lookup, int height) {
if (minTime == maxTime) if (minTime == maxTime)
return 0; return 0;
uint256 workDiff = pb->nChainWork - pb0->nChainWork; arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
int64_t timeDiff = maxTime - minTime; int64_t timeDiff = maxTime - minTime;
return (int64_t)(workDiff.getdouble() / timeDiff); return (int64_t)(workDiff.getdouble() / timeDiff);
@ -562,7 +562,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
Object aux; Object aux;
aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end()))); aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
uint256 hashTarget = uint256().SetCompact(pblock->nBits); arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
static Array aMutable; static Array aMutable;
if (aMutable.empty()) if (aMutable.empty())

12
src/test/pmt_tests.cpp

@ -6,6 +6,7 @@
#include "serialize.h" #include "serialize.h"
#include "streams.h" #include "streams.h"
#include "uint256.h" #include "uint256.h"
#include "arith_uint256.h"
#include "version.h" #include "version.h"
#include <vector> #include <vector>
@ -22,8 +23,7 @@ public:
void Damage() { void Damage() {
unsigned int n = rand() % vHash.size(); unsigned int n = rand() % vHash.size();
int bit = rand() % 256; int bit = rand() % 256;
uint256 &hash = vHash[n]; *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
hash ^= ((uint256)1 << bit);
} }
}; };
@ -107,7 +107,13 @@ BOOST_AUTO_TEST_CASE(pmt_test1)
BOOST_AUTO_TEST_CASE(pmt_malleability) BOOST_AUTO_TEST_CASE(pmt_malleability)
{ {
std::vector<uint256> vTxid = boost::assign::list_of(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(9)(10); std::vector<uint256> vTxid = boost::assign::list_of
(ArithToUint256(1))(ArithToUint256(2))
(ArithToUint256(3))(ArithToUint256(4))
(ArithToUint256(5))(ArithToUint256(6))
(ArithToUint256(7))(ArithToUint256(8))
(ArithToUint256(9))(ArithToUint256(10))
(ArithToUint256(9))(ArithToUint256(10));
std::vector<bool> vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false); std::vector<bool> vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false);
CPartialMerkleTree tree(vTxid, vMatch); CPartialMerkleTree tree(vTxid, vMatch);

12
src/test/skiplist_tests.cpp

@ -49,12 +49,12 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
std::vector<uint256> vHashMain(100000); std::vector<uint256> vHashMain(100000);
std::vector<CBlockIndex> vBlocksMain(100000); std::vector<CBlockIndex> vBlocksMain(100000);
for (unsigned int i=0; i<vBlocksMain.size(); i++) { for (unsigned int i=0; i<vBlocksMain.size(); i++) {
vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances. vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
vBlocksMain[i].nHeight = i; vBlocksMain[i].nHeight = i;
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL; vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
vBlocksMain[i].phashBlock = &vHashMain[i]; vBlocksMain[i].phashBlock = &vHashMain[i];
vBlocksMain[i].BuildSkip(); vBlocksMain[i].BuildSkip();
BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight); BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1); BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
} }
@ -62,12 +62,12 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
std::vector<uint256> vHashSide(50000); std::vector<uint256> vHashSide(50000);
std::vector<CBlockIndex> vBlocksSide(50000); std::vector<CBlockIndex> vBlocksSide(50000);
for (unsigned int i=0; i<vBlocksSide.size(); i++) { for (unsigned int i=0; i<vBlocksSide.size(); i++) {
vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height. vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
vBlocksSide[i].nHeight = i + 50000; vBlocksSide[i].nHeight = i + 50000;
vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999]; vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
vBlocksSide[i].phashBlock = &vHashSide[i]; vBlocksSide[i].phashBlock = &vHashSide[i];
vBlocksSide[i].BuildSkip(); vBlocksSide[i].BuildSkip();
BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight); BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1); BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
} }
@ -87,13 +87,13 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
// Entries 1 through 11 (inclusive) go back one step each. // Entries 1 through 11 (inclusive) go back one step each.
for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) { for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i); BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i);
} }
// The further ones (excluding the last one) go back with exponential steps. // The further ones (excluding the last one) go back with exponential steps.
unsigned int dist = 2; unsigned int dist = 2;
for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) { for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist); BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist);
dist *= 2; dist *= 2;
} }
} }

Loading…
Cancel
Save