2 using System.Collections.Generic;
3 using System.Diagnostics.Contracts;
6 using System.Threading.Tasks;
10 public class uint160 : base_uint
12 new protected readonly int nWidth = 5;
16 pn = new uint[nWidth];
18 for (int i = 0; i < nWidth; i++)
24 public uint160(uint160 b)
26 pn = new uint[nWidth];
28 for (int i = 0; i < nWidth; i++)
35 public uint160(ulong n)
37 pn = new uint[nWidth];
40 pn[1] = (uint)(n >> 32);
41 for (int i = 2; i < nWidth; i++)
47 public uint160(byte[] bytes)
49 Contract.Requires<ArgumentException>(bytes.Length == 20, "Incorrect array length");
51 pn = Interop.ToUInt32Array(bytes);
54 public uint160(string hex)
56 Contract.Requires<ArgumentException>(hex.Length == 40, "Incorrect string");
58 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
59 pn = Interop.ToUInt32Array(bytes);
62 public static uint160 operator ~(uint160 a)
64 var ret = new uint160();
65 for (int i = 0; i < a.nWidth; i++)
72 public static uint160 operator -(uint160 a)
74 var ret = new uint160();
75 for (int i = 0; i < a.nWidth; i++)
84 public static uint160 operator ++(uint160 a)
87 while (++a.pn[i] == 0 && i < a.nWidth - 1)
94 public static uint160 operator --(uint160 a)
97 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
104 public static uint160 operator ^(uint160 a, uint160 b)
106 var result = new uint160();
107 result.pn = new uint[a.nWidth];
108 for (int i = 0; i < result.nWidth; i++)
110 result.pn[i] = a.pn[i] ^ b.pn[i];
115 public static uint160 operator +(uint160 a, uint160 b)
117 var result = new uint160();
119 for (int i = 0; i < result.nWidth; i++)
121 ulong n = carry + a.pn[i] + b.pn[i];
122 result.pn[i] = (uint)(n & 0xffffffff);
128 public static uint160 operator +(uint160 a, ulong b)
130 return a + new uint160(b);
133 public static uint160 operator -(uint160 a, uint160 b)
138 public static uint160 operator -(uint160 a, ulong b)
140 return a - new uint160(b);
143 public static uint160 operator &(uint160 a, uint160 b)
145 var result = new uint160();
146 result.pn = new uint[a.nWidth];
147 for (int i = 0; i < result.nWidth; i++)
149 result.pn[i] = a.pn[i] & b.pn[i];
154 public static uint160 operator |(uint160 a, uint160 b)
156 var result = new uint160();
157 result.pn = new uint[a.nWidth];
158 for (int i = 0; i < result.nWidth; i++)
160 result.pn[i] = a.pn[i] | b.pn[i];
165 public static uint160 operator <<(uint160 a, int shift)
167 var result = new uint160();
171 for (int i = 0; i < a.nWidth; i++)
173 if (i + k + 1 < a.nWidth && shift != 0)
175 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
178 if (i + k < a.nWidth)
180 result.pn[i + k] |= (a.pn[i] << shift);
187 public static uint160 operator >>(uint160 a, int shift)
189 var result = new uint160();
193 for (int i = 0; i < a.nWidth; i++)
195 if (i - k - 1 >= 0 && shift != 0)
197 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
202 result.pn[i - k] |= (a.pn[i] >> shift);