uint256: SetCompact() and GetCompact implementations
authorCryptoManiac <balthazar@yandex.ru>
Fri, 8 Apr 2016 18:15:36 +0000 (21:15 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Fri, 8 Apr 2016 18:15:36 +0000 (21:15 +0300)
novacoin-qt.pro
src/kernel_worker.cpp
src/makefile.bsd
src/makefile.linux-mingw
src/makefile.mingw
src/makefile.osx
src/makefile.unix
src/uint256.cpp [new file with mode: 0644]
src/uint256.h

index e59a4ff..c681887 100644 (file)
@@ -363,6 +363,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
     src/qt/secondauthdialog.cpp \
     src/base58.cpp \
     src/cryptogram.cpp \
+    src/uint256.cpp \
     src/ecies.cpp \
     src/ipcollector.cpp
 
index 42e6172..0155361 100644 (file)
@@ -75,13 +75,11 @@ vector<pair<uint256,uint32_t> >& KernelWorker::GetSolutions()
 
 bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, pair<uint32_t, uint32_t> &SearchInterval, pair<uint256, uint32_t> &solution)
 {
-    CBigNum bnTargetPerCoinDay;
-    bnTargetPerCoinDay.SetCompact(nBits);
-
-    CBigNum bnValueIn(nValueIn);
+    uint256 nTargetPerCoinDay;
+    nTargetPerCoinDay.SetCompact(nBits);
 
     // Get maximum possible target to filter out the majority of obviously insufficient hashes
-    auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
+    auto nMaxTarget = nTargetPerCoinDay * (uint64_t)nValueIn * (uint64_t)nStakeMaxAge / (uint64_t)COIN / (uint64_t)nOneDay;
 
     SHA256_CTX ctx, workerCtx;
     // Init new sha256 context and update it
@@ -110,14 +108,12 @@ bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTx
         if (hashProofOfStake > nMaxTarget)
             continue;
 
-        auto bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
-        auto bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
+        auto nCoinDayWeight = uint256(nValueIn) * (uint64_t)GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / (uint64_t)COIN / (uint64_t)nOneDay; // TODO: Stop using signed types for value, time, weight and so on, because all these casts are really stupid.
+        auto nTargetProofOfStake = nCoinDayWeight * nTargetPerCoinDay;
 
-        if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
+        if (nTargetProofOfStake >= hashProofOfStake)
         {
-            solution.first = hashProofOfStake;
-            solution.second = nTimeTx;
-
+            solution = { hashProofOfStake, nTimeTx };
             return true;
         }
     }
index 17cb56d..1ae9cf6 100644 (file)
@@ -135,6 +135,7 @@ OBJS= \
     obj/walletdb.o \
     obj/noui.o \
     obj/kernel.o \
+    obj/uint256.o \
     obj/kernel_worker.o \
     obj/ecies.o \
     obj/cryptogram.o \
index 5660e42..a2f4720 100644 (file)
@@ -99,6 +99,7 @@ OBJS= \
     obj/walletdb.o \
     obj/noui.o \
     obj/kernel.o \
+    obj/uint256.o \
     obj/kernel_worker.o \
     obj/ecies.o \
     obj/cryptogram.o \
index b2c1d92..d8d81c4 100644 (file)
@@ -89,6 +89,7 @@ OBJS= \
     obj/walletdb.o \
     obj/noui.o \
     obj/kernel.o \
+    obj/uint256.o \
     obj/kernel_worker.o \
     obj/ecies.o \
     obj/cryptogram.o \
index a288717..da13b8a 100644 (file)
@@ -97,6 +97,7 @@ OBJS= \
     obj/walletdb.o \
     obj/noui.o \
     obj/kernel.o \
+    obj/uint256.o \
     obj/kernel_worker.o \
     obj/ecies.o \
     obj/cryptogram.o \
index f7b5129..13b75a5 100644 (file)
@@ -136,6 +136,7 @@ OBJS= \
     obj/walletdb.o \
     obj/noui.o \
     obj/kernel.o \
+    obj/uint256.o \
     obj/kernel_worker.o \
     obj/ecies.o \
     obj/cryptogram.o \
diff --git a/src/uint256.cpp b/src/uint256.cpp
new file mode 100644 (file)
index 0000000..aad4bac
--- /dev/null
@@ -0,0 +1,217 @@
+// 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 http://www.opensource.org/licenses/mit-license.php.
+
+#include <limits>
+#include <stdio.h>
+#include <string.h>
+#include <cassert>
+#include  <stdexcept>
+
+#include "uint256.h"
+
+using namespace std;
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// uint160
+//
+
+uint160::uint160()
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = 0;
+}
+
+uint160::uint160(const basetype& b)
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = b.pn[i];
+}
+
+uint160& uint160::operator=(const basetype& b)
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = b.pn[i];
+    return *this;
+}
+
+uint160::uint160(uint64_t b)
+{
+    pn[0] = (uint32_t)b;
+    pn[1] = (uint32_t)(b >> 32);
+    for (int i = 2; i < WIDTH; i++)
+        pn[i] = 0;
+}
+
+uint160& uint160::operator=(uint64_t b)
+{
+    pn[0] = (uint32_t)b;
+    pn[1] = (uint32_t)(b >> 32);
+    for (int i = 2; i < WIDTH; i++)
+        pn[i] = 0;
+    return *this;
+}
+
+uint160::uint160(const std::string& str)
+{
+    SetHex(str);
+}
+
+uint160::uint160(const std::vector<unsigned char>& vch)
+{
+    if (vch.size() == sizeof(pn))
+        memcpy(pn, &vch[0], sizeof(pn));
+    else
+        *this = 0;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// uint256
+//
+
+uint256::uint256()
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = 0;
+}
+
+uint256::uint256(const basetype& b)
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = b.pn[i];
+}
+
+uint256& uint256::operator=(const basetype& b)
+{
+    for (int i = 0; i < WIDTH; i++)
+        pn[i] = b.pn[i];
+    return *this;
+}
+
+uint256::uint256(uint64_t b)
+{
+    pn[0] = (uint32_t)b;
+    pn[1] = (uint32_t)(b >> 32);
+    for (int i = 2; i < WIDTH; i++)
+        pn[i] = 0;
+}
+
+uint256& uint256::operator=(uint64_t b)
+{
+    pn[0] = (uint32_t)b;
+    pn[1] = (uint32_t)(b >> 32);
+    for (int i = 2; i < WIDTH; i++)
+        pn[i] = 0;
+    return *this;
+}
+
+uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
+{
+    int nSize = nCompact >> 24;
+    uint32_t nWord = nCompact & 0x007fffff;
+    if (nSize <= 3) {
+        nWord >>= 8*(3-nSize);
+        *this = nWord;
+    } else {
+        *this = nWord;
+        *this <<= 8*(nSize-3);
+    }
+    if (pfNegative)
+        *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
+    if (pfOverflow)
+        *pfOverflow = nWord != 0 && ((nSize > 34) ||
+                                 (nWord > 0xff && nSize > 33) ||
+                                 (nWord > 0xffff && nSize > 32));
+    return *this;
+}
+
+uint32_t uint256::GetCompact(bool fNegative) const
+{
+    int nSize = (bits() + 7) / 8;
+    uint32_t nCompact = 0;
+    if (nSize <= 3) {
+        nCompact = Get64(0) << 8*(3-nSize);
+    } else {
+        uint256 n = *this;
+        uint256 bn = n >> 8*(nSize-3);
+        nCompact = bn.Get64(0);
+    }
+    // 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++;
+    }
+    assert((nCompact & ~0x007fffff) == 0);
+    assert(nSize < 256);
+    nCompact |= nSize << 24;
+    nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
+    return nCompact;
+}
+
+uint256& uint256::operator*=(uint32_t b32)
+{
+    uint64_t carry = 0;
+    for (int i = 0; i < WIDTH; i++) {
+        uint64_t n = carry + (uint64_t)b32 * pn[i];
+        pn[i] = n & 0xffffffff;
+        carry = n >> 32;
+    }
+    return *this;
+}
+
+uint256& uint256::operator*=(const uint256& b)
+{
+    uint256 a = *this;
+    *this = 0;
+    for (int j = 0; j < WIDTH; j++) {
+        uint64_t carry = 0;
+        for (int i = 0; i + j < WIDTH; i++) {
+            uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
+            pn[i + j] = n & 0xffffffff;
+            carry = n >> 32;
+        }
+    }
+    return *this;
+}
+
+uint256& uint256::operator/=(const uint256& b)
+{
+    uint256 div = b;     // make a copy, so we can shift.
+    uint256 num = *this; // make a copy, so we can subtract.
+    *this = 0;                   // the quotient.
+    int num_bits = num.bits();
+    int div_bits = div.bits();
+    if (div_bits == 0)
+        throw uint256_error("Division by zero");
+    if (div_bits > num_bits) // the result is certainly 0.
+        return *this;
+    int shift = num_bits - div_bits;
+    div <<= shift; // shift so that div and num align.
+    while (shift >= 0) {
+        if (num >= div) {
+            num -= div;
+            pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
+        }
+        div >>= 1; // shift back.
+        shift--;
+    }
+    // num now contains the remainder of the division.
+    return *this;
+}
+
+uint256::uint256(const std::string& str)
+{
+    SetHex(str);
+}
+
+uint256::uint256(const std::vector<unsigned char>& vch)
+{
+    if (vch.size() == sizeof(pn))
+        memcpy(pn, &vch[0], sizeof(pn));
+    else
+        *this = 0;
+}
index 3bb0167..10eb332 100644 (file)
@@ -5,15 +5,13 @@
 #ifndef BITCOIN_UINT256_H
 #define BITCOIN_UINT256_H
 
-#include <limits.h>
-#include <stdio.h>
+#include <limits>
 #include <string.h>
 #include <string>
 #include <vector>
 #include <stdint.h>
 
-inline int Testuint256AdHoc(std::vector<std::string> vArg);
-
+using namespace std;
 
 
 /** Base class without constructors for uint256 and uint160.
@@ -27,6 +25,14 @@ protected:
     uint32_t pn[WIDTH];
 public:
 
+    const base_uint max()
+    {
+        base_uint ret;
+        for (int i = 0; i < WIDTH; i++)
+            ret.pn[i] = std::numeric_limits<uint32_t>::max();
+        return ret;
+    }
+
     bool operator!() const
     {
         for (int i = 0; i < WIDTH; i++)
@@ -374,6 +380,20 @@ public:
         return sizeof(pn);
     }
 
+    unsigned int bits() const
+    {
+        for (int pos = WIDTH - 1; pos >= 0; pos--) {
+            if (pn[pos]) {
+                for (int bits = 31; bits > 0; bits--) {
+                    if (pn[pos] & 1 << bits)
+                        return 32 * pos + bits + 1;
+                }
+                return 32 * pos + 1;
+            }
+        }
+        return 0;
+    }
+
     template<typename Stream>
     void Serialize(Stream& s, int nType, int nVersion) const
     {
@@ -388,7 +408,6 @@ public:
 
     friend class uint160;
     friend class uint256;
-    friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
 };
 
 typedef base_uint<160> base_uint160;
@@ -412,54 +431,13 @@ class uint160 : public base_uint160
 public:
     typedef base_uint160 basetype;
 
-    uint160()
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = 0;
-    }
-
-    uint160(const basetype& b)
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = b.pn[i];
-    }
-
-    uint160& operator=(const basetype& b)
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = b.pn[i];
-        return *this;
-    }
-
-    uint160(uint64_t b)
-    {
-        pn[0] = (uint32_t)b;
-        pn[1] = (uint32_t)(b >> 32);
-        for (int i = 2; i < WIDTH; i++)
-            pn[i] = 0;
-    }
-
-    uint160& operator=(uint64_t b)
-    {
-        pn[0] = (uint32_t)b;
-        pn[1] = (uint32_t)(b >> 32);
-        for (int i = 2; i < WIDTH; i++)
-            pn[i] = 0;
-        return *this;
-    }
-
-    explicit uint160(const std::string& str)
-    {
-        SetHex(str);
-    }
-
-    explicit uint160(const std::vector<unsigned char>& vch)
-    {
-        if (vch.size() == sizeof(pn))
-            memcpy(pn, &vch[0], sizeof(pn));
-        else
-            *this = 0;
-    }
+    uint160();
+    uint160(const basetype& b);
+    uint160& operator=(const basetype& b);
+    uint160(uint64_t b);
+    uint160& operator=(uint64_t b);
+    explicit uint160(const std::string& str);
+    explicit uint160(const std::vector<unsigned char>& vch);
 };
 
 inline bool operator==(const uint160& a, uint64_t b)                           { return (base_uint160)a == b; }
@@ -521,60 +499,37 @@ inline const uint160 operator-(const uint160& a, const uint160& b)      { return
 // uint256
 //
 
+
+/** Errors thrown by the uint256 class */
+class uint256_error : public std::exception
+{
+public:
+        explicit uint256_error(const std::string& error_message)  : what_(error_message) {}
+
+        virtual const char* what() const throw () { return what_.c_str(); }
+        virtual ~uint256_error() throw () {}
+private:
+        std::string what_; 
+};
+
 /** 256-bit unsigned integer */
 class uint256 : public base_uint256
 {
 public:
     typedef base_uint256 basetype;
 
-    uint256()
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = 0;
-    }
-
-    uint256(const basetype& b)
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = b.pn[i];
-    }
-
-    uint256& operator=(const basetype& b)
-    {
-        for (int i = 0; i < WIDTH; i++)
-            pn[i] = b.pn[i];
-        return *this;
-    }
-
-    uint256(uint64_t b)
-    {
-        pn[0] = (uint32_t)b;
-        pn[1] = (uint32_t)(b >> 32);
-        for (int i = 2; i < WIDTH; i++)
-            pn[i] = 0;
-    }
-
-    uint256& operator=(uint64_t b)
-    {
-        pn[0] = (uint32_t)b;
-        pn[1] = (uint32_t)(b >> 32);
-        for (int i = 2; i < WIDTH; i++)
-            pn[i] = 0;
-        return *this;
-    }
-
-    explicit uint256(const std::string& str)
-    {
-        SetHex(str);
-    }
-
-    explicit uint256(const std::vector<unsigned char>& vch)
-    {
-        if (vch.size() == sizeof(pn))
-            memcpy(pn, &vch[0], sizeof(pn));
-        else
-            *this = 0;
-    }
+    uint256();
+    uint256(const basetype& b);
+    uint256& operator=(const basetype& b);
+    uint256(uint64_t b);
+    uint256& operator=(uint64_t b);
+    uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
+    uint32_t GetCompact(bool fNegative = false) const;
+    uint256& operator*=(uint32_t b32);
+    uint256& operator*=(const uint256& b);
+    uint256& operator/=(const uint256& b);
+    explicit uint256(const std::string& str);
+    explicit uint256(const std::vector<unsigned char>& vch);
 };
 
 inline bool operator==(const uint256& a, uint64_t b)                           { return (base_uint256)a == b; }
@@ -588,6 +543,8 @@ inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { r
 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
+inline const uint256 operator*(const base_uint256& a, const base_uint256& b) { return uint256(a) *= b; }
+inline const uint256 operator/(const base_uint256& a, const base_uint256& b) { return uint256(a) /= b; }
 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
 
 inline bool operator<(const base_uint256& a, const uint256& b)          { return (base_uint256)a <  (base_uint256)b; }
@@ -600,6 +557,8 @@ inline const uint256 operator^(const base_uint256& a, const uint256& b) { return
 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a &  (base_uint256)b; }
 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a |  (base_uint256)b; }
 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a +  (base_uint256)b; }
+inline const uint256 operator*(const base_uint256& a, const uint256& b) { return (base_uint256)a *  (base_uint256)b; }
+inline const uint256 operator/(const base_uint256& a, const uint256& b) { return (base_uint256)a /  (base_uint256)b; }
 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a -  (base_uint256)b; }
 
 inline bool operator<(const uint256& a, const base_uint256& b)          { return (base_uint256)a <  (base_uint256)b; }