123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- namespace Org.BouncyCastle.Math.EC.Custom.Sec
- {
- internal class SecT571K1Point
- : AbstractF2mPoint
- {
- /**
- * @deprecated Use ECCurve.createPoint to construct points
- */
- public SecT571K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
- : this(curve, x, y, false)
- {
- }
- /**
- * @deprecated per-point compression property will be removed, refer {@link #getEncoded(bool)}
- */
- public SecT571K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
- : base(curve, x, y, withCompression)
- {
- if ((x == null) != (y == null))
- throw new ArgumentException("Exactly one of the field elements is null");
- }
- internal SecT571K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
- : base(curve, x, y, zs, withCompression)
- {
- }
- protected override ECPoint Detach()
- {
- return new SecT571K1Point(null, this.AffineXCoord, this.AffineYCoord); // earlier JDK
- }
- public override ECFieldElement YCoord
- {
- get
- {
- ECFieldElement X = RawXCoord, L = RawYCoord;
- if (this.IsInfinity || X.IsZero)
- return L;
- // Y is actually Lambda (X + Y/X) here; convert to affine value on the fly
- ECFieldElement Y = L.Add(X).Multiply(X);
- ECFieldElement Z = RawZCoords[0];
- if (!Z.IsOne)
- {
- Y = Y.Divide(Z);
- }
- return Y;
- }
- }
- protected internal override bool CompressionYTilde
- {
- get
- {
- ECFieldElement X = this.RawXCoord;
- if (X.IsZero)
- return false;
- ECFieldElement Y = this.RawYCoord;
- // Y is actually Lambda (X + Y/X) here
- return Y.TestBitZero() != X.TestBitZero();
- }
- }
- public override ECPoint Add(ECPoint b)
- {
- if (this.IsInfinity)
- return b;
- if (b.IsInfinity)
- return this;
- ECCurve curve = this.Curve;
- ECFieldElement X1 = this.RawXCoord;
- ECFieldElement X2 = 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];
- bool Z1IsOne = Z1.IsOne;
- ECFieldElement U2 = X2, S2 = L2;
- if (!Z1IsOne)
- {
- U2 = U2.Multiply(Z1);
- S2 = S2.Multiply(Z1);
- }
- bool Z2IsOne = Z2.IsOne;
- ECFieldElement U1 = X1, S1 = L1;
- if (!Z2IsOne)
- {
- U1 = U1.Multiply(Z2);
- S1 = S1.Multiply(Z2);
- }
- ECFieldElement A = S1.Add(S2);
- ECFieldElement B = U1.Add(U2);
- if (B.IsZero)
- {
- if (A.IsZero)
- 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;
- ECFieldElement Y1 = p.YCoord;
- ECFieldElement Y2 = L2;
- ECFieldElement L = Y1.Add(Y2).Divide(X1);
- //X3 = L.Square().Add(L).Add(X1).Add(curve.A);
- X3 = L.Square().Add(L).Add(X1).AddOne();
- if (X3.IsZero)
- {
- //return new SecT571K1Point(curve, X3, curve.B.sqrt(), IsCompressed);
- return new SecT571K1Point(curve, X3, curve.B, IsCompressed);
- }
- ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
- L3 = Y3.Divide(X3).Add(X3);
- Z3 = curve.FromBigInteger(BigInteger.One);
- }
- else
- {
- B = B.Square();
- ECFieldElement AU1 = A.Multiply(U1);
- ECFieldElement AU2 = A.Multiply(U2);
- X3 = AU1.Multiply(AU2);
- if (X3.IsZero)
- {
- //return new SecT571K1Point(curve, X3, curve.B.sqrt(), IsCompressed);
- return new SecT571K1Point(curve, X3, curve.B, IsCompressed);
- }
- ECFieldElement ABZ2 = A.Multiply(B);
- if (!Z2IsOne)
- {
- ABZ2 = ABZ2.Multiply(Z2);
- }
- L3 = AU2.Add(B).SquarePlusProduct(ABZ2, L1.Add(Z1));
- Z3 = ABZ2;
- if (!Z1IsOne)
- {
- Z3 = Z3.Multiply(Z1);
- }
- }
- return new SecT571K1Point(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
- }
- public override ECPoint Twice()
- {
- if (this.IsInfinity)
- return this;
- ECCurve curve = this.Curve;
- ECFieldElement X1 = this.RawXCoord;
- if (X1.IsZero)
- {
- // A point with X == 0 is it's own Additive inverse
- return curve.Infinity;
- }
- ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
- bool Z1IsOne = Z1.IsOne;
- ECFieldElement Z1Sq = Z1IsOne ? Z1 : Z1.Square();
- ECFieldElement T;
- if (Z1IsOne)
- {
- T = L1.Square().Add(L1);
- }
- else
- {
- T = L1.Add(Z1).Multiply(L1);
- }
- if (T.IsZero)
- {
- //return new SecT571K1Point(curve, T, curve.B.sqrt(), withCompression);
- return new SecT571K1Point(curve, T, curve.B, IsCompressed);
- }
- ECFieldElement X3 = T.Square();
- ECFieldElement Z3 = Z1IsOne ? T : T.Multiply(Z1Sq);
- ECFieldElement t1 = L1.Add(X1).Square();
- ECFieldElement t2 = Z1IsOne ? Z1 : Z1Sq.Square();
- ECFieldElement L3 = t1.Add(T).Add(Z1Sq).Multiply(t1).Add(t2).Add(X3).Add(Z3);
- return new SecT571K1Point(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
- }
- public override ECPoint TwicePlus(ECPoint b)
- {
- if (this.IsInfinity)
- return b;
- if (b.IsInfinity)
- return Twice();
- ECCurve curve = this.Curve;
- ECFieldElement X1 = this.RawXCoord;
- if (X1.IsZero)
- {
- // A point with X == 0 is it's own Additive inverse
- return b;
- }
- // NOTE: TwicePlus() only optimized for lambda-affine argument
- ECFieldElement X2 = b.RawXCoord, Z2 = b.RawZCoords[0];
- if (X2.IsZero || !Z2.IsOne)
- {
- return Twice().Add(b);
- }
- ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
- ECFieldElement L2 = b.RawYCoord;
- ECFieldElement X1Sq = X1.Square();
- ECFieldElement L1Sq = L1.Square();
- ECFieldElement Z1Sq = Z1.Square();
- ECFieldElement L1Z1 = L1.Multiply(Z1);
- //ECFieldElement T = curve.A.Multiply(Z1Sq).Add(L1Sq).Add(L1Z1);
- ECFieldElement T = L1Sq.Add(L1Z1);
- ECFieldElement L2plus1 = L2.AddOne();
- //ECFieldElement A = curve.A.Add(L2plus1).Multiply(Z1Sq).Add(L1Sq).MultiplyPlusProduct(T, X1Sq, Z1Sq);
- ECFieldElement A = L2plus1.Multiply(Z1Sq).Add(L1Sq).MultiplyPlusProduct(T, X1Sq, Z1Sq);
- ECFieldElement X2Z1Sq = X2.Multiply(Z1Sq);
- ECFieldElement B = X2Z1Sq.Add(T).Square();
- if (B.IsZero)
- {
- if (A.IsZero)
- return b.Twice();
- return curve.Infinity;
- }
- if (A.IsZero)
- {
- //return new SecT571K1Point(curve, A, curve.B.sqrt(), withCompression);
- return new SecT571K1Point(curve, A, curve.B, IsCompressed);
- }
- 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 SecT571K1Point(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
- }
- public override ECPoint Negate()
- {
- if (this.IsInfinity)
- return this;
- ECFieldElement X = this.RawXCoord;
- if (X.IsZero)
- return this;
- // L is actually Lambda (X + Y/X) here
- ECFieldElement L = this.RawYCoord, Z = this.RawZCoords[0];
- return new SecT571K1Point(Curve, X, L.Add(Z), new ECFieldElement[] { Z }, IsCompressed);
- }
- }
- }
- #endif
|