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;
25 public class uint256 : base_uint
27 #region Access to internal representation
28 new protected int nWidth {
29 get { return base.nWidth; }
30 private set { base.nWidth = value; }
32 new protected uint[] pn {
33 get { return base.pn; }
34 private set { base.pn = value; }
42 pn = new uint[nWidth];
45 public uint256(uint256 b) : this()
47 for (int i = 0; i < nWidth; i++)
53 public uint256(ulong n) : this()
56 pn[1] = (uint)(n >> 32);
57 for (int i = 2; i < nWidth; i++)
63 public uint256(byte[] bytes) : this()
65 Contract.Requires<ArgumentException>(bytes.Length == 32, "Incorrect array length");
67 pn = Interop.ToUInt32Array(bytes);
70 public uint256(string hex) : this()
72 Contract.Requires<ArgumentException>(hex.Length == 64, "Incorrect string");
74 var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
75 pn = Interop.ToUInt32Array(bytes);
79 #region Cast operators
80 public static implicit operator uint256(byte[] bytes)
82 return new uint256(bytes);
85 public static implicit operator uint256(ulong n)
87 return new uint256(n);
91 #region Compact representation
93 /// Compact representation of unsigned 256bit numbers.
95 /// N = (-1^sign) * m * 256^(exp-3)
97 /// http://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty
103 int nSize = (bits + 7) / 8;
106 nCompact = ((uint)Low64) << 8 * (3 - nSize);
109 uint256 bn = this >> 8 * (nSize - 3);
110 nCompact = (uint)bn.Low64;
113 if ((nCompact & 0x00800000) != 0)
119 Contract.Assert((nCompact & ~0x007fffff) == 0);
120 Contract.Assert(nSize < 256);
122 nCompact |= (uint)nSize << 24;
128 int nSize = (int)value >> 24;
129 uint nWord = value & 0x007fffff;
135 nWord >>= 8 * (3 - nSize);
136 i = new uint256(nWord);
140 i = new uint256(nWord);
141 i <<= 8 * (nSize - 3);
149 #region Bitwise operations
150 public static uint256 operator ~(uint256 a)
152 var ret = new uint256();
153 for (int i = 0; i < a.nWidth; i++)
155 ret.pn[i] = ~a.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, uint256 b)
173 var result = new uint256();
174 result.pn = new uint[a.nWidth];
175 for (int i = 0; i < result.nWidth; i++)
177 result.pn[i] = a.pn[i] & b.pn[i];
182 public static uint256 operator |(uint256 a, uint256 b)
184 var result = new uint256();
185 result.pn = new uint[a.nWidth];
186 for (int i = 0; i < result.nWidth; i++)
188 result.pn[i] = a.pn[i] | b.pn[i];
194 #region Basic arithmetics
195 public static uint256 operator -(uint256 a)
197 var ret = new uint256();
198 for (int i = 0; i < a.nWidth; i++)
200 ret.pn[i] = ~a.pn[i];
206 public static uint256 operator ++(uint256 a)
209 while (++a.pn[i] == 0 && i < a.nWidth - 1)
216 public static uint256 operator --(uint256 a)
219 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
227 public static uint256 operator +(uint256 a, uint256 b)
229 var result = new uint256();
231 for (int i = 0; i < result.nWidth; i++)
233 ulong n = carry + a.pn[i] + b.pn[i];
234 result.pn[i] = (uint)(n & 0xffffffff);
240 public static uint256 operator +(uint256 a, ulong b)
242 return a + new uint256(b);
245 public static uint256 operator -(uint256 a, uint256 b)
250 public static uint256 operator -(uint256 a, ulong b)
252 return a - new uint256(b);
255 public static uint256 operator /(uint256 a, uint b)
257 var result = new uint256();
266 result.pn[i] = (uint)(r / b);
273 public static uint256 operator *(uint256 a, uint b)
275 var result = new uint256();
282 c += a.pn[i] * (ulong)b;
283 result.pn[i] = (uint)c;
285 } while (++i < result.nWidth);
290 public static uint operator %(uint256 a, uint b)
305 public static uint256 operator /(uint256 a, uint256 b)
312 uint256 result = new uint256();
315 uint[] remainder_value;
317 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
318 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
320 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
322 quotient.CopyTo(result.pn, 0);
327 public static uint256 operator %(uint256 a, uint256 b)
334 uint256 result = new uint256();
337 uint[] remainder_value;
339 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
340 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
342 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
344 remainder_value.CopyTo(result.pn, 0);
352 public static uint256 operator <<(uint256 a, int shift)
354 var result = new uint256();
358 for (int i = 0; i < a.nWidth; i++)
360 if (i + k + 1 < a.nWidth && shift != 0)
362 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
365 if (i + k < a.nWidth)
367 result.pn[i + k] |= (a.pn[i] << shift);
374 public static uint256 operator >>(uint256 a, int shift)
376 var result = new uint256();
380 for (int i = 0; i < a.nWidth; i++)
382 if (i - k - 1 >= 0 && shift != 0)
384 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
389 result.pn[i - k] |= (a.pn[i] >> shift);