Add() public abstract method

public abstract Add ( ECFieldElement b ) : ECFieldElement
b ECFieldElement
return ECFieldElement
Exemplo n.º 1
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            //IL_009b: Unknown result type (might be due to invalid IL or missing references)
            ECFieldElement eCFieldElement  = FromBigInteger(X1);
            ECFieldElement eCFieldElement2 = null;

            if (eCFieldElement.IsZero)
            {
                eCFieldElement2 = B.Sqrt();
            }
            else
            {
                ECFieldElement beta = eCFieldElement.Square().Invert().Multiply(B)
                                      .Add(A)
                                      .Add(eCFieldElement);
                ECFieldElement eCFieldElement3 = SolveQuadradicEquation(beta);
                if (eCFieldElement3 != null)
                {
                    if (eCFieldElement3.TestBitZero() != (yTilde == 1))
                    {
                        eCFieldElement3 = eCFieldElement3.AddOne();
                    }
                    switch (CoordinateSystem)
                    {
                    case 5:
                    case 6:
                        eCFieldElement2 = eCFieldElement3.Add(eCFieldElement);
                        break;

                    default:
                        eCFieldElement2 = eCFieldElement3.Multiply(eCFieldElement);
                        break;
                    }
                }
            }
            if (eCFieldElement2 == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            return(CreateRawPoint(eCFieldElement, eCFieldElement2, withCompression: true));
        }
Exemplo n.º 2
0
        public override ECPoint ScaleY(ECFieldElement scale)
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            switch (CurveCoordinateSystem)
            {
            case 5:
            case 6:
            {
                ECFieldElement rawXCoord = base.RawXCoord;
                ECFieldElement rawYCoord = base.RawYCoord;
                ECFieldElement y         = rawYCoord.Add(rawXCoord).Multiply(scale).Add(rawXCoord);
                return(Curve.CreateRawPoint(rawXCoord, y, base.RawZCoords, base.IsCompressed));
            }

            default:
                return(base.ScaleY(scale));
            }
        }
Exemplo n.º 3
0
        /**
         * Decompresses a compressed point P = (xp, yp) (X9.62 s 4.2.2).
         *
         * @param xEnc
         *            The encoding of field element xp.
         * @param ypBit
         *            ~yp, an indication bit for the decompression of yp.
         * @return the decompressed point.
         */
        private ECPoint decompressPoint(
            byte[] xEnc,
            int ypBit)
        {
            ECFieldElement xp = new F2mFieldElement(
                this.m, this.k1, this.k2, this.k3, new BigInteger(1, xEnc));
            ECFieldElement yp = null;

            if (xp.x.SignValue == 0)
            {
                yp = (F2mFieldElement)b;
                for (int i = 0; i < m - 1; i++)
                {
                    yp = yp.Square();
                }
            }
            else
            {
                ECFieldElement beta = xp.Add(a).Add(
                    b.Multiply(xp.Square().Invert()));
                ECFieldElement z = solveQuadradicEquation(beta);
                if (z == null)
                {
                    throw new ArithmeticException("Invalid point compression");
                }
                int zBit = 0;
                if (z.x.TestBit(0))
                {
                    zBit = 1;
                }
                if (zBit != ypBit)
                {
                    z = z.Add(new F2mFieldElement(this.m, this.k1, this.k2,
                                                  this.k3, BigInteger.One));
                }
                yp = xp.Multiply(z);
            }

            return(new F2mPoint(this, xp, yp));
        }
Exemplo n.º 4
0
        public virtual int Trace()
        {
            int            m  = FieldSize;
            ECFieldElement fe = this;
            ECFieldElement tr = fe;

            for (int i = 1; i < m; ++i)
            {
                fe = fe.Square();
                tr = tr.Add(fe);
            }
            if (tr.IsZero)
            {
                return(0);
            }
            if (tr.IsOne)
            {
                return(1);
            }

            throw new InvalidOperationException("Internal error in trace calculation");
        }
Exemplo n.º 5
0
        public override ECPoint ThreeTimes()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(this);
            }
            switch (this.Curve.CoordinateSystem)
            {
            case 0:
            {
                ECFieldElement rawXCoord = base.RawXCoord;
                ECFieldElement b         = this.Two(rawYCoord);
                ECFieldElement element4  = b.Square();
                ECFieldElement element5  = this.Three(rawXCoord.Square()).Add(this.Curve.A);
                ECFieldElement element6  = element5.Square();
                ECFieldElement element7  = this.Three(rawXCoord).Multiply(element4).Subtract(element6);
                if (element7.IsZero)
                {
                    return(this.Curve.Infinity);
                }
                ECFieldElement element9  = element7.Multiply(b).Invert();
                ECFieldElement element10 = element7.Multiply(element9).Multiply(element5);
                ECFieldElement element11 = element4.Square().Multiply(element9).Subtract(element10);
                ECFieldElement x         = element11.Subtract(element10).Multiply(element10.Add(element11)).Add(rawXCoord);
                return(new FpPoint(this.Curve, x, rawXCoord.Subtract(x).Multiply(element11).Subtract(rawYCoord), base.IsCompressed));
            }

            case 4:
                return(this.TwiceJacobianModified(false).Add(this));
            }
            return(this.Twice().Add(this));
        }
Exemplo n.º 6
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement eCFieldElement  = this.FromBigInteger(X1);
            ECFieldElement eCFieldElement2 = null;

            if (eCFieldElement.IsZero)
            {
                eCFieldElement2 = this.m_b.Sqrt();
            }
            else
            {
                ECFieldElement beta            = eCFieldElement.Square().Invert().Multiply(this.B).Add(this.A).Add(eCFieldElement);
                ECFieldElement eCFieldElement3 = this.SolveQuadradicEquation(beta);
                if (eCFieldElement3 != null)
                {
                    if (eCFieldElement3.TestBitZero() != (yTilde == 1))
                    {
                        eCFieldElement3 = eCFieldElement3.AddOne();
                    }
                    switch (this.CoordinateSystem)
                    {
                    case 5:
                    case 6:
                        eCFieldElement2 = eCFieldElement3.Add(eCFieldElement);
                        break;

                    default:
                        eCFieldElement2 = eCFieldElement3.Multiply(eCFieldElement);
                        break;
                    }
                }
            }
            if (eCFieldElement2 == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            return(this.CreateRawPoint(eCFieldElement, eCFieldElement2, true));
        }
Exemplo n.º 7
0
        protected internal override ECPoint DecompressPoint(
            int yTilde,
            BigInteger X1)
        {
            ECFieldElement xp = FromBigInteger(X1);
            ECFieldElement yp = null;

            if (xp.ToBigInteger().SignValue == 0)
            {
                yp = (F2mFieldElement)b;
                for (int i = 0; i < m - 1; i++)
                {
                    yp = yp.Square();
                }
            }
            else
            {
                ECFieldElement beta = xp.Add(a).Add(
                    b.Multiply(xp.Square().Invert()));
                ECFieldElement z = solveQuadradicEquation(beta);

                if (z == null)
                {
                    throw new ArithmeticException("Invalid point compression");
                }

                int zBit = z.ToBigInteger().TestBit(0) ? 1 : 0;
                if (zBit != yTilde)
                {
                    z = z.Add(FromBigInteger(BigInteger.One));
                }

                yp = xp.Multiply(z);
            }

            return(new F2mPoint(this, xp, yp, true));
        }
Exemplo n.º 8
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement b = this.FromBigInteger(X1);
            ECFieldElement y = null;

            if (b.IsZero)
            {
                y = this.B.Sqrt();
            }
            else
            {
                ECFieldElement beta     = b.Square().Invert().Multiply(this.B).Add(this.A).Add(b);
                ECFieldElement element4 = this.SolveQuadradicEquation(beta);
                if (element4 != null)
                {
                    if (element4.TestBitZero() != (yTilde == 1))
                    {
                        element4 = element4.AddOne();
                    }
                    switch (this.CoordinateSystem)
                    {
                    case 5:
                    case 6:
                        y = element4.Add(b);
                        goto Label_00A9;
                    }
                    y = element4.Multiply(b);
                }
            }
Label_00A9:
            if (y == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            return(this.CreateRawPoint(b, y, true));
        }
Exemplo n.º 9
0
        public override ECPoint ThreeTimes()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(this);
            }
            ECCurve curve = Curve;

            switch (curve.CoordinateSystem)
            {
            case 0:
            {
                ECFieldElement rawXCoord       = base.RawXCoord;
                ECFieldElement eCFieldElement  = Two(rawYCoord);
                ECFieldElement eCFieldElement2 = eCFieldElement.Square();
                ECFieldElement eCFieldElement3 = Three(rawXCoord.Square()).Add(Curve.A);
                ECFieldElement b = eCFieldElement3.Square();
                ECFieldElement eCFieldElement4 = Three(rawXCoord).Multiply(eCFieldElement2).Subtract(b);
                if (eCFieldElement4.IsZero)
                {
                    return(Curve.Infinity);
                }
                ECFieldElement eCFieldElement5 = eCFieldElement4.Multiply(eCFieldElement);
                ECFieldElement b2 = eCFieldElement5.Invert();
                ECFieldElement eCFieldElement6 = eCFieldElement4.Multiply(b2).Multiply(eCFieldElement3);
                ECFieldElement eCFieldElement7 = eCFieldElement2.Square().Multiply(b2).Subtract(eCFieldElement6);
                ECFieldElement eCFieldElement8 = eCFieldElement7.Subtract(eCFieldElement6).Multiply(eCFieldElement6.Add(eCFieldElement7)).Add(rawXCoord);
                ECFieldElement y = rawXCoord.Subtract(eCFieldElement8).Multiply(eCFieldElement7).Subtract(rawYCoord);
                return(new FpPoint(Curve, eCFieldElement8, y, base.IsCompressed));
            }

            case 4:
                return(TwiceJacobianModified(calculateW: false).Add(this));

            default:
                return(Twice().Add(this));
            }
        }
Exemplo n.º 10
0
        public override ECPoint Twice()
        {
            //IL_0386: Unknown result type (might be due to invalid IL or missing references)
            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = Curve;
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(curve.Infinity);
            }
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement x3 = rawXCoord.Square();
                ECFieldElement eCFieldElement10 = Three(x3).Add(Curve.A).Divide(Two(rawYCoord));
                ECFieldElement eCFieldElement11 = eCFieldElement10.Square().Subtract(Two(rawXCoord));
                ECFieldElement y2 = eCFieldElement10.Multiply(rawXCoord.Subtract(eCFieldElement11)).Subtract(rawYCoord);
                return(new FpPoint(Curve, eCFieldElement11, y2, base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement eCFieldElement12 = base.RawZCoords[0];
                bool           isOne2           = eCFieldElement12.IsOne;
                ECFieldElement eCFieldElement13 = curve.A;
                if (!eCFieldElement13.IsZero && !isOne2)
                {
                    eCFieldElement13 = eCFieldElement13.Multiply(eCFieldElement12.Square());
                }
                eCFieldElement13 = eCFieldElement13.Add(Three(rawXCoord.Square()));
                ECFieldElement eCFieldElement14 = (isOne2 ? rawYCoord : rawYCoord.Multiply(eCFieldElement12));
                ECFieldElement eCFieldElement15 = (isOne2 ? rawYCoord.Square() : eCFieldElement14.Multiply(rawYCoord));
                ECFieldElement x4 = rawXCoord.Multiply(eCFieldElement15);
                ECFieldElement eCFieldElement16 = Four(x4);
                ECFieldElement eCFieldElement17 = eCFieldElement13.Square().Subtract(Two(eCFieldElement16));
                ECFieldElement eCFieldElement18 = Two(eCFieldElement14);
                ECFieldElement x5 = eCFieldElement17.Multiply(eCFieldElement18);
                ECFieldElement eCFieldElement19 = Two(eCFieldElement15);
                ECFieldElement y3 = eCFieldElement16.Subtract(eCFieldElement17).Multiply(eCFieldElement13).Subtract(Two(eCFieldElement19.Square()));
                ECFieldElement x6 = (isOne2 ? Two(eCFieldElement19) : eCFieldElement18.Square());
                ECFieldElement eCFieldElement20 = Two(x6).Multiply(eCFieldElement14);
                return(new FpPoint(curve, x5, y3, new ECFieldElement[1] {
                        eCFieldElement20
                    }, base.IsCompressed));
            }

            case 2:
            {
                ECFieldElement eCFieldElement  = base.RawZCoords[0];
                bool           isOne           = eCFieldElement.IsOne;
                ECFieldElement eCFieldElement2 = rawYCoord.Square();
                ECFieldElement x = eCFieldElement2.Square();
                ECFieldElement a = curve.A;
                ECFieldElement eCFieldElement3 = a.Negate();
                ECFieldElement eCFieldElement4;
                ECFieldElement eCFieldElement5;
                if (eCFieldElement3.ToBigInteger().Equals(BigInteger.ValueOf(3L)))
                {
                    ECFieldElement b = (isOne ? eCFieldElement : eCFieldElement.Square());
                    eCFieldElement4 = Three(rawXCoord.Add(b).Multiply(rawXCoord.Subtract(b)));
                    eCFieldElement5 = Four(eCFieldElement2.Multiply(rawXCoord));
                }
                else
                {
                    ECFieldElement x2 = rawXCoord.Square();
                    eCFieldElement4 = Three(x2);
                    if (isOne)
                    {
                        eCFieldElement4 = eCFieldElement4.Add(a);
                    }
                    else if (!a.IsZero)
                    {
                        ECFieldElement eCFieldElement6 = (isOne ? eCFieldElement : eCFieldElement.Square());
                        ECFieldElement eCFieldElement7 = eCFieldElement6.Square();
                        eCFieldElement4 = ((eCFieldElement3.BitLength >= a.BitLength) ? eCFieldElement4.Add(eCFieldElement7.Multiply(a)) : eCFieldElement4.Subtract(eCFieldElement7.Multiply(eCFieldElement3)));
                    }
                    eCFieldElement5 = Four(rawXCoord.Multiply(eCFieldElement2));
                }
                ECFieldElement eCFieldElement8 = eCFieldElement4.Square().Subtract(Two(eCFieldElement5));
                ECFieldElement y = eCFieldElement5.Subtract(eCFieldElement8).Multiply(eCFieldElement4).Subtract(Eight(x));
                ECFieldElement eCFieldElement9 = Two(rawYCoord);
                if (!isOne)
                {
                    eCFieldElement9 = eCFieldElement9.Multiply(eCFieldElement);
                }
                return(new FpPoint(curve, eCFieldElement8, y, new ECFieldElement[1] {
                        eCFieldElement9
                    }, base.IsCompressed));
            }

            case 4:
                return(TwiceJacobianModified(calculateW: true));

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
        }
Exemplo n.º 11
0
        protected override bool SatisfiesCurveEquation()
        {
            //IL_0167: Unknown result type (might be due to invalid IL or missing references)
            ECCurve        curve            = Curve;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawYCoord        = base.RawYCoord;
            ECFieldElement eCFieldElement   = curve.A;
            ECFieldElement eCFieldElement2  = curve.B;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement eCFieldElement5;
            ECFieldElement eCFieldElement4;

            if (coordinateSystem == 6)
            {
                ECFieldElement eCFieldElement3 = base.RawZCoords[0];
                bool           isOne           = eCFieldElement3.IsOne;
                if (rawXCoord.IsZero)
                {
                    eCFieldElement4 = rawYCoord.Square();
                    eCFieldElement5 = eCFieldElement2;
                    if (!isOne)
                    {
                        ECFieldElement b = eCFieldElement3.Square();
                        eCFieldElement5 = eCFieldElement5.Multiply(b);
                    }
                }
                else
                {
                    ECFieldElement eCFieldElement6 = rawYCoord;
                    ECFieldElement eCFieldElement7 = rawXCoord.Square();
                    if (isOne)
                    {
                        eCFieldElement4 = eCFieldElement6.Square().Add(eCFieldElement6).Add(eCFieldElement);
                        eCFieldElement5 = eCFieldElement7.Square().Add(eCFieldElement2);
                    }
                    else
                    {
                        ECFieldElement eCFieldElement8 = eCFieldElement3.Square();
                        ECFieldElement y = eCFieldElement8.Square();
                        eCFieldElement4 = eCFieldElement6.Add(eCFieldElement3).MultiplyPlusProduct(eCFieldElement6, eCFieldElement, eCFieldElement8);
                        eCFieldElement5 = eCFieldElement7.SquarePlusProduct(eCFieldElement2, y);
                    }
                    eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement7);
                }
            }
            else
            {
                eCFieldElement4 = rawYCoord.Add(rawXCoord).Multiply(rawYCoord);
                switch (coordinateSystem)
                {
                case 1:
                {
                    ECFieldElement eCFieldElement9 = base.RawZCoords[0];
                    if (!eCFieldElement9.IsOne)
                    {
                        ECFieldElement b2 = eCFieldElement9.Square();
                        ECFieldElement b3 = eCFieldElement9.Multiply(b2);
                        eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement9);
                        eCFieldElement  = eCFieldElement.Multiply(eCFieldElement9);
                        eCFieldElement2 = eCFieldElement2.Multiply(b3);
                    }
                    break;
                }

                default:
                    throw new InvalidOperationException("unsupported coordinate system");

                case 0:
                    break;
                }
                eCFieldElement5 = rawXCoord.Add(eCFieldElement).Multiply(rawXCoord.Square()).Add(eCFieldElement2);
            }
            return(eCFieldElement4.Equals(eCFieldElement5));
        }
Exemplo n.º 12
0
        public override ECPoint Add(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve            = this.Curve;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawXCoord2       = b.RawXCoord;
            int            num = coordinateSystem;

            switch (num)
            {
            case 0:
            {
                ECFieldElement rawYCoord       = base.RawYCoord;
                ECFieldElement rawYCoord2      = b.RawYCoord;
                ECFieldElement eCFieldElement  = rawXCoord.Add(rawXCoord2);
                ECFieldElement eCFieldElement2 = rawYCoord.Add(rawYCoord2);
                if (!eCFieldElement.IsZero)
                {
                    ECFieldElement eCFieldElement3 = eCFieldElement2.Divide(eCFieldElement);
                    ECFieldElement eCFieldElement4 = eCFieldElement3.Square().Add(eCFieldElement3).Add(eCFieldElement).Add(curve.A);
                    ECFieldElement y = eCFieldElement3.Multiply(rawXCoord.Add(eCFieldElement4)).Add(eCFieldElement4).Add(rawYCoord);
                    return(new F2mPoint(curve, eCFieldElement4, y, base.IsCompressed));
                }
                if (eCFieldElement2.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }

            case 1:
            {
                ECFieldElement rawYCoord3      = base.RawYCoord;
                ECFieldElement eCFieldElement5 = base.RawZCoords[0];
                ECFieldElement rawYCoord4      = b.RawYCoord;
                ECFieldElement eCFieldElement6 = b.RawZCoords[0];
                bool           isOne           = eCFieldElement5.IsOne;
                ECFieldElement eCFieldElement7 = rawYCoord4;
                ECFieldElement eCFieldElement8 = rawXCoord2;
                if (!isOne)
                {
                    eCFieldElement7 = eCFieldElement7.Multiply(eCFieldElement5);
                    eCFieldElement8 = eCFieldElement8.Multiply(eCFieldElement5);
                }
                bool           isOne2           = eCFieldElement6.IsOne;
                ECFieldElement eCFieldElement9  = rawYCoord3;
                ECFieldElement eCFieldElement10 = rawXCoord;
                if (!isOne2)
                {
                    eCFieldElement9  = eCFieldElement9.Multiply(eCFieldElement6);
                    eCFieldElement10 = eCFieldElement10.Multiply(eCFieldElement6);
                }
                ECFieldElement eCFieldElement11 = eCFieldElement7.Add(eCFieldElement9);
                ECFieldElement eCFieldElement12 = eCFieldElement8.Add(eCFieldElement10);
                if (!eCFieldElement12.IsZero)
                {
                    ECFieldElement eCFieldElement13 = eCFieldElement12.Square();
                    ECFieldElement eCFieldElement14 = eCFieldElement13.Multiply(eCFieldElement12);
                    ECFieldElement b2 = isOne ? eCFieldElement6 : (isOne2 ? eCFieldElement5 : eCFieldElement5.Multiply(eCFieldElement6));
                    ECFieldElement eCFieldElement15 = eCFieldElement11.Add(eCFieldElement12);
                    ECFieldElement eCFieldElement16 = eCFieldElement15.MultiplyPlusProduct(eCFieldElement11, eCFieldElement13, curve.A).Multiply(b2).Add(eCFieldElement14);
                    ECFieldElement x  = eCFieldElement12.Multiply(eCFieldElement16);
                    ECFieldElement b3 = isOne2 ? eCFieldElement13 : eCFieldElement13.Multiply(eCFieldElement6);
                    ECFieldElement y2 = eCFieldElement11.MultiplyPlusProduct(rawXCoord, eCFieldElement12, rawYCoord3).MultiplyPlusProduct(b3, eCFieldElement15, eCFieldElement16);
                    ECFieldElement eCFieldElement17 = eCFieldElement14.Multiply(b2);
                    return(new F2mPoint(curve, x, y2, new ECFieldElement[]
                        {
                            eCFieldElement17
                        }, base.IsCompressed));
                }
                if (eCFieldElement11.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }

            default:
                if (num != 6)
                {
                    throw new InvalidOperationException("unsupported coordinate system");
                }
                if (rawXCoord.IsZero)
                {
                    if (rawXCoord2.IsZero)
                    {
                        return(curve.Infinity);
                    }
                    return(b.Add(this));
                }
                else
                {
                    ECFieldElement rawYCoord5       = base.RawYCoord;
                    ECFieldElement eCFieldElement18 = base.RawZCoords[0];
                    ECFieldElement rawYCoord6       = b.RawYCoord;
                    ECFieldElement eCFieldElement19 = b.RawZCoords[0];
                    bool           isOne3           = eCFieldElement18.IsOne;
                    ECFieldElement eCFieldElement20 = rawXCoord2;
                    ECFieldElement eCFieldElement21 = rawYCoord6;
                    if (!isOne3)
                    {
                        eCFieldElement20 = eCFieldElement20.Multiply(eCFieldElement18);
                        eCFieldElement21 = eCFieldElement21.Multiply(eCFieldElement18);
                    }
                    bool           isOne4           = eCFieldElement19.IsOne;
                    ECFieldElement eCFieldElement22 = rawXCoord;
                    ECFieldElement eCFieldElement23 = rawYCoord5;
                    if (!isOne4)
                    {
                        eCFieldElement22 = eCFieldElement22.Multiply(eCFieldElement19);
                        eCFieldElement23 = eCFieldElement23.Multiply(eCFieldElement19);
                    }
                    ECFieldElement eCFieldElement24 = eCFieldElement23.Add(eCFieldElement21);
                    ECFieldElement eCFieldElement25 = eCFieldElement22.Add(eCFieldElement20);
                    if (!eCFieldElement25.IsZero)
                    {
                        ECFieldElement eCFieldElement27;
                        ECFieldElement y3;
                        ECFieldElement eCFieldElement29;
                        if (rawXCoord2.IsZero)
                        {
                            ECPoint eCPoint = this.Normalize();
                            rawXCoord = eCPoint.RawXCoord;
                            ECFieldElement yCoord           = eCPoint.YCoord;
                            ECFieldElement b4               = rawYCoord6;
                            ECFieldElement eCFieldElement26 = yCoord.Add(b4).Divide(rawXCoord);
                            eCFieldElement27 = eCFieldElement26.Square().Add(eCFieldElement26).Add(rawXCoord).Add(curve.A);
                            if (eCFieldElement27.IsZero)
                            {
                                return(new F2mPoint(curve, eCFieldElement27, curve.B.Sqrt(), base.IsCompressed));
                            }
                            ECFieldElement eCFieldElement28 = eCFieldElement26.Multiply(rawXCoord.Add(eCFieldElement27)).Add(eCFieldElement27).Add(yCoord);
                            y3 = eCFieldElement28.Divide(eCFieldElement27).Add(eCFieldElement27);
                            eCFieldElement29 = curve.FromBigInteger(BigInteger.One);
                        }
                        else
                        {
                            eCFieldElement25 = eCFieldElement25.Square();
                            ECFieldElement eCFieldElement30 = eCFieldElement24.Multiply(eCFieldElement22);
                            ECFieldElement eCFieldElement31 = eCFieldElement24.Multiply(eCFieldElement20);
                            eCFieldElement27 = eCFieldElement30.Multiply(eCFieldElement31);
                            if (eCFieldElement27.IsZero)
                            {
                                return(new F2mPoint(curve, eCFieldElement27, curve.B.Sqrt(), base.IsCompressed));
                            }
                            ECFieldElement eCFieldElement32 = eCFieldElement24.Multiply(eCFieldElement25);
                            if (!isOne4)
                            {
                                eCFieldElement32 = eCFieldElement32.Multiply(eCFieldElement19);
                            }
                            y3 = eCFieldElement31.Add(eCFieldElement25).SquarePlusProduct(eCFieldElement32, rawYCoord5.Add(eCFieldElement18));
                            eCFieldElement29 = eCFieldElement32;
                            if (!isOne3)
                            {
                                eCFieldElement29 = eCFieldElement29.Multiply(eCFieldElement18);
                            }
                        }
                        return(new F2mPoint(curve, eCFieldElement27, y3, new ECFieldElement[]
                        {
                            eCFieldElement29
                        }, base.IsCompressed));
                    }
                    if (eCFieldElement24.IsZero)
                    {
                        return(this.Twice());
                    }
                    return(curve.Infinity);
                }
                break;
            }
        }
Exemplo n.º 13
0
 protected virtual ECFieldElement DoubleProductFromSquares(ECFieldElement a, ECFieldElement b, ECFieldElement aSquared, ECFieldElement bSquared)
 {
     return(a.Add(b).Square().Subtract(aSquared)
            .Subtract(bSquared));
 }
Exemplo n.º 14
0
        public override ECPoint TwicePlus(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(Twice());
            }
            ECCurve        curve     = Curve;
            ECFieldElement rawXCoord = base.RawXCoord;

            if (rawXCoord.IsZero)
            {
                return(b);
            }
            int coordinateSystem = curve.CoordinateSystem;
            int num = coordinateSystem;

            if (num == 6)
            {
                ECFieldElement rawXCoord2     = b.RawXCoord;
                ECFieldElement eCFieldElement = b.RawZCoords[0];
                if (rawXCoord2.IsZero || !eCFieldElement.IsOne)
                {
                    return(Twice().Add(b));
                }
                ECFieldElement rawYCoord       = base.RawYCoord;
                ECFieldElement eCFieldElement2 = base.RawZCoords[0];
                ECFieldElement rawYCoord2      = b.RawYCoord;
                ECFieldElement x  = rawXCoord.Square();
                ECFieldElement b2 = rawYCoord.Square();
                ECFieldElement eCFieldElement3 = eCFieldElement2.Square();
                ECFieldElement b3 = rawYCoord.Multiply(eCFieldElement2);
                ECFieldElement b4 = curve.A.Multiply(eCFieldElement3).Add(b2).Add(b3);
                ECFieldElement eCFieldElement4 = rawYCoord2.AddOne();
                ECFieldElement eCFieldElement5 = curve.A.Add(eCFieldElement4).Multiply(eCFieldElement3).Add(b2)
                                                 .MultiplyPlusProduct(b4, x, eCFieldElement3);
                ECFieldElement eCFieldElement6 = rawXCoord2.Multiply(eCFieldElement3);
                ECFieldElement eCFieldElement7 = eCFieldElement6.Add(b4).Square();
                if (eCFieldElement7.IsZero)
                {
                    if (eCFieldElement5.IsZero)
                    {
                        return(b.Twice());
                    }
                    return(curve.Infinity);
                }
                if (eCFieldElement5.IsZero)
                {
                    return(new F2mPoint(curve, eCFieldElement5, curve.B.Sqrt(), base.IsCompressed));
                }
                ECFieldElement x2 = eCFieldElement5.Square().Multiply(eCFieldElement6);
                ECFieldElement eCFieldElement8 = eCFieldElement5.Multiply(eCFieldElement7).Multiply(eCFieldElement3);
                ECFieldElement y = eCFieldElement5.Add(eCFieldElement7).Square().MultiplyPlusProduct(b4, eCFieldElement4, eCFieldElement8);
                return(new F2mPoint(curve, x2, y, new ECFieldElement[1]
                {
                    eCFieldElement8
                }, base.IsCompressed));
            }
            return(Twice().Add(b));
        }
Exemplo n.º 15
0
        protected override bool SatisfiesCurveEquation()
        {
            ECFieldElement element5;
            ECFieldElement element6;
            ECCurve        curve            = this.Curve;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawYCoord        = base.RawYCoord;
            ECFieldElement a                = curve.A;
            ECFieldElement b                = curve.B;
            int            coordinateSystem = curve.CoordinateSystem;

            if (coordinateSystem == 6)
            {
                ECFieldElement element7 = base.RawZCoords[0];
                bool           isOne    = element7.IsOne;
                if (rawXCoord.IsZero)
                {
                    element5 = rawYCoord.Square();
                    element6 = b;
                    if (!isOne)
                    {
                        ECFieldElement element8 = element7.Square();
                        element6 = element6.Multiply(element8);
                    }
                }
                else
                {
                    ECFieldElement element9  = rawYCoord;
                    ECFieldElement element10 = rawXCoord.Square();
                    if (isOne)
                    {
                        element5 = element9.Square().Add(element9).Add(a);
                        element6 = element10.Square().Add(b);
                    }
                    else
                    {
                        ECFieldElement y         = element7.Square();
                        ECFieldElement element12 = y.Square();
                        element5 = element9.Add(element7).MultiplyPlusProduct(element9, a, y);
                        element6 = element10.SquarePlusProduct(b, element12);
                    }
                    element5 = element5.Multiply(element10);
                }
            }
            else
            {
                element5 = rawYCoord.Add(rawXCoord).Multiply(rawYCoord);
                if (coordinateSystem != 0)
                {
                    if (coordinateSystem != 1)
                    {
                        throw new InvalidOperationException("unsupported coordinate system");
                    }
                    ECFieldElement element13 = base.RawZCoords[0];
                    if (!element13.IsOne)
                    {
                        ECFieldElement element14 = element13.Square();
                        ECFieldElement element15 = element13.Multiply(element14);
                        element5 = element5.Multiply(element13);
                        a        = a.Multiply(element13);
                        b        = b.Multiply(element15);
                    }
                }
                element6 = rawXCoord.Add(a).Multiply(rawXCoord.Square()).Add(b);
            }
            return(element5.Equals(element6));
        }
Exemplo n.º 16
0
        public override ECPoint TimesPow2(int e)
        {
            //IL_000e: Unknown result type (might be due to invalid IL or missing references)
            //IL_029e: Unknown result type (might be due to invalid IL or missing references)
            if (e < 0)
            {
                throw new ArgumentException("cannot be negative", "e");
            }
            if (e == 0 || base.IsInfinity)
            {
                return(this);
            }
            if (e == 1)
            {
                return(Twice());
            }
            ECCurve        curve          = Curve;
            ECFieldElement eCFieldElement = base.RawYCoord;

            if (eCFieldElement.IsZero)
            {
                return(curve.Infinity);
            }
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement eCFieldElement2  = curve.A;
            ECFieldElement eCFieldElement3  = base.RawXCoord;
            ECFieldElement eCFieldElement4  = ((base.RawZCoords.Length < 1) ? curve.FromBigInteger(BigInteger.One) : base.RawZCoords[0]);

            if (!eCFieldElement4.IsOne)
            {
                switch (coordinateSystem)
                {
                case 1:
                {
                    ECFieldElement eCFieldElement5 = eCFieldElement4.Square();
                    eCFieldElement3 = eCFieldElement3.Multiply(eCFieldElement4);
                    eCFieldElement  = eCFieldElement.Multiply(eCFieldElement5);
                    eCFieldElement2 = CalculateJacobianModifiedW(eCFieldElement4, eCFieldElement5);
                    break;
                }

                case 2:
                    eCFieldElement2 = CalculateJacobianModifiedW(eCFieldElement4, null);
                    break;

                case 4:
                    eCFieldElement2 = GetJacobianModifiedW();
                    break;
                }
            }
            for (int i = 0; i < e; i++)
            {
                if (eCFieldElement.IsZero)
                {
                    return(curve.Infinity);
                }
                ECFieldElement x = eCFieldElement3.Square();
                ECFieldElement eCFieldElement6 = Three(x);
                ECFieldElement eCFieldElement7 = Two(eCFieldElement);
                ECFieldElement eCFieldElement8 = eCFieldElement7.Multiply(eCFieldElement);
                ECFieldElement eCFieldElement9 = Two(eCFieldElement3.Multiply(eCFieldElement8));
                ECFieldElement x2 = eCFieldElement8.Square();
                ECFieldElement eCFieldElement10 = Two(x2);
                if (!eCFieldElement2.IsZero)
                {
                    eCFieldElement6 = eCFieldElement6.Add(eCFieldElement2);
                    eCFieldElement2 = Two(eCFieldElement10.Multiply(eCFieldElement2));
                }
                eCFieldElement3 = eCFieldElement6.Square().Subtract(Two(eCFieldElement9));
                eCFieldElement  = eCFieldElement6.Multiply(eCFieldElement9.Subtract(eCFieldElement3)).Subtract(eCFieldElement10);
                eCFieldElement4 = (eCFieldElement4.IsOne ? eCFieldElement7 : eCFieldElement7.Multiply(eCFieldElement4));
            }
            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement eCFieldElement11 = eCFieldElement4.Invert();
                ECFieldElement eCFieldElement12 = eCFieldElement11.Square();
                ECFieldElement b = eCFieldElement12.Multiply(eCFieldElement11);
                return(new FpPoint(curve, eCFieldElement3.Multiply(eCFieldElement12), eCFieldElement.Multiply(b), base.IsCompressed));
            }

            case 1:
                eCFieldElement3 = eCFieldElement3.Multiply(eCFieldElement4);
                eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement4.Square());
                return(new FpPoint(curve, eCFieldElement3, eCFieldElement, new ECFieldElement[1] {
                    eCFieldElement4
                }, base.IsCompressed));

            case 2:
                return(new FpPoint(curve, eCFieldElement3, eCFieldElement, new ECFieldElement[1] {
                    eCFieldElement4
                }, base.IsCompressed));

            case 4:
                return(new FpPoint(curve, eCFieldElement3, eCFieldElement, new ECFieldElement[2] {
                    eCFieldElement4, eCFieldElement2
                }, base.IsCompressed));

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
        }
Exemplo n.º 17
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement eCFieldElement  = FromBigInteger(X1);
            ECFieldElement eCFieldElement2 = null;

            if (eCFieldElement.IsZero)
            {
                eCFieldElement2 = m_b.Sqrt();
            }
            else
            {
                ECFieldElement beta = eCFieldElement.Square().Invert().Multiply(B)
                                      .Add(A)
                                      .Add(eCFieldElement);
                ECFieldElement eCFieldElement3 = SolveQuadradicEquation(beta);
                if (eCFieldElement3 != null)
                {
                    if (eCFieldElement3.TestBitZero() != (yTilde == 1))
                    {
                        eCFieldElement3 = eCFieldElement3.AddOne();
                    }
                    int coordinateSystem = CoordinateSystem;
                    eCFieldElement2 = ((coordinateSystem != 5 && coordinateSystem != 6) ? eCFieldElement3.Multiply(eCFieldElement) : eCFieldElement3.Add(eCFieldElement));
                }
            }
            if (eCFieldElement2 == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            return(CreateRawPoint(eCFieldElement, eCFieldElement2, withCompression: true));
        }
Exemplo n.º 18
0
        public override ECPoint ThreeTimes()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(this);
            }
            ECCurve curve            = this.Curve;
            int     coordinateSystem = curve.CoordinateSystem;
            int     num = coordinateSystem;

            if (num != 0)
            {
                if (num != 4)
                {
                    return(this.Twice().Add(this));
                }
                return(this.TwiceJacobianModified(false).Add(this));
            }
            else
            {
                ECFieldElement rawXCoord       = base.RawXCoord;
                ECFieldElement eCFieldElement  = this.Two(rawYCoord);
                ECFieldElement eCFieldElement2 = eCFieldElement.Square();
                ECFieldElement eCFieldElement3 = this.Three(rawXCoord.Square()).Add(this.Curve.A);
                ECFieldElement b = eCFieldElement3.Square();
                ECFieldElement eCFieldElement4 = this.Three(rawXCoord).Multiply(eCFieldElement2).Subtract(b);
                if (eCFieldElement4.IsZero)
                {
                    return(this.Curve.Infinity);
                }
                ECFieldElement eCFieldElement5 = eCFieldElement4.Multiply(eCFieldElement);
                ECFieldElement b2 = eCFieldElement5.Invert();
                ECFieldElement eCFieldElement6 = eCFieldElement4.Multiply(b2).Multiply(eCFieldElement3);
                ECFieldElement eCFieldElement7 = eCFieldElement2.Square().Multiply(b2).Subtract(eCFieldElement6);
                ECFieldElement eCFieldElement8 = eCFieldElement7.Subtract(eCFieldElement6).Multiply(eCFieldElement6.Add(eCFieldElement7)).Add(rawXCoord);
                ECFieldElement y = rawXCoord.Subtract(eCFieldElement8).Multiply(eCFieldElement7).Subtract(rawYCoord);
                return(new FpPoint(this.Curve, eCFieldElement8, y, base.IsCompressed));
            }
        }
Exemplo n.º 19
0
        public override ECPoint Twice()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = this.Curve;
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(curve.Infinity);
            }
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement x = rawXCoord.Square();
                ECFieldElement eCFieldElement  = this.Three(x).Add(this.Curve.A).Divide(this.Two(rawYCoord));
                ECFieldElement eCFieldElement2 = eCFieldElement.Square().Subtract(this.Two(rawXCoord));
                ECFieldElement y = eCFieldElement.Multiply(rawXCoord.Subtract(eCFieldElement2)).Subtract(rawYCoord);
                return(new FpPoint(this.Curve, eCFieldElement2, y, base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement eCFieldElement3 = base.RawZCoords[0];
                bool           isOne           = eCFieldElement3.IsOne;
                ECFieldElement eCFieldElement4 = curve.A;
                if (!eCFieldElement4.IsZero && !isOne)
                {
                    eCFieldElement4 = eCFieldElement4.Multiply(eCFieldElement3.Square());
                }
                eCFieldElement4 = eCFieldElement4.Add(this.Three(rawXCoord.Square()));
                ECFieldElement eCFieldElement5 = isOne ? rawYCoord : rawYCoord.Multiply(eCFieldElement3);
                ECFieldElement eCFieldElement6 = isOne ? rawYCoord.Square() : eCFieldElement5.Multiply(rawYCoord);
                ECFieldElement x2 = rawXCoord.Multiply(eCFieldElement6);
                ECFieldElement eCFieldElement7 = this.Four(x2);
                ECFieldElement eCFieldElement8 = eCFieldElement4.Square().Subtract(this.Two(eCFieldElement7));
                ECFieldElement eCFieldElement9 = this.Two(eCFieldElement5);
                ECFieldElement x3 = eCFieldElement8.Multiply(eCFieldElement9);
                ECFieldElement eCFieldElement10 = this.Two(eCFieldElement6);
                ECFieldElement y2 = eCFieldElement7.Subtract(eCFieldElement8).Multiply(eCFieldElement4).Subtract(this.Two(eCFieldElement10.Square()));
                ECFieldElement x4 = isOne ? this.Two(eCFieldElement10) : eCFieldElement9.Square();
                ECFieldElement eCFieldElement11 = this.Two(x4).Multiply(eCFieldElement5);
                return(new FpPoint(curve, x3, y2, new ECFieldElement[]
                    {
                        eCFieldElement11
                    }, base.IsCompressed));
            }

            case 2:
            {
                ECFieldElement eCFieldElement12 = base.RawZCoords[0];
                bool           isOne2           = eCFieldElement12.IsOne;
                ECFieldElement eCFieldElement13 = rawYCoord.Square();
                ECFieldElement x5 = eCFieldElement13.Square();
                ECFieldElement a  = curve.A;
                ECFieldElement eCFieldElement14 = a.Negate();
                ECFieldElement eCFieldElement15;
                ECFieldElement eCFieldElement16;
                if (eCFieldElement14.ToBigInteger().Equals(BigInteger.ValueOf(3L)))
                {
                    ECFieldElement b = isOne2 ? eCFieldElement12 : eCFieldElement12.Square();
                    eCFieldElement15 = this.Three(rawXCoord.Add(b).Multiply(rawXCoord.Subtract(b)));
                    eCFieldElement16 = this.Four(eCFieldElement13.Multiply(rawXCoord));
                }
                else
                {
                    ECFieldElement x6 = rawXCoord.Square();
                    eCFieldElement15 = this.Three(x6);
                    if (isOne2)
                    {
                        eCFieldElement15 = eCFieldElement15.Add(a);
                    }
                    else if (!a.IsZero)
                    {
                        ECFieldElement eCFieldElement17 = isOne2 ? eCFieldElement12 : eCFieldElement12.Square();
                        ECFieldElement eCFieldElement18 = eCFieldElement17.Square();
                        if (eCFieldElement14.BitLength < a.BitLength)
                        {
                            eCFieldElement15 = eCFieldElement15.Subtract(eCFieldElement18.Multiply(eCFieldElement14));
                        }
                        else
                        {
                            eCFieldElement15 = eCFieldElement15.Add(eCFieldElement18.Multiply(a));
                        }
                    }
                    eCFieldElement16 = this.Four(rawXCoord.Multiply(eCFieldElement13));
                }
                ECFieldElement eCFieldElement19 = eCFieldElement15.Square().Subtract(this.Two(eCFieldElement16));
                ECFieldElement y3 = eCFieldElement16.Subtract(eCFieldElement19).Multiply(eCFieldElement15).Subtract(this.Eight(x5));
                ECFieldElement eCFieldElement20 = this.Two(rawYCoord);
                if (!isOne2)
                {
                    eCFieldElement20 = eCFieldElement20.Multiply(eCFieldElement12);
                }
                return(new FpPoint(curve, eCFieldElement19, y3, new ECFieldElement[]
                    {
                        eCFieldElement20
                    }, base.IsCompressed));
            }

            case 4:
                return(this.TwiceJacobianModified(true));
            }
            throw new InvalidOperationException("unsupported coordinate system");
        }
Exemplo n.º 20
0
        public override ECPoint Twice()
        {
            ECFieldElement element26;
            ECFieldElement element27;
            ECFieldElement element29;

            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = this.Curve;
            ECFieldElement rawXCoord = base.RawXCoord;

            if (rawXCoord.IsZero)
            {
                return(curve.Infinity);
            }
            switch (curve.CoordinateSystem)
            {
            case 0:
            {
                ECFieldElement b = base.RawYCoord.Divide(rawXCoord).Add(rawXCoord);
                ECFieldElement x = b.Square().Add(b).Add(curve.A);
                return(new F2mPoint(curve, x, rawXCoord.SquarePlusProduct(x, b.AddOne()), base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement rawYCoord = base.RawYCoord;
                ECFieldElement b         = base.RawZCoords[0];
                bool           isOne     = b.IsOne;
                ECFieldElement element8  = !isOne?rawXCoord.Multiply(b) : rawXCoord;

                ECFieldElement element9 = !isOne?rawYCoord.Multiply(b) : rawYCoord;

                ECFieldElement element10 = rawXCoord.Square();
                ECFieldElement element11 = element10.Add(element9);
                ECFieldElement element12 = element8;
                ECFieldElement x         = element12.Square();
                ECFieldElement y         = element11.Add(element12);
                ECFieldElement element15 = y.MultiplyPlusProduct(element11, x, curve.A);
                ECFieldElement element16 = element12.Multiply(element15);
                ECFieldElement element17 = element10.Square().MultiplyPlusProduct(element12, element15, y);
                ECFieldElement element18 = element12.Multiply(x);
                return(new F2mPoint(curve, element16, element17, new ECFieldElement[] { element18 }, base.IsCompressed));
            }

            case 6:
            {
                ECFieldElement element31;
                ECFieldElement rawYCoord = base.RawYCoord;
                ECFieldElement b         = base.RawZCoords[0];
                bool           isOne     = b.IsOne;
                ECFieldElement element21 = !isOne?rawYCoord.Multiply(b) : rawYCoord;

                ECFieldElement element22 = !isOne?b.Square() : b;

                ECFieldElement a         = curve.A;
                ECFieldElement element24 = !isOne?a.Multiply(element22) : a;

                ECFieldElement x = rawYCoord.Square().Add(element21).Add(element24);
                if (x.IsZero)
                {
                    return(new F2mPoint(curve, x, curve.B.Sqrt(), base.IsCompressed));
                }
                element26 = x.Square();
                element27 = !isOne?x.Multiply(element22) : x;

                ECFieldElement element28 = curve.B;
                if (element28.BitLength >= (curve.FieldSize >> 1))
                {
                    ECFieldElement element32 = !isOne?rawXCoord.Multiply(b) : rawXCoord;

                    element29 = element32.SquarePlusProduct(x, element21).Add(element26).Add(element27);
                    break;
                }
                ECFieldElement element30 = rawYCoord.Add(rawXCoord).Square();
                if (element28.IsOne)
                {
                    element31 = element24.Add(element22).Square();
                }
                else
                {
                    element31 = element24.SquarePlusProduct(element28, element22.Square());
                }
                element29 = element30.Add(x).Add(element22).Multiply(element30).Add(element31).Add(element26);
                if (a.IsZero)
                {
                    element29 = element29.Add(element27);
                }
                else if (!a.IsOne)
                {
                    element29 = element29.Add(a.AddOne().Multiply(element27));
                }
                break;
            }

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
            return(new F2mPoint(curve, element26, element29, new ECFieldElement[] { element27 }, base.IsCompressed));
        }
Exemplo n.º 21
0
        public override ECPoint Add(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve            = this.Curve;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement element2         = b.RawXCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement rawYCoord = base.RawYCoord;
                ECFieldElement element4  = b.RawYCoord;
                ECFieldElement element5  = rawXCoord.Add(element2);
                ECFieldElement element6  = rawYCoord.Add(element4);
                if (!element5.IsZero)
                {
                    ECFieldElement element7 = element6.Divide(element5);
                    ECFieldElement x        = element7.Square().Add(element7).Add(element5).Add(curve.A);
                    return(new F2mPoint(curve, x, element7.Multiply(rawXCoord.Add(x)).Add(x).Add(rawYCoord), base.IsCompressed));
                }
                if (element6.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }

            case 1:
            {
                ECFieldElement rawYCoord = base.RawYCoord;
                ECFieldElement element11 = base.RawZCoords[0];
                ECFieldElement element12 = b.RawYCoord;
                ECFieldElement element13 = b.RawZCoords[0];
                bool           isOne     = element11.IsOne;
                ECFieldElement element14 = element12;
                ECFieldElement element15 = element2;
                if (!isOne)
                {
                    element14 = element14.Multiply(element11);
                    element15 = element15.Multiply(element11);
                }
                bool           flag2     = element13.IsOne;
                ECFieldElement element16 = rawYCoord;
                ECFieldElement element17 = rawXCoord;
                if (!flag2)
                {
                    element16 = element16.Multiply(element13);
                    element17 = element17.Multiply(element13);
                }
                ECFieldElement element18 = element14.Add(element16);
                ECFieldElement element19 = element15.Add(element17);
                if (!element19.IsZero)
                {
                    ECFieldElement x         = element19.Square();
                    ECFieldElement element21 = x.Multiply(element19);
                    ECFieldElement element22 = !isOne ? (!flag2 ? element11.Multiply(element13) : element11) : element13;
                    ECFieldElement element23 = element18.Add(element19);
                    ECFieldElement element24 = element23.MultiplyPlusProduct(element18, x, curve.A).Multiply(element22).Add(element21);
                    ECFieldElement element25 = element19.Multiply(element24);
                    ECFieldElement element26 = !flag2?x.Multiply(element13) : x;

                    ECFieldElement y         = element18.MultiplyPlusProduct(rawXCoord, element19, rawYCoord).MultiplyPlusProduct(element26, element23, element24);
                    ECFieldElement element28 = element21.Multiply(element22);
                    return(new F2mPoint(curve, element25, y, new ECFieldElement[] { element28 }, base.IsCompressed));
                }
                if (element18.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }

            case 6:
                if (!rawXCoord.IsZero)
                {
                    ECFieldElement element39;
                    ECFieldElement element40;
                    ECFieldElement element41;
                    ECFieldElement rawYCoord = base.RawYCoord;
                    ECFieldElement element30 = base.RawZCoords[0];
                    ECFieldElement element31 = b.RawYCoord;
                    ECFieldElement element32 = b.RawZCoords[0];
                    bool           isOne     = element30.IsOne;
                    ECFieldElement element33 = element2;
                    ECFieldElement element34 = element31;
                    if (!isOne)
                    {
                        element33 = element33.Multiply(element30);
                        element34 = element34.Multiply(element30);
                    }
                    bool           flag4     = element32.IsOne;
                    ECFieldElement element35 = rawXCoord;
                    ECFieldElement element36 = rawYCoord;
                    if (!flag4)
                    {
                        element35 = element35.Multiply(element32);
                        element36 = element36.Multiply(element32);
                    }
                    ECFieldElement element37 = element36.Add(element34);
                    ECFieldElement element38 = element35.Add(element33);
                    if (element38.IsZero)
                    {
                        if (element37.IsZero)
                        {
                            return(this.Twice());
                        }
                        return(curve.Infinity);
                    }
                    if (element2.IsZero)
                    {
                        ECPoint point = this.Normalize();
                        rawXCoord = point.RawXCoord;
                        ECFieldElement yCoord    = point.YCoord;
                        ECFieldElement element43 = element31;
                        ECFieldElement element44 = yCoord.Add(element43).Divide(rawXCoord);
                        element39 = element44.Square().Add(element44).Add(rawXCoord).Add(curve.A);
                        if (element39.IsZero)
                        {
                            return(new F2mPoint(curve, element39, curve.B.Sqrt(), base.IsCompressed));
                        }
                        element40 = element44.Multiply(rawXCoord.Add(element39)).Add(element39).Add(yCoord).Divide(element39).Add(element39);
                        element41 = curve.FromBigInteger(BigInteger.One);
                    }
                    else
                    {
                        element38 = element38.Square();
                        ECFieldElement element46 = element37.Multiply(element35);
                        ECFieldElement element47 = element37.Multiply(element33);
                        element39 = element46.Multiply(element47);
                        if (element39.IsZero)
                        {
                            return(new F2mPoint(curve, element39, curve.B.Sqrt(), base.IsCompressed));
                        }
                        ECFieldElement x = element37.Multiply(element38);
                        if (!flag4)
                        {
                            x = x.Multiply(element32);
                        }
                        element40 = element47.Add(element38).SquarePlusProduct(x, rawYCoord.Add(element30));
                        element41 = x;
                        if (!isOne)
                        {
                            element41 = element41.Multiply(element30);
                        }
                    }
                    return(new F2mPoint(curve, element39, element40, new ECFieldElement[] { element41 }, base.IsCompressed));
                }
                if (element2.IsZero)
                {
                    return(curve.Infinity);
                }
                return(b.Add(this));
            }
            throw new InvalidOperationException("unsupported coordinate system");
        }
Exemplo n.º 22
0
 protected virtual ECFieldElement Two(ECFieldElement x)
 {
     return(x.Add(x));
 }
Exemplo n.º 23
0
        public override ECPoint TimesPow2(int e)
        {
            if (e < 0)
            {
                throw new ArgumentException("cannot be negative", "e");
            }
            if ((e == 0) || base.IsInfinity)
            {
                return(this);
            }
            if (e == 1)
            {
                return(this.Twice());
            }
            ECCurve        curve     = this.Curve;
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(curve.Infinity);
            }
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement a         = curve.A;
            ECFieldElement rawXCoord = base.RawXCoord;
            ECFieldElement b         = (base.RawZCoords.Length >= 1) ? base.RawZCoords[0] : curve.FromBigInteger(BigInteger.One);

            if (!b.IsOne)
            {
                switch (coordinateSystem)
                {
                case 1:
                {
                    ECFieldElement element5 = b.Square();
                    rawXCoord = rawXCoord.Multiply(b);
                    rawYCoord = rawYCoord.Multiply(element5);
                    a         = this.CalculateJacobianModifiedW(b, element5);
                    break;
                }

                case 2:
                    a = this.CalculateJacobianModifiedW(b, null);
                    break;

                case 4:
                    a = this.GetJacobianModifiedW();
                    break;
                }
            }
            for (int i = 0; i < e; i++)
            {
                if (rawYCoord.IsZero)
                {
                    return(curve.Infinity);
                }
                ECFieldElement x         = rawXCoord.Square();
                ECFieldElement element7  = this.Three(x);
                ECFieldElement element8  = this.Two(rawYCoord);
                ECFieldElement element9  = element8.Multiply(rawYCoord);
                ECFieldElement element10 = this.Two(rawXCoord.Multiply(element9));
                ECFieldElement element11 = element9.Square();
                ECFieldElement element12 = this.Two(element11);
                if (!a.IsZero)
                {
                    element7 = element7.Add(a);
                    a        = this.Two(element12.Multiply(a));
                }
                rawXCoord = element7.Square().Subtract(this.Two(element10));
                rawYCoord = element7.Multiply(element10.Subtract(rawXCoord)).Subtract(element12);
                b         = !b.IsOne ? element8.Multiply(b) : element8;
            }
            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement element13 = b.Invert();
                ECFieldElement element14 = element13.Square();
                ECFieldElement element15 = element14.Multiply(element13);
                return(new FpPoint(curve, rawXCoord.Multiply(element14), rawYCoord.Multiply(element15), base.IsCompressed));
            }

            case 1:
                rawXCoord = rawXCoord.Multiply(b);
                b         = b.Multiply(b.Square());
                return(new FpPoint(curve, rawXCoord, rawYCoord, new ECFieldElement[] { b }, base.IsCompressed));

            case 2:
                return(new FpPoint(curve, rawXCoord, rawYCoord, new ECFieldElement[] { b }, base.IsCompressed));

            case 4:
                return(new FpPoint(curve, rawXCoord, rawYCoord, new ECFieldElement[] { b, a }, base.IsCompressed));
            }
            throw new InvalidOperationException("unsupported coordinate system");
        }
Exemplo n.º 24
0
        public override ECPoint Twice()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = Curve;
            ECFieldElement rawXCoord = base.RawXCoord;

            if (!rawXCoord.IsZero)
            {
                switch (curve.CoordinateSystem)
                {
                case 0:
                {
                    ECFieldElement rawYCoord2       = base.RawYCoord;
                    ECFieldElement eCFieldElement11 = rawYCoord2.Divide(rawXCoord).Add(rawXCoord);
                    ECFieldElement x = eCFieldElement11.Square().Add(eCFieldElement11).Add(curve.A);
                    ECFieldElement y = rawXCoord.SquarePlusProduct(x, eCFieldElement11.AddOne());
                    return(new F2mPoint(curve, x, y, base.IsCompressed));
                }

                case 1:
                {
                    ECFieldElement rawYCoord3       = base.RawYCoord;
                    ECFieldElement eCFieldElement12 = base.RawZCoords[0];
                    bool           isOne2           = eCFieldElement12.IsOne;
                    ECFieldElement eCFieldElement13 = (!isOne2) ? rawXCoord.Multiply(eCFieldElement12) : rawXCoord;
                    ECFieldElement b3 = (!isOne2) ? rawYCoord3.Multiply(eCFieldElement12) : rawYCoord3;
                    ECFieldElement eCFieldElement14 = rawXCoord.Square();
                    ECFieldElement eCFieldElement15 = eCFieldElement14.Add(b3);
                    ECFieldElement eCFieldElement16 = eCFieldElement13;
                    ECFieldElement eCFieldElement17 = eCFieldElement16.Square();
                    ECFieldElement eCFieldElement18 = eCFieldElement15.Add(eCFieldElement16);
                    ECFieldElement eCFieldElement19 = eCFieldElement18.MultiplyPlusProduct(eCFieldElement15, eCFieldElement17, curve.A);
                    ECFieldElement x2 = eCFieldElement16.Multiply(eCFieldElement19);
                    ECFieldElement y2 = eCFieldElement14.Square().MultiplyPlusProduct(eCFieldElement16, eCFieldElement19, eCFieldElement18);
                    ECFieldElement eCFieldElement20 = eCFieldElement16.Multiply(eCFieldElement17);
                    return(new F2mPoint(curve, x2, y2, new ECFieldElement[1]
                        {
                            eCFieldElement20
                        }, base.IsCompressed));
                }

                case 6:
                {
                    ECFieldElement rawYCoord       = base.RawYCoord;
                    ECFieldElement eCFieldElement  = base.RawZCoords[0];
                    bool           isOne           = eCFieldElement.IsOne;
                    ECFieldElement eCFieldElement2 = (!isOne) ? rawYCoord.Multiply(eCFieldElement) : rawYCoord;
                    ECFieldElement eCFieldElement3 = (!isOne) ? eCFieldElement.Square() : eCFieldElement;
                    ECFieldElement a = curve.A;
                    ECFieldElement eCFieldElement4 = (!isOne) ? a.Multiply(eCFieldElement3) : a;
                    ECFieldElement eCFieldElement5 = rawYCoord.Square().Add(eCFieldElement2).Add(eCFieldElement4);
                    if (eCFieldElement5.IsZero)
                    {
                        return(new F2mPoint(curve, eCFieldElement5, curve.B.Sqrt(), base.IsCompressed));
                    }
                    ECFieldElement eCFieldElement6 = eCFieldElement5.Square();
                    ECFieldElement eCFieldElement7 = (!isOne) ? eCFieldElement5.Multiply(eCFieldElement3) : eCFieldElement5;
                    ECFieldElement b = curve.B;
                    ECFieldElement eCFieldElement9;
                    if (b.BitLength < curve.FieldSize >> 1)
                    {
                        ECFieldElement eCFieldElement8 = rawYCoord.Add(rawXCoord).Square();
                        ECFieldElement b2 = (!b.IsOne) ? eCFieldElement4.SquarePlusProduct(b, eCFieldElement3.Square()) : eCFieldElement4.Add(eCFieldElement3).Square();
                        eCFieldElement9 = eCFieldElement8.Add(eCFieldElement5).Add(eCFieldElement3).Multiply(eCFieldElement8)
                                          .Add(b2)
                                          .Add(eCFieldElement6);
                        if (a.IsZero)
                        {
                            eCFieldElement9 = eCFieldElement9.Add(eCFieldElement7);
                        }
                        else if (!a.IsOne)
                        {
                            eCFieldElement9 = eCFieldElement9.Add(a.AddOne().Multiply(eCFieldElement7));
                        }
                    }
                    else
                    {
                        ECFieldElement eCFieldElement10 = (!isOne) ? rawXCoord.Multiply(eCFieldElement) : rawXCoord;
                        eCFieldElement9 = eCFieldElement10.SquarePlusProduct(eCFieldElement5, eCFieldElement2).Add(eCFieldElement6).Add(eCFieldElement7);
                    }
                    return(new F2mPoint(curve, eCFieldElement6, eCFieldElement9, new ECFieldElement[1]
                        {
                            eCFieldElement7
                        }, base.IsCompressed));
                }

                default:
                    throw new InvalidOperationException("unsupported coordinate system");
                }
            }
            return(curve.Infinity);
        }
Exemplo n.º 25
0
 protected virtual ECFieldElement Two(ECFieldElement x)
 {
     return x.Add(x);
 }
Exemplo n.º 26
0
        public override ECPoint Add(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (!b.IsInfinity)
            {
                ECCurve        curve            = Curve;
                int            coordinateSystem = curve.CoordinateSystem;
                ECFieldElement rawXCoord        = base.RawXCoord;
                ECFieldElement rawXCoord2       = b.RawXCoord;
                switch (coordinateSystem)
                {
                case 0:
                {
                    ECFieldElement rawYCoord5       = base.RawYCoord;
                    ECFieldElement rawYCoord6       = b.RawYCoord;
                    ECFieldElement eCFieldElement29 = rawXCoord.Add(rawXCoord2);
                    ECFieldElement eCFieldElement30 = rawYCoord5.Add(rawYCoord6);
                    if (eCFieldElement29.IsZero)
                    {
                        if (eCFieldElement30.IsZero)
                        {
                            return(Twice());
                        }
                        return(curve.Infinity);
                    }
                    ECFieldElement eCFieldElement31 = eCFieldElement30.Divide(eCFieldElement29);
                    ECFieldElement eCFieldElement32 = eCFieldElement31.Square().Add(eCFieldElement31).Add(eCFieldElement29)
                                                      .Add(curve.A);
                    ECFieldElement y3 = eCFieldElement31.Multiply(rawXCoord.Add(eCFieldElement32)).Add(eCFieldElement32).Add(rawYCoord5);
                    return(new F2mPoint(curve, eCFieldElement32, y3, base.IsCompressed));
                }

                case 1:
                {
                    ECFieldElement rawYCoord3       = base.RawYCoord;
                    ECFieldElement eCFieldElement16 = base.RawZCoords[0];
                    ECFieldElement rawYCoord4       = b.RawYCoord;
                    ECFieldElement eCFieldElement17 = b.RawZCoords[0];
                    bool           isOne3           = eCFieldElement16.IsOne;
                    ECFieldElement eCFieldElement18 = rawYCoord4;
                    ECFieldElement eCFieldElement19 = rawXCoord2;
                    if (!isOne3)
                    {
                        eCFieldElement18 = eCFieldElement18.Multiply(eCFieldElement16);
                        eCFieldElement19 = eCFieldElement19.Multiply(eCFieldElement16);
                    }
                    bool           isOne4           = eCFieldElement17.IsOne;
                    ECFieldElement eCFieldElement20 = rawYCoord3;
                    ECFieldElement eCFieldElement21 = rawXCoord;
                    if (!isOne4)
                    {
                        eCFieldElement20 = eCFieldElement20.Multiply(eCFieldElement17);
                        eCFieldElement21 = eCFieldElement21.Multiply(eCFieldElement17);
                    }
                    ECFieldElement eCFieldElement22 = eCFieldElement18.Add(eCFieldElement20);
                    ECFieldElement eCFieldElement23 = eCFieldElement19.Add(eCFieldElement21);
                    if (eCFieldElement23.IsZero)
                    {
                        if (eCFieldElement22.IsZero)
                        {
                            return(Twice());
                        }
                        return(curve.Infinity);
                    }
                    ECFieldElement eCFieldElement24 = eCFieldElement23.Square();
                    ECFieldElement eCFieldElement25 = eCFieldElement24.Multiply(eCFieldElement23);
                    ECFieldElement b3 = isOne3 ? eCFieldElement17 : ((!isOne4) ? eCFieldElement16.Multiply(eCFieldElement17) : eCFieldElement16);
                    ECFieldElement eCFieldElement26 = eCFieldElement22.Add(eCFieldElement23);
                    ECFieldElement eCFieldElement27 = eCFieldElement26.MultiplyPlusProduct(eCFieldElement22, eCFieldElement24, curve.A).Multiply(b3).Add(eCFieldElement25);
                    ECFieldElement x  = eCFieldElement23.Multiply(eCFieldElement27);
                    ECFieldElement b4 = (!isOne4) ? eCFieldElement24.Multiply(eCFieldElement17) : eCFieldElement24;
                    ECFieldElement y2 = eCFieldElement22.MultiplyPlusProduct(rawXCoord, eCFieldElement23, rawYCoord3).MultiplyPlusProduct(b4, eCFieldElement26, eCFieldElement27);
                    ECFieldElement eCFieldElement28 = eCFieldElement25.Multiply(b3);
                    return(new F2mPoint(curve, x, y2, new ECFieldElement[1]
                        {
                            eCFieldElement28
                        }, base.IsCompressed));
                }

                case 6:
                {
                    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.RawXCoord;
                        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 F2mPoint(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 F2mPoint(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 F2mPoint(curve, eCFieldElement10, y, new ECFieldElement[1]
                        {
                            eCFieldElement12
                        }, base.IsCompressed));
                }

                default:
                    throw new InvalidOperationException("unsupported coordinate system");
                }
            }
            return(this);
        }
Exemplo n.º 27
0
        public override ECPoint Twice()
        {
            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = this.Curve;
            ECFieldElement rawXCoord = base.RawXCoord;

            if (rawXCoord.IsZero)
            {
                return(curve.Infinity);
            }
            int coordinateSystem = curve.CoordinateSystem;
            int num = coordinateSystem;

            switch (num)
            {
            case 0:
            {
                ECFieldElement rawYCoord      = base.RawYCoord;
                ECFieldElement eCFieldElement = rawYCoord.Divide(rawXCoord).Add(rawXCoord);
                ECFieldElement x = eCFieldElement.Square().Add(eCFieldElement).Add(curve.A);
                ECFieldElement y = rawXCoord.SquarePlusProduct(x, eCFieldElement.AddOne());
                return(new F2mPoint(curve, x, y, base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement rawYCoord2      = base.RawYCoord;
                ECFieldElement eCFieldElement2 = base.RawZCoords[0];
                bool           isOne           = eCFieldElement2.IsOne;
                ECFieldElement eCFieldElement3 = isOne ? rawXCoord : rawXCoord.Multiply(eCFieldElement2);
                ECFieldElement b = isOne ? rawYCoord2 : rawYCoord2.Multiply(eCFieldElement2);
                ECFieldElement eCFieldElement4 = rawXCoord.Square();
                ECFieldElement eCFieldElement5 = eCFieldElement4.Add(b);
                ECFieldElement eCFieldElement6 = eCFieldElement3;
                ECFieldElement eCFieldElement7 = eCFieldElement6.Square();
                ECFieldElement eCFieldElement8 = eCFieldElement5.Add(eCFieldElement6);
                ECFieldElement eCFieldElement9 = eCFieldElement8.MultiplyPlusProduct(eCFieldElement5, eCFieldElement7, curve.A);
                ECFieldElement x2 = eCFieldElement6.Multiply(eCFieldElement9);
                ECFieldElement y2 = eCFieldElement4.Square().MultiplyPlusProduct(eCFieldElement6, eCFieldElement9, eCFieldElement8);
                ECFieldElement eCFieldElement10 = eCFieldElement6.Multiply(eCFieldElement7);
                return(new F2mPoint(curve, x2, y2, new ECFieldElement[]
                    {
                        eCFieldElement10
                    }, base.IsCompressed));
            }

            default:
            {
                if (num != 6)
                {
                    throw new InvalidOperationException("unsupported coordinate system");
                }
                ECFieldElement rawYCoord3       = base.RawYCoord;
                ECFieldElement eCFieldElement11 = base.RawZCoords[0];
                bool           isOne2           = eCFieldElement11.IsOne;
                ECFieldElement eCFieldElement12 = isOne2 ? rawYCoord3 : rawYCoord3.Multiply(eCFieldElement11);
                ECFieldElement eCFieldElement13 = isOne2 ? eCFieldElement11 : eCFieldElement11.Square();
                ECFieldElement a = curve.A;
                ECFieldElement eCFieldElement14 = isOne2 ? a : a.Multiply(eCFieldElement13);
                ECFieldElement eCFieldElement15 = rawYCoord3.Square().Add(eCFieldElement12).Add(eCFieldElement14);
                if (eCFieldElement15.IsZero)
                {
                    return(new F2mPoint(curve, eCFieldElement15, curve.B.Sqrt(), base.IsCompressed));
                }
                ECFieldElement eCFieldElement16 = eCFieldElement15.Square();
                ECFieldElement eCFieldElement17 = isOne2 ? eCFieldElement15 : eCFieldElement15.Multiply(eCFieldElement13);
                ECFieldElement b2 = curve.B;
                ECFieldElement eCFieldElement19;
                if (b2.BitLength < curve.FieldSize >> 1)
                {
                    ECFieldElement eCFieldElement18 = rawYCoord3.Add(rawXCoord).Square();
                    ECFieldElement b3;
                    if (b2.IsOne)
                    {
                        b3 = eCFieldElement14.Add(eCFieldElement13).Square();
                    }
                    else
                    {
                        b3 = eCFieldElement14.SquarePlusProduct(b2, eCFieldElement13.Square());
                    }
                    eCFieldElement19 = eCFieldElement18.Add(eCFieldElement15).Add(eCFieldElement13).Multiply(eCFieldElement18).Add(b3).Add(eCFieldElement16);
                    if (a.IsZero)
                    {
                        eCFieldElement19 = eCFieldElement19.Add(eCFieldElement17);
                    }
                    else if (!a.IsOne)
                    {
                        eCFieldElement19 = eCFieldElement19.Add(a.AddOne().Multiply(eCFieldElement17));
                    }
                }
                else
                {
                    ECFieldElement eCFieldElement20 = isOne2 ? rawXCoord : rawXCoord.Multiply(eCFieldElement11);
                    eCFieldElement19 = eCFieldElement20.SquarePlusProduct(eCFieldElement15, eCFieldElement12).Add(eCFieldElement16).Add(eCFieldElement17);
                }
                return(new F2mPoint(curve, eCFieldElement16, eCFieldElement19, new ECFieldElement[]
                    {
                        eCFieldElement17
                    }, base.IsCompressed));
            }
            }
        }
Exemplo n.º 28
0
        public override ECPoint Twice()
        {
            ECFieldElement element20;
            bool           isOne;
            ECFieldElement element22;
            ECFieldElement element25;
            ECFieldElement element26;

            if (base.IsInfinity)
            {
                return(this);
            }
            ECCurve        curve     = this.Curve;
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(curve.Infinity);
            }
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement x        = rawXCoord.Square();
                ECFieldElement element4 = this.Three(x).Add(this.Curve.A).Divide(this.Two(rawYCoord));
                ECFieldElement element5 = element4.Square().Subtract(this.Two(rawXCoord));
                return(new FpPoint(this.Curve, element5, element4.Multiply(rawXCoord.Subtract(element5)).Subtract(rawYCoord), base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement element7 = base.RawZCoords[0];
                bool           isOne    = element7.IsOne;
                ECFieldElement a        = curve.A;
                if (!a.IsZero && !isOne)
                {
                    a = a.Multiply(element7.Square());
                }
                a = a.Add(this.Three(rawXCoord.Square()));
                ECFieldElement x = !isOne?rawYCoord.Multiply(element7) : rawYCoord;

                ECFieldElement element10 = !isOne?x.Multiply(rawYCoord) : rawYCoord.Square();

                ECFieldElement element11 = rawXCoord.Multiply(element10);
                ECFieldElement element12 = this.Four(element11);
                ECFieldElement element13 = a.Square().Subtract(this.Two(element12));
                ECFieldElement element14 = this.Two(x);
                ECFieldElement element15 = element13.Multiply(element14);
                ECFieldElement element16 = this.Two(element10);
                ECFieldElement element17 = element12.Subtract(element13).Multiply(a).Subtract(this.Two(element16.Square()));
                ECFieldElement element18 = !isOne?element14.Square() : this.Two(element16);

                ECFieldElement element19 = this.Two(element18).Multiply(x);
                return(new FpPoint(curve, element15, element17, new ECFieldElement[] { element19 }, base.IsCompressed));
            }

            case 2:
            {
                element20 = base.RawZCoords[0];
                isOne     = element20.IsOne;
                ECFieldElement element21 = rawYCoord.Square();
                element22 = element21.Square();
                ECFieldElement a         = curve.A;
                ECFieldElement element24 = a.Negate();
                if (!element24.ToBigInteger().Equals(BigInteger.ValueOf(3L)))
                {
                    ECFieldElement x = rawXCoord.Square();
                    element25 = this.Three(x);
                    if (isOne)
                    {
                        element25 = element25.Add(a);
                    }
                    else if (!a.IsZero)
                    {
                        ECFieldElement element30 = (!isOne ? element20.Square() : element20).Square();
                        if (element24.BitLength < a.BitLength)
                        {
                            element25 = element25.Subtract(element30.Multiply(element24));
                        }
                        else
                        {
                            element25 = element25.Add(element30.Multiply(a));
                        }
                    }
                    element26 = this.Four(rawXCoord.Multiply(element21));
                    break;
                }
                ECFieldElement element27 = !isOne?element20.Square() : element20;

                element25 = this.Three(rawXCoord.Add(element27).Multiply(rawXCoord.Subtract(element27)));
                element26 = this.Four(element21.Multiply(rawXCoord));
                break;
            }

            case 4:
                return(this.TwiceJacobianModified(true));

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
            ECFieldElement b         = element25.Square().Subtract(this.Two(element26));
            ECFieldElement y         = element26.Subtract(b).Multiply(element25).Subtract(this.Eight(element22));
            ECFieldElement element33 = this.Two(rawYCoord);

            if (!isOne)
            {
                element33 = element33.Multiply(element20);
            }
            return(new FpPoint(curve, b, y, new ECFieldElement[] { element33 }, base.IsCompressed));
        }
Exemplo n.º 29
0
 protected virtual ECFieldElement Two(ECFieldElement x) =>
 x.Add(x);
Exemplo n.º 30
0
 protected virtual ECFieldElement DoubleProductFromSquares(ECFieldElement a, ECFieldElement b,
     ECFieldElement aSquared, ECFieldElement bSquared)
 {
     /*
      * NOTE: If squaring in the field is faster than multiplication, then this is a quicker
      * way to calculate 2.A.B, if A^2 and B^2 are already known.
      */
     return a.Add(b).Square().Subtract(aSquared).Subtract(bSquared);
 }
Exemplo n.º 31
0
        public override ECPoint TwicePlus(ECPoint b)
        {
            if (this == b)
            {
                return(this.ThreeTimes());
            }
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this.Twice());
            }
            ECFieldElement rawYCoord = base.RawYCoord;

            if (rawYCoord.IsZero)
            {
                return(b);
            }
            switch (this.Curve.CoordinateSystem)
            {
            case 0:
            {
                ECFieldElement rawXCoord = base.RawXCoord;
                ECFieldElement element3  = b.RawXCoord;
                ECFieldElement element4  = b.RawYCoord;
                ECFieldElement element5  = element3.Subtract(rawXCoord);
                ECFieldElement element6  = element4.Subtract(rawYCoord);
                if (!element5.IsZero)
                {
                    ECFieldElement element7 = element5.Square();
                    ECFieldElement element8 = element6.Square();
                    ECFieldElement element9 = element7.Multiply(this.Two(rawXCoord).Add(element3)).Subtract(element8);
                    if (element9.IsZero)
                    {
                        return(this.Curve.Infinity);
                    }
                    ECFieldElement element11 = element9.Multiply(element5).Invert();
                    ECFieldElement element12 = element9.Multiply(element11).Multiply(element6);
                    ECFieldElement element13 = this.Two(rawYCoord).Multiply(element7).Multiply(element5).Multiply(element11).Subtract(element12);
                    ECFieldElement x         = element13.Subtract(element12).Multiply(element12.Add(element13)).Add(element3);
                    return(new FpPoint(this.Curve, x, rawXCoord.Subtract(x).Multiply(element13).Subtract(rawYCoord), base.IsCompressed));
                }
                if (element6.IsZero)
                {
                    return(this.ThreeTimes());
                }
                return(this);
            }

            case 4:
                return(this.TwiceJacobianModified(false).Add(b));
            }
            return(this.Twice().Add(b));
        }