diff options
Diffstat (limited to 'crypto/src/math/ec/ECPoint.cs')
-rw-r--r-- | crypto/src/math/ec/ECPoint.cs | 169 |
1 files changed, 68 insertions, 101 deletions
diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs index 2a9d0832a..f32376455 100644 --- a/crypto/src/math/ec/ECPoint.cs +++ b/crypto/src/math/ec/ECPoint.cs @@ -50,23 +50,21 @@ namespace Org.BouncyCastle.Math.EC protected internal readonly ECCurve m_curve; protected internal readonly ECFieldElement m_x, m_y; protected internal readonly ECFieldElement[] m_zs; - protected internal readonly bool m_withCompression; // Dictionary is (string -> PreCompInfo) protected internal IDictionary m_preCompTable = null; - protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression) - : this(curve, x, y, GetInitialZCoords(curve), withCompression) + protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) + : this(curve, x, y, GetInitialZCoords(curve)) { } - internal ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) + internal ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) { this.m_curve = curve; this.m_x = x; this.m_y = y; this.m_zs = zs; - this.m_withCompression = withCompression; } protected abstract bool SatisfiesCurveEquation(); @@ -284,7 +282,7 @@ namespace Org.BouncyCastle.Math.EC protected virtual ECPoint CreateScaledPoint(ECFieldElement sx, ECFieldElement sy) { - return Curve.CreateRawPoint(RawXCoord.Multiply(sx), RawYCoord.Multiply(sy), IsCompressed); + return Curve.CreateRawPoint(RawXCoord.Multiply(sx), RawYCoord.Multiply(sy)); } public bool IsInfinity @@ -292,11 +290,6 @@ namespace Org.BouncyCastle.Math.EC get { return m_x == null && m_y == null; } } - public bool IsCompressed - { - get { return m_withCompression; } - } - public bool IsValid() { return ImplIsValid(false, true); @@ -321,28 +314,28 @@ namespace Org.BouncyCastle.Math.EC { return IsInfinity ? this - : Curve.CreateRawPoint(RawXCoord.Multiply(scale), RawYCoord, RawZCoords, IsCompressed); + : Curve.CreateRawPoint(RawXCoord.Multiply(scale), RawYCoord, RawZCoords); } public virtual ECPoint ScaleXNegateY(ECFieldElement scale) { return IsInfinity ? this - : Curve.CreateRawPoint(RawXCoord.Multiply(scale), RawYCoord.Negate(), RawZCoords, IsCompressed); + : Curve.CreateRawPoint(RawXCoord.Multiply(scale), RawYCoord.Negate(), RawZCoords); } public virtual ECPoint ScaleY(ECFieldElement scale) { return IsInfinity ? this - : Curve.CreateRawPoint(RawXCoord, RawYCoord.Multiply(scale), RawZCoords, IsCompressed); + : Curve.CreateRawPoint(RawXCoord, RawYCoord.Multiply(scale), RawZCoords); } public virtual ECPoint ScaleYNegateX(ECFieldElement scale) { return IsInfinity ? this - : Curve.CreateRawPoint(RawXCoord.Negate(), RawYCoord.Multiply(scale), RawZCoords, IsCompressed); + : Curve.CreateRawPoint(RawXCoord.Negate(), RawYCoord.Multiply(scale), RawZCoords); } public override bool Equals(object obj) @@ -440,7 +433,7 @@ namespace Org.BouncyCastle.Math.EC public virtual byte[] GetEncoded() { - return GetEncoded(m_withCompression); + return GetEncoded(false); } public abstract byte[] GetEncoded(bool compressed); @@ -527,17 +520,13 @@ namespace Org.BouncyCastle.Math.EC public abstract class ECPointBase : ECPoint { - protected internal ECPointBase( - ECCurve curve, - ECFieldElement x, - ECFieldElement y, - bool withCompression) - : base(curve, x, y, withCompression) + protected internal ECPointBase(ECCurve curve, ECFieldElement x, ECFieldElement y) + : base(curve, x, y) { } - protected internal ECPointBase(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) - : base(curve, x, y, zs, withCompression) + protected internal ECPointBase(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) + : base(curve, x, y, zs) { } @@ -547,9 +536,7 @@ namespace Org.BouncyCastle.Math.EC public override byte[] GetEncoded(bool compressed) { if (this.IsInfinity) - { return new byte[1]; - } ECPoint normed = Normalize(); @@ -588,13 +575,13 @@ namespace Org.BouncyCastle.Math.EC public abstract class AbstractFpPoint : ECPointBase { - protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression) - : base(curve, x, y, withCompression) + protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) + : base(curve, x, y) { } - protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) - : base(curve, x, y, zs, withCompression) + protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) + : base(curve, x, y, zs) { } @@ -661,30 +648,21 @@ namespace Org.BouncyCastle.Math.EC public class FpPoint : AbstractFpPoint { - /** - * Create a point that encodes with or without point compression. - * - * @param curve the curve to use - * @param x affine x co-ordinate - * @param y affine y co-ordinate - * @param withCompression if true encode with point compression - */ - [Obsolete("Per-point compression property will be removed, see GetEncoded(bool)")] - public FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression) - : base(curve, x, y, withCompression) + internal FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) + : base(curve, x, y) { if ((x == null) != (y == null)) throw new ArgumentException("Exactly one of the field elements is null"); } - internal FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) - : base(curve, x, y, zs, withCompression) + internal FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) + : base(curve, x, y, zs) { } protected override ECPoint Detach() { - return new FpPoint(null, AffineXCoord, AffineYCoord, false); + return new FpPoint(null, AffineXCoord, AffineYCoord); } public override ECFieldElement GetZCoord(int index) @@ -735,7 +713,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X3 = gamma.Square().Subtract(X1).Subtract(X2); ECFieldElement Y3 = gamma.Multiply(X1.Subtract(X3)).Subtract(Y1); - return new FpPoint(Curve, X3, Y3, IsCompressed); + return new FpPoint(Curve, X3, Y3); } case ECCurve.COORD_HOMOGENEOUS: @@ -777,7 +755,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement Y3 = vSquaredV2.Subtract(A).MultiplyMinusProduct(u, u2, vCubed); ECFieldElement Z3 = vCubed.Multiply(w); - return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed); + return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }); } case ECCurve.COORD_JACOBIAN: @@ -907,7 +885,7 @@ namespace Org.BouncyCastle.Math.EC zs = new ECFieldElement[] { Z3 }; } - return new FpPoint(curve, X3, Y3, zs, IsCompressed); + return new FpPoint(curve, X3, Y3, zs); } default: @@ -942,7 +920,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X3 = gamma.Square().Subtract(Two(X1)); ECFieldElement Y3 = gamma.Multiply(X1.Subtract(X3)).Subtract(Y1); - return new FpPoint(Curve, X3, Y3, IsCompressed); + return new FpPoint(Curve, X3, Y3); } case ECCurve.COORD_HOMOGENEOUS: @@ -972,7 +950,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement _4sSquared = Z1IsOne ? Two(_2t) : _2s.Square(); ECFieldElement Z3 = Two(_4sSquared).Multiply(s); - return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed); + return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }); } case ECCurve.COORD_JACOBIAN: @@ -1031,7 +1009,7 @@ namespace Org.BouncyCastle.Math.EC // Alternative calculation of Z3 using fast square //ECFieldElement Z3 = doubleProductFromSquares(Y1, Z1, Y1Squared, Z1Squared); - return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed); + return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }); } case ECCurve.COORD_JACOBIAN_MODIFIED: @@ -1102,7 +1080,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X4 = (L2.Subtract(L1)).Multiply(L1.Add(L2)).Add(X2); ECFieldElement Y4 = (X1.Subtract(X4)).Multiply(L2).Subtract(Y1); - return new FpPoint(Curve, X4, Y4, IsCompressed); + return new FpPoint(Curve, X4, Y4); } case ECCurve.COORD_JACOBIAN_MODIFIED: { @@ -1151,7 +1129,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X4 = (L2.Subtract(L1)).Multiply(L1.Add(L2)).Add(X1); ECFieldElement Y4 = (X1.Subtract(X4)).Multiply(L2).Subtract(Y1); - return new FpPoint(Curve, X4, Y4, IsCompressed); + return new FpPoint(Curve, X4, Y4); } case ECCurve.COORD_JACOBIAN_MODIFIED: { @@ -1233,15 +1211,15 @@ namespace Org.BouncyCastle.Math.EC { case ECCurve.COORD_AFFINE: ECFieldElement zInv = Z1.Invert(), zInv2 = zInv.Square(), zInv3 = zInv2.Multiply(zInv); - return new FpPoint(curve, X1.Multiply(zInv2), Y1.Multiply(zInv3), IsCompressed); + return new FpPoint(curve, X1.Multiply(zInv2), Y1.Multiply(zInv3)); case ECCurve.COORD_HOMOGENEOUS: X1 = X1.Multiply(Z1); Z1 = Z1.Multiply(Z1.Square()); - return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }, IsCompressed); + return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }); case ECCurve.COORD_JACOBIAN: - return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }, IsCompressed); + return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }); case ECCurve.COORD_JACOBIAN_MODIFIED: - return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1, W1 }, IsCompressed); + return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1, W1 }); default: throw new InvalidOperationException("unsupported coordinate system"); } @@ -1287,10 +1265,10 @@ namespace Org.BouncyCastle.Math.EC if (ECCurve.COORD_AFFINE != coord) { - return new FpPoint(curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed); + return new FpPoint(curve, RawXCoord, RawYCoord.Negate(), RawZCoords); } - return new FpPoint(curve, RawXCoord, RawYCoord.Negate(), IsCompressed); + return new FpPoint(curve, RawXCoord, RawYCoord.Negate()); } protected virtual ECFieldElement CalculateJacobianModifiedW(ECFieldElement Z, ECFieldElement ZSquared) @@ -1345,20 +1323,20 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement W3 = calculateW ? Two(_8T.Multiply(W1)) : null; ECFieldElement Z3 = Z1.IsOne ? _2Y1 : _2Y1.Multiply(Z1); - return new FpPoint(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }, IsCompressed); + return new FpPoint(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }); } } public abstract class AbstractF2mPoint : ECPointBase { - protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression) - : base(curve, x, y, withCompression) + protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) + : base(curve, x, y) { } - protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) - : base(curve, x, y, zs, withCompression) + protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) + : base(curve, x, y, zs) { } @@ -1498,7 +1476,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X2 = X.Multiply(scale); ECFieldElement L2 = L.Add(X).Divide(scale).Add(X2); - return Curve.CreateRawPoint(X, L2, RawZCoords, IsCompressed); + return Curve.CreateRawPoint(X, L2, RawZCoords); } case ECCurve.COORD_LAMBDA_PROJECTIVE: { @@ -1510,7 +1488,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement L2 = L.Add(X).Add(X2); ECFieldElement Z2 = Z.Multiply(scale); - return Curve.CreateRawPoint(X, L2, new ECFieldElement[] { Z2 }, IsCompressed); + return Curve.CreateRawPoint(X, L2, new ECFieldElement[] { Z2 }); } default: { @@ -1539,7 +1517,7 @@ namespace Org.BouncyCastle.Math.EC // Y is actually Lambda (X + Y/X) here ECFieldElement L2 = L.Add(X).Multiply(scale).Add(X); - return Curve.CreateRawPoint(X, L2, RawZCoords, IsCompressed); + return Curve.CreateRawPoint(X, L2, RawZCoords); } default: { @@ -1578,14 +1556,14 @@ namespace Org.BouncyCastle.Math.EC case ECCurve.COORD_LAMBDA_AFFINE: { ECFieldElement Y1 = this.RawYCoord; - return (AbstractF2mPoint)curve.CreateRawPoint(X1.Square(), Y1.Square(), IsCompressed); + return (AbstractF2mPoint)curve.CreateRawPoint(X1.Square(), Y1.Square()); } case ECCurve.COORD_HOMOGENEOUS: case ECCurve.COORD_LAMBDA_PROJECTIVE: { ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0]; return (AbstractF2mPoint)curve.CreateRawPoint(X1.Square(), Y1.Square(), - new ECFieldElement[] { Z1.Square() }, IsCompressed); + new ECFieldElement[] { Z1.Square() }); } default: { @@ -1610,14 +1588,14 @@ namespace Org.BouncyCastle.Math.EC case ECCurve.COORD_LAMBDA_AFFINE: { ECFieldElement Y1 = this.RawYCoord; - return (AbstractF2mPoint)curve.CreateRawPoint(X1.SquarePow(pow), Y1.SquarePow(pow), IsCompressed); + return (AbstractF2mPoint)curve.CreateRawPoint(X1.SquarePow(pow), Y1.SquarePow(pow)); } case ECCurve.COORD_HOMOGENEOUS: case ECCurve.COORD_LAMBDA_PROJECTIVE: { ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0]; return (AbstractF2mPoint)curve.CreateRawPoint(X1.SquarePow(pow), Y1.SquarePow(pow), - new ECFieldElement[] { Z1.SquarePow(pow) }, IsCompressed); + new ECFieldElement[] { Z1.SquarePow(pow) }); } default: { @@ -1633,19 +1611,8 @@ namespace Org.BouncyCastle.Math.EC public class F2mPoint : AbstractF2mPoint { - /** - * @param curve base curve - * @param x x point - * @param y y point - * @param withCompression true if encode with point compression. - */ - [Obsolete("Per-point compression property will be removed, see GetEncoded(bool)")] - public F2mPoint( - ECCurve curve, - ECFieldElement x, - ECFieldElement y, - bool withCompression) - : base(curve, x, y, withCompression) + internal F2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) + : base(curve, x, y) { if ((x == null) != (y == null)) { @@ -1665,14 +1632,14 @@ namespace Org.BouncyCastle.Math.EC } } - internal F2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) - : base(curve, x, y, zs, withCompression) + internal F2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) + : base(curve, x, y, zs) { } protected override ECPoint Detach() { - return new F2mPoint(null, AffineXCoord, AffineYCoord, false); + return new F2mPoint(null, AffineXCoord, AffineYCoord); } public override ECFieldElement YCoord @@ -1775,7 +1742,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X3 = L.Square().Add(L).Add(dx).Add(curve.A); ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1); - return new F2mPoint(curve, X3, Y3, IsCompressed); + return new F2mPoint(curve, X3, Y3); } case ECCurve.COORD_HOMOGENEOUS: { @@ -1822,7 +1789,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement Y3 = U.MultiplyPlusProduct(X1, V, Y1).MultiplyPlusProduct(VSqZ2, uv, A); ECFieldElement Z3 = VCu.Multiply(W); - return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed); + return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }); } case ECCurve.COORD_LAMBDA_PROJECTIVE: { @@ -1880,7 +1847,7 @@ namespace Org.BouncyCastle.Math.EC X3 = L.Square().Add(L).Add(X1).Add(curve.A); if (X3.IsZero) { - return new F2mPoint(curve, X3, curve.B.Sqrt(), IsCompressed); + return new F2mPoint(curve, X3, curve.B.Sqrt()); } ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1); @@ -1897,7 +1864,7 @@ namespace Org.BouncyCastle.Math.EC X3 = AU1.Multiply(AU2); if (X3.IsZero) { - return new F2mPoint(curve, X3, curve.B.Sqrt(), IsCompressed); + return new F2mPoint(curve, X3, curve.B.Sqrt()); } ECFieldElement ABZ2 = A.Multiply(B); @@ -1915,7 +1882,7 @@ namespace Org.BouncyCastle.Math.EC } } - return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed); + return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }); } default: { @@ -1954,7 +1921,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement X3 = L1.Square().Add(L1).Add(curve.A); ECFieldElement Y3 = X1.SquarePlusProduct(X3, L1.AddOne()); - return new F2mPoint(curve, X3, Y3, IsCompressed); + return new F2mPoint(curve, X3, Y3); } case ECCurve.COORD_HOMOGENEOUS: { @@ -1975,7 +1942,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement Y3 = X1Sq.Square().MultiplyPlusProduct(V, h, sv); ECFieldElement Z3 = V.Multiply(vSquared); - return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed); + return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }); } case ECCurve.COORD_LAMBDA_PROJECTIVE: { @@ -1989,7 +1956,7 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement T = L1.Square().Add(L1Z1).Add(aZ1Sq); if (T.IsZero) { - return new F2mPoint(curve, T, curve.B.Sqrt(), IsCompressed); + return new F2mPoint(curve, T, curve.B.Sqrt()); } ECFieldElement X3 = T.Square(); @@ -2026,7 +1993,7 @@ namespace Org.BouncyCastle.Math.EC L3 = X1Z1.SquarePlusProduct(T, L1Z1).Add(X3).Add(Z3); } - return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed); + return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }); } default: { @@ -2090,14 +2057,14 @@ namespace Org.BouncyCastle.Math.EC if (A.IsZero) { - return new F2mPoint(curve, A, curve.B.Sqrt(), IsCompressed); + return new F2mPoint(curve, A, curve.B.Sqrt()); } ECFieldElement X3 = A.Square().Multiply(X2Z1Sq); ECFieldElement Z3 = A.Multiply(B).Multiply(Z1Sq); ECFieldElement L3 = A.Add(B).Square().MultiplyPlusProduct(T, L2plus1, Z3); - return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed); + return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }); } default: { @@ -2123,23 +2090,23 @@ namespace Org.BouncyCastle.Math.EC case ECCurve.COORD_AFFINE: { ECFieldElement Y = this.RawYCoord; - return new F2mPoint(curve, X, Y.Add(X), IsCompressed); + return new F2mPoint(curve, X, Y.Add(X)); } case ECCurve.COORD_HOMOGENEOUS: { ECFieldElement Y = this.RawYCoord, Z = this.RawZCoords[0]; - return new F2mPoint(curve, X, Y.Add(X), new ECFieldElement[] { Z }, IsCompressed); + return new F2mPoint(curve, X, Y.Add(X), new ECFieldElement[] { Z }); } case ECCurve.COORD_LAMBDA_AFFINE: { ECFieldElement L = this.RawYCoord; - return new F2mPoint(curve, X, L.AddOne(), IsCompressed); + return new F2mPoint(curve, X, L.AddOne()); } case ECCurve.COORD_LAMBDA_PROJECTIVE: { // L is actually Lambda (X + Y/X) here ECFieldElement L = this.RawYCoord, Z = this.RawZCoords[0]; - return new F2mPoint(curve, X, L.Add(Z), new ECFieldElement[] { Z }, IsCompressed); + return new F2mPoint(curve, X, L.Add(Z), new ECFieldElement[] { Z }); } default: { |