Skip to content

Commit 77e10c6

Browse files
Fix some comments and add some more checks in Scalar8x32
1 parent 50d09ac commit 77e10c6

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public unsafe Scalar8x32(uint* hPt, out bool overflow)
9898
/// <summary>
9999
/// Initializes a new instance of <see cref="Scalar8x32"/> using the given pointer.
100100
/// </summary>
101-
/// <param name="hPt"><see cref="Hashing.Sha512.hashState"/> pointer</param>
101+
/// <param name="hPt">SHA512 hashState pointer</param>
102102
/// <param name="overflow">Returns true if value is bigger than or equal to curve order; otherwise false</param>
103103
public unsafe Scalar8x32(ulong* hPt, out bool overflow)
104104
{
@@ -1148,7 +1148,13 @@ public int NegateConditional(int flag, out Scalar8x32 result)
11481148
}
11491149

11501150

1151-
1151+
/// <summary>
1152+
/// Conditional move. Sets <paramref name="r"/> equal to <paramref name="a"/> if flag is true (=1).
1153+
/// </summary>
1154+
/// <param name="r"></param>
1155+
/// <param name="a"></param>
1156+
/// <param name="flag">Zero or one. Sets <paramref name="r"/> equal to <paramref name="a"/> if flag is one.</param>
1157+
/// <returns></returns>
11521158
public static Scalar8x32 CMov(in Scalar8x32 r, in Scalar8x32 a, uint flag)
11531159
{
11541160
Debug.Assert(GetOverflow(r) == 0);
@@ -1183,9 +1189,18 @@ internal static void Split128(in Scalar8x32 k, out Scalar8x32 r1, out Scalar8x32
11831189
r2 = new Scalar8x32(k.b4, k.b5, k.b6, k.b7, 0, 0, 0, 0);
11841190
}
11851191

1192+
/// <summary>
1193+
/// Find r1 and r2 such that r1+r2*lambda = k, where r1 and r2 or their negations are
1194+
/// maximum 128 bits long (see <see cref="Point.MulLambda"/>).
1195+
/// </summary>
1196+
/// <param name="r1"></param>
1197+
/// <param name="r2"></param>
1198+
/// <param name="k"></param>
11861199
internal static void SplitLambda(out Scalar8x32 r1, out Scalar8x32 r2, in Scalar8x32 k)
11871200
{
1188-
// these _var calls are constant time since the shift amount is constant
1201+
Debug.Assert(GetOverflow(k) == 0);
1202+
1203+
// these *Var calls are constant time since the shift amount is constant
11891204
Scalar8x32 c1 = MulShiftVar(k, G1, 384);
11901205
Scalar8x32 c2 = MulShiftVar(k, G2, 384);
11911206
c1 = c1.Multiply(Minus_b1);
@@ -1195,6 +1210,8 @@ internal static void SplitLambda(out Scalar8x32 r1, out Scalar8x32 r2, in Scalar
11951210
r1 = r1.Negate();
11961211
r1 = r1.Add(k, out _);
11971212

1213+
Debug.Assert(GetOverflow(r1) == 0);
1214+
Debug.Assert(GetOverflow(r2) == 0);
11981215
#if DEBUG
11991216
SplitLambdaVerify(r1, r2, k);
12001217
#endif
@@ -1235,7 +1252,8 @@ private static void SplitLambdaVerify(in Scalar8x32 r1, in Scalar8x32 r2, in Sca
12351252
Debug.Assert(MemCmpVar(buf1, k2_bound, 32) < 0 || MemCmpVar(buf2, k2_bound, 32) < 0);
12361253
}
12371254

1238-
private static int MemCmpVar(Span<byte> s1, Span<byte> s2, int n)
1255+
// https://github.com/bitcoin-core/secp256k1/blob/b314cf28334a91db2fe144d04f86077e2bfd7a25/src/util.h#L212-L228
1256+
private static int MemCmpVar(ReadOnlySpan<byte> s1, ReadOnlySpan<byte> s2, int n)
12391257
{
12401258
for (int i = 0; i < n; i++)
12411259
{
@@ -1247,7 +1265,8 @@ private static int MemCmpVar(Span<byte> s1, Span<byte> s2, int n)
12471265
}
12481266
return 0;
12491267
}
1250-
#endif
1268+
#endif // DEBUG
1269+
12511270

12521271
/// <summary>
12531272
/// Returns byte array representation of this instance

0 commit comments

Comments
 (0)