Skip to content

Commit 2fc5cc9

Browse files
[release/7.0] Ensure that the result for PowMod is fully initialized (#74672)
* Adding a regression test for dotnet/performance#2575 * Ensure that the result span is fully initialized. Co-authored-by: Tanner Gooding <[email protected]>
1 parent 9c87579 commit 2fc5cc9

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ stackalloc uint[StackAllocThreshold]
3535
value.CopyTo(valueCopy);
3636
valueCopy.Slice(value.Length).Clear();
3737

38-
PowCore(valueCopy, value.Length, temp, power, bits).CopyTo(bits);
38+
Span<uint> result = PowCore(valueCopy, value.Length, temp, power, bits);
39+
result.CopyTo(bits);
40+
bits.Slice(result.Length).Clear();
3941

4042
if (tempFromPool != null)
4143
ArrayPool<uint>.Shared.Return(tempFromPool);
@@ -50,19 +52,19 @@ private static Span<uint> PowCore(Span<uint> value, int valueLength, Span<uint>
5052
Debug.Assert(value.Length == temp.Length);
5153

5254
result[0] = 1;
53-
int bitsLength = 1;
55+
int resultLength = 1;
5456

5557
// The basic pow algorithm using square-and-multiply.
5658
while (power != 0)
5759
{
5860
if ((power & 1) == 1)
59-
bitsLength = MultiplySelf(ref result, bitsLength, value.Slice(0, valueLength), ref temp);
61+
resultLength = MultiplySelf(ref result, resultLength, value.Slice(0, valueLength), ref temp);
6062
if (power != 1)
6163
valueLength = SquareSelf(ref value, valueLength, ref temp);
6264
power >>= 1;
6365
}
6466

65-
return result;
67+
return result.Slice(0, resultLength);
6668
}
6769

6870
private static int MultiplySelf(ref Span<uint> left, int leftLength, ReadOnlySpan<uint> right, ref Span<uint> temp)
@@ -313,7 +315,9 @@ private static void PowCore(Span<uint> value, int valueLength,
313315

314316
if (modulus.Length < ReducerThreshold)
315317
{
316-
PowCore(value, valueLength, power, modulus, bits, 1, temp).CopyTo(bits);
318+
Span<uint> result = PowCore(value, valueLength, power, modulus, bits, 1, temp);
319+
result.CopyTo(bits);
320+
bits.Slice(result.Length).Clear();
317321
}
318322
else
319323
{
@@ -349,7 +353,9 @@ stackalloc uint[StackAllocThreshold]
349353
if (rFromPool != null)
350354
ArrayPool<uint>.Shared.Return(rFromPool);
351355

352-
PowCore(value, valueLength, power, reducer, bits, 1, temp).CopyTo(bits);
356+
Span<uint> result = PowCore(value, valueLength, power, reducer, bits, 1, temp);
357+
result.CopyTo(bits);
358+
bits.Slice(result.Length).Clear();
353359

354360
if (muFromPool != null)
355361
ArrayPool<uint>.Shared.Return(muFromPool);
@@ -369,7 +375,9 @@ private static void PowCore(Span<uint> value, int valueLength,
369375

370376
if (modulus.Length < ReducerThreshold)
371377
{
372-
PowCore(value, valueLength, power, modulus, bits, 1, temp).CopyTo(bits);
378+
Span<uint> result = PowCore(value, valueLength, power, modulus, bits, 1, temp);
379+
result.CopyTo(bits);
380+
bits.Slice(result.Length).Clear();
373381
}
374382
else
375383
{
@@ -405,7 +413,9 @@ stackalloc uint[StackAllocThreshold]
405413
if (rFromPool != null)
406414
ArrayPool<uint>.Shared.Return(rFromPool);
407415

408-
PowCore(value, valueLength, power, reducer, bits, 1, temp).CopyTo(bits);
416+
Span<uint> result = PowCore(value, valueLength, power, reducer, bits, 1, temp);
417+
result.CopyTo(bits);
418+
bits.Slice(result.Length).Clear();
409419

410420
if (muFromPool != null)
411421
ArrayPool<uint>.Shared.Return(muFromPool);
@@ -472,7 +482,7 @@ private static Span<uint> PowCore(Span<uint> value, int valueLength,
472482
power >>= 1;
473483
}
474484

475-
return result;
485+
return result.Slice(0, resultLength);
476486
}
477487

478488
private static Span<uint> PowCore(Span<uint> value, int valueLength,
@@ -531,7 +541,7 @@ private static Span<uint> PowCore(Span<uint> value, int valueLength,
531541
power >>= 1;
532542
}
533543

534-
return result;
544+
return result.Slice(0, resultLength);
535545
}
536546
}
537547
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static void ModPowAxiom()
275275
}
276276

277277
[Fact]
278-
public static void RegressionIssue70330()
278+
public static void RegressionIssueRuntime70330()
279279
{
280280
byte[] tempByteArray1 = { 226, 32 };
281281
byte[] tempByteArray2 = { 113 };
@@ -286,6 +286,19 @@ public static void RegressionIssue70330()
286286
);
287287
}
288288

289+
[Fact]
290+
public static void RegressionIssuePerformance2575()
291+
{
292+
byte[] tempByteArray1 = { 0x93, 0x30, 0x70, 0xD8, 0x74, 0x0A, 0x70, 0x79, 0x18, 0x5A, 0xCD, 0x2D, 0x39, 0xBF, 0x36, 0xC6, 0x24, 0xDE, 0x4B, 0xD5, 0xC7, 0xB4, 0x56, 0x23, 0xB2, 0xB4, 0xB7, 0x43, 0xE5, 0x05, 0xDD, 0xAF, 0x97, 0x81, 0x67, 0xCB, 0x67, 0xE9, 0x53, 0x5A, 0x00, 0x42, 0x9B, 0x20, 0x56, 0xFA, 0xBE, 0x27, 0x6A, 0x14, 0x36, 0x17, 0x49, 0xC5, 0xAC, 0xDF, 0xDA, 0x4C, 0x26, 0xC0, 0x52, 0x2C, 0x93, 0x13, 0x7D, 0x1E, 0x96, 0xB8, 0x58, 0x6B, 0x5F, 0x3A, 0x8B, 0xF1, 0xD5, 0x84, 0x18, 0x36, 0xCC, 0xB7, 0xF5, 0x90, 0xD9, 0xD1, 0xCE, 0xC5, 0x65, 0xD2, 0xD5, 0x87, 0xF0, 0x1B, 0xC3, 0x92, 0x07, 0xD3, 0xAF, 0x88, 0xA2, 0x38, 0x64, 0x06, 0xCE, 0xFE, 0xB5, 0xFC, 0x8C, 0x58, 0xEF, 0x27, 0xC6, 0xA4, 0x7F, 0x6E, 0xCA, 0xC2, 0x53, 0xC2, 0x44, 0xB7, 0xB8, 0xC3, 0xE2, 0xD0, 0x7A, 0x43, 0x76, 0xF8, 0x00 };
293+
byte[] tempByteArray2 = { 0x02, 0xD4, 0x90, 0x93, 0x94, 0x52, 0xEF, 0xC1, 0xDA, 0x1B, 0xD2, 0x39, 0x0D, 0xE3, 0xAD, 0xC1, 0x4C, 0x9B, 0x54, 0xC8, 0x44, 0x7F, 0xED, 0x43, 0xEA, 0x7F, 0xA1, 0x23, 0xFE, 0x84, 0x71, 0x85, 0x93, 0x6E, 0xEC, 0x53, 0x35, 0x10, 0xEB, 0x1C, 0xDA, 0x01, 0x78, 0xA6, 0x71, 0x7E, 0xAB, 0xE0, 0x35, 0x0F, 0x2E, 0xA8, 0x21, 0x30, 0xA5, 0x83, 0xE7, 0x4C, 0xA9, 0x14, 0xB0, 0xCC, 0xC3, 0x56, 0xAA, 0x4C, 0xA5, 0x9E, 0xF6, 0x5A, 0x8B, 0x3C, 0xAF, 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 };
294+
byte[] tempByteArray3 = { 0xDF, 0x05, 0xE4, 0x4A, 0xAD, 0x93, 0xC6, 0xD7, 0x00 };
295+
byte[] tempByteArray4 = { 0xC6, 0x57, 0x30, 0x3F, 0xCE, 0x21, 0xA0, 0x3D };
296+
VerifyIdentityString(
297+
Print(tempByteArray3) + Print(tempByteArray2) + Print(tempByteArray1) + "tModPow",
298+
Print(tempByteArray4)
299+
);
300+
}
301+
289302
[Fact]
290303
public static void ModPowBoundary()
291304
{

0 commit comments

Comments
 (0)