Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Random failures in System.Numerics.Tests.modpowTest.ModPowAxiom test #74112

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
dakersnar marked this conversation as resolved.
Show resolved Hide resolved

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this slice basically unnecessary in all cases, and causing problems in this edge case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct. It doesn't exist for overload of PowCore with exponent of arbitrary length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this slice introduced a regression for the case of:

value: 174475926811173371108797680334274448482027679060635889276326576767485928691470272416521788770673419883076040463212449631887959313694345475337324466914914839368969988215395980439434099840700753576299320684517028939674493640310781015739837159625716992654178036326745036841619965661654305787656989089177185300627
exponent: 152266387011071825363527751452194927195391511201743678326712300944812006670853968954038119879530327815990523885596782666640476470053822766345969220504425389995206265094671158964311234812226260664473986637197447367726400093404731405598815953979171704175885394570388678952091206779039984338579147114424357147650
modulus: 15548277136038299103

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this scenario, the length of result is greater than the length of bits and so the result.CopyTo(bits) operation fails as the destination is too short.

I expect the actual issue here is that resultLength isn't being correctly tracked somewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not that resultLength is incorrect, its that bits will have been mutated and may contain temporary data

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"
);
dakersnar marked this conversation as resolved.
Show resolved Hide resolved
}

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