2 using System.Collections.Generic;
3 using System.Diagnostics.Contracts;
6 using System.Threading.Tasks;
10 public class uint256 : base_uint
12 new public readonly int nWidth = 8;
17 pn = new uint[nWidth];
19 for (int i = 0; i < nWidth; i++)
25 public uint256(uint256 b)
28 pn = new uint[nWidth];
30 for (int i = 0; i < nWidth; i++)
37 public uint256(ulong n)
40 pn = new uint[nWidth];
43 pn[1] = (uint)(n >> 32);
44 for (int i = 2; i < nWidth; i++)
50 public uint256(byte[] bytes)
52 Contract.Requires<ArgumentException>(bytes.Length == 32, "Incorrect array length");
55 pn = Interop.ToUInt32Array(bytes);
58 public uint256(string hex)
60 Contract.Requires<ArgumentException>(hex.Length == 64, "Incorrect string");
63 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
64 pn = Interop.ToUInt32Array(bytes);
68 public static uint256 operator ~(uint256 a)
70 var ret = new uint256();
71 for (int i = 0; i < a.nWidth; i++)
78 public static uint256 operator -(uint256 a)
80 var ret = new uint256();
81 for (int i = 0; i < a.nWidth; i++)
90 public static uint256 operator ++(uint256 a)
93 while (++a.pn[i] == 0 && i < a.nWidth - 1)
100 public static uint256 operator --(uint256 a)
103 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
110 public static uint256 operator ^(uint256 a, uint256 b)
112 var result = new uint256();
113 result.pn = new uint[a.nWidth];
114 for (int i = 0; i < result.nWidth; i++)
116 result.pn[i] = a.pn[i] ^ b.pn[i];
121 public static uint256 operator +(uint256 a, uint256 b)
123 var result = new uint256();
125 for (int i = 0; i < result.nWidth; i++)
127 ulong n = carry + a.pn[i] + b.pn[i];
128 result.pn[i] = (uint)(n & 0xffffffff);
134 public static uint256 operator +(uint256 a, ulong b)
136 return a + new uint256(b);
139 public static uint256 operator -(uint256 a, uint256 b)
144 public static uint256 operator -(uint256 a, ulong b)
146 return a - new uint256(b);
149 public static uint256 operator &(uint256 a, uint256 b)
151 var result = new uint256();
152 result.pn = new uint[a.nWidth];
153 for (int i = 0; i < result.nWidth; i++)
155 result.pn[i] = a.pn[i] & b.pn[i];
160 public static uint256 operator |(uint256 a, uint256 b)
162 var result = new uint256();
163 result.pn = new uint[a.nWidth];
164 for (int i = 0; i < result.nWidth; i++)
166 result.pn[i] = a.pn[i] | b.pn[i];
171 public static uint256 operator <<(uint256 a, int shift)
173 var result = new uint256();
177 for (int i = 0; i < a.nWidth; i++)
179 if (i + k + 1 < a.nWidth && shift != 0)
181 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
184 if (i + k < a.nWidth)
186 result.pn[i + k] |= (a.pn[i] << shift);
193 public static uint256 operator >>(uint256 a, int shift)
195 var result = new uint256();
199 for (int i = 0; i < a.nWidth; i++)
201 if (i - k - 1 >= 0 && shift != 0)
203 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
208 result.pn[i - k] |= (a.pn[i] >> shift);