X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fuint256.cs;h=317b8d651b418e1942b1228e2158bb6783724de0;hb=1dcac5faa2b1477034f82466ffb16170fa2e9bb6;hp=15a9931425f25c7870a685ee62a1f7b517972c73;hpb=1ad33d77c24d57f7ac25e0d6cf5f430464cdb058;p=NovacoinLibrary.git diff --git a/Novacoin/uint256.cs b/Novacoin/uint256.cs index 15a9931..317b8d6 100644 --- a/Novacoin/uint256.cs +++ b/Novacoin/uint256.cs @@ -1,41 +1,56 @@ -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 uint256 : base_uint { - new public readonly int nWidth = 8; + #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 uint256() { + nWidth = 8; pn = new uint[nWidth]; - - for (int i = 0; i < nWidth; i++) - { - pn[i] = 0; - } } - public uint256(uint256 b) + public uint256(uint256 b) : this() { - pn = new uint[nWidth]; - for (int i = 0; i < nWidth; i++) { pn[i] = b.pn[i]; } } - - public uint256(ulong n) + public uint256(ulong n) : this() { - pn = new uint[nWidth]; - pn[0] = (uint)n; pn[1] = (uint)(n >> 32); for (int i = 2; i < nWidth; i++) @@ -44,22 +59,93 @@ namespace Novacoin } } - public uint256(byte[] bytes) + public uint256(byte[] bytes) : this() { Contract.Requires(bytes.Length == 32, "Incorrect array length"); pn = Interop.ToUInt32Array(bytes); } - public uint256(string hex) + public uint256(string hex) : this() { Contract.Requires(hex.Length == 64, "Incorrect string"); var bytes = Interop.ReverseBytes(Interop.HexToArray(hex)); pn = Interop.ToUInt32Array(bytes); } + #endregion + #region Cast operators + public static implicit operator uint256(byte[] bytes) + { + return new uint256(bytes); + } + public static implicit operator uint256(ulong n) + { + return new uint256(n); + } + #endregion + + #region Compact representation + /// + /// Compact representation of unsigned 256bit numbers. + /// + /// N = (-1^sign) * m * 256^(exp-3) + /// + /// http://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty + /// + public uint Compact + { + get + { + int nSize = (bits + 7) / 8; + uint nCompact = 0; + if (nSize <= 3) + nCompact = ((uint)Low64) << 8 * (3 - nSize); + else + { + uint256 bn = this >> 8 * (nSize - 3); + nCompact = (uint)bn.Low64; + } + + if ((nCompact & 0x00800000) != 0) + { + nCompact >>= 8; + nSize++; + } + + Contract.Assert((nCompact & ~0x007fffff) == 0); + Contract.Assert(nSize < 256); + + nCompact |= (uint)nSize << 24; + nCompact |= 0; + + return nCompact; + } + set { + int nSize = (int)value >> 24; + uint nWord = value & 0x007fffff; + + uint256 i; + + if (nSize <= 3) + { + nWord >>= 8 * (3 - nSize); + i = new uint256(nWord); + } + else + { + i = new uint256(nWord); + i <<= 8 * (nSize - 3); + } + + pn = i.pn; + } + } + #endregion + + #region Bitwise operations public static uint256 operator ~(uint256 a) { var ret = new uint256(); @@ -70,6 +156,41 @@ namespace Novacoin return ret; } + public static uint256 operator ^(uint256 a, uint256 b) + { + var result = new uint256(); + result.pn = new uint[a.nWidth]; + for (int i = 0; i < result.nWidth; i++) + { + result.pn[i] = a.pn[i] ^ b.pn[i]; + } + return result; + } + + public static uint256 operator &(uint256 a, uint256 b) + { + var result = new uint256(); + result.pn = new uint[a.nWidth]; + for (int i = 0; i < result.nWidth; i++) + { + result.pn[i] = a.pn[i] & b.pn[i]; + } + return result; + } + + public static uint256 operator |(uint256 a, uint256 b) + { + var result = new uint256(); + result.pn = new uint[a.nWidth]; + for (int i = 0; i < result.nWidth; i++) + { + result.pn[i] = a.pn[i] | b.pn[i]; + } + return result; + } + #endregion + + #region Basic arithmetics public static uint256 operator -(uint256 a) { var ret = new uint256(); @@ -102,16 +223,6 @@ namespace Novacoin return a; } - public static uint256 operator ^(uint256 a, uint256 b) - { - var result = new uint256(); - result.pn = new uint[a.nWidth]; - for (int i = 0; i < result.nWidth; i++) - { - result.pn[i] = a.pn[i] ^ b.pn[i]; - } - return result; - } public static uint256 operator +(uint256 a, uint256 b) { @@ -140,29 +251,9 @@ namespace Novacoin { return a - new uint256(b); } + #endregion - public static uint256 operator &(uint256 a, uint256 b) - { - var result = new uint256(); - result.pn = new uint[a.nWidth]; - for (int i = 0; i < result.nWidth; i++) - { - result.pn[i] = a.pn[i] & b.pn[i]; - } - return result; - } - - public static uint256 operator |(uint256 a, uint256 b) - { - var result = new uint256(); - result.pn = new uint[a.nWidth]; - for (int i = 0; i < result.nWidth; i++) - { - result.pn[i] = a.pn[i] | b.pn[i]; - } - return result; - } - + #region Shift public static uint256 operator <<(uint256 a, int shift) { var result = new uint256(); @@ -206,5 +297,6 @@ namespace Novacoin return result; } + #endregion } }