port code from +17 branch, fix operator *=
[novacoin.git] / src / bignum.h
index 1c9d7fb..e3838bc 100644 (file)
@@ -5,10 +5,14 @@
 #ifndef BITCOIN_BIGNUM_H
 #define BITCOIN_BIGNUM_H
 
+
+#include "util.h"
+#include "uint256.h"
+
+#include <openssl/bn.h>
+
 #include <stdexcept>
 #include <vector>
-#include <openssl/bn.h>
-#include "util.h"
 
 /** Errors thrown by the bignum class */
 class bignum_error : public std::runtime_error
@@ -59,29 +63,41 @@ public:
 
     CBigNum(const CBigNum& b)
     {
-        bn = BN_new();
-        if (!BN_copy(bn, b.bn))
+        BIGNUM *dup = BN_dup(b.bn);
+        if (!dup)
         {
-            BN_clear_free(bn);
-            throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
+            throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_dup failed");
         }
+        bn = dup;
     }
 
     CBigNum& operator=(const CBigNum& b)
     {
-        bn = BN_new();
-        if (!BN_copy(bn, b.bn)) {
-            BN_clear_free(bn);
-            throw bignum_error("CBigNum::operator= : BN_copy failed");
+        BIGNUM *dup = BN_dup(b.bn);
+        if (!dup)
+        {
+            throw bignum_error("CBigNum::operator= : BN_dup failed");
         }
+        bn = dup;
         return (*this);
     }
 
+    CBigNum(const BIGNUM *bnp) {
+        BIGNUM *dup = BN_dup(bnp);
+        if (!dup)
+        {
+            throw bignum_error("CBigNum::CBigNum(const BIGNUM*) : BN_dup failed");
+        }
+        bn = dup;
+    }
+
     ~CBigNum()
     {
         BN_clear_free(bn);
     }
 
+    CBigNum(bool n)     { bn = BN_new(); setuint32(n); }
+
     CBigNum(int8_t  n)  { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); }
     CBigNum(int16_t n)  { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); }
     CBigNum(int32_t n)  { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); }
@@ -468,7 +484,7 @@ public:
     }
 
     BIGNUM* get() const {
-        return bn;
+        return BN_dup(bn);
     }
 
     unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
@@ -595,7 +611,7 @@ public:
     */
     bool isPrime(const int checks=BN_prime_checks) const {
         CAutoBN_CTX pctx;
-        int ret = BN_is_prime(bn, checks, NULL, pctx, NULL);
+        int ret = BN_is_prime_ex(bn, checks, pctx, NULL);
         if(ret < 0){
             throw bignum_error("CBigNum::isPrime :BN_is_prime");
         }
@@ -708,9 +724,7 @@ public:
     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);
     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);
     
     friend inline const CBigNum operator-(const CBigNum& a);