summary refs log tree commit diff
path: root/crypto/src/math/ec/ECCurve.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-01-26 11:34:07 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-01-26 11:34:07 +0700
commit2943ddadcaeb987181708bfada6006b3feabe14b (patch)
tree9ec338361719122d537523abfdfa6c5dc90c57d5 /crypto/src/math/ec/ECCurve.cs
parentMake Barrett reduction available for more prime moduli (diff)
downloadBouncyCastle.NET-ed25519-2943ddadcaeb987181708bfada6006b3feabe14b.tar.xz
Port of jacobian/-modified coordinates from Java
Make jacobian-modified the default coordinates for Fp
Diffstat (limited to 'crypto/src/math/ec/ECCurve.cs')
-rw-r--r--crypto/src/math/ec/ECCurve.cs117
1 files changed, 65 insertions, 52 deletions
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index 9679fdb89..4b58d7626 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -95,7 +95,11 @@ namespace Org.BouncyCastle.Math.EC
             return CreatePoint(x, y, false);
         }
 
-        public abstract ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression);
+        [Obsolete("Per-point compression property will be removed")]
+        public virtual ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
+        {
+            return CreateRawPoint(FromBigInteger(x), FromBigInteger(y), withCompression);
+        }
 
         protected abstract ECCurve CloneCurve();
 
@@ -343,7 +347,7 @@ namespace Org.BouncyCastle.Math.EC
     public class FpCurve
         : ECCurve
     {
-        private const int FP_DEFAULT_COORDS = COORD_HOMOGENEOUS;
+        private const int FP_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED;
 
         protected readonly BigInteger m_q, m_r;
         protected readonly FpPoint m_infinity;
@@ -383,8 +387,8 @@ namespace Org.BouncyCastle.Math.EC
             {
                 case COORD_AFFINE:
                 case COORD_HOMOGENEOUS:
-                //case COORD_JACOBIAN:
-                //case COORD_JACOBIAN_MODIFIED:
+                case COORD_JACOBIAN:
+                case COORD_JACOBIAN_MODIFIED:
                     return true;
                 default:
                     return false;
@@ -416,17 +420,26 @@ namespace Org.BouncyCastle.Math.EC
             return new FpPoint(this, x, y, withCompression);
         }
 
-        public override ECPoint CreatePoint(
-            BigInteger	X1,
-            BigInteger	Y1,
-            bool		withCompression)
+        public override ECPoint ImportPoint(ECPoint p)
         {
-            // TODO Validation of X1, Y1?
-            return new FpPoint(
-                this,
-                FromBigInteger(X1),
-                FromBigInteger(Y1),
-                withCompression);
+            if (this != p.Curve && this.CoordinateSystem == COORD_JACOBIAN && !p.IsInfinity)
+            {
+                switch (p.Curve.CoordinateSystem)
+                {
+                    case COORD_JACOBIAN:
+                    case COORD_JACOBIAN_CHUDNOVSKY:
+                    case COORD_JACOBIAN_MODIFIED:
+                        return new FpPoint(this,
+                            FromBigInteger(p.RawXCoord.ToBigInteger()),
+                            FromBigInteger(p.RawYCoord.ToBigInteger()),
+                            new ECFieldElement[] { FromBigInteger(p.GetZCoord(0).ToBigInteger()) },
+                            p.IsCompressed);
+                    default:
+                        break;
+                }
+            }
+
+            return base.ImportPoint(p);
         }
 
         protected override ECPoint DecompressPoint(
@@ -744,24 +757,54 @@ namespace Org.BouncyCastle.Math.EC
             return base.CreateDefaultMultiplier();
         }
 
-        protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
+        public override int FieldSize
         {
-            return new F2mPoint(this, x, y, withCompression);
+            get { return m; }
         }
 
-        public override ECPoint Infinity
+        public override ECFieldElement FromBigInteger(BigInteger x)
         {
-            get { return m_infinity; }
+            return new F2mFieldElement(this.m, this.k1, this.k2, this.k3, x);
         }
 
-        public override int FieldSize
+        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
         {
-            get { return m; }
+            ECFieldElement X = FromBigInteger(x), Y = FromBigInteger(y);
+
+            switch (this.CoordinateSystem)
+            {
+                case COORD_LAMBDA_AFFINE:
+                case COORD_LAMBDA_PROJECTIVE:
+                    {
+                        if (X.IsZero)
+                        {
+                            if (!Y.Square().Equals(B))
+                                throw new ArgumentException();
+                        }
+                        else
+                        {
+                            // Y becomes Lambda (X + Y/X) here
+                            Y = Y.Divide(X).Add(X);
+                        }
+                        break;
+                    }
+                default:
+                    {
+                        break;
+                    }
+            }
+
+            return CreateRawPoint(X, Y, withCompression);
         }
 
-        public override ECFieldElement FromBigInteger(BigInteger x)
+        protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
         {
-            return new F2mFieldElement(this.m, this.k1, this.k2, this.k3, x);
+            return new F2mPoint(this, x, y, withCompression);
+        }
+
+        public override ECPoint Infinity
+        {
+            get { return m_infinity; }
         }
 
         /**
@@ -818,36 +861,6 @@ namespace Org.BouncyCastle.Math.EC
             return si;
         }
 
-        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
-        {
-            ECFieldElement X = FromBigInteger(x), Y = FromBigInteger(y);
-
-            switch (this.CoordinateSystem)
-            {
-                case COORD_LAMBDA_AFFINE:
-                case COORD_LAMBDA_PROJECTIVE:
-                {
-                    if (X.IsZero)
-                    {
-                        if (!Y.Square().Equals(B))
-                            throw new ArgumentException();
-                    }
-                    else
-                    {
-                        // Y becomes Lambda (X + Y/X) here
-                        Y = Y.Divide(X).Add(X);
-                    }
-                    break;
-                }
-                default:
-                {
-                    break;
-                }
-            }
-
-            return CreateRawPoint(X, Y, withCompression);
-        }
-
         protected override ECPoint DecompressPoint(
             int			yTilde,
             BigInteger	X1)