Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[NovacoinLibrary.git] / Novacoin / uint160.cs
index 595aa1a..4da1929 100644 (file)
@@ -1,14 +1,29 @@
-\feffusing System;
-using System.Collections.Generic;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
 using System.Diagnostics.Contracts;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace Novacoin
 {
     public class uint160 : base_uint
     {
+        #region Access to internal representation
         new protected int nWidth
         {
             get { return base.nWidth; }
@@ -19,7 +34,9 @@ namespace Novacoin
             get { return base.pn; }
             private set { base.pn = value; }
         }
+        #endregion
 
+        #region Constructors
         public uint160()
         {
             nWidth = 5;
@@ -57,60 +74,66 @@ namespace Novacoin
             var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
             pn = Interop.ToUInt32Array(bytes);
         }
+        #endregion
 
-        public static uint160 operator ~(uint160 a)
+        #region Cast operators
+        public static implicit operator uint160(byte[] bytes)
         {
-            var ret = new uint160();
-            for (int i = 0; i < a.nWidth; i++)
-            {
-                ret.pn[i] = ~a.pn[i];
-            }
-            return ret;
+            return new uint160(bytes);
         }
 
-        public static uint160 operator -(uint160 a)
+        public static implicit operator uint160(ulong n)
+        {
+            return new uint160(n);
+        }
+        #endregion
+
+        #region Bitwise operations
+        public static uint160 operator ~(uint160 a)
         {
             var ret = new uint160();
             for (int i = 0; i < a.nWidth; i++)
             {
                 ret.pn[i] = ~a.pn[i];
             }
-            ret++;
             return ret;
         }
 
-
-        public static uint160 operator ++(uint160 a)
+        public static uint160 operator ^(uint160 a, uint160 b)
         {
-            int i = 0;
-            while (++a.pn[i] == 0 && i < a.nWidth - 1)
+            var result = new uint160();
+            result.pn = new uint[a.nWidth];
+            for (int i = 0; i < result.nWidth; i++)
             {
-                i++;
+                result.pn[i] = a.pn[i] ^ b.pn[i];
             }
-            return a;
+            return result;
         }
 
-        public static uint160 operator --(uint160 a)
+        public static uint160 operator &(uint160 a, uint160 b)
         {
-            int i = 0;
-            while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
+            var result = new uint160();
+            result.pn = new uint[a.nWidth];
+            for (int i = 0; i < result.nWidth; i++)
             {
-                i++;
+                result.pn[i] = a.pn[i] & b.pn[i];
             }
-            return a;
+            return result;
         }
 
-        public static uint160 operator ^(uint160 a, uint160 b)
+        public static uint160 operator |(uint160 a, uint160 b)
         {
             var result = new uint160();
             result.pn = new uint[a.nWidth];
             for (int i = 0; i < result.nWidth; i++)
             {
-                result.pn[i] = a.pn[i] ^ b.pn[i];
+                result.pn[i] = a.pn[i] | b.pn[i];
             }
             return result;
         }
+        #endregion
 
+        #region Basic arithmetics
         public static uint160 operator +(uint160 a, uint160 b)
         {
             var result = new uint160();
@@ -139,28 +162,41 @@ namespace Novacoin
             return a - new uint160(b);
         }
 
-        public static uint160 operator &(uint160 a, uint160 b)
+        public static uint160 operator -(uint160 a)
         {
-            var result = new uint160();
-            result.pn = new uint[a.nWidth];
-            for (int i = 0; i < result.nWidth; i++)
+            var ret = new uint160();
+            for (int i = 0; i < a.nWidth; i++)
             {
-                result.pn[i] = a.pn[i] & b.pn[i];
+                ret.pn[i] = ~a.pn[i];
             }
-            return result;
+            ret++;
+            return ret;
         }
 
-        public static uint160 operator |(uint160 a, uint160 b)
+
+        public static uint160 operator ++(uint160 a)
         {
-            var result = new uint160();
-            result.pn = new uint[a.nWidth];
-            for (int i = 0; i < result.nWidth; i++)
+            int i = 0;
+            while (++a.pn[i] == 0 && i < a.nWidth - 1)
             {
-                result.pn[i] = a.pn[i] | b.pn[i];
+                i++;
             }
-            return result;
+            return a;
         }
 
+        public static uint160 operator --(uint160 a)
+        {
+            int i = 0;
+            while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
+            {
+                i++;
+            }
+            return a;
+        }
+
+        #endregion
+
+        #region Shift
         public static uint160 operator <<(uint160 a, int shift)
         {
             var result = new uint160();
@@ -204,5 +240,6 @@ namespace Novacoin
 
             return result;
         }
+        #endregion
     }
 }