X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fbase_uint.cs;h=adfa75f0f735c1ddf68fab0611fd85b74e4a45b9;hb=1dcac5faa2b1477034f82466ffb16170fa2e9bb6;hp=e5a36bf9c1bbdb1371449bd31fc16ba7c6a08ad4;hpb=584a6e678d645766a522fd5d25e5ae49726f7634;p=NovacoinLibrary.git diff --git a/Novacoin/base_uint.cs b/Novacoin/base_uint.cs index e5a36bf..adfa75f 100644 --- a/Novacoin/base_uint.cs +++ b/Novacoin/base_uint.cs @@ -18,6 +18,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; namespace Novacoin @@ -25,40 +26,55 @@ namespace Novacoin /// /// Base class for uint256 and uint160. /// - public class base_uint : IComparable, IEquatable + public class base_uint : IComparable, IEquatable, IEqualityComparer { + #region Internal representation + /// + /// Length of internal representation + /// protected int nWidth; + /// + /// Big numbers are stored as array of unsigned 32-bit integers. + /// protected uint[] pn; + #endregion - public double getDouble() + #region Helper properties + public double Double { - double ret = 0.0; - double fact = 1.0; - - for (int i = 0; i < nWidth; i++) + get { - ret += fact * pn[i]; - fact *= 4294967296.0; - } + double ret = 0.0; + double fact = 1.0; - return ret; + for (int i = 0; i < nWidth; i++) + { + ret += fact * pn[i]; + fact *= 4294967296.0; + } + + return ret; + } } - public ulong GetLow64() + public ulong Low64 { - return pn[0] | (ulong)pn[1] << 32; + get { return pn[0] | (ulong)pn[1] << 32; } } - public uint GetLow32() + public uint Low32 { - return pn[0]; + get { return pn[0]; } } + /// + /// Total size in bytes. + /// public int Size { get { - return nWidth; + return nWidth * sizeof(uint); } } @@ -71,11 +87,11 @@ namespace Novacoin { for (int pos = nWidth - 1; pos >= 0; pos--) { - if (pn[pos]!=0) + if (pn[pos] != 0) { for (int bits = 31; bits > 0; bits--) { - if ((pn[pos] & 1 << bits)!=0) + if ((pn[pos] & 1 << bits) != 0) return 32 * pos + bits + 1; } return 32 * pos + 1; @@ -84,7 +100,14 @@ namespace Novacoin return 0; } } + #endregion + + /// + /// Negation operator + /// + /// Value + /// True if value is zero, false otherwise. public static bool operator !(base_uint a) { for (int i = 0; i < a.nWidth; i++) @@ -98,6 +121,7 @@ namespace Novacoin } + #region Comparison operations public static bool operator <(base_uint a, base_uint b) { for (int i = a.nWidth - 1; i >= 0; i--) @@ -161,7 +185,9 @@ namespace Novacoin } return true; } + #endregion + #region Equality operators public static bool operator ==(base_uint a, base_uint b) { if (object.ReferenceEquals(a, b)) @@ -210,22 +236,45 @@ namespace Novacoin { return (!(a == b)); } + #endregion + #region Cast oerations + /// + /// True cast operator + /// + /// + /// public static bool operator true(base_uint a) { return (a != 0); } + /// + /// False cast operator. + /// + /// Value + /// Boolean result public static bool operator false(base_uint a) { return (a == 0); } + /// + /// Imlicit byte[] cast operator. + /// + /// Value public static implicit operator byte[] (base_uint a) { return Interop.LEBytes(a.pn); } + #endregion + /// + /// Arrays equality checking helper method. + /// + /// Array 1 + /// Array 2 + /// Result. private static bool ArraysEqual(uint[] a, uint[] b) { Contract.Requires(a.Length == b.Length, "Array length mismatch."); @@ -240,19 +289,26 @@ namespace Novacoin return true; } - public override int GetHashCode() + + #region IEqualityComparer + public bool Equals(base_uint a, base_uint b) { - int hash = 17; - unchecked + if (object.ReferenceEquals(a, b)) { - foreach (var element in pn) - { - hash = hash * 31 + element.GetHashCode(); - } + return true; } - return hash; + + return ArraysEqual(a.pn, b.pn); + } + + public int GetHashCode(base_uint a) + { + return a.GetHashCode(); } + #endregion + + #region IComparable public int CompareTo(base_uint item) { if (this > item) @@ -266,10 +322,12 @@ namespace Novacoin return 0; } + #endregion + #region IEquatable public bool Equals(base_uint a) { - if (a == null) + if (object.ReferenceEquals(a, null)) { return false; } @@ -277,11 +335,29 @@ namespace Novacoin return ArraysEqual(pn, a.pn); } + public override int GetHashCode() + { + int hash = 17; + unchecked + { + foreach (var element in pn) + { + hash = hash * 31 + element.GetHashCode(); + } + } + return hash; + } + public override bool Equals(object o) { return Equals(o as base_uint); } + #endregion + /// + /// Serialize to string. + /// + /// public override string ToString() { return Interop.ToHex(Interop.ReverseBytes(this));