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>
31 #region Internal representation
33 /// Length of internal representation
37 /// Big numbers are stored as array of unsigned 32-bit integers.
42 #region Helper properties
50 for (int i = 0; i < nWidth; i++)
61 /// First 64 bits as ulong value.
65 get { return pn[0] | (ulong)pn[1] << 32; }
69 /// First 32 bits as uint value.
77 /// Total size in bytes.
83 return nWidth * sizeof(uint);
88 /// Zero or the position of highest non-zero bit plus one.
94 for (int pos = nWidth - 1; pos >= 0; pos--)
98 for (int bits = 31; bits > 0; bits--)
100 if ((pn[pos] & 1 << bits) != 0)
101 return 32 * pos + bits + 1;
113 /// Negation operator
115 /// <param name="a">Value</param>
116 /// <returns>True if value is zero, false otherwise.</returns>
117 public static bool operator !(base_uint a)
119 for (int i = 0; i < a.nWidth; i++)
130 #region Comparison operations
131 public static bool operator <(base_uint a, base_uint b)
133 for (int i = a.nWidth - 1; i >= 0; i--)
135 if (a.pn[i] < b.pn[i])
139 else if (a.pn[i] > b.pn[i])
147 public static bool operator <=(base_uint a, base_uint b)
149 for (int i = a.nWidth - 1; i >= 0; i--)
151 if (a.pn[i] < b.pn[i])
155 else if (a.pn[i] > b.pn[i])
163 public static bool operator >(base_uint a, base_uint b)
165 for (int i = a.nWidth - 1; i >= 0; i--)
167 if (a.pn[i] > b.pn[i])
171 else if (a.pn[i] < b.pn[i])
179 public static bool operator >=(base_uint a, base_uint b)
181 for (int i = a.nWidth - 1; i >= 0; i--)
183 if (a.pn[i] > b.pn[i])
187 else if (a.pn[i] < b.pn[i])
196 #region Equality operators
197 public static bool operator ==(base_uint a, base_uint b)
199 if (object.ReferenceEquals(a, b))
204 for (int i = 0; i < a.nWidth; i++)
206 if (a.pn[i] != b.pn[i])
214 public static bool operator ==(base_uint a, ulong b)
216 if (a.pn[0] != (uint)b)
221 if (a.pn[1] != (uint)(b >> 32))
226 for (int i = 2; i < a.nWidth; i++)
236 public static bool operator !=(base_uint a, base_uint b)
241 public static bool operator !=(base_uint a, ulong b)
247 #region Cast operations
249 /// True cast operator
251 /// <param name="a"></param>
252 /// <returns></returns>
253 public static bool operator true(base_uint a)
259 /// False cast operator.
261 /// <param name="a">Value</param>
262 /// <returns>Boolean result</returns>
263 public static bool operator false(base_uint a)
269 /// Imlicit byte[] cast operator.
271 /// <param name="a">Value</param>
272 public static implicit operator byte[] (base_uint a)
274 return Interop.LEBytes(a.pn);
279 /// Arrays equality checking helper method.
281 /// <param name="a">Array 1</param>
282 /// <param name="b">Array 2</param>
283 /// <returns>Result.</returns>
284 private static bool ArraysEqual(uint[] a, uint[] b)
286 Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
288 for (int i = 0; i < a.Length; i++)
299 #region IEqualityComparer
300 public bool Equals(base_uint a, base_uint b)
302 if (object.ReferenceEquals(a, b))
307 return ArraysEqual(a.pn, b.pn);
310 public int GetHashCode(base_uint a)
312 return a.GetHashCode();
318 public int CompareTo(base_uint item)
324 else if (this < item)
334 public bool Equals(base_uint a)
336 if (object.ReferenceEquals(a, null))
341 return ArraysEqual(pn, a.pn);
344 public override int GetHashCode()
349 foreach (var element in pn)
351 hash = hash * 31 + element.GetHashCode();
357 public override bool Equals(object o)
359 return Equals(o as base_uint);
364 /// Serialize to string.
366 /// <returns></returns>
367 public override string ToString()
369 return Interop.ToHex(Interop.ReverseBytes(this));