diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.PowMod.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.PowMod.cs index 02a478818ebc38..d3edb7d44afb95 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.PowMod.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.PowMod.cs @@ -35,7 +35,9 @@ stackalloc uint[StackAllocThreshold] value.CopyTo(valueCopy); valueCopy.Slice(value.Length).Clear(); - PowCore(valueCopy, value.Length, temp, power, bits).CopyTo(bits); + Span result = PowCore(valueCopy, value.Length, temp, power, bits); + result.CopyTo(bits); + bits.Slice(result.Length).Clear(); if (tempFromPool != null) ArrayPool.Shared.Return(tempFromPool); @@ -50,19 +52,19 @@ private static Span PowCore(Span value, int valueLength, Span Debug.Assert(value.Length == temp.Length); result[0] = 1; - int bitsLength = 1; + int resultLength = 1; // The basic pow algorithm using square-and-multiply. while (power != 0) { if ((power & 1) == 1) - bitsLength = MultiplySelf(ref result, bitsLength, value.Slice(0, valueLength), ref temp); + resultLength = MultiplySelf(ref result, resultLength, value.Slice(0, valueLength), ref temp); if (power != 1) valueLength = SquareSelf(ref value, valueLength, ref temp); power >>= 1; } - return result; + return result.Slice(0, resultLength); } private static int MultiplySelf(ref Span left, int leftLength, ReadOnlySpan right, ref Span temp) @@ -313,7 +315,9 @@ private static void PowCore(Span value, int valueLength, if (modulus.Length < ReducerThreshold) { - PowCore(value, valueLength, power, modulus, bits, 1, temp).CopyTo(bits); + Span result = PowCore(value, valueLength, power, modulus, bits, 1, temp); + result.CopyTo(bits); + bits.Slice(result.Length).Clear(); } else { @@ -349,7 +353,9 @@ stackalloc uint[StackAllocThreshold] if (rFromPool != null) ArrayPool.Shared.Return(rFromPool); - PowCore(value, valueLength, power, reducer, bits, 1, temp).CopyTo(bits); + Span result = PowCore(value, valueLength, power, reducer, bits, 1, temp); + result.CopyTo(bits); + bits.Slice(result.Length).Clear(); if (muFromPool != null) ArrayPool.Shared.Return(muFromPool); @@ -369,7 +375,9 @@ private static void PowCore(Span value, int valueLength, if (modulus.Length < ReducerThreshold) { - PowCore(value, valueLength, power, modulus, bits, 1, temp).CopyTo(bits); + Span result = PowCore(value, valueLength, power, modulus, bits, 1, temp); + result.CopyTo(bits); + bits.Slice(result.Length).Clear(); } else { @@ -405,7 +413,9 @@ stackalloc uint[StackAllocThreshold] if (rFromPool != null) ArrayPool.Shared.Return(rFromPool); - PowCore(value, valueLength, power, reducer, bits, 1, temp).CopyTo(bits); + Span result = PowCore(value, valueLength, power, reducer, bits, 1, temp); + result.CopyTo(bits); + bits.Slice(result.Length).Clear(); if (muFromPool != null) ArrayPool.Shared.Return(muFromPool); @@ -472,7 +482,7 @@ private static Span PowCore(Span value, int valueLength, power >>= 1; } - return result; + return result.Slice(0, resultLength); } private static Span PowCore(Span value, int valueLength, @@ -531,7 +541,7 @@ private static Span PowCore(Span value, int valueLength, power >>= 1; } - return result; + return result.Slice(0, resultLength); } } } diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs index fe6169e5d91f74..68f67892fcf76c 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs @@ -275,7 +275,7 @@ public static void ModPowAxiom() } [Fact] - public static void RegressionIssue70330() + public static void RegressionIssueRuntime70330() { byte[] tempByteArray1 = { 226, 32 }; byte[] tempByteArray2 = { 113 }; @@ -286,6 +286,19 @@ public static void RegressionIssue70330() ); } + [Fact] + public static void RegressionIssuePerformance2575() + { + byte[] tempByteArray1 = { 0x93, 0x30, 0x70, 0xD8, 0x74, 0x0A, 0x70, 0x79, 0x18, 0x5A, 0xCD, 0x2D, 0x39, 0xBF, 0x36, 0xC6, 0x24, 0xDE, 0x4B, 0xD5, 0xC7, 0xB4, 0x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 0x38, 0xED, 0x43, 0x06, 0x46, 0xD2, 0x6C, 0xD3, 0xC1, 0xED, 0xEE, 0x55, 0xC8, 0x63, 0x63, 0xC9, 0x69, 0x6B, 0xE8, 0x67, 0xD6, 0x6B, 0x0D, 0x3E, 0x22, 0xFC, 0x24, 0xD8, 0x0C, 0xEA, 0xDB, 0x83, 0xF2, 0xF9, 0xE9, 0x43, 0x61, 0x7C, 0xA0, 0x7A, 0x3D, 0x41, 0xEF, 0xDF, 0xD4, 0x00, 0xE1, 0xE9, 0x42, 0xD5, 0x8C, 0xEE, 0xA3, 0xD5, 0xD8, 0x00 }; + byte[] tempByteArray3 = { 0xDF, 0x05, 0xE4, 0x4A, 0xAD, 0x93, 0xC6, 0xD7, 0x00 }; + byte[] tempByteArray4 = { 0xC6, 0x57, 0x30, 0x3F, 0xCE, 0x21, 0xA0, 0x3D }; + VerifyIdentityString( + Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "tModPow", + Print(tempByteArray4) + ); + } + [Fact] public static void ModPowBoundary() {