summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-29 14:47:53 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-29 14:47:53 +0700
commitbeba3f6941c540af864926f8de9c3a5a8a703983 (patch)
tree23709b363b0e6a179bad32e6a313b530d954024d
parentTnaf perf. opts. (diff)
downloadBouncyCastle.NET-ed25519-beba3f6941c540af864926f8de9c3a5a8a703983.tar.xz
Koblitz curve perf. opts.
-rw-r--r--crypto/src/math/ec/abc/Tnaf.cs17
-rw-r--r--crypto/src/math/ec/multiplier/WTauNafMultiplier.cs3
2 files changed, 16 insertions, 4 deletions
diff --git a/crypto/src/math/ec/abc/Tnaf.cs b/crypto/src/math/ec/abc/Tnaf.cs
index cd3e90f46..944f0e229 100644
--- a/crypto/src/math/ec/abc/Tnaf.cs
+++ b/crypto/src/math/ec/abc/Tnaf.cs
@@ -722,7 +722,7 @@ namespace Org.BouncyCastle.Math.EC.Abc
             int i = 0;
 
             // while lambda <> (0, 0)
-            while (!(r0.Equals(BigInteger.Zero) && r1.Equals(BigInteger.Zero)))
+            while ((r0.SignValue | r1.SignValue) != 0)
             {
                 // if r0 is odd
                 if (r0.TestBit(0)) 
@@ -802,7 +802,20 @@ namespace Org.BouncyCastle.Math.EC.Abc
                 if (existing is PartModPreCompInfo)
                     return existing;
 
-                var lucas = GetLucas(m_mu, m_curve.FieldSize, m_doV)[1];
+                BigInteger lucas;
+                if (m_curve.IsKoblitz)
+                {
+                    /*
+                     * Jerome A. Solinas, "Improved Algorithms for Arithmetic on Anomalous Binary Curves", (21).
+                     */
+                    lucas = BigInteger.One.ShiftLeft(m_curve.FieldSize).Add(BigInteger.One).Subtract(
+                        m_curve.Order.Multiply(m_curve.Cofactor));
+                }
+                else
+                {
+                    lucas = GetLucas(m_mu, m_curve.FieldSize, m_doV)[1];
+                }
+
                 var si = GetSi(m_curve);
 
                 return new PartModPreCompInfo(lucas, si[0], si[1]);
diff --git a/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs b/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs
index d986e7f01..6631657f4 100644
--- a/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs
+++ b/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs
@@ -24,8 +24,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier
         */
         protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
         {
-            AbstractF2mPoint p = point as AbstractF2mPoint;
-            if (p == null)
+            if (!(point is AbstractF2mPoint p))
                 throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
 
             AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;