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/>.
21 using System.Diagnostics.Contracts;
26 /// Base class for uint256 and uint160.
28 public class base_uint : IComparable<base_uint>, IEquatable<base_uint>
30 protected readonly int nWidth;
33 public double getDouble()
38 for (int i = 0; i < nWidth; i++)
47 public ulong GetLow64()
49 return pn[0] | (ulong)pn[1] << 32;
52 public uint GetLow32()
65 public static bool operator !(base_uint a)
67 for (int i = 0; i < a.nWidth; i++)
78 public static bool operator <(base_uint a, base_uint b)
80 for (int i = a.nWidth - 1; i >= 0; i--)
82 if (a.pn[i] < b.pn[i])
86 else if (a.pn[i] > b.pn[i])
94 public static bool operator <=(base_uint a, base_uint b)
96 for (int i = a.nWidth - 1; i >= 0; i--)
98 if (a.pn[i] < b.pn[i])
102 else if (a.pn[i] > b.pn[i])
110 public static bool operator >(base_uint a, base_uint b)
112 for (int i = a.nWidth - 1; i >= 0; i--)
114 if (a.pn[i] > b.pn[i])
118 else if (a.pn[i] < b.pn[i])
126 public static bool operator >=(base_uint a, base_uint b)
128 for (int i = a.nWidth - 1; i >= 0; i--)
130 if (a.pn[i] > b.pn[i])
134 else if (a.pn[i] < b.pn[i])
142 public static bool operator ==(base_uint a, base_uint b)
144 if (object.ReferenceEquals(a, b))
149 for (int i = 0; i < a.nWidth; i++)
151 if (a.pn[i] != b.pn[i])
159 public static bool operator ==(base_uint a, ulong b)
161 if (a.pn[0] != (uint)b)
166 if (a.pn[1] != (uint)(b >> 32))
171 for (int i = 2; i < a.nWidth; i++)
181 public static bool operator !=(base_uint a, base_uint b)
186 public static bool operator !=(base_uint a, ulong b)
191 public static bool operator true(base_uint a)
196 public static bool operator false(base_uint a)
201 public static implicit operator byte[] (base_uint a)
203 return Interop.LEBytes(a.pn);
206 private static bool ArraysEqual(uint[] a, uint[] b)
208 Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
210 for (int i = 0; i < a.Length; i++)
220 public override int GetHashCode()
225 foreach (var element in pn)
227 hash = hash * 31 + element.GetHashCode();
233 public int CompareTo(base_uint item)
239 else if (this < item)
247 public bool Equals(base_uint a)
254 return ArraysEqual(pn, a.pn);
257 public override bool Equals(object o)
259 return Equals(o as base_uint);
262 public override string ToString()
264 return Interop.ToHex(Interop.ReverseBytes(this));