From 4e88394961c9ce8002e6093ca388bfa4ca943a76 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jan 2014 15:13:41 +0700 Subject: Optimization in ModReduce --- crypto/src/math/ec/ECFieldElement.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'crypto/src') 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); } -- cgit 1.5.1