2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 using System.Diagnostics.Contracts;
24 public class uint160 : base_uint
26 #region Access to internal representation
27 new protected int nWidth
29 get { return base.nWidth; }
30 private set { base.nWidth = value; }
32 new protected uint[] pn
34 get { return base.pn; }
35 private set { base.pn = value; }
43 pn = new uint[nWidth];
46 public uint160(uint160 b) : this()
48 for (int i = 0; i < nWidth; i++)
55 public uint160(ulong n) : this()
58 pn[1] = (uint)(n >> 32);
59 for (int i = 2; i < nWidth; i++)
65 public uint160(byte[] bytes) : this()
67 Contract.Requires<ArgumentException>(bytes.Length == 20, "Incorrect array length");
68 pn = Interop.ToUInt32Array(bytes);
71 public uint160(string hex) : this()
73 Contract.Requires<ArgumentException>(hex.Length == 40, "Incorrect string");
74 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
75 pn = Interop.ToUInt32Array(bytes);
79 #region Cast operators
80 public static implicit operator uint160(byte[] bytes)
82 return new uint160(bytes);
85 public static implicit operator uint160(ulong n)
87 return new uint160(n);
91 #region Bitwise operations
92 public static uint160 operator ~(uint160 a)
94 var ret = new uint160();
95 for (int i = 0; i < a.nWidth; i++)
102 public static uint160 operator ^(uint160 a, uint160 b)
104 var result = new uint160();
105 result.pn = new uint[a.nWidth];
106 for (int i = 0; i < result.nWidth; i++)
108 result.pn[i] = a.pn[i] ^ b.pn[i];
113 public static uint160 operator &(uint160 a, uint160 b)
115 var result = new uint160();
116 result.pn = new uint[a.nWidth];
117 for (int i = 0; i < result.nWidth; i++)
119 result.pn[i] = a.pn[i] & b.pn[i];
124 public static uint160 operator |(uint160 a, uint160 b)
126 var result = new uint160();
127 result.pn = new uint[a.nWidth];
128 for (int i = 0; i < result.nWidth; i++)
130 result.pn[i] = a.pn[i] | b.pn[i];
136 #region Basic arithmetics
137 public static uint160 operator +(uint160 a, uint160 b)
139 var result = new uint160();
141 for (int i = 0; i < result.nWidth; i++)
143 ulong n = carry + a.pn[i] + b.pn[i];
144 result.pn[i] = (uint)(n & 0xffffffff);
150 public static uint160 operator +(uint160 a, ulong b)
152 return a + new uint160(b);
155 public static uint160 operator -(uint160 a, uint160 b)
160 public static uint160 operator -(uint160 a, ulong b)
162 return a - new uint160(b);
165 public static uint160 operator -(uint160 a)
167 var ret = new uint160();
168 for (int i = 0; i < a.nWidth; i++)
170 ret.pn[i] = ~a.pn[i];
177 public static uint160 operator ++(uint160 a)
180 while (++a.pn[i] == 0 && i < a.nWidth - 1)
187 public static uint160 operator --(uint160 a)
190 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
197 public static uint160 operator /(uint160 a, uint divisor)
199 var result = new uint160();
208 result.pn[i] = (uint)(r / divisor);
215 public static uint operator %(uint160 a, uint divisor)
233 public static uint160 operator <<(uint160 a, int shift)
235 var result = new uint160();
239 for (int i = 0; i < a.nWidth; i++)
241 if (i + k + 1 < a.nWidth && shift != 0)
243 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
246 if (i + k < a.nWidth)
248 result.pn[i + k] |= (a.pn[i] << shift);
255 public static uint160 operator >>(uint160 a, int shift)
257 var result = new uint160();
261 for (int i = 0; i < a.nWidth; i++)
263 if (i - k - 1 >= 0 && shift != 0)
265 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
270 result.pn[i - k] |= (a.pn[i] >> shift);