Skip to content

Commit e9070dd

Browse files
[release/7.0-rc1] Fix for Random failures in System.Numerics.Tests.modpowTest.ModPowAxiom test (#74181)
* Fixed #70330 * Removed pessimistic buffer cleanup Co-authored-by: sakno <[email protected]>
1 parent 3c73d4d commit e9070dd

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.PowMod.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ public static void Pow(ReadOnlySpan<uint> value, uint power,
217217
Span<uint> valueCopy = (size <= StackAllocThreshold ?
218218
stackalloc uint[StackAllocThreshold]
219219
: valueCopyFromPool = ArrayPool<uint>.Shared.Rent(size)).Slice(0, size);
220-
valueCopy.Clear();
220+
221+
// smallish optimization here:
222+
// subsequent operations will copy the elements to the beginning of the buffer,
223+
// no need to clear everything
224+
valueCopy.Slice(value.Length).Clear();
221225

222226
if (value.Length > modulus.Length)
223227
{
@@ -262,7 +266,11 @@ public static void Pow(ReadOnlySpan<uint> value, ReadOnlySpan<uint> power,
262266
Span<uint> valueCopy = (size <= StackAllocThreshold ?
263267
stackalloc uint[StackAllocThreshold]
264268
: valueCopyFromPool = ArrayPool<uint>.Shared.Rent(size)).Slice(0, size);
265-
valueCopy.Clear();
269+
270+
// smallish optimization here:
271+
// subsequent operations will copy the elements to the beginning of the buffer,
272+
// no need to clear everything
273+
valueCopy.Slice(value.Length).Clear();
266274

267275
if (value.Length > modulus.Length)
268276
{
@@ -464,7 +472,7 @@ private static Span<uint> PowCore(Span<uint> value, int valueLength,
464472
power >>= 1;
465473
}
466474

467-
return result.Slice(0, resultLength);
475+
return result;
468476
}
469477

470478
private static Span<uint> PowCore(Span<uint> value, int valueLength,

src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ public static void ModPowAxiom()
274274
}
275275
}
276276

277+
[Fact]
278+
public static void RegressionIssue70330()
279+
{
280+
byte[] tempByteArray1 = { 226, 32 };
281+
byte[] tempByteArray2 = { 113 };
282+
byte[] tempByteArray3 = { 15, 8, 201, 158, 96, 200, 233, 243, 184, 0, 33, 203, 210, 80, 174, 198, 244, 177, 223, 221, 168, 243, 233, 133, 103, 252, 219, 195, 187, 227, 215, 54, 66, 248, 37, 186, 232, 45, 227, 147, 100, 14, 121, 244, 56, 89, 181, 120, 205, 4, 59, 48, 65, 239, 221, 28, 30, 68, 55, 99, 237, 38, 56, 213, 40, 234, 136, 218, 42, 244, 222, 198, 205 };
283+
VerifyIdentityString(
284+
Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "tModPow",
285+
Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "bPow" + " bRemainder"
286+
);
287+
}
288+
277289
[Fact]
278290
public static void ModPowBoundary()
279291
{

0 commit comments

Comments
 (0)