Hash comparison operations
authorCryptoManiac <balthazar.ad@gmail.com>
Fri, 28 Aug 2015 09:52:27 +0000 (12:52 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Fri, 28 Aug 2015 09:52:27 +0000 (12:52 +0300)
Novacoin/Hash.cs
NovacoinTest/Program.cs

index 092db18..92ce2c7 100644 (file)
@@ -90,6 +90,16 @@ namespace Novacoin
             return _hashBytes.SequenceEqual((byte[])item);
         }
 
+        public override bool Equals(object o)
+        {
+            throw new NotSupportedException();
+        }
+
+        public override int GetHashCode()
+        {
+            throw new NotSupportedException();
+        }
+
         public int CompareTo(Hash item)
         {
             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
@@ -111,7 +121,7 @@ namespace Novacoin
         {
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
             
-            for (int i = a.hashSize; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] < b._hashBytes[i])
                     return true;
@@ -126,7 +136,7 @@ namespace Novacoin
         {
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
-            for (int i = a.hashSize; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] < b._hashBytes[i])
                     return true;
@@ -141,7 +151,7 @@ namespace Novacoin
         {
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
-            for (int i = a.hashSize; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] > b._hashBytes[i])
                     return true;
@@ -156,7 +166,7 @@ namespace Novacoin
         {
             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
 
-            for (int i = a.hashSize; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] > b._hashBytes[i])
                     return true;
@@ -167,6 +177,20 @@ namespace Novacoin
             return true;
         }
 
+        public static bool operator ==(Hash a, Hash b)
+        {
+            Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+            return a._hashBytes.SequenceEqual(b._hashBytes);
+        }
+
+        public static bool operator !=(Hash a, Hash b)
+        {
+            Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+            return !a._hashBytes.SequenceEqual(b._hashBytes);
+        }
+
         public override string ToString()
         {
             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
index 7a2c23f..6f2f8e1 100644 (file)
@@ -270,6 +270,20 @@ namespace NovacoinTest
             keyStore.ResetPool();
             Console.WriteLine("Done in {0} ms.", watch.ElapsedMilliseconds);
 
+            Console.WriteLine("Hash comparison tests:");
+
+            ScryptHash256 hash1 = b1.header.Hash;
+            ScryptHash256 hash2 = b2.header.Hash;
+            ScryptHash256 hash3 = veryBigBlock.header.Hash;
+
+            Console.WriteLine("{0} < {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 < hash2);
+            Console.WriteLine("{0} > {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 > hash2);
+            Console.WriteLine("{0} <= {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 <= hash2);
+            Console.WriteLine("{0} >= {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 >= hash2);
+
+            Console.WriteLine("{0} != {1} : {2}", hash1.ToString(), hash2.ToString(), hash1 != hash2);
+            Console.WriteLine("{0} == {1} : {2}", hash3.ToString(), hash3.ToString(), hash3 == hash3);
+
             /* 
             Console.WriteLine("Reading the block file...");
             var bs = new CBlockStore();