From 14c311b95724b965b57534bc91e020c9cd3a13c4 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 9 Feb 2024 10:22:48 +0000 Subject: [PATCH 1/2] Temporarily disable failing assertions triggered by the FastReducer implementation. --- .../Numerics/BigIntegerCalculator.AddSub.cs | 6 +- .../tests/BigInteger/modpow.cs | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs index a94d4d1df47cb9..19523609ebe55b 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs @@ -132,7 +132,8 @@ public static void Subtract(ReadOnlySpan left, ReadOnlySpan right, S private static void SubtractSelf(Span left, ReadOnlySpan right) { Debug.Assert(left.Length >= right.Length); - Debug.Assert(Compare(left, right) >= 0); + // Assertion failing per https://github.com/dotnet/runtime/issues/97780 + // Debug.Assert(Compare(left, right) >= 0); int i = 0; long carry = 0L; @@ -158,7 +159,8 @@ private static void SubtractSelf(Span left, ReadOnlySpan right) carry = digit >> 32; } - Debug.Assert(carry == 0); + // Assertion failing per https://github.com/dotnet/runtime/issues/97780 + //Debug.Assert(carry == 0); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs index b392bd3875a9d7..ac9ddc8d79478b 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; using Xunit; namespace System.Numerics.Tests @@ -385,5 +386,80 @@ private static string Print(byte[] bytes) { return MyBigIntImp.Print(bytes); } + + [Theory] + [MemberData(nameof(AssertFailureRegressionTest_InputData))] + public static void FastReducer_AssertFailure_RegressionTest(BigInteger value, int exponent, BigInteger modulus) + { + // Regression test exercising the code path that was resulting in + // assertion failures per https://github.com/dotnet/runtime/issues/97780. + // The failures seemingly have no impact on functional correctness, + // so they have been disabled until the underlying issue is resolved. + + // Act + BigInteger result = BigInteger.ModPow(value, exponent, modulus); + + // Assert + BigInteger expected = 1; + for (int i = 0; i < exponent; i++) expected = (expected * value) % modulus; + Assert.Equal(expected, result); + } + + public static IEnumerable AssertFailureRegressionTest_InputData() + { + // Minimal repro + BigInteger modulus = (BigInteger)3 << 1024; + BigInteger value = modulus; + int exponent = 2; + yield return [value, exponent, modulus]; + + // Original reproductions as reported in https://github.com/dotnet/runtime/issues/97780. + modulus = new BigInteger( + [ + 0xB1, 0x7C, 0xEE, 0x77, 0xB4, 0x59, 0xA4, 0x65, + 0x92, 0x8D, 0x7F, 0x55, 0x77, 0x80, 0x39, 0xBA, + 0x22, 0xBA, 0x29, 0xA5, 0xFF, 0xE5, 0x53, 0xFB, + 0x40, 0x98, 0xA8, 0x35, 0xE5, 0x2D, 0x0A, 0xDC, + 0x85, 0x17, 0x6E, 0xE4, 0xD6, 0x93, 0x82, 0x20, + 0x71, 0x8D, 0xE9, 0xDD, 0xC9, 0xD5, 0xBD, 0x30, + 0x47, 0xEE, 0x59, 0xB9, 0xE6, 0xA8, 0x79, 0x9E, + 0x47, 0x96, 0x8E, 0x63, 0xCD, 0xA6, 0x28, 0x9D, + 0x7B, 0x6D, 0x83, 0xAA, 0x68, 0xE2, 0x46, 0xD6, + 0x1C, 0x8C, 0x2B, 0xA1, 0xC4, 0x04, 0x12, 0xAE, + 0x61, 0x07, 0xAF, 0x6F, 0xE9, 0x2B, 0x48, 0x5C, + 0xCA, 0xC2, 0x0E, 0x52, 0x71, 0x21, 0x03, 0xE0, + 0x05, 0x1C, 0x07, 0xC0, 0x56, 0xFD, 0x8A, 0x61, + 0x7A, 0x95, 0x39, 0x4B, 0xAA, 0x5A, 0x9D, 0x03, + 0x6D, 0x7A, 0x51, 0x3E, 0x7A, 0xE4, 0xAB, 0xED, + 0xB4, 0x0A, 0x88, 0x80, 0x3C, 0x07, 0xED, 0x19, + 0x21, 0xA2, 0xDC, 0xD7, 0x9C, 0x3F, 0x3B, 0x19, + 0x59, 0x33, 0x39, 0x8A, 0x25, 0xDB, 0xCE, 0x8D, + 0x8E, 0x10, 0xDA, 0xB1, 0x38, 0x32, 0xCA, 0x59, + 0xA1, 0x69, 0x3C, 0x23, 0x76, 0x50, 0x37, 0xB3, + 0xBF, 0x73, 0x58, 0x77, 0x38, 0xC5, 0x16, 0x03, + 0x60, 0x36, 0x0D, 0x31, 0x8C, 0xBE, 0xA5, 0x12, + 0x2F, 0xE5, 0x16, 0xAD, 0x1C, 0x80, 0x01, 0x50, + 0xEB, 0x3C, 0x86, 0x79, 0x22, 0xD3, 0x7F, 0xD4, + 0x90, 0x85, 0xB8, 0xEB, 0xB0, 0x7D, 0xD8, 0xC8, + 0x8B, 0xBB, 0x88, 0xBE, 0xFE, 0xB8, 0xBA, 0xAD, + 0x61, 0xC7, 0xF9, 0x68, 0x20, 0xB2, 0xA9, 0x1D, + 0xB4, 0xDC, 0xB0, 0x5B, 0xC7, 0xB3, 0xF2, 0x83, + 0x35, 0x43, 0xB0, 0xAE, 0x19, 0x2B, 0xA6, 0xFA, + 0x88, 0x48, 0xB9, 0xFB, 0xB3, 0xCD, 0xF8, 0xA9, + 0x9C, 0x20, 0x6F, 0x63, 0x23, 0xE5, 0xC2, 0x85, + 0xCD, 0x75, 0x7A, 0x55, 0x04, 0xA4, 0x08, 0x99, + ], true, true); + + exponent = 65537; + var buf = new byte[256]; + buf[1] = 2; + buf.AsSpan(2, 8).Fill(1); + value = new BigInteger(buf, true, true); + yield return [value, exponent, modulus]; + + buf[^1] = 1; + value = new BigInteger(buf, true, true); + yield return [value, exponent, modulus]; + } } } From 1a9af468f4e20e592dc0972d7e35828aa59884b1 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 9 Feb 2024 10:28:11 +0000 Subject: [PATCH 2/2] Add argument labels. --- .../System.Runtime.Numerics/tests/BigInteger/modpow.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs index ac9ddc8d79478b..1308337bb679f6 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/modpow.cs @@ -448,17 +448,17 @@ public static IEnumerable AssertFailureRegressionTest_InputData() 0x88, 0x48, 0xB9, 0xFB, 0xB3, 0xCD, 0xF8, 0xA9, 0x9C, 0x20, 0x6F, 0x63, 0x23, 0xE5, 0xC2, 0x85, 0xCD, 0x75, 0x7A, 0x55, 0x04, 0xA4, 0x08, 0x99, - ], true, true); + ], isUnsigned: true, isBigEndian: true); exponent = 65537; var buf = new byte[256]; buf[1] = 2; buf.AsSpan(2, 8).Fill(1); - value = new BigInteger(buf, true, true); + value = new BigInteger(buf, isUnsigned: true, isBigEndian: true); yield return [value, exponent, modulus]; buf[^1] = 1; - value = new BigInteger(buf, true, true); + value = new BigInteger(buf, isUnsigned: true, isBigEndian: true); yield return [value, exponent, modulus]; } }