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;
17 pn = new uint[nWidth];
19 for (int i = 0; i < nWidth; i++)
25 public uint160(uint160 b)
28 pn = new uint[nWidth];
30 for (int i = 0; i < nWidth; i++)
37 public uint160(ulong n)
40 pn = new uint[nWidth];
43 pn[1] = (uint)(n >> 32);
44 for (int i = 2; i < nWidth; i++)
50 public uint160(byte[] bytes)
52 Contract.Requires<ArgumentException>(bytes.Length == 20, "Incorrect array length");
55 pn = Interop.ToUInt32Array(bytes);
58 public uint160(string hex)
60 Contract.Requires<ArgumentException>(hex.Length == 40, "Incorrect string");
63 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
64 pn = Interop.ToUInt32Array(bytes);
67 public static uint160 operator ~(uint160 a)
69 var ret = new uint160();
70 for (int i = 0; i < a.nWidth; i++)
77 public static uint160 operator -(uint160 a)
79 var ret = new uint160();
80 for (int i = 0; i < a.nWidth; i++)
89 public static uint160 operator ++(uint160 a)
92 while (++a.pn[i] == 0 && i < a.nWidth - 1)
99 public static uint160 operator --(uint160 a)
102 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
109 public static uint160 operator ^(uint160 a, uint160 b)
111 var result = new uint160();
112 result.pn = new uint[a.nWidth];
113 for (int i = 0; i < result.nWidth; i++)
115 result.pn[i] = a.pn[i] ^ b.pn[i];
120 public static uint160 operator +(uint160 a, uint160 b)
122 var result = new uint160();
124 for (int i = 0; i < result.nWidth; i++)
126 ulong n = carry + a.pn[i] + b.pn[i];
127 result.pn[i] = (uint)(n & 0xffffffff);
133 public static uint160 operator +(uint160 a, ulong b)
135 return a + new uint160(b);
138 public static uint160 operator -(uint160 a, uint160 b)
143 public static uint160 operator -(uint160 a, ulong b)
145 return a - new uint160(b);
148 public static uint160 operator &(uint160 a, uint160 b)
150 var result = new uint160();
151 result.pn = new uint[a.nWidth];
152 for (int i = 0; i < result.nWidth; i++)
154 result.pn[i] = a.pn[i] & b.pn[i];
159 public static uint160 operator |(uint160 a, uint160 b)
161 var result = new uint160();
162 result.pn = new uint[a.nWidth];
163 for (int i = 0; i < result.nWidth; i++)
165 result.pn[i] = a.pn[i] | b.pn[i];
170 public static uint160 operator <<(uint160 a, int shift)
172 var result = new uint160();
176 for (int i = 0; i < a.nWidth; i++)
178 if (i + k + 1 < a.nWidth && shift != 0)
180 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
183 if (i + k < a.nWidth)
185 result.pn[i + k] |= (a.pn[i] << shift);
192 public static uint160 operator >>(uint160 a, int shift)
194 var result = new uint160();
198 for (int i = 0; i < a.nWidth; i++)
200 if (i - k - 1 >= 0 && shift != 0)
202 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
207 result.pn[i - k] |= (a.pn[i] >> shift);