X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=blobdiff_plain;f=Novacoin%2Fuint256.cs;h=09e1a2c7572be9d8aa2769fe6e007e7919145f10;hp=e54b2be4a8f3751c0c141ae75f0e7b024b22f30b;hb=584a6e678d645766a522fd5d25e5ae49726f7634;hpb=80b9c15a0c2d1b87180824a36948e37894cebaca diff --git a/Novacoin/uint256.cs b/Novacoin/uint256.cs index e54b2be..09e1a2c 100644 --- a/Novacoin/uint256.cs +++ b/Novacoin/uint256.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Numerics; namespace Novacoin { @@ -64,6 +65,66 @@ namespace Novacoin pn = Interop.ToUInt32Array(bytes); } + /// + /// 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)GetLow64()) << 8 * (3 - nSize); + else + { + uint256 bn = this >> 8 * (nSize - 3); + nCompact = (uint)bn.GetLow64(); + } + + 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; + } + } + + private void SetBytes(byte[] bytes) + { + pn = Interop.ToUInt32Array(Interop.ReverseBytes(bytes)); + } public static uint256 operator ~(uint256 a) {