Пример #1
0
    public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
    {
        ECFieldElement eCFieldElement  = FromBigInteger(x);
        ECFieldElement eCFieldElement2 = FromBigInteger(y);

        switch (CoordinateSystem)
        {
        case 5:
        case 6:
            if (eCFieldElement.IsZero)
            {
                if (!eCFieldElement2.Square().Equals(B))
                {
                    throw new ArgumentException();
                }
            }
            else
            {
                eCFieldElement2 = eCFieldElement2.Divide(eCFieldElement).Add(eCFieldElement);
            }
            break;
        }
        return(CreateRawPoint(eCFieldElement, eCFieldElement2, withCompression));
    }
Пример #2
0
        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);
                if (X3.IsZero)
                {
                    //return new SecT233K1Point(curve, X3, curve.B.sqrt(), IsCompressed);
                    return(new SecT233K1Point(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 SecT233K1Point(curve, X3, curve.B.sqrt(), IsCompressed);
                    return(new SecT233K1Point(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 SecT233K1Point(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed));
        }
Пример #3
0
        public override ECPoint Add(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve          = this.Curve;
            ECFieldElement eCFieldElement = base.RawXCoord;
            ECFieldElement rawXCoord      = b.RawXCoord;

            if (eCFieldElement.IsZero)
            {
                if (rawXCoord.IsZero)
                {
                    return(curve.Infinity);
                }
                return(b.Add(this));
            }
            else
            {
                ECFieldElement rawYCoord       = base.RawYCoord;
                ECFieldElement eCFieldElement2 = base.RawZCoords[0];
                ECFieldElement rawYCoord2      = b.RawYCoord;
                ECFieldElement eCFieldElement3 = b.RawZCoords[0];
                bool           isOne           = eCFieldElement2.IsOne;
                ECFieldElement eCFieldElement4 = rawXCoord;
                ECFieldElement eCFieldElement5 = rawYCoord2;
                if (!isOne)
                {
                    eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement2);
                    eCFieldElement5 = eCFieldElement5.Multiply(eCFieldElement2);
                }
                bool           isOne2          = eCFieldElement3.IsOne;
                ECFieldElement eCFieldElement6 = eCFieldElement;
                ECFieldElement eCFieldElement7 = rawYCoord;
                if (!isOne2)
                {
                    eCFieldElement6 = eCFieldElement6.Multiply(eCFieldElement3);
                    eCFieldElement7 = eCFieldElement7.Multiply(eCFieldElement3);
                }
                ECFieldElement eCFieldElement8 = eCFieldElement7.Add(eCFieldElement5);
                ECFieldElement eCFieldElement9 = eCFieldElement6.Add(eCFieldElement4);
                if (!eCFieldElement9.IsZero)
                {
                    ECFieldElement eCFieldElement11;
                    ECFieldElement y;
                    ECFieldElement eCFieldElement13;
                    if (rawXCoord.IsZero)
                    {
                        ECPoint eCPoint = this.Normalize();
                        eCFieldElement = eCPoint.XCoord;
                        ECFieldElement yCoord           = eCPoint.YCoord;
                        ECFieldElement b2               = rawYCoord2;
                        ECFieldElement eCFieldElement10 = yCoord.Add(b2).Divide(eCFieldElement);
                        eCFieldElement11 = eCFieldElement10.Square().Add(eCFieldElement10).Add(eCFieldElement).AddOne();
                        if (eCFieldElement11.IsZero)
                        {
                            return(new SecT409R1Point(curve, eCFieldElement11, curve.B.Sqrt(), base.IsCompressed));
                        }
                        ECFieldElement eCFieldElement12 = eCFieldElement10.Multiply(eCFieldElement.Add(eCFieldElement11)).Add(eCFieldElement11).Add(yCoord);
                        y = eCFieldElement12.Divide(eCFieldElement11).Add(eCFieldElement11);
                        eCFieldElement13 = curve.FromBigInteger(BigInteger.One);
                    }
                    else
                    {
                        eCFieldElement9 = eCFieldElement9.Square();
                        ECFieldElement eCFieldElement14 = eCFieldElement8.Multiply(eCFieldElement6);
                        ECFieldElement eCFieldElement15 = eCFieldElement8.Multiply(eCFieldElement4);
                        eCFieldElement11 = eCFieldElement14.Multiply(eCFieldElement15);
                        if (eCFieldElement11.IsZero)
                        {
                            return(new SecT409R1Point(curve, eCFieldElement11, curve.B.Sqrt(), base.IsCompressed));
                        }
                        ECFieldElement eCFieldElement16 = eCFieldElement8.Multiply(eCFieldElement9);
                        if (!isOne2)
                        {
                            eCFieldElement16 = eCFieldElement16.Multiply(eCFieldElement3);
                        }
                        y = eCFieldElement15.Add(eCFieldElement9).SquarePlusProduct(eCFieldElement16, rawYCoord.Add(eCFieldElement2));
                        eCFieldElement13 = eCFieldElement16;
                        if (!isOne)
                        {
                            eCFieldElement13 = eCFieldElement13.Multiply(eCFieldElement2);
                        }
                    }
                    return(new SecT409R1Point(curve, eCFieldElement11, y, new ECFieldElement[]
                    {
                        eCFieldElement13
                    }, base.IsCompressed));
                }
                if (eCFieldElement8.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }
        }
    public override ECPoint Add(ECPoint b)
    {
        if (base.IsInfinity)
        {
            return(b);
        }
        if (b.IsInfinity)
        {
            return(this);
        }
        ECCurve        curve      = Curve;
        ECFieldElement rawXCoord  = base.RawXCoord;
        ECFieldElement rawXCoord2 = b.RawXCoord;

        if (rawXCoord.IsZero)
        {
            if (rawXCoord2.IsZero)
            {
                return(curve.Infinity);
            }
            return(b.Add(this));
        }
        ECFieldElement rawYCoord       = base.RawYCoord;
        ECFieldElement eCFieldElement  = base.RawZCoords[0];
        ECFieldElement rawYCoord2      = b.RawYCoord;
        ECFieldElement eCFieldElement2 = b.RawZCoords[0];
        bool           isOne           = eCFieldElement.IsOne;
        ECFieldElement eCFieldElement3 = rawXCoord2;
        ECFieldElement eCFieldElement4 = rawYCoord2;

        if (!isOne)
        {
            eCFieldElement3 = eCFieldElement3.Multiply(eCFieldElement);
            eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement);
        }
        bool           isOne2          = eCFieldElement2.IsOne;
        ECFieldElement eCFieldElement5 = rawXCoord;
        ECFieldElement eCFieldElement6 = rawYCoord;

        if (!isOne2)
        {
            eCFieldElement5 = eCFieldElement5.Multiply(eCFieldElement2);
            eCFieldElement6 = eCFieldElement6.Multiply(eCFieldElement2);
        }
        ECFieldElement eCFieldElement7 = eCFieldElement6.Add(eCFieldElement4);
        ECFieldElement eCFieldElement8 = eCFieldElement5.Add(eCFieldElement3);

        if (eCFieldElement8.IsZero)
        {
            if (eCFieldElement7.IsZero)
            {
                return(Twice());
            }
            return(curve.Infinity);
        }
        ECFieldElement eCFieldElement10;
        ECFieldElement y;
        ECFieldElement eCFieldElement12;

        if (rawXCoord2.IsZero)
        {
            ECPoint eCPoint = Normalize();
            rawXCoord = eCPoint.XCoord;
            ECFieldElement yCoord          = eCPoint.YCoord;
            ECFieldElement b2              = rawYCoord2;
            ECFieldElement eCFieldElement9 = yCoord.Add(b2).Divide(rawXCoord);
            eCFieldElement10 = eCFieldElement9.Square().Add(eCFieldElement9).Add(rawXCoord)
                               .Add(curve.A);
            if (eCFieldElement10.IsZero)
            {
                return(new SecT131R2Point(curve, eCFieldElement10, curve.B.Sqrt(), base.IsCompressed));
            }
            ECFieldElement eCFieldElement11 = eCFieldElement9.Multiply(rawXCoord.Add(eCFieldElement10)).Add(eCFieldElement10).Add(yCoord);
            y = eCFieldElement11.Divide(eCFieldElement10).Add(eCFieldElement10);
            eCFieldElement12 = curve.FromBigInteger(BigInteger.One);
        }
        else
        {
            eCFieldElement8 = eCFieldElement8.Square();
            ECFieldElement eCFieldElement13 = eCFieldElement7.Multiply(eCFieldElement5);
            ECFieldElement eCFieldElement14 = eCFieldElement7.Multiply(eCFieldElement3);
            eCFieldElement10 = eCFieldElement13.Multiply(eCFieldElement14);
            if (eCFieldElement10.IsZero)
            {
                return(new SecT131R2Point(curve, eCFieldElement10, curve.B.Sqrt(), base.IsCompressed));
            }
            ECFieldElement eCFieldElement15 = eCFieldElement7.Multiply(eCFieldElement8);
            if (!isOne2)
            {
                eCFieldElement15 = eCFieldElement15.Multiply(eCFieldElement2);
            }
            y = eCFieldElement14.Add(eCFieldElement8).SquarePlusProduct(eCFieldElement15, rawYCoord.Add(eCFieldElement));
            eCFieldElement12 = eCFieldElement15;
            if (!isOne)
            {
                eCFieldElement12 = eCFieldElement12.Multiply(eCFieldElement);
            }
        }
        return(new SecT131R2Point(curve, eCFieldElement10, y, new ECFieldElement[1]
        {
            eCFieldElement12
        }, base.IsCompressed));
    }
Пример #5
0
        public override ECPoint Add(ECPoint b)
        {
            if (this.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }

            ECCurve curve = this.Curve;

            SecT571FieldElement X1 = (SecT571FieldElement)this.RawXCoord;
            SecT571FieldElement X2 = (SecT571FieldElement)b.RawXCoord;

            if (X1.IsZero)
            {
                if (X2.IsZero)
                {
                    return(curve.Infinity);
                }

                return(b.Add(this));
            }

            SecT571FieldElement L1 = (SecT571FieldElement)this.RawYCoord, Z1 = (SecT571FieldElement)this.RawZCoords[0];
            SecT571FieldElement L2 = (SecT571FieldElement)b.RawYCoord, Z2 = (SecT571FieldElement)b.RawZCoords[0];

            ulong[] t1 = Nat576.Create64();
            ulong[] t2 = Nat576.Create64();
            ulong[] t3 = Nat576.Create64();
            ulong[] t4 = Nat576.Create64();

            ulong[] Z1Precomp = Z1.IsOne ? null : SecT571Field.PrecompMultiplicand(Z1.x);
            ulong[] U2, S2;
            if (Z1Precomp == null)
            {
                U2 = X2.x;
                S2 = L2.x;
            }
            else
            {
                SecT571Field.MultiplyPrecomp(X2.x, Z1Precomp, U2 = t2);
                SecT571Field.MultiplyPrecomp(L2.x, Z1Precomp, S2 = t4);
            }

            ulong[] Z2Precomp = Z2.IsOne ? null : SecT571Field.PrecompMultiplicand(Z2.x);
            ulong[] U1, S1;
            if (Z2Precomp == null)
            {
                U1 = X1.x;
                S1 = L1.x;
            }
            else
            {
                SecT571Field.MultiplyPrecomp(X1.x, Z2Precomp, U1 = t1);
                SecT571Field.MultiplyPrecomp(L1.x, Z2Precomp, S1 = t3);
            }

            ulong[] A = t3;
            SecT571Field.Add(S1, S2, A);

            ulong[] B = t4;
            SecT571Field.Add(U1, U2, B);

            if (Nat576.IsZero64(B))
            {
                if (Nat576.IsZero64(A))
                {
                    return(Twice());
                }

                return(curve.Infinity);
            }

            SecT571FieldElement X3, L3, Z3;

            if (X2.IsZero)
            {
                // TODO This can probably be optimized quite a bit
                ECPoint p = this.Normalize();
                X1 = (SecT571FieldElement)p.XCoord;
                ECFieldElement Y1 = p.YCoord;

                ECFieldElement Y2 = L2;
                ECFieldElement L  = Y1.Add(Y2).Divide(X1);

                X3 = (SecT571FieldElement)L.Square().Add(L).Add(X1).AddOne();
                if (X3.IsZero)
                {
                    return(new SecT571R1Point(curve, X3, SecT571R1Curve.SecT571R1_B_SQRT, IsCompressed));
                }

                ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
                L3 = (SecT571FieldElement)Y3.Divide(X3).Add(X3);
                Z3 = (SecT571FieldElement)curve.FromBigInteger(BigInteger.One);
            }
            else
            {
                SecT571Field.Square(B, B);

                ulong[] APrecomp = SecT571Field.PrecompMultiplicand(A);

                ulong[] AU1 = t1;
                ulong[] AU2 = t2;

                SecT571Field.MultiplyPrecomp(U1, APrecomp, AU1);
                SecT571Field.MultiplyPrecomp(U2, APrecomp, AU2);

                X3 = new SecT571FieldElement(t1);
                SecT571Field.Multiply(AU1, AU2, X3.x);

                if (X3.IsZero)
                {
                    return(new SecT571R1Point(curve, X3, SecT571R1Curve.SecT571R1_B_SQRT, IsCompressed));
                }

                Z3 = new SecT571FieldElement(t3);
                SecT571Field.MultiplyPrecomp(B, APrecomp, Z3.x);

                if (Z2Precomp != null)
                {
                    SecT571Field.MultiplyPrecomp(Z3.x, Z2Precomp, Z3.x);
                }

                ulong[] tt = Nat576.CreateExt64();

                SecT571Field.Add(AU2, B, t4);
                SecT571Field.SquareAddToExt(t4, tt);

                SecT571Field.Add(L1.x, Z1.x, t4);
                SecT571Field.MultiplyAddToExt(t4, Z3.x, tt);

                L3 = new SecT571FieldElement(t4);
                SecT571Field.Reduce(tt, L3.x);

                if (Z1Precomp != null)
                {
                    SecT571Field.MultiplyPrecomp(Z3.x, Z1Precomp, Z3.x);
                }
            }

            return(new SecT571R1Point(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed));
        }