3
3
// Distributed under the MIT software license, see the accompanying
4
4
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5
5
6
+ using Autarkysoft . Bitcoin . Cryptography . EllipticCurve ;
6
7
using Autarkysoft . Bitcoin . Cryptography . Hashing ;
7
8
using System ;
8
9
using System . Numerics ;
@@ -23,30 +24,17 @@ public sealed class Rfc6979 : IDisposable
23
24
/// </summary>
24
25
public Rfc6979 ( )
25
26
{
26
- // Curve.N
27
- order = BigInteger . Parse ( "115792089237316195423570985008687907853269984665640564039457584007908834671663" ) ;
28
- HmacK = new HmacSha256 ( ) ;
29
- }
30
-
31
- /// <summary>
32
- /// Initializes a new instance of <see cref="Rfc6979"/> with the given order used only for testing.
33
- /// </summary>
34
- /// <param name="order">Order of the test curve</param>
35
- public Rfc6979 ( BigInteger order )
36
- {
37
- this . order = order ;
38
27
HmacK = new HmacSha256 ( ) ;
39
28
}
40
29
41
30
42
31
43
32
private const int QLen = 256 ;
44
- private readonly BigInteger order ;
45
33
private HmacSha256 HmacK ;
46
34
47
35
48
36
49
- private BigInteger BitsToInt ( byte [ ] ba )
37
+ private static BigInteger BitsToInt ( byte [ ] ba )
50
38
{
51
39
BigInteger big = ba . ToBigInt ( true , true ) ;
52
40
int vLen = ba . Length * 8 ;
@@ -81,11 +69,12 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
81
69
byte [ ] k = new byte [ 32 ] ;
82
70
83
71
// d.
84
- // K = HMAC_K(V || 0x01 || int2octets(x) || bits2octets(h1))
72
+ // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1))
85
73
int entLen = extraEntropy is null ? 0 : extraEntropy . Length ;
86
74
// 97 = 32 + 1 + 32 + 32
87
75
byte [ ] bytesToHash = new byte [ 97 + entLen ] ;
88
- byte [ ] dataBa = ( data . ToBigInt ( true , true ) % order ) . ToByteArray ( true , true ) ;
76
+ Scalar8x32 sc = new Scalar8x32 ( data , out _ ) ;
77
+ byte [ ] dataBa = sc . ToByteArray ( ) ;
89
78
90
79
Buffer . BlockCopy ( v , 0 , bytesToHash , 0 , 32 ) ;
91
80
// Set item at index 32 to 0x00
@@ -101,7 +90,7 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
101
90
// e.
102
91
v = HmacK . ComputeHash ( v , k ) ;
103
92
104
- // f.
93
+ // f. K = HMAC_K(V || 0x01 || int2octets(x) || bits2octets(h1))
105
94
Buffer . BlockCopy ( v , 0 , bytesToHash , 0 , 32 ) ;
106
95
// Set item at index 33 to 0x01 this time
107
96
bytesToHash [ 32 ] = 0x01 ;
@@ -118,10 +107,10 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
118
107
v = HmacK . ComputeHash ( v , k ) ;
119
108
120
109
// h.3.
121
- BigInteger kTemp = BitsToInt ( v ) ;
122
- if ( kTemp != 0 && kTemp < order )
110
+ Scalar8x32 temp = new Scalar8x32 ( v , out bool of ) ;
111
+ if ( ! temp . IsZero && ! of )
123
112
{
124
- return kTemp ;
113
+ return new BigInteger ( v , isUnsigned : true , isBigEndian : true ) ;
125
114
}
126
115
else
127
116
{
0 commit comments