forked from kevinkinnett/BitSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataCalculator.cs
118 lines (99 loc) · 3.73 KB
/
DataCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using BitSharp.Common;
using BitSharp.Core.Domain;
using System;
using System.Diagnostics;
using System.Numerics;
namespace BitSharp.Core
{
public static class DataCalculator
{
private static readonly BigInteger _2Pow256 = BigInteger.Pow(2, 256);
public static UInt256 CalculateBlockHash(BlockHeader blockHeader)
{
return new UInt256(SHA256Static.ComputeDoubleHash(DataEncoder.EncodeBlockHeader(blockHeader)));
}
public static UInt256 CalculateBlockHash(UInt32 Version, UInt256 PreviousBlock, UInt256 MerkleRoot, DateTimeOffset Time, UInt32 Bits, UInt32 Nonce)
{
return new UInt256(SHA256Static.ComputeDoubleHash(DataEncoder.EncodeBlockHeader(Version, PreviousBlock, MerkleRoot, Time, Bits, Nonce)));
}
public static UInt256 CalculateWork(BlockHeader blockHeader)
{
bool negative, overflow;
var target = FromCompact(blockHeader.Bits, out negative, out overflow);
if (negative || overflow || target == UInt256.Zero)
return UInt256.Zero;
return new UInt256(_2Pow256 / (target.ToBigInteger() + 1));
}
public static UInt256 FromCompact(uint compact)
{
bool negative, overflow;
return FromCompact(compact, out negative, out overflow);
}
public static UInt256 FromCompact(uint compact, out bool negative, out bool overflow)
{
var size = (int)(compact >> 24);
var word = compact & 0x007fffff;
UInt256 value;
if (size <= 3)
{
word >>= 8 * (3 - size);
value = (UInt256)word;
}
else
{
value = (UInt256)word;
value <<= 8 * (size - 3);
}
negative = word != 0 && (compact & 0x00800000) != 0;
overflow = word != 0 && ((size > 34) ||
(word > 0xff && size > 33) ||
(word > 0xffff && size > 32));
return value;
}
public static uint ToCompact(UInt256 value, bool negative = false)
{
var size = (HighBit(value) + 7) / 8;
var compact = 0U;
if (size <= 3)
{
compact = (uint)(value.Part4 << 8 * (3 - size));
}
else
{
value >>= 8 * (size - 3);
compact = (uint)(value.Part4);
}
// The 0x00800000 bit denotes the sign.
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
if ((compact & 0x00800000) != 0)
{
compact >>= 8;
size++;
}
Debug.Assert((compact & ~0x007fffff) == 0);
Debug.Assert(size < 256);
compact |= (uint)(size << 24);
if (negative && (compact & 0x007fffff) != 0)
compact |= 0x00800000;
return compact;
}
private static int HighBit(UInt256 value)
{
var parts = value.Parts;
for (var pos = parts.Length - 1; pos >= 0; pos--)
{
var part = parts[pos];
if (part != 0)
{
for (var bits = 63; bits > 0; bits--)
{
if ((part & 1UL << bits) != 0)
return 64 * pos + bits + 1;
}
return 64 * pos + 1;
}
}
return 0;
}
}
}