Merge pull request #361 from svost/master
[novacoin.git] / src / bignum.h
index 6b2bada..4bda99d 100644 (file)
@@ -8,8 +8,7 @@
 #include <stdexcept>
 #include <vector>
 #include <openssl/bn.h>
-
-#include "util.h" // for uint64
+#include "util.h"
 
 /** Errors thrown by the bignum class */
 class bignum_error : public std::runtime_error
@@ -78,20 +77,18 @@ public:
         BN_clear_free(this);
     }
 
-    //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(int64 n)            { BN_init(this); 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); }
-    CBigNum(uint64 n)           { BN_init(this); setuint64(n); }
-    explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
+    CBigNum(int8_t  n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
+    CBigNum(int16_t n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
+    CBigNum(int32_t n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
+    CBigNum(int64_t n)  { BN_init(this); if (n >= 0) setuint64(n); else setint64(n); }
 
-    explicit CBigNum(const std::vector<unsigned char>& vch)
+    CBigNum(uint8_t  n) { BN_init(this); setuint32(n); }
+    CBigNum(uint16_t n) { BN_init(this); setuint32(n); }
+    CBigNum(uint32_t n) { BN_init(this); setuint32(n); }
+    CBigNum(uint64_t n) { BN_init(this); setuint64(n); }
+
+    explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
+    explicit CBigNum(const std::vector<uint8_t>& vch)
     {
         BN_init(this);
         setvch(vch);
@@ -131,39 +128,34 @@ public:
     }
 
 
-    void setulong(unsigned long n)
+    void setuint32(uint32_t n)
     {
         if (!BN_set_word(this, n))
-            throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
-    }
-
-    unsigned long getulong() const
-    {
-        return BN_get_word(this);
+            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);
     }
 
-    int getint() const
+    int32_t getint32() const
     {
-        unsigned long n = BN_get_word(this);
+        uint64_t n = BN_get_word(this);
         if (!BN_is_negative(this))
-            return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
+            return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::max() : (int32_t)n);
         else
-            return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
+            return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::min() : -(int32_t)n);
     }
 
-    void setint64(int64 sn)
+    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 n;
+        uint64_t n;
 
-        if (sn < (int64)0)
+        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);
@@ -177,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)
             {
@@ -191,37 +183,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), this);
     }
 
-    uint64 getuint64()
+    uint64_t getuint64()
     {
-        unsigned int nSize = BN_bn2mpi(this, NULL);
+        size_t nSize = BN_bn2mpi(this, NULL);
         if (nSize < 4)
             return 0;
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &vch[0]);
         if (vch.size() > 4)
             vch[4] &= 0x7f;
-        uint64 n = 0;
-        for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
-            ((unsigned char*)&n)[i] = vch[j];
+        uint64_t n = 0;
+        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 setuint64(uint64 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)
             {
@@ -233,24 +236,67 @@ 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), this);
+    }
+
+    void setuint160(uint160 n)
+    {
+        uint8_t pch[sizeof(n) + 6];
+        uint8_t* p = pch + 4;
+        bool fLeadingZeroes = true;
+        uint8_t* pbegin = (uint8_t*)&n;
+        uint8_t* psrc = pbegin + sizeof(n);
+        while (psrc != pbegin)
+        {
+            uint8_t c = *(--psrc);
+            if (fLeadingZeroes)
+            {
+                if (c == 0)
+                    continue;
+                if (c & 0x80)
+                    *p++ = 0;
+                fLeadingZeroes = false;
+            }
+            *p++ = c;
+        }
+        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, (int) (p - pch), this);
+    }
+
+    uint160 getuint160() const
+    {
+        unsigned int nSize = BN_bn2mpi(this, NULL);
+        if (nSize < 4)
+            return 0;
+        std::vector<uint8_t> vch(nSize);
+        BN_bn2mpi(this, &vch[0]);
+        if (vch.size() > 4)
+            vch[4] &= 0x7f;
+        uint160 n = 0;
+        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)
@@ -261,12 +307,12 @@ 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), this);
     }
 
     uint256 getuint256() const
@@ -274,21 +320,39 @@ public:
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize < 4)
             return 0;
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> 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<uint8_t>& vchBytes)
+    {
+        BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), this);
+    }
 
-    void setvch(const std::vector<unsigned char>& vch)
+    std::vector<uint8_t> getBytes() const
     {
-        std::vector<unsigned char> vch2(vch.size() + 4);
-        unsigned int nSize = vch.size();
+        int nBytes = BN_num_bytes(this);
+
+        std::vector<uint8_t> vchBytes(nBytes);
+
+        int n = BN_bn2bin(this, &vchBytes[0]);
+        if (n != nBytes) {
+            throw bignum_error("CBigNum::getBytes : BN_bn2bin failed");
+        }
+
+        return vchBytes;
+    }
+
+    void setvch(const std::vector<uint8_t>& vch)
+    {
+        std::vector<uint8_t> 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;
@@ -297,40 +361,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(), this);
     }
 
-    std::vector<unsigned char> getvch() const
+    std::vector<uint8_t> getvch() const
     {
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize <= 4)
-            return std::vector<unsigned char>();
-        std::vector<unsigned char> vch(nSize);
+            return std::vector<uint8_t>();
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &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<unsigned char> vch(4 + nSize);
+        uint32_t nSize = nCompact >> 24;
+        std::vector<uint8_t> 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;
     }
 
-    unsigned int GetCompact() const
+    uint32_t GetCompact() const
     {
-        unsigned int nSize = BN_bn2mpi(this, NULL);
-        std::vector<unsigned char> vch(nSize);
+        uint32_t nSize = BN_bn2mpi(this, NULL);
+        std::vector<uint8_t> vch(nSize);
         nSize -= 4;
         BN_bn2mpi(this, &vch[0]);
-        unsigned int nCompact = nSize << 24;
+        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);
@@ -360,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)
@@ -384,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.getulong();
+            uint32_t c = rem.getuint32();
             str += "0123456789abcdef"[c];
         }
         if (BN_is_negative(this))
@@ -412,7 +476,7 @@ public:
     template<typename Stream>
     void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
     {
-        std::vector<unsigned char> vch;
+        std::vector<uint8_t> vch;
         ::Unserialize(s, vch, nType, nVersion);
         setvch(vch);
     }
@@ -449,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;
     }
 
@@ -526,7 +590,7 @@ public:
         if(ret < 0){
             throw bignum_error("CBigNum::isPrime :BN_is_prime");
         }
-        return ret;
+        return ret != 0;
     }
 
     bool isOne() const {