summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/math/ec/ECFieldElement.cs9
1 files changed, 6 insertions, 3 deletions
diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs
index f29d1f1b0..d8813bf0b 100644
--- a/crypto/src/math/ec/ECFieldElement.cs
+++ b/crypto/src/math/ec/ECFieldElement.cs
@@ -77,7 +77,8 @@ namespace Org.BouncyCastle.Math.EC
         internal static BigInteger CalculateResidue(BigInteger p)
         {
             int bitLength = p.BitLength;
-            if (bitLength > 128)
+            //if (bitLength > 128)
+            if (bitLength > 64)
             {
                 /*
                  * NOTE: Due to poor performance of BigInteger.Mod in C#, the residue-based reduction is
@@ -345,11 +346,13 @@ namespace Org.BouncyCastle.Math.EC
                     x = x.Abs();
                 }
                 int qLen = q.BitLength;
+                BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
+                bool rIsOne = r.Equals(BigInteger.One);
                 while (x.BitLength > (qLen + 1))
                 {
                     BigInteger u = x.ShiftRight(qLen);
-                    BigInteger v = x.Subtract(u.ShiftLeft(qLen));
-                    if (!r.Equals(BigInteger.One))
+                    BigInteger v = x.Remainder(qMod);
+                    if (!rIsOne)
                     {
                         u = u.Multiply(r);
                     }