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/>.
24 public class base_uint : IComparable<base_uint>, IEquatable<base_uint>
29 public double getDouble()
34 for (int i = 0; i < nWidth; i++)
43 public ulong GetLow64()
45 return pn[0] | (ulong)pn[1] << 32;
48 public uint GetLow32()
62 public static bool operator !(base_uint a)
64 for (int i = 0; i < a.nWidth; i++)
74 public static base_uint operator ~(base_uint a)
76 var ret = new base_uint();
77 for (int i = 0; i < a.nWidth; i++)
84 public static base_uint operator -(base_uint a)
86 var ret = new base_uint();
87 for (int i = 0; i < a.nWidth; i++)
96 public static base_uint operator ++(base_uint a)
99 while (++a.pn[i] == 0 && i < a.nWidth - 1)
106 public static base_uint operator --(base_uint a)
109 while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1)
116 public static base_uint operator ^(base_uint a, base_uint b)
118 var result = new base_uint();
119 result.pn = new uint[a.nWidth];
120 for (int i = 0; i < result.nWidth; i++)
122 result.pn[i] = a.pn[i] ^ b.pn[i];
127 public static base_uint operator +(base_uint a, base_uint b)
129 var result = new base_uint();
131 for (int i = 0; i < result.nWidth; i++)
133 ulong n = carry + a.pn[i] + b.pn[i];
134 result.pn[i] = (uint)(n & 0xffffffff);
140 public static base_uint operator -(base_uint a, base_uint b)
145 public static base_uint operator &(base_uint a, base_uint b)
147 var result = new base_uint();
148 result.pn = new uint[a.nWidth];
149 for (int i = 0; i < result.nWidth; i++)
151 result.pn[i] = a.pn[i] & b.pn[i];
156 public static base_uint operator |(base_uint a, base_uint b)
158 var result = new base_uint();
159 result.pn = new uint[a.nWidth];
160 for (int i = 0; i < result.nWidth; i++)
162 result.pn[i] = a.pn[i] | b.pn[i];
167 public static bool operator <(base_uint a, base_uint b)
169 for (int i = a.nWidth - 1; i >= 0; i--)
171 if (a.pn[i] < b.pn[i])
175 else if (a.pn[i] > b.pn[i])
183 public static bool operator <=(base_uint a, base_uint b)
185 for (int i = a.nWidth - 1; i >= 0; i--)
187 if (a.pn[i] < b.pn[i])
191 else if (a.pn[i] > b.pn[i])
199 public static bool operator >(base_uint a, base_uint b)
201 for (int i = a.nWidth - 1; i >= 0; i--)
203 if (a.pn[i] > b.pn[i])
207 else if (a.pn[i] < b.pn[i])
215 public static bool operator >=(base_uint a, base_uint b)
217 for (int i = a.nWidth - 1; i >= 0; i--)
219 if (a.pn[i] > b.pn[i])
223 else if (a.pn[i] < b.pn[i])
231 public static bool operator ==(base_uint a, base_uint b)
233 if (object.ReferenceEquals(a, b))
238 for (int i = 0; i < a.nWidth; i++)
240 if (a.pn[i] != b.pn[i])
248 public static bool operator ==(base_uint a, ulong b)
250 if (a.pn[0] != (uint)b)
254 if (a.pn[1] != (uint)(b >> 32))
258 for (int i = 2; i < a.nWidth; i++)
268 public static bool operator !=(base_uint a, base_uint b)
273 public static bool operator !=(base_uint a, ulong b)
278 public static bool operator true(base_uint a)
283 public static bool operator false(base_uint a)
288 public static implicit operator byte[] (base_uint a)
290 var result = new byte[a.nWidth];
291 for (int i = 0; i < a.nWidth; i++)
293 Buffer.BlockCopy(BitConverter.GetBytes(a.pn[i]), 0, result, 4 * i, 4);
298 private static bool ArraysEqual(uint[] a, uint[] b)
300 if (a.Length != b.Length)
304 for (int i = 0; i < a.Length; i++)
314 public override int GetHashCode()
319 foreach (var element in pn)
321 hash = hash * 31 + element.GetHashCode();
327 public int CompareTo(base_uint item)
333 else if (this < item)
341 public bool Equals(base_uint a)
348 return ArraysEqual(pn, a.pn);
351 public override bool Equals(object o)
353 return Equals(o as base_uint);