Add stake modifier checkpoints & do some cleanup
[NovacoinLibrary.git] / Novacoin / uint256.cs
index 606acf0..856dbdc 100644 (file)
@@ -103,11 +103,11 @@ namespace Novacoin
                 int nSize = (bits + 7) / 8;
                 uint nCompact = 0;
                 if (nSize <= 3)
-                    nCompact = ((uint)Low64) << 8 * (3 - nSize);
+                    nCompact = Low32 << 8 * (3 - nSize);
                 else
                 {
                     uint256 bn = this >> 8 * (nSize - 3);
-                    nCompact = (uint)bn.Low64;
+                    nCompact = bn.Low32;
                 }
 
                 if ((nCompact & 0x00800000) != 0)
@@ -270,7 +270,7 @@ namespace Novacoin
             return result;
         }
 
-        public static uint256 operator *(uint256 a, uint b)
+        public static uint256 operator *(uint256 a, ulong b)
         {
             var result = new uint256();
 
@@ -279,7 +279,7 @@ namespace Novacoin
 
             do
             {
-                c += a.pn[i] * (ulong)b;
+                c += a.pn[i] * b;
                 result.pn[i] = (uint)c;
                 c >>= 32;
             } while (++i < result.nWidth);
@@ -287,6 +287,66 @@ namespace Novacoin
             return result;
         }
 
+        public static uint256 operator *(uint256 a, uint256 b)
+        {
+            if (!a || !b)
+            {
+                // Multiplication by zero results with zero.
+                return 0;
+            }
+            else if (b.bits <= 32)
+            {
+                if (b.pn[0] == 1)
+                {
+                    // If right is 1 then return left operand value
+                    return a;
+                }
+
+                return a * b.pn[0];
+            }
+            else if (a.bits <= 32)
+            {
+                if (a.pn[0] == 1)
+                {
+                    // If left is 1 then return right operand value
+                    return b;
+                }
+
+                return b * a.pn[0];
+            }
+
+            int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
+            int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
+
+            uint256 result = new uint256();
+
+            uint[] left = a.pn.Take(m).ToArray();
+            uint[] right = b.pn.Take(n).ToArray();
+
+            for (int i = 0; i < m; ++i)
+            {
+                uint ai = left[i];
+                int k = i;
+
+                ulong temp = 0;
+                for (int j = 0; j < n; ++j)
+                {
+                    temp = temp + ((ulong)ai) * right[j] + result.pn[k];
+                    result.pn[k++] = (uint)temp;
+                    temp >>= 32;
+                }
+
+                while (temp != 0)
+                {
+                    temp += result.pn[k];
+                    result.pn[k++] = (uint)temp;
+                    temp >>= 32;
+                }
+            }
+
+            return result;
+        }
+
         public static uint operator %(uint256 a, uint b)
         {
             ulong r = 0;