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, ulong multiplier)
218 var result = new uint160();
225 c += a.pn[i] * multiplier;
226 result.pn[i] = (uint)c;
228 } while (++i < result.nWidth);
233 public static uint160 operator *(uint160 a, uint160 b)
237 // Multiplication by zero results with zero.
240 else if (b.bits <= 32)
244 // If right is 1 then return left operand value
250 else if (a.bits <= 32)
254 // If left is 1 then return right operand value
261 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
262 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
264 uint160 result = new uint160();
266 uint[] left = a.pn.Take(m).ToArray();
267 uint[] right = b.pn.Take(n).ToArray();
269 for (int i = 0; i < m; ++i)
275 for (int j = 0; j < n; ++j)
277 temp = temp + ((ulong)ai) * right[j] + result.pn[k];
278 result.pn[k++] = (uint)temp;
284 temp += result.pn[k];
285 result.pn[k++] = (uint)temp;
293 public static uint operator %(uint160 a, uint divisor)
308 public static uint160 operator /(uint160 a, uint160 b)
315 uint160 result = new uint160();
318 uint[] remainder_value;
320 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
321 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
323 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
325 quotient.CopyTo(result.pn, 0);
330 public static uint160 operator %(uint160 a, uint160 b)
337 uint160 result = new uint160();
340 uint[] remainder_value;
342 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
343 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
345 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
347 remainder_value.CopyTo(result.pn, 0);
356 public static uint160 operator <<(uint160 a, int shift)
358 var result = new uint160();
362 for (int i = 0; i < a.nWidth; i++)
364 if (i + k + 1 < a.nWidth && shift != 0)
366 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
369 if (i + k < a.nWidth)
371 result.pn[i + k] |= (a.pn[i] << shift);
378 public static uint160 operator >>(uint160 a, int shift)
380 var result = new uint160();
384 for (int i = 0; i < a.nWidth; i++)
386 if (i - k - 1 >= 0 && shift != 0)
388 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
393 result.pn[i - k] |= (a.pn[i] >> shift);