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];
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);
257 public static uint256 operator <<(uint256 a, int shift)
259 var result = new uint256();
263 for (int i = 0; i < a.nWidth; i++)
265 if (i + k + 1 < a.nWidth && shift != 0)
267 result.pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
270 if (i + k < a.nWidth)
272 result.pn[i + k] |= (a.pn[i] << shift);
279 public static uint256 operator >>(uint256 a, int shift)
281 var result = new uint256();
285 for (int i = 0; i < a.nWidth; i++)
287 if (i - k - 1 >= 0 && shift != 0)
289 result.pn[i - k - 1] |= (a.pn[i] << (32 - shift));
294 result.pn[i - k] |= (a.pn[i] >> shift);