Skip to content

Commit 84461fc

Browse files
Remove verify from scalar ctor with uints (the caller has to call Verify now)
1 parent 5f4e7c4 commit 84461fc

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

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

+31-11
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public Scalar8x32(uint u)
2525
{
2626
b0 = u;
2727
b1 = 0; b1 = 0; b2 = 0; b3 = 0; b4 = 0; b5 = 0; b6 = 0; b7 = 0;
28-
Debug.Assert(CheckOverflow() == 0);
28+
Debug.Assert(Verify());
2929
}
3030

3131
/// <summary>
3232
/// Initializes a new instance of <see cref="Scalar8x32"/> using the given parameters.
3333
/// </summary>
3434
/// <remarks>
35-
/// Assumes there is no overflow
35+
/// Assumes caller handles overflow
3636
/// </remarks>
3737
/// <param name="u0">1st 32 bits</param>
3838
/// <param name="u1">2nd 32 bits</param>
@@ -46,14 +46,13 @@ public Scalar8x32(uint u0, uint u1, uint u2, uint u3, uint u4, uint u5, uint u6,
4646
{
4747
b0 = u0; b1 = u1; b2 = u2; b3 = u3;
4848
b4 = u4; b5 = u5; b6 = u6; b7 = u7;
49-
Debug.Assert(CheckOverflow() == 0);
5049
}
5150

5251
/// <summary>
5352
/// Initializes a new instance of <see cref="Scalar8x32"/> using the given array.
5453
/// </summary>
5554
/// <remarks>
56-
/// Assumes there is no overflow
55+
/// Assumes caller handles overflow
5756
/// </remarks>
5857
/// <exception cref="ArgumentNullException"/>
5958
/// <exception cref="ArgumentOutOfRangeException"/>
@@ -67,7 +66,6 @@ public Scalar8x32(Span<uint> array)
6766

6867
b0 = array[0]; b1 = array[1]; b2 = array[2]; b3 = array[3];
6968
b4 = array[4]; b5 = array[5]; b6 = array[6]; b7 = array[7];
70-
Debug.Assert(CheckOverflow() == 0);
7169
}
7270

7371
/// <summary>
@@ -350,11 +348,18 @@ public Scalar8x32 Half()
350348
// in full 64 bits to make sure the top 32 bits are indeed zero.
351349
Debug.Assert((t + (b7 >> 1) + (NH7 & mask)) >> 32 == 0);
352350

353-
return new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
351+
Scalar8x32 result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
352+
Debug.Assert(result.Verify());
353+
return result;
354354
}
355355

356356

357-
public uint CheckOverflow()
357+
public bool Verify()
358+
{
359+
return CheckOverflow() == 0;
360+
}
361+
362+
private uint CheckOverflow()
358363
{
359364
uint yes = 0U;
360365
uint no = 0U;
@@ -507,7 +512,9 @@ public Scalar8x32 CAddBit(uint bit, uint flag)
507512

508513
Debug.Assert((t >> 32) == 0);
509514

510-
return new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
515+
Scalar8x32 result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
516+
Debug.Assert(result.Verify());
517+
return result;
511518
}
512519

513520

@@ -1112,7 +1119,10 @@ private static Scalar8x32 Reduce(in Scalar8x32 r, uint overflow)
11121119
uint r6 = (uint)t; t >>= 32;
11131120
t += r.b7;
11141121
uint r7 = (uint)t;
1115-
return new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1122+
1123+
Scalar8x32 result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1124+
Debug.Assert(result.Verify());
1125+
return result;
11161126
}
11171127

11181128

@@ -1148,6 +1158,7 @@ public static unsafe Scalar8x32 MulShiftVar(in Scalar8x32 a, in Scalar8x32 b, in
11481158
uint r7 = shift < 288 ? (l[7 + shLimbs] >> shiftlow) : 0;
11491159

11501160
Scalar8x32 r = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1161+
Debug.Assert(r.Verify());
11511162
return r.CAddBit(0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
11521163
}
11531164

@@ -1180,7 +1191,9 @@ public Scalar8x32 Negate()
11801191
t += (ulong)(~b7) + N7;
11811192
uint r7 = (uint)(t & nonzero);
11821193

1183-
return new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1194+
Scalar8x32 result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1195+
Debug.Assert(result.Verify());
1196+
return result;
11841197
}
11851198

11861199
/// <summary>
@@ -1214,7 +1227,9 @@ public int NegateConditional(int flag, out Scalar8x32 result)
12141227
uint r6 = (uint)(t & nonzero); t >>= 32;
12151228
t += (ulong)(b7 ^ mask) + (N7 & mask);
12161229
uint r7 = (uint)(t & nonzero);
1230+
12171231
result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1232+
Debug.Assert(result.Verify());
12181233
// return 2 * (mask == 0) - 1;
12191234
return mask == 0 ? 1 : -1;
12201235
}
@@ -1243,7 +1258,9 @@ public static Scalar8x32 CMov(in Scalar8x32 r, in Scalar8x32 a, uint flag)
12431258
uint r6 = (r.b6 & mask0) | (a.b6 & mask1);
12441259
uint r7 = (r.b7 & mask0) | (a.b7 & mask1);
12451260

1246-
return new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1261+
Scalar8x32 result = new Scalar8x32(r0, r1, r2, r3, r4, r5, r6, r7);
1262+
Debug.Assert(result.Verify());
1263+
return result;
12471264
}
12481265

12491266

@@ -1259,6 +1276,9 @@ internal static void Split128(in Scalar8x32 k, out Scalar8x32 r1, out Scalar8x32
12591276

12601277
r1 = new Scalar8x32(k.b0, k.b1, k.b2, k.b3, 0, 0, 0, 0);
12611278
r2 = new Scalar8x32(k.b4, k.b5, k.b6, k.b7, 0, 0, 0, 0);
1279+
1280+
Debug.Assert(r1.Verify());
1281+
Debug.Assert(r2.Verify());
12621282
}
12631283

12641284
/// <summary>

Src/Tests/Bitcoin/Cryptography/EllipticCurve/Scalar8x32Tests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ public void Libsecp256k1Tests() // run_scalar_tests
654654
fixed (uint* ptr = arr)
655655
{
656656
Scalar8x32 s = new(ptr);
657-
Assert.True(s.CheckOverflow() == 1);
657+
Assert.False(s.Verify());
658658
}
659659
}
660660
}
@@ -701,7 +701,6 @@ public void Libsecp256k1_HalfTest(in Scalar8x32 n)
701701
Assert.Equal(n, s);
702702
}
703703

704-
705704
#endregion
706705
}
707706
}

0 commit comments

Comments
 (0)