summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-01-28 10:08:25 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-01-28 10:08:25 +0700
commitd209b6b28b2b0e396e8813c6bb80ef98e1e2ba30 (patch)
treed05d952919e07437a07cf0ebabfcaf5a71120b28
parentPort of latest EC multipliers from Java (diff)
downloadBouncyCastle.NET-ed25519-d209b6b28b2b0e396e8813c6bb80ef98e1e2ba30.tar.xz
Port from Java order/cofactor for all curves
-rw-r--r--crypto/src/crypto/ec/CustomNamedCurves.cs10
-rw-r--r--crypto/src/math/ec/ECCurve.cs75
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Curve.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Curve.cs4
4 files changed, 52 insertions, 41 deletions
diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs
index 3cc9fe342..72931bc7e 100644
--- a/crypto/src/crypto/ec/CustomNamedCurves.cs
+++ b/crypto/src/crypto/ec/CustomNamedCurves.cs
@@ -46,10 +46,7 @@ namespace Org.BouncyCastle.Crypto.EC
                 ECPoint G = curve.DecodePoint(Hex.Decode("04"
                     + "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
                     + "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"));
-                //return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
-                BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
-                BigInteger h = BigInteger.ValueOf(1);
-                return new X9ECParameters(curve, G, n, h, S);
+                return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
             }
         }
 
@@ -70,10 +67,7 @@ namespace Org.BouncyCastle.Crypto.EC
                 ECPoint G = curve.DecodePoint(Hex.Decode("04"
                     + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
                     + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"));
-                //return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
-                BigInteger n = FromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
-                BigInteger h = BigInteger.ValueOf(1);
-                return new X9ECParameters(curve, G, n, h, S);
+                return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
             }
         }
 
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index ed413597a..ee3bcc69b 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -71,8 +71,9 @@ namespace Org.BouncyCastle.Math.EC
             }
         }
 
-        protected IFiniteField m_field;
+        protected readonly IFiniteField m_field;
         protected ECFieldElement m_a, m_b;
+        protected BigInteger m_order, m_cofactor;
 
         protected int m_coord = COORD_AFFINE;
         protected ECMultiplier m_multiplier = null;
@@ -220,6 +221,16 @@ namespace Org.BouncyCastle.Math.EC
             get { return m_b; }
         }
 
+        public virtual BigInteger Order
+        {
+            get { return m_order; }
+        }
+
+        public virtual BigInteger Cofactor
+        {
+            get { return m_cofactor; }
+        }
+
         public virtual int CoordinateSystem
         {
             get { return m_coord; }
@@ -353,6 +364,11 @@ namespace Org.BouncyCastle.Math.EC
         protected readonly FpPoint m_infinity;
 
         public FpCurve(BigInteger q, BigInteger a, BigInteger b)
+            : this(q, a, b, null, null)
+        {
+        }
+
+        public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor)
             : base(FiniteFields.GetPrimeField(q))
         {
             this.m_q = q;
@@ -361,10 +377,17 @@ namespace Org.BouncyCastle.Math.EC
 
             this.m_a = FromBigInteger(a);
             this.m_b = FromBigInteger(b);
+            this.m_order = order;
+            this.m_cofactor = cofactor;
             this.m_coord = FP_DEFAULT_COORDS;
         }
 
         protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b)
+            : this(q, r, a, b, null, null)
+        {
+        }
+
+        protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b, BigInteger order, BigInteger cofactor)
             : base(FiniteFields.GetPrimeField(q))
         {
             this.m_q = q;
@@ -373,12 +396,14 @@ namespace Org.BouncyCastle.Math.EC
 
             this.m_a = a;
             this.m_b = b;
+            this.m_order = order;
+            this.m_cofactor = cofactor;
             this.m_coord = FP_DEFAULT_COORDS;
         }
 
         protected override ECCurve CloneCurve()
         {
-            return new FpCurve(m_q, m_r, m_a, m_b);
+            return new FpCurve(m_q, m_r, m_a, m_b, m_order, m_cofactor);
         }
 
         public override bool SupportsCoordinateSystem(int coord)
@@ -535,16 +560,6 @@ namespace Org.BouncyCastle.Math.EC
         private readonly int k3;
 
         /**
-         * The order of the base point of the curve.
-         */
-        private readonly BigInteger n;
-
-        /**
-         * The cofactor of the curve.
-         */
-        private readonly BigInteger h;
-
-        /**
          * The point at infinity on this curve.
          */
         protected readonly F2mPoint m_infinity;
@@ -598,8 +613,8 @@ namespace Org.BouncyCastle.Math.EC
          * @param b The coefficient <code>b</code> in the Weierstrass equation
          * for non-supersingular elliptic curves over
          * <code>F<sub>2<sup>m</sup></sub></code>.
-         * @param n The order of the main subgroup of the elliptic curve.
-         * @param h The cofactor of the elliptic curve, i.e.
+         * @param order The order of the main subgroup of the elliptic curve.
+         * @param cofactor The cofactor of the elliptic curve, i.e.
          * <code>#E<sub>a</sub>(F<sub>2<sup>m</sup></sub>) = h * n</code>.
          */
         public F2mCurve(
@@ -607,9 +622,9 @@ namespace Org.BouncyCastle.Math.EC
             int			k, 
             BigInteger	a, 
             BigInteger	b,
-            BigInteger	n,
-            BigInteger	h)
-            : this(m, k, 0, 0, a, b, n, h)
+            BigInteger	order,
+            BigInteger	cofactor)
+            : this(m, k, 0, 0, a, b, order, cofactor)
         {
         }
 
@@ -663,8 +678,8 @@ namespace Org.BouncyCastle.Math.EC
          * @param b The coefficient <code>b</code> in the Weierstrass equation
          * for non-supersingular elliptic curves over
          * <code>F<sub>2<sup>m</sup></sub></code>.
-         * @param n The order of the main subgroup of the elliptic curve.
-         * @param h The cofactor of the elliptic curve, i.e.
+         * @param order The order of the main subgroup of the elliptic curve.
+         * @param cofactor The cofactor of the elliptic curve, i.e.
          * <code>#E<sub>a</sub>(F<sub>2<sup>m</sup></sub>) = h * n</code>.
          */
         public F2mCurve(
@@ -674,16 +689,16 @@ namespace Org.BouncyCastle.Math.EC
             int			k3,
             BigInteger	a, 
             BigInteger	b,
-            BigInteger	n,
-            BigInteger	h)
+            BigInteger	order,
+            BigInteger	cofactor)
             : base(BuildField(m, k1, k2, k3))
         {
             this.m = m;
             this.k1 = k1;
             this.k2 = k2;
             this.k3 = k3;
-            this.n = n;
-            this.h = h;
+            this.m_order = order;
+            this.m_cofactor = cofactor;
             this.m_infinity = new F2mPoint(this, null, null);
 
             if (k1 == 0)
@@ -715,8 +730,8 @@ namespace Org.BouncyCastle.Math.EC
             this.k1 = k1;
             this.k2 = k2;
             this.k3 = k3;
-            this.n = order;
-            this.h = cofactor;
+            this.m_order = order;
+            this.m_cofactor = cofactor;
 
             this.m_infinity = new F2mPoint(this, null, null);
             this.m_a = a;
@@ -726,7 +741,7 @@ namespace Org.BouncyCastle.Math.EC
 
         protected override ECCurve CloneCurve()
         {
-            return new F2mCurve(m, k1, k2, k3, m_a, m_b, n, h);
+            return new F2mCurve(m, k1, k2, k3, m_a, m_b, m_order, m_cofactor);
         }
 
         public override bool SupportsCoordinateSystem(int coord)
@@ -810,7 +825,7 @@ namespace Org.BouncyCastle.Math.EC
         {
             get
             {
-                return n != null && h != null && m_a.BitLength <= 1 && m_b.IsOne;
+                return m_order != null && m_cofactor != null && m_b.IsOne && (m_a.IsZero || m_a.IsOne);
             }
         }
 
@@ -973,14 +988,16 @@ namespace Org.BouncyCastle.Math.EC
             get { return k3; }
         }
 
+        [Obsolete("Use 'Order' property instead")]
         public BigInteger N
         {
-            get { return n; }
+            get { return m_order; }
         }
 
+        [Obsolete("Use 'Cofactor' property instead")]
         public BigInteger H
         {
-            get { return h; }
+            get { return m_cofactor; }
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
index 2e0a4a5e4..95139a014 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
@@ -22,8 +22,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             this.m_a = FromBigInteger(BigInteger.Zero);
             this.m_b = FromBigInteger(BigInteger.ValueOf(7));
-            //this.order = new BigInteger(1, Hex.decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"));
-            //this.cofactor = BigInteger.valueOf(1);
+            this.m_order = new BigInteger(1, Hex.Decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"));
+            this.m_cofactor = BigInteger.ValueOf(1);
             this.m_coord = SECP256K1_DEFAULT_COORDS;
         }
 
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs b/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
index 252ec345f..c0620574c 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
@@ -24,8 +24,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 Hex.Decode("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")));
             this.m_b = FromBigInteger(new BigInteger(1,
                 Hex.Decode("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")));
-            //this.order = new BigInteger(1, Hex.decode("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"));
-            //this.cofactor = BigInteger.valueOf(1);
+            this.m_order = new BigInteger(1, Hex.Decode("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"));
+            this.m_cofactor = BigInteger.ValueOf(1);
             this.m_coord = SecP256R1_DEFAULT_COORDS;
         }