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;
24 public class uint256 : base_uint
26 #region Access to internal representation
27 new protected int nWidth {
28 get { return base.nWidth; }
29 private set { base.nWidth = value; }
31 new protected uint[] pn {
32 get { return base.pn; }
33 private set { base.pn = value; }
41 pn = new uint[nWidth];
44 public uint256(uint256 b) : this()
46 for (int i = 0; i < nWidth; i++)
52 public uint256(ulong n) : this()
55 pn[1] = (uint)(n >> 32);
56 for (int i = 2; i < nWidth; i++)
62 public uint256(byte[] bytes) : this()
64 Contract.Requires<ArgumentException>(bytes.Length == 32, "Incorrect array length");
66 pn = Interop.ToUInt32Array(bytes);
69 public uint256(string hex) : this()
71 Contract.Requires<ArgumentException>(hex.Length == 64, "Incorrect string");
73 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
74 pn = Interop.ToUInt32Array(bytes);
78 #region Cast operators
79 public static implicit operator uint256(byte[] bytes)
81 return new uint256(bytes);
84 public static implicit operator uint256(ulong n)
86 return new uint256(n);
90 #region Compact representation
92 /// Compact representation of unsigned 256bit numbers.
94 /// N = (-1^sign) * m * 256^(exp-3)
96 /// http://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty
102 int nSize = (bits + 7) / 8;
105 nCompact = ((uint)Low64) << 8 * (3 - nSize);
108 uint256 bn = this >> 8 * (nSize - 3);
109 nCompact = (uint)bn.Low64;
112 if ((nCompact & 0x00800000) != 0)
118 Contract.Assert((nCompact & ~0x007fffff) == 0);
119 Contract.Assert(nSize < 256);
121 nCompact |= (uint)nSize << 24;
127 int nSize = (int)value >> 24;
128 uint nWord = value & 0x007fffff;
134 nWord >>= 8 * (3 - nSize);
135 i = new uint256(nWord);
139 i = new uint256(nWord);
140 i <<= 8 * (nSize - 3);
148 #region Bitwise operations
149 public static uint256 operator ~(uint256 a)
151 var ret = new uint256();
152 for (int i = 0; i < a.nWidth; i++)
154 ret.pn[i] = ~a.pn[i];
159 public static uint256 operator ^(uint256 a, uint256 b)
161 var result = new uint256();
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 uint256 operator &(uint256 a, uint256 b)
172 var result = new uint256();
173 result.pn = new uint[a.nWidth];
174 for (int i = 0; i < result.nWidth; i++)
176 result.pn[i] = a.pn[i] & b.pn[i];
181 public static uint256 operator |(uint256 a, uint256 b)
183 var result = new uint256();
184 result.pn = new uint[a.nWidth];
185 for (int i = 0; i < result.nWidth; i++)
187 result.pn[i] = a.pn[i] | b.pn[i];
193 #region Basic arithmetics
194 public static uint256 operator -(uint256 a)
196 var ret = new uint256();
197 for (int i = 0; i < a.nWidth; i++)
199 ret.pn[i] = ~a.pn[i];
205 public static uint256 operator ++(uint256 a)
208 while (++a.pn[i] == 0 && i < a.nWidth - 1)
215 public static uint256 operator --(uint256 a)
218 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
226 public static uint256 operator +(uint256 a, uint256 b)
228 var result = new uint256();
230 for (int i = 0; i < result.nWidth; i++)
232 ulong n = carry + a.pn[i] + b.pn[i];
233 result.pn[i] = (uint)(n & 0xffffffff);
239 public static uint256 operator +(uint256 a, ulong b)
241 return a + new uint256(b);
244 public static uint256 operator -(uint256 a, uint256 b)
249 public static uint256 operator -(uint256 a, ulong b)
251 return a - new uint256(b);
254 public static uint256 operator /(uint256 a, uint divisor)
256 var result = new uint256();
265 result.pn[i] = (uint)(r / divisor);
272 public static uint256 operator *(uint256 a, uint multiplier)
274 var result = new uint256();
281 c += a.pn[i] * (ulong)multiplier;
282 result.pn[i] = (uint)c;
284 } while (++i < result.nWidth);
289 public static uint operator %(uint256 a, uint divisor)
304 public static uint256 operator /(uint256 a, uint256 divisor)
306 if (divisor.bits <= 32)
308 return a / divisor.Low32;
311 return Divide(a, divisor)[0];
314 public static uint256 operator %(uint256 a, uint256 divisor)
316 if (divisor.bits <= 32)
318 return a % divisor.Low32;
321 return Divide(a, divisor)[1];
325 public static uint256[] Divide(uint256 bi1, uint256 bi2)
329 uint256[] ret = new uint256[2] { 0, 0 };
337 public static uint256 operator <<(uint256 a, int shift)
339 var result = new uint256();
343 for (int i = 0; i < a.nWidth; i++)
345 if (i + k + 1 < a.nWidth && shift != 0)
347 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
350 if (i + k < a.nWidth)
352 result.pn[i + k] |= (a.pn[i] << shift);
359 public static uint256 operator >>(uint256 a, int shift)
361 var result = new uint256();
365 for (int i = 0; i < a.nWidth; i++)
367 if (i - k - 1 >= 0 && shift != 0)
369 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
374 result.pn[i - k] |= (a.pn[i] >> shift);