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.Collections.Generic;
22 using System.Diagnostics.Contracts;
27 /// Base class for uint256 and uint160.
29 public class base_uint : IComparable<base_uint>, IEquatable<base_uint>, IEqualityComparer<base_uint>
34 public double getDouble()
39 for (int i = 0; i < nWidth; i++)
48 public ulong GetLow64()
50 return pn[0] | (ulong)pn[1] << 32;
53 public uint GetLow32()
59 /// Total size in bytes.
65 return nWidth * sizeof(uint);
70 /// Zero or the position of highest non-zero bit plus one.
76 for (int pos = nWidth - 1; pos >= 0; pos--)
80 for (int bits = 31; bits > 0; bits--)
82 if ((pn[pos] & 1 << bits)!=0)
83 return 32 * pos + bits + 1;
92 public static bool operator !(base_uint a)
94 for (int i = 0; i < a.nWidth; i++)
105 #region Comparison operations
106 public static bool operator <(base_uint a, base_uint b)
108 for (int i = a.nWidth - 1; i >= 0; i--)
110 if (a.pn[i] < b.pn[i])
114 else if (a.pn[i] > b.pn[i])
122 public static bool operator <=(base_uint a, base_uint b)
124 for (int i = a.nWidth - 1; i >= 0; i--)
126 if (a.pn[i] < b.pn[i])
130 else if (a.pn[i] > b.pn[i])
138 public static bool operator >(base_uint a, base_uint b)
140 for (int i = a.nWidth - 1; i >= 0; i--)
142 if (a.pn[i] > b.pn[i])
146 else if (a.pn[i] < b.pn[i])
154 public static bool operator >=(base_uint a, base_uint b)
156 for (int i = a.nWidth - 1; i >= 0; i--)
158 if (a.pn[i] > b.pn[i])
162 else if (a.pn[i] < b.pn[i])
171 #region Equality operators
172 public static bool operator ==(base_uint a, base_uint b)
174 if (object.ReferenceEquals(a, b))
179 for (int i = 0; i < a.nWidth; i++)
181 if (a.pn[i] != b.pn[i])
189 public static bool operator ==(base_uint a, ulong b)
191 if (a.pn[0] != (uint)b)
196 if (a.pn[1] != (uint)(b >> 32))
201 for (int i = 2; i < a.nWidth; i++)
211 public static bool operator !=(base_uint a, base_uint b)
216 public static bool operator !=(base_uint a, ulong b)
222 #region Cast oerations
224 /// True cast operator
226 /// <param name="a"></param>
227 /// <returns></returns>
228 public static bool operator true(base_uint a)
234 /// False cast operator.
236 /// <param name="a">Value</param>
237 /// <returns>Boolean result</returns>
238 public static bool operator false(base_uint a)
244 /// Imlicit byte[] cast operator.
246 /// <param name="a">Value</param>
247 public static implicit operator byte[] (base_uint a)
249 return Interop.LEBytes(a.pn);
254 /// Arrays equality checking helper method.
256 /// <param name="a">Array 1</param>
257 /// <param name="b">Array 2</param>
258 /// <returns>Result.</returns>
259 private static bool ArraysEqual(uint[] a, uint[] b)
261 Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
263 for (int i = 0; i < a.Length; i++)
274 #region IEqualityComparer
275 public bool Equals(base_uint a, base_uint b)
277 if (object.ReferenceEquals(a, b))
282 return ArraysEqual(a.pn, b.pn);
285 public int GetHashCode(base_uint a)
287 return a.GetHashCode();
293 public int CompareTo(base_uint item)
299 else if (this < item)
309 public bool Equals(base_uint a)
316 return ArraysEqual(pn, a.pn);
319 public override int GetHashCode()
324 foreach (var element in pn)
326 hash = hash * 31 + element.GetHashCode();
332 public override bool Equals(object o)
334 return Equals(o as base_uint);
339 /// Serialize to string.
341 /// <returns></returns>
342 public override string ToString()
344 return Interop.ToHex(Interop.ReverseBytes(this));