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;
25 public class uint160 : base_uint
27 #region Access to internal representation
28 new protected int nWidth
30 get { return base.nWidth; }
31 private set { base.nWidth = value; }
33 new protected uint[] pn
35 get { return base.pn; }
36 private set { base.pn = value; }
44 pn = new uint[nWidth];
47 public uint160(uint160 b) : this()
49 for (int i = 0; i < nWidth; i++)
56 public uint160(ulong n) : this()
59 pn[1] = (uint)(n >> 32);
60 for (int i = 2; i < nWidth; i++)
66 public uint160(byte[] bytes) : this()
68 Contract.Requires<ArgumentException>(bytes.Length == 20, "Incorrect array length");
69 pn = Interop.ToUInt32Array(bytes);
72 public uint160(string hex) : this()
74 Contract.Requires<ArgumentException>(hex.Length == 40, "Incorrect string");
75 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
76 pn = Interop.ToUInt32Array(bytes);
80 #region Cast operators
81 public static implicit operator uint160(byte[] bytes)
83 return new uint160(bytes);
86 public static implicit operator uint160(ulong n)
88 return new uint160(n);
92 #region Bitwise operations
93 public static uint160 operator ~(uint160 a)
95 var ret = new uint160();
96 for (int i = 0; i < a.nWidth; i++)
103 public static uint160 operator ^(uint160 a, uint160 b)
105 var result = new uint160();
106 result.pn = new uint[a.nWidth];
107 for (int i = 0; i < result.nWidth; i++)
109 result.pn[i] = a.pn[i] ^ b.pn[i];
114 public static uint160 operator &(uint160 a, uint160 b)
116 var result = new uint160();
117 result.pn = new uint[a.nWidth];
118 for (int i = 0; i < result.nWidth; i++)
120 result.pn[i] = a.pn[i] & b.pn[i];
125 public static uint160 operator |(uint160 a, uint160 b)
127 var result = new uint160();
128 result.pn = new uint[a.nWidth];
129 for (int i = 0; i < result.nWidth; i++)
131 result.pn[i] = a.pn[i] | b.pn[i];
137 #region Basic arithmetics
138 public static uint160 operator +(uint160 a, uint160 b)
140 var result = new uint160();
142 for (int i = 0; i < result.nWidth; i++)
144 ulong n = carry + a.pn[i] + b.pn[i];
145 result.pn[i] = (uint)(n & 0xffffffff);
151 public static uint160 operator +(uint160 a, ulong b)
153 return a + new uint160(b);
156 public static uint160 operator -(uint160 a, uint160 b)
161 public static uint160 operator -(uint160 a, ulong b)
163 return a - new uint160(b);
166 public static uint160 operator -(uint160 a)
168 var ret = new uint160();
169 for (int i = 0; i < a.nWidth; i++)
171 ret.pn[i] = ~a.pn[i];
178 public static uint160 operator ++(uint160 a)
181 while (++a.pn[i] == 0 && i < a.nWidth - 1)
188 public static uint160 operator --(uint160 a)
191 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
198 public static uint160 operator /(uint160 a, uint divisor)
200 var result = new uint160();
209 result.pn[i] = (uint)(r / divisor);
216 public static uint160 operator *(uint160 a, uint multiplier)
218 var result = new uint160();
225 c += a.pn[i] * (ulong)multiplier;
226 result.pn[i] = (uint)c;
228 } while (++i < result.nWidth);
233 public static uint operator %(uint160 a, uint divisor)
248 public static uint160 operator /(uint160 a, uint160 b)
255 uint160 result = new uint160();
258 uint[] remainder_value;
260 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
261 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
263 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
265 quotient.CopyTo(result.pn, 0);
270 public static uint160 operator %(uint160 a, uint160 b)
277 uint160 result = new uint160();
280 uint[] remainder_value;
282 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
283 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
285 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
287 remainder_value.CopyTo(result.pn, 0);
296 public static uint160 operator <<(uint160 a, int shift)
298 var result = new uint160();
302 for (int i = 0; i < a.nWidth; i++)
304 if (i + k + 1 < a.nWidth && shift != 0)
306 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
309 if (i + k < a.nWidth)
311 result.pn[i + k] |= (a.pn[i] << shift);
318 public static uint160 operator >>(uint160 a, int shift)
320 var result = new uint160();
324 for (int i = 0; i < a.nWidth; i++)
326 if (i - k - 1 >= 0 && shift != 0)
328 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
333 result.pn[i - k] |= (a.pn[i] >> shift);