Skip to content

Commit 3862c35

Browse files
Remove order from RFC6979 and use scalar to reduce data
1 parent 8dce0e9 commit 3862c35

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

Src/Autarkysoft.Bitcoin/Cryptography/Rfc6979.cs

+9-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
55

6+
using Autarkysoft.Bitcoin.Cryptography.EllipticCurve;
67
using Autarkysoft.Bitcoin.Cryptography.Hashing;
78
using System;
89
using System.Numerics;
@@ -23,30 +24,17 @@ public sealed class Rfc6979 : IDisposable
2324
/// </summary>
2425
public Rfc6979()
2526
{
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;
3827
HmacK = new HmacSha256();
3928
}
4029

4130

4231

4332
private const int QLen = 256;
44-
private readonly BigInteger order;
4533
private HmacSha256 HmacK;
4634

4735

4836

49-
private BigInteger BitsToInt(byte[] ba)
37+
private static BigInteger BitsToInt(byte[] ba)
5038
{
5139
BigInteger big = ba.ToBigInt(true, true);
5240
int vLen = ba.Length * 8;
@@ -81,11 +69,12 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
8169
byte[] k = new byte[32];
8270

8371
// d.
84-
// K = HMAC_K(V || 0x01 || int2octets(x) || bits2octets(h1))
72+
// K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1))
8573
int entLen = extraEntropy is null ? 0 : extraEntropy.Length;
8674
// 97 = 32 + 1 + 32 + 32
8775
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();
8978

9079
Buffer.BlockCopy(v, 0, bytesToHash, 0, 32);
9180
// Set item at index 32 to 0x00
@@ -101,7 +90,7 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
10190
// e.
10291
v = HmacK.ComputeHash(v, k);
10392

104-
// f.
93+
// f. K = HMAC_K(V || 0x01 || int2octets(x) || bits2octets(h1))
10594
Buffer.BlockCopy(v, 0, bytesToHash, 0, 32);
10695
// Set item at index 33 to 0x01 this time
10796
bytesToHash[32] = 0x01;
@@ -118,10 +107,10 @@ public BigInteger GetK(byte[] data, byte[] keyBytes, byte[] extraEntropy)
118107
v = HmacK.ComputeHash(v, k);
119108

120109
// 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)
123112
{
124-
return kTemp;
113+
return new BigInteger(v, isUnsigned: true, isBigEndian: true);
125114
}
126115
else
127116
{

Src/Tests/Bitcoin/Cryptography/Rfc6979Tests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void GetK_RFCTest1()
2222
byte[] data = Helper.HexToBytes("AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF");
2323
byte[] keyBytes = Helper.HexToBytes("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721");
2424

25-
using Rfc6979 rfc = new Rfc6979(order);
25+
using Rfc6979 rfc = new();
2626

2727
BigInteger actual = rfc.GetK(data, keyBytes, null);
2828
BigInteger expected = BigInteger.Parse("00A6E3C57DD01ABE90086538398355DD4C3B17AA873382B0F24D6129493D8AAD60", NumberStyles.HexNumber);
@@ -38,7 +38,7 @@ public void GetK_RFCTest2()
3838
byte[] data = Helper.HexToBytes("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
3939
byte[] keyBytes = Helper.HexToBytes("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721");
4040

41-
using Rfc6979 rfc = new Rfc6979(order);
41+
using Rfc6979 rfc = new();
4242

4343
BigInteger actual = rfc.GetK(data, keyBytes, null);
4444
BigInteger expected = BigInteger.Parse("00D16B6AE827F17175E040871A1C7EC3500192C4C92677336EC2537ACAEE0008E0", NumberStyles.HexNumber);

0 commit comments

Comments
 (0)