Skip to content

Commit

Permalink
[release/7.0-rc1] Fix for Random failures in System.Numerics.Tests.mo…
Browse files Browse the repository at this point in the history
…dpowTest.ModPowAxiom test (#74181)

* Fixed #70330

* Removed pessimistic buffer cleanup

Co-authored-by: sakno <roman.sakno@gmail.com>
  • Loading branch information
github-actions[bot] and sakno authored Aug 20, 2022
1 parent 3c73d4d commit e9070dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ public static void Pow(ReadOnlySpan<uint> value, uint power,
Span<uint> valueCopy = (size <= StackAllocThreshold ?
stackalloc uint[StackAllocThreshold]
: valueCopyFromPool = ArrayPool<uint>.Shared.Rent(size)).Slice(0, size);
valueCopy.Clear();

// smallish optimization here:
// subsequent operations will copy the elements to the beginning of the buffer,
// no need to clear everything
valueCopy.Slice(value.Length).Clear();

if (value.Length > modulus.Length)
{
Expand Down Expand Up @@ -262,7 +266,11 @@ public static void Pow(ReadOnlySpan<uint> value, ReadOnlySpan<uint> power,
Span<uint> valueCopy = (size <= StackAllocThreshold ?
stackalloc uint[StackAllocThreshold]
: valueCopyFromPool = ArrayPool<uint>.Shared.Rent(size)).Slice(0, size);
valueCopy.Clear();

// smallish optimization here:
// subsequent operations will copy the elements to the beginning of the buffer,
// no need to clear everything
valueCopy.Slice(value.Length).Clear();

if (value.Length > modulus.Length)
{
Expand Down Expand Up @@ -464,7 +472,7 @@ private static Span<uint> PowCore(Span<uint> value, int valueLength,
power >>= 1;
}

return result.Slice(0, resultLength);
return result;
}

private static Span<uint> PowCore(Span<uint> value, int valueLength,
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ public static void ModPowAxiom()
}
}

[Fact]
public static void RegressionIssue70330()
{
byte[] tempByteArray1 = { 226, 32 };
byte[] tempByteArray2 = { 113 };
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 };
VerifyIdentityString(
Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "tModPow",
Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "bPow" + " bRemainder"
);
}

[Fact]
public static void ModPowBoundary()
{
Expand Down

0 comments on commit e9070dd

Please sign in to comment.