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 = Low32 << 8 * (3 - nSize);
109 uint256 bn = this >> 8 * (nSize - 3);
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, ulong b)
275 var result = new uint256();
283 result.pn[i] = (uint)c;
285 } while (++i < result.nWidth);
290 public static uint256 operator *(uint256 a, uint256 b)
294 // Multiplication by zero results with zero.
297 else if (b.bits <= 32)
301 // If right is 1 then return left operand value
307 else if (a.bits <= 32)
311 // If left is 1 then return right operand value
318 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
319 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
321 uint256 result = new uint256();
323 uint[] left = a.pn.Take(m).ToArray();
324 uint[] right = b.pn.Take(n).ToArray();
326 for (int i = 0; i < m; ++i)
332 for (int j = 0; j < n; ++j)
334 temp = temp + ((ulong)ai) * right[j] + result.pn[k];
335 result.pn[k++] = (uint)temp;
341 temp += result.pn[k];
342 result.pn[k++] = (uint)temp;
350 public static uint operator %(uint256 a, uint b)
365 public static uint256 operator /(uint256 a, uint256 b)
372 uint256 result = new uint256();
375 uint[] remainder_value;
377 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
378 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
380 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
382 quotient.CopyTo(result.pn, 0);
387 public static uint256 operator %(uint256 a, uint256 b)
394 uint256 result = new uint256();
397 uint[] remainder_value;
399 int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0);
400 int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0);
402 BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value);
404 remainder_value.CopyTo(result.pn, 0);
412 public static uint256 operator <<(uint256 a, int shift)
414 var result = new uint256();
418 for (int i = 0; i < a.nWidth; i++)
420 if (i + k + 1 < a.nWidth && shift != 0)
422 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
425 if (i + k < a.nWidth)
427 result.pn[i + k] |= (a.pn[i] << shift);
434 public static uint256 operator >>(uint256 a, int shift)
436 var result = new uint256();
440 for (int i = 0; i < a.nWidth; i++)
442 if (i - k - 1 >= 0 && shift != 0)
444 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
449 result.pn[i - k] |= (a.pn[i] >> shift);