Redefine uint160.nWidth, uint160.pn, uint256.nWidth and uint256.pn as protected prope...
[NovacoinLibrary.git] / Novacoin / uint256.cs
index 15a9931..0c451ef 100644 (file)
@@ -4,38 +4,37 @@ using System.Diagnostics.Contracts;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Numerics;
 
 namespace Novacoin
 {
     public class uint256 : base_uint
     {
-        new public readonly int nWidth = 8;
+        new protected int nWidth {
+            get { return base.nWidth; }
+            private set { base.nWidth = value; }
+        }
+        new protected uint[] pn {
+            get { return base.pn; }
+            private set { base.pn = value; }
+        }
 
         public uint256()
         {
+            nWidth = 8;
             pn = new uint[nWidth];
-
-            for (int i = 0; i < nWidth; i++)
-            {
-                pn[i] = 0;
-            }
         }
 
-        public uint256(uint256 b)
+        public uint256(uint256 b) : this()
         {
-            pn = new uint[nWidth];
-
             for (int i = 0; i < nWidth; i++)
             {
                 pn[i] = b.pn[i];
             }
         }
 
-
-        public uint256(ulong n)
+        public uint256(ulong n) : this()
         {
-            pn = new uint[nWidth];
-
             pn[0] = (uint)n;
             pn[1] = (uint)(n >> 32);
             for (int i = 2; i < nWidth; i++)
@@ -44,14 +43,14 @@ namespace Novacoin
             }
         }
 
-        public uint256(byte[] bytes)
+        public uint256(byte[] bytes) : this()
         {
             Contract.Requires<ArgumentException>(bytes.Length == 32, "Incorrect array length");
 
             pn = Interop.ToUInt32Array(bytes);
         }
 
-        public uint256(string hex)
+        public uint256(string hex) : this()
         {
             Contract.Requires<ArgumentException>(hex.Length == 64, "Incorrect string");
 
@@ -59,6 +58,66 @@ namespace Novacoin
             pn = Interop.ToUInt32Array(bytes);
         }
 
+        /// <summary>
+        /// Compact representation of unsigned 256bit numbers.
+        /// 
+        /// N = (-1^sign) * m * 256^(exp-3)
+        /// 
+        /// http://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty
+        /// </summary>
+        public uint Compact
+        {
+            get
+            {
+                int nSize = (bits + 7) / 8;
+                uint nCompact = 0;
+                if (nSize <= 3)
+                    nCompact = ((uint)GetLow64()) << 8 * (3 - nSize);
+                else
+                {
+                    uint256 bn = this >> 8 * (nSize - 3);
+                    nCompact = (uint)bn.GetLow64();
+                }
+
+                if ((nCompact & 0x00800000) != 0)
+                {
+                    nCompact >>= 8;
+                    nSize++;
+                }
+
+                Contract.Assert((nCompact & ~0x007fffff) == 0);
+                Contract.Assert(nSize < 256);
+
+                nCompact |= (uint)nSize << 24;
+                nCompact |= 0;
+
+                return nCompact;
+            }
+            set {
+                int nSize = (int)value >> 24;
+                uint nWord = value & 0x007fffff;
+
+                uint256 i;
+
+                if (nSize <= 3)
+                {
+                    nWord >>= 8 * (3 - nSize);
+                    i = new uint256(nWord);
+                }
+                else
+                {
+                    i = new uint256(nWord);
+                    i <<= 8 * (nSize - 3);
+                }
+
+                pn = i.pn;
+            }
+        }
+
+        private void SetBytes(byte[] bytes)
+        {
+            pn = Interop.ToUInt32Array(Interop.ReverseBytes(bytes));
+        }
 
         public static uint256 operator ~(uint256 a)
         {