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;
16 pn = new uint[nWidth];
18 for (int i = 0; i < nWidth; i++)
24 public uint256(uint256 b)
26 pn = new uint[nWidth];
28 for (int i = 0; i < nWidth; i++)
35 public uint256(ulong n)
37 pn = new uint[nWidth];
40 pn[1] = (uint)(n >> 32);
41 for (int i = 2; i < nWidth; i++)
47 public uint256(byte[] bytes)
49 Contract.Requires<ArgumentException>(bytes.Length == 32, "Incorrect array length");
51 pn = Interop.ToUInt32Array(bytes);
54 public uint256(string hex)
56 Contract.Requires<ArgumentException>(hex.Length == 64, "Incorrect string");
58 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
59 pn = Interop.ToUInt32Array(bytes);
63 public static uint256 operator ~(uint256 a)
65 var ret = new uint256();
66 for (int i = 0; i < a.nWidth; i++)
73 public static uint256 operator -(uint256 a)
75 var ret = new uint256();
76 for (int i = 0; i < a.nWidth; i++)
85 public static uint256 operator ++(uint256 a)
88 while (++a.pn[i] == 0 && i < a.nWidth - 1)
95 public static uint256 operator --(uint256 a)
98 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
105 public static uint256 operator ^(uint256 a, uint256 b)
107 var result = new uint256();
108 result.pn = new uint[a.nWidth];
109 for (int i = 0; i < result.nWidth; i++)
111 result.pn[i] = a.pn[i] ^ b.pn[i];
116 public static uint256 operator +(uint256 a, uint256 b)
118 var result = new uint256();
120 for (int i = 0; i < result.nWidth; i++)
122 ulong n = carry + a.pn[i] + b.pn[i];
123 result.pn[i] = (uint)(n & 0xffffffff);
129 public static uint256 operator +(uint256 a, ulong b)
131 return a + new uint256(b);
134 public static uint256 operator -(uint256 a, uint256 b)
139 public static uint256 operator -(uint256 a, ulong b)
141 return a - new uint256(b);
144 public static uint256 operator &(uint256 a, uint256 b)
146 var result = new uint256();
147 result.pn = new uint[a.nWidth];
148 for (int i = 0; i < result.nWidth; i++)
150 result.pn[i] = a.pn[i] & b.pn[i];
155 public static uint256 operator |(uint256 a, uint256 b)
157 var result = new uint256();
158 result.pn = new uint[a.nWidth];
159 for (int i = 0; i < result.nWidth; i++)
161 result.pn[i] = a.pn[i] | b.pn[i];
166 public static uint256 operator <<(uint256 a, int shift)
168 var result = new uint256();
172 for (int i = 0; i < a.nWidth; i++)
174 if (i + k + 1 < a.nWidth && shift != 0)
176 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
179 if (i + k < a.nWidth)
181 result.pn[i + k] |= (a.pn[i] << shift);
188 public static uint256 operator >>(uint256 a, int shift)
190 var result = new uint256();
194 for (int i = 0; i < a.nWidth; i++)
196 if (i - k - 1 >= 0 && shift != 0)
198 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
203 result.pn[i - k] |= (a.pn[i] >> shift);