summary refs log tree commit diff
path: root/crypto/src/util/BigIntegers.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-12-10 23:06:49 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-12-10 23:06:49 +0700
commitabece317fd90c4f0fb1393e03e937cac77116889 (patch)
treeddc2189099b009a69192635debbe666f1a05f2e0 /crypto/src/util/BigIntegers.cs
parentAdd fast coprime test (diff)
downloadBouncyCastle.NET-ed25519-abece317fd90c4f0fb1393e03e937cac77116889.tar.xz
Update safegcd implementation
Diffstat (limited to '')
-rw-r--r--crypto/src/util/BigIntegers.cs10
1 files changed, 6 insertions, 4 deletions
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index 8c2068688..419e98701 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -190,7 +190,7 @@ namespace Org.BouncyCastle.Utilities
                 throw new ArgumentException("must be odd", nameof(M));
             if (M.SignValue != 1)
                 throw new ArithmeticException("BigInteger: modulus not positive");
-            if (X.SignValue < 0 || X.CompareTo(M) >= 0)
+            if (X.SignValue < 0 || X.BitLength > M.BitLength)
             {
                 X = X.Mod(M);
             }
@@ -231,7 +231,7 @@ namespace Org.BouncyCastle.Utilities
                 throw new ArithmeticException("BigInteger: modulus not positive");
             if (M.Equals(One))
                 return Zero;
-            if (X.SignValue < 0 || X.CompareTo(M) >= 0)
+            if (X.SignValue < 0 || X.BitLength > M.BitLength)
             {
                 X = X.Mod(M);
             }
@@ -272,7 +272,7 @@ namespace Org.BouncyCastle.Utilities
                 throw new ArgumentException("must be odd", nameof(M));
             if (M.SignValue != 1)
                 throw new ArithmeticException("BigInteger: modulus not positive");
-            if (X.SignValue < 0 || X.CompareTo(M) >= 0)
+            if (X.SignValue < 0 || X.BitLength > M.BitLength)
             {
                 X = X.Mod(M);
             }
@@ -304,10 +304,12 @@ namespace Org.BouncyCastle.Utilities
                 throw new ArgumentException("must be odd", nameof(M));
             if (M.SignValue != 1)
                 throw new ArithmeticException("BigInteger: modulus not positive");
-            if (X.SignValue < 0 || X.CompareTo(M) >= 0)
+            if (X.SignValue < 0 || X.BitLength > M.BitLength)
             {
                 X = X.Mod(M);
             }
+            if (X.Equals(One))
+                return true;
 
             int bits = M.BitLength;