X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fuint160.cs;h=be4d9180a4e5542b4c8b9cc19dbb74f44ae3f593;hb=a1df90fa7b7daf76469bd5e691910c74d92bf802;hp=14175629de789509738a78d6cc8b52e30fa5bdd0;hpb=80b9c15a0c2d1b87180824a36948e37894cebaca;p=NovacoinLibrary.git diff --git a/Novacoin/uint160.cs b/Novacoin/uint160.cs index 1417562..be4d918 100644 --- a/Novacoin/uint160.cs +++ b/Novacoin/uint160.cs @@ -1,32 +1,50 @@ -using System; -using System.Collections.Generic; +/** + * Novacoin classes library + * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com) + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +using System; using System.Diagnostics.Contracts; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Novacoin { public class uint160 : base_uint { - new protected readonly int nWidth = 5; + #region Access to internal representation + new protected int nWidth + { + get { return base.nWidth; } + private set { base.nWidth = value; } + } + new protected uint[] pn + { + get { return base.pn; } + private set { base.pn = value; } + } + #endregion + #region Constructors public uint160() { - base.nWidth = nWidth; + nWidth = 5; pn = new uint[nWidth]; - - for (int i = 0; i < nWidth; i++) - { - pn[i] = 0; - } } - public uint160(uint160 b) + public uint160(uint160 b) : this() { - base.nWidth = nWidth; - pn = new uint[nWidth]; - for (int i = 0; i < nWidth; i++) { pn[i] = b.pn[i]; @@ -34,11 +52,8 @@ namespace Novacoin } - public uint160(ulong n) + public uint160(ulong n) : this() { - base.nWidth = nWidth; - pn = new uint[nWidth]; - pn[0] = (uint)n; pn[1] = (uint)(n >> 32); for (int i = 2; i < nWidth; i++) @@ -47,76 +62,78 @@ namespace Novacoin } } - public uint160(byte[] bytes) + public uint160(byte[] bytes) : this() { Contract.Requires(bytes.Length == 20, "Incorrect array length"); - - base.nWidth = nWidth; pn = Interop.ToUInt32Array(bytes); } - public uint160(string hex) + public uint160(string hex) : this() { Contract.Requires(hex.Length == 40, "Incorrect string"); - - base.nWidth = nWidth; var bytes = Interop.ReverseBytes(Interop.HexToArray(hex)); pn = Interop.ToUInt32Array(bytes); } + #endregion - public static uint160 operator ~(uint160 a) + #region Cast operators + public static implicit operator uint160(byte[] bytes) { - var ret = new uint160(); - for (int i = 0; i < a.nWidth; i++) - { - ret.pn[i] = ~a.pn[i]; - } - return ret; + return new uint160(bytes); } - public static uint160 operator -(uint160 a) + public static implicit operator uint160(ulong n) + { + return new uint160(n); + } + #endregion + + #region Bitwise operations + public static uint160 operator ~(uint160 a) { var ret = new uint160(); for (int i = 0; i < a.nWidth; i++) { ret.pn[i] = ~a.pn[i]; } - ret++; return ret; } - - public static uint160 operator ++(uint160 a) + public static uint160 operator ^(uint160 a, uint160 b) { - int i = 0; - while (++a.pn[i] == 0 && i < a.nWidth - 1) + var result = new uint160(); + result.pn = new uint[a.nWidth]; + for (int i = 0; i < result.nWidth; i++) { - i++; + result.pn[i] = a.pn[i] ^ b.pn[i]; } - return a; + return result; } - public static uint160 operator --(uint160 a) + public static uint160 operator &(uint160 a, uint160 b) { - int i = 0; - while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1) + var result = new uint160(); + result.pn = new uint[a.nWidth]; + for (int i = 0; i < result.nWidth; i++) { - i++; + result.pn[i] = a.pn[i] & b.pn[i]; } - return a; + return result; } - public static uint160 operator ^(uint160 a, uint160 b) + public static uint160 operator |(uint160 a, uint160 b) { var result = new uint160(); result.pn = new uint[a.nWidth]; for (int i = 0; i < result.nWidth; i++) { - result.pn[i] = a.pn[i] ^ b.pn[i]; + result.pn[i] = a.pn[i] | b.pn[i]; } return result; } + #endregion + #region Basic arithmetics public static uint160 operator +(uint160 a, uint160 b) { var result = new uint160(); @@ -145,28 +162,74 @@ namespace Novacoin return a - new uint160(b); } - public static uint160 operator &(uint160 a, uint160 b) + public static uint160 operator -(uint160 a) { - var result = new uint160(); - result.pn = new uint[a.nWidth]; - for (int i = 0; i < result.nWidth; i++) + var ret = new uint160(); + for (int i = 0; i < a.nWidth; i++) { - result.pn[i] = a.pn[i] & b.pn[i]; + ret.pn[i] = ~a.pn[i]; } - return result; + ret++; + return ret; } - public static uint160 operator |(uint160 a, uint160 b) + + public static uint160 operator ++(uint160 a) + { + int i = 0; + while (++a.pn[i] == 0 && i < a.nWidth - 1) + { + i++; + } + return a; + } + + public static uint160 operator --(uint160 a) + { + int i = 0; + while (--a.pn[i] == uint.MaxValue && i < a.nWidth - 1) + { + i++; + } + return a; + } + + public static uint160 operator /(uint160 a, uint divisor) { var result = new uint160(); - result.pn = new uint[a.nWidth]; - for (int i = 0; i < result.nWidth; i++) + + ulong r = 0; + int i = a.nWidth; + + while (i-- > 0) { - result.pn[i] = a.pn[i] | b.pn[i]; + r <<= 32; + r |= a.pn[i]; + result.pn[i] = (uint)(r / divisor); + r %= divisor; } + return result; } + public static uint operator %(uint160 a, uint divisor) + { + ulong r = 0; + int i = a.nWidth; + + while (i-- > 0) + { + r <<= 32; + r |= a.pn[i]; + r %= divisor; + } + + return (uint)r; + } + + #endregion + + #region Shift public static uint160 operator <<(uint160 a, int shift) { var result = new uint160(); @@ -210,5 +273,6 @@ namespace Novacoin return result; } + #endregion } }