diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-24 15:13:41 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-24 15:13:41 +0700 |
commit | 4e88394961c9ce8002e6093ca388bfa4ca943a76 (patch) | |
tree | 5bf118b929b3a78b1e8bca71135541f7d20a002f /crypto/src/math/ec/ECFieldElement.cs | |
parent | Track carries for a, b to avoid unnecessary add/sub of prime modulus (diff) | |
download | BouncyCastle.NET-ed25519-4e88394961c9ce8002e6093ca388bfa4ca943a76.tar.xz |
Optimization in ModReduce
Diffstat (limited to 'crypto/src/math/ec/ECFieldElement.cs')
-rw-r--r-- | crypto/src/math/ec/ECFieldElement.cs | 9 |
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); } |