From a34cabc773083997c69a55d881ec07113197f5e0 Mon Sep 17 00:00:00 2001 From: sakno Date: Thu, 18 Aug 2022 02:34:38 +0300 Subject: [PATCH 1/2] Fixed #70330 --- .../System/Numerics/BigIntegerCalculator.PowMod.cs | 2 +- .../tests/BigInteger/modpow.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 7e59c6dd00260..c5efe4151d270 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 @@ -464,7 +464,7 @@ private static Span PowCore(Span value, int valueLength, power >>= 1; } - return result.Slice(0, resultLength); + return result; } private static Span PowCore(Span value, int valueLength, diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs index 54af681215170..fe6169e5d91f7 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs @@ -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() { From 59dc49744acc0601af2b23c8fb074272b2473147 Mon Sep 17 00:00:00 2001 From: sakno Date: Thu, 18 Aug 2022 02:48:56 +0300 Subject: [PATCH 2/2] Removed pessimistic buffer cleanup --- .../System/Numerics/BigIntegerCalculator.PowMod.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 c5efe4151d270..02a478818ebc3 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 @@ -217,7 +217,11 @@ public static void Pow(ReadOnlySpan value, uint power, Span valueCopy = (size <= StackAllocThreshold ? stackalloc uint[StackAllocThreshold] : valueCopyFromPool = ArrayPool.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) { @@ -262,7 +266,11 @@ public static void Pow(ReadOnlySpan value, ReadOnlySpan power, Span valueCopy = (size <= StackAllocThreshold ? stackalloc uint[StackAllocThreshold] : valueCopyFromPool = ArrayPool.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) {