Improve CryptoUtils with wrappers for managed implementations of standard hashing...
[NovacoinLibrary.git] / Novacoin / base_uint.cs
index 3ec7d7b..502d871 100644 (file)
@@ -18,6 +18,7 @@
 
 
 using System;
+using System.Collections.Generic;
 using System.Diagnostics.Contracts;
 
 namespace Novacoin
@@ -25,9 +26,9 @@ namespace Novacoin
     /// <summary>
     /// Base class for uint256 and uint160.
     /// </summary>
-    public class base_uint : IComparable<base_uint>, IEquatable<base_uint>
+    public class base_uint : IComparable<base_uint>, IEquatable<base_uint>, IEqualityComparer<base_uint>
     {
-        protected readonly int nWidth;
+        protected int nWidth;
         protected uint[] pn;
 
         public double getDouble()
@@ -54,11 +55,37 @@ namespace Novacoin
             return pn[0];
         }
 
+        /// <summary>
+        /// Total size in bytes.
+        /// </summary>
         public int Size
         {
             get
             {
-                return nWidth;
+                return nWidth * sizeof(uint);
+            }
+        }
+
+        /// <summary>
+        /// Zero or the position of highest non-zero bit plus one.
+        /// </summary>
+        protected int bits
+        {
+            get
+            {
+                for (int pos = nWidth - 1; pos >= 0; pos--)
+                {
+                    if (pn[pos]!=0)
+                    {
+                        for (int bits = 31; bits > 0; bits--)
+                        {
+                            if ((pn[pos] & 1 << bits)!=0)
+                                return 32 * pos + bits + 1;
+                        }
+                        return 32 * pos + 1;
+                    }
+                }
+                return 0;
             }
         }
 
@@ -75,6 +102,7 @@ namespace Novacoin
         }
 
 
+        #region Comparison operations
         public static bool operator <(base_uint a, base_uint b)
         {
             for (int i = a.nWidth - 1; i >= 0; i--)
@@ -138,7 +166,9 @@ namespace Novacoin
             }
             return true;
         }
+        #endregion
 
+        #region Equality operators
         public static bool operator ==(base_uint a, base_uint b)
         {
             if (object.ReferenceEquals(a, b))
@@ -187,22 +217,45 @@ namespace Novacoin
         {
             return (!(a == b));
         }
-
+        #endregion
+
+        #region Cast oerations
+        /// <summary>
+        /// True cast operator
+        /// </summary>
+        /// <param name="a"></param>
+        /// <returns></returns>
         public static bool operator true(base_uint a)
         {
             return (a != 0);
         }
 
+        /// <summary>
+        /// False cast operator.
+        /// </summary>
+        /// <param name="a">Value</param>
+        /// <returns>Boolean result</returns>
         public static bool operator false(base_uint a)
         {
             return (a == 0);
         }
 
+        /// <summary>
+        /// Imlicit byte[] cast operator.
+        /// </summary>
+        /// <param name="a">Value</param>
         public static implicit operator byte[] (base_uint a)
         {
             return Interop.LEBytes(a.pn);
         }
-
+        #endregion
+
+        /// <summary>
+        /// Arrays equality checking helper method.
+        /// </summary>
+        /// <param name="a">Array 1</param>
+        /// <param name="b">Array 2</param>
+        /// <returns>Result.</returns>
         private static bool ArraysEqual(uint[] a, uint[] b)
         {
             Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
@@ -217,19 +270,26 @@ namespace Novacoin
             return true;
         }
 
-        public override int GetHashCode()
+
+        #region IEqualityComparer
+        public bool Equals(base_uint a, base_uint b)
         {
-            int hash = 17;
-            unchecked
+            if (object.ReferenceEquals(a, b))
             {
-                foreach (var element in pn)
-                {
-                    hash = hash * 31 + element.GetHashCode();
-                }
+                return true;
             }
-            return hash;
+
+            return ArraysEqual(a.pn, b.pn);
+        }
+
+        public int GetHashCode(base_uint a)
+        {
+            return a.GetHashCode();
         }
 
+        #endregion
+
+        #region IComparable
         public int CompareTo(base_uint item)
         {
             if (this > item)
@@ -243,7 +303,9 @@ namespace Novacoin
 
             return 0;
         }
+        #endregion
 
+        #region IEquatable
         public bool Equals(base_uint a)
         {
             if (a == null)
@@ -254,11 +316,29 @@ namespace Novacoin
             return ArraysEqual(pn, a.pn);
         }
 
+        public override int GetHashCode()
+        {
+            int hash = 17;
+            unchecked
+            {
+                foreach (var element in pn)
+                {
+                    hash = hash * 31 + element.GetHashCode();
+                }
+            }
+            return hash;
+        }
+
         public override bool Equals(object o)
         {
             return Equals(o as base_uint);
         }
+        #endregion
 
+        /// <summary>
+        /// Serialize to string.
+        /// </summary>
+        /// <returns></returns>
         public override string ToString()
         {
             return Interop.ToHex(Interop.ReverseBytes(this));