Skip to content

Commit cf5b429

Browse files
Cleanup Context object
1 parent d69a781 commit cf5b429

File tree

1 file changed

+30
-15
lines changed
  • Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve

1 file changed

+30
-15
lines changed

Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/Context.cs

+30-15
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,49 @@
88

99
namespace Autarkysoft.Bitcoin.Cryptography.EllipticCurve
1010
{
11+
/// <summary>
12+
/// Context object used to store randomization data for enhanced protection against side-channel leakage.
13+
/// </summary>
1114
public class Context
1215
{
13-
private Context()
16+
/// <summary>
17+
/// Instantiate a new instance of <see cref="Context"/> with default values.
18+
/// </summary>
19+
public Context()
1420
{
21+
// secp256k1_context_create() -> secp256k1_ecmult_gen_context_build -> secp256k1_ecmult_gen_blind(ctx, NULL)
22+
// When seed is NULL, reset the initial point and blinding value.
23+
initial = Point.G.ToPointJacobian().Negate();
24+
blind = Scalar8x32.One;
1525
}
1626

1727
// Blinding values used when computing (n-b)G + bG.
18-
Scalar8x32 blind; /* -b */
19-
PointJacobian initial;/* bG */
28+
Scalar8x32 blind; // -b
29+
PointJacobian initial;// bG
2030

2131
private const int ECMULT_GEN_PREC_BITS = 4;
2232
private static int ECMULT_GEN_PREC_N(int bits) => (256 / bits);
2333
private static int ECMULT_GEN_PREC_G(int bits) => (1 << bits);
2434

2535

26-
public static Context Create()
36+
/// <summary>
37+
/// Randomizes this context to provide enhanced protection against side-channel leakage.
38+
/// </summary>
39+
/// <remarks>
40+
/// It is highly recommended to call this method after instantiation and before using it in
41+
/// computations involving secret keys like signing and public key generation. It is possible
42+
/// to call this method more than once, and doing so before every few computations involving
43+
/// secret keys is recommended as a defense-in-depth measure.
44+
/// </remarks>
45+
/// <param name="seed32">a 32-byte random seed</param>
46+
public void Randomize(ReadOnlySpan<byte> seed32)
2747
{
28-
Context ctx = new Context();
29-
secp256k1_ecmult_gen_blind(ctx, null);
30-
return ctx;
31-
}
32-
33-
public void secp256k1_context_randomize(byte[] seed32)
34-
{
35-
secp256k1_ecmult_gen_blind(this, seed32);
48+
GenBlind(this, seed32);
3649
}
3750

3851

39-
private static void secp256k1_ecmult_gen_blind(Context ctx, ReadOnlySpan<byte> seed32)
52+
// secp256k1_ecmult_gen_blind
53+
private static void GenBlind(Context ctx, ReadOnlySpan<byte> seed32)
4054
{
4155
if (seed32 == null)
4256
{
@@ -71,7 +85,7 @@ private static void secp256k1_ecmult_gen_blind(Context ctx, ReadOnlySpan<byte> s
7185
nonce32.Clear();
7286

7387
// The random projection in ctx->initial ensures that gb will have a random projection.
74-
secp256k1_ecmult_gen(ctx, out PointJacobian gb, b);
88+
EcMultGen(ctx, out PointJacobian gb, b);
7589
ctx.blind = b.Negate();
7690
ctx.initial = gb;
7791
b = Scalar8x32.Zero;
@@ -91,8 +105,9 @@ private static void secp256k1_ecmult_gen_blind(Context ctx, ReadOnlySpan<byte> s
91105
// None of the resulting prec group elements have a known scalar, and neither do any of
92106
// the intermediate sums while computing a*G.
93107
// The prec values are stored in secp256k1_ecmult_gen_prec_table[i][n_i] = n_i * (PREC_G)^i * G + U_i.
94-
static unsafe void secp256k1_ecmult_gen(Context ctx, out PointJacobian r, in Scalar8x32 gn)
108+
private static unsafe void EcMultGen(Context ctx, out PointJacobian r, in Scalar8x32 gn)
95109
{
110+
// secp256k1_ecmult_gen
96111
int bits = ECMULT_GEN_PREC_BITS;
97112
int g = ECMULT_GEN_PREC_G(bits);
98113
int n = ECMULT_GEN_PREC_N(bits);

0 commit comments

Comments
 (0)