summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/math/ec/custom/sec/SecT233K1Point.cs109
1 files changed, 62 insertions, 47 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecT233K1Point.cs b/crypto/src/math/ec/custom/sec/SecT233K1Point.cs
index 53053a144..0ed107d49 100644
--- a/crypto/src/math/ec/custom/sec/SecT233K1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecT233K1Point.cs
@@ -1,5 +1,7 @@
 using System;
 
+using Org.BouncyCastle.Math.Raw;
+
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
 {
     internal class SecT233K1Point
@@ -66,99 +68,112 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             ECCurve curve = this.Curve;
 
-            ECFieldElement X1 = this.RawXCoord;
-            ECFieldElement X2 = b.RawXCoord;
+            SecT233FieldElement X1 = (SecT233FieldElement)this.RawXCoord;
+            SecT233FieldElement X2 = (SecT233FieldElement)b.RawXCoord;
 
             if (X1.IsZero)
             {
                 if (X2.IsZero)
-                {
                     return curve.Infinity;
-                }
 
                 return b.Add(this);
             }
 
-            ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
-            ECFieldElement L2 = b.RawYCoord, Z2 = b.RawZCoords[0];
+            SecT233FieldElement L1 = (SecT233FieldElement)this.RawYCoord, Z1 = (SecT233FieldElement)this.RawZCoords[0];
+            SecT233FieldElement L2 = (SecT233FieldElement)b.RawYCoord, Z2 = (SecT233FieldElement)b.RawZCoords[0];
+
+            ulong[] tt0 = Nat256.CreateExt64();
+            ulong[] t1 = Nat256.Create64();
+            ulong[] t2 = Nat256.Create64();
+            ulong[] t3 = Nat256.Create64();
 
             bool Z1IsOne = Z1.IsOne;
-            ECFieldElement U2 = X2, S2 = L2;
-            if (!Z1IsOne)
+            if (Z1IsOne)
             {
-                U2 = U2.Multiply(Z1);
-                S2 = S2.Multiply(Z1);
+                Nat256.Copy64(X2.x, t1);                    // U2
+                Nat256.Copy64(L2.x, t2);                    // S2
+            }
+            else
+            {
+                SecT233Field.Multiply(X2.x, Z1.x, t1);      // U2
+                SecT233Field.Multiply(L2.x, Z1.x, t2);      // S2
             }
 
             bool Z2IsOne = Z2.IsOne;
-            ECFieldElement U1 = X1, S1 = L1;
-            if (!Z2IsOne)
+            if (Z2IsOne)
+            {
+                Nat256.Copy64(X1.x, t3);                    // U1
+                Nat256.Copy64(L1.x, tt0);                   // S1
+            }
+            else
             {
-                U1 = U1.Multiply(Z2);
-                S1 = S1.Multiply(Z2);
+                SecT233Field.Multiply(X1.x, Z2.x, t3);      // U1
+                SecT233Field.Multiply(L1.x, Z2.x, tt0);     // S1
             }
 
-            ECFieldElement A = S1.Add(S2);
-            ECFieldElement B = U1.Add(U2);
+            SecT233Field.AddTo(tt0, t2);                    // A
+            SecT233Field.Add(t3, t1, tt0);                  // B
 
-            if (B.IsZero)
+            if (Nat256.IsZero64(tt0))
             {
-                if (A.IsZero)
+                if (Nat256.IsZero64(t2))
                     return Twice();
 
                 return curve.Infinity;
             }
 
-            ECFieldElement X3, L3, Z3;
             if (X2.IsZero)
             {
                 // TODO This can probably be optimized quite a bit
                 ECPoint p = this.Normalize();
-                X1 = p.XCoord;
+                X1 = (SecT233FieldElement)p.XCoord;
                 ECFieldElement Y1 = p.YCoord;
 
                 ECFieldElement Y2 = L2;
                 ECFieldElement L = Y1.Add(Y2).Divide(X1);
 
-                X3 = L.Square().Add(L).Add(X1);
+                ECFieldElement X3 = L.Square().Add(L).Add(X1);
                 if (X3.IsZero)
-                {
                     return new SecT233K1Point(curve, X3, curve.B);
-                }
 
                 ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
-                L3 = Y3.Divide(X3).Add(X3);
-                Z3 = curve.FromBigInteger(BigInteger.One);
+                ECFieldElement L3 = Y3.Divide(X3).Add(X3);
+                ECFieldElement Z3 = curve.FromBigInteger(BigInteger.One);
+
+                return new SecT233K1Point(curve, X3, L3, new ECFieldElement[]{ Z3 });
             }
-            else
-            {
-                B = B.Square();
 
-                ECFieldElement AU1 = A.Multiply(U1);
-                ECFieldElement AU2 = A.Multiply(U2);
+            SecT233Field.Square(tt0, tt0);
 
-                X3 = AU1.Multiply(AU2);
-                if (X3.IsZero)
-                {
-                    return new SecT233K1Point(curve, X3, curve.B);
-                }
+            SecT233Field.Multiply(t3, t2, t3);      // AU1
+            SecT233Field.Multiply(t1, t2, t1);      // AU2
 
-                ECFieldElement ABZ2 = A.Multiply(B);
-                if (!Z2IsOne)
-                {
-                    ABZ2 = ABZ2.Multiply(Z2);
-                }
+            ulong[] _X3 = t3;
+            SecT233Field.Multiply(_X3, t1, _X3);
+            if (Nat256.IsZero64(_X3))
+                return new SecT233K1Point(curve, new SecT233FieldElement(_X3), curve.B);
 
-                L3 = AU2.Add(B).SquarePlusProduct(ABZ2, L1.Add(Z1));
+            ulong[] _Z3 = t2;
+            SecT233Field.Multiply(_Z3, tt0, _Z3);   // ABZ2
+            if (!Z2IsOne)
+            {
+                SecT233Field.Multiply(_Z3, Z2.x, _Z3);
+            }
 
-                Z3 = ABZ2;
-                if (!Z1IsOne)
-                {
-                    Z3 = Z3.Multiply(Z1);
-                }
+            ulong[] _L3 = t1;
+            SecT233Field.AddTo(tt0, _L3);
+            SecT233Field.SquareExt(_L3, tt0);
+            SecT233Field.Add(L1.x, Z1.x, _L3);
+            SecT233Field.MultiplyAddToExt(_Z3, _L3, tt0);
+            SecT233Field.Reduce(tt0, _L3);
+
+            if (!Z1IsOne)
+            {
+                SecT233Field.Multiply(_Z3, Z1.x, _Z3);
             }
 
-            return new SecT233K1Point(curve, X3, L3, new ECFieldElement[] { Z3 });
+            return new SecT233K1Point(curve, new SecT233FieldElement(_X3), new SecT233FieldElement(_L3),
+                new ECFieldElement[]{ new SecT233FieldElement(_Z3) });
         }
 
         public override ECPoint Twice()