Remove cancellation-aware UInt128 ModPow overload

This commit is contained in:
Marek Lemańczyk committed 2025-10-20 11:48:14 +02:00
1 parent c892e1c30c
commit 0f51454496
2 files changed
+38 -38

No files matched your search

@@ -105,7 +105,7 @@ public static class MersennePrimeFactorTester
}
candidate = order / prime;
if (UInt128Numbers.Two.ModPow(candidate, q, ct) == UInt128.One)
if (ModPowWithCancellation(UInt128Numbers.Two, candidate, q, ct) == UInt128.One)
{
order = candidate;
}
@@ -130,6 +130,43 @@ public static class MersennePrimeFactorTester
return order;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static UInt128 ModPowWithCancellation(UInt128 baseValue, UInt128 exponent, UInt128 modulus, CancellationToken ct)
{
// Keep this helper local to the factoring path so we can preserve the token-aware early outs
// without carrying a separate overload on the shared UInt128 extensions.
if (modulus <= UInt128.One)
{
return UInt128.Zero;
}
UInt128 result = UInt128.One;
UInt128 baseResidue = baseValue % modulus;
while (exponent != UInt128.Zero)
{
if (ct.IsCancellationRequested)
{
return UInt128.Zero;
}
if ((exponent & UInt128.One) != UInt128.Zero)
{
result = result.MulMod(baseResidue, modulus);
}
if (ct.IsCancellationRequested)
{
return UInt128.Zero;
}
baseResidue = baseResidue.MulMod(baseResidue, modulus);
exponent >>= 1;
}
return result;
}
private static readonly ConcurrentBag<List<ulong>> ListPool = new();
private static readonly ConcurrentBag<Dictionary<ulong, byte>> DictPool = new();
private static readonly Dictionary<ulong, ulong[]> FactorCache = new();