Subtract() public abstract method

public abstract Subtract ( ECFieldElement b ) : ECFieldElement
b ECFieldElement
return ECFieldElement
Exemplo n.º 1
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));
        }
Exemplo n.º 2
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.º 3
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.º 4
0
        protected virtual FpPoint TwiceJacobianModified(bool calculateW)
        {
            ECFieldElement rawXCoord         = base.RawXCoord;
            ECFieldElement rawYCoord         = base.RawYCoord;
            ECFieldElement b                 = base.RawZCoords[0];
            ECFieldElement jacobianModifiedW = this.GetJacobianModifiedW();
            ECFieldElement x                 = rawXCoord.Square();
            ECFieldElement element6          = this.Three(x).Add(jacobianModifiedW);
            ECFieldElement element7          = this.Two(rawYCoord);
            ECFieldElement element8          = element7.Multiply(rawYCoord);
            ECFieldElement element9          = this.Two(rawXCoord.Multiply(element8));
            ECFieldElement element10         = element6.Square().Subtract(this.Two(element9));
            ECFieldElement element11         = element8.Square();
            ECFieldElement element12         = this.Two(element11);
            ECFieldElement y                 = element6.Multiply(element9.Subtract(element10)).Subtract(element12);
            ECFieldElement element14         = !calculateW ? null : this.Two(element12.Multiply(jacobianModifiedW));
            ECFieldElement element15         = !b.IsOne ? element7.Multiply(b) : element7;

            return(new FpPoint(this.Curve, element10, y, new ECFieldElement[] { element15, element14 }, base.IsCompressed));
        }
Exemplo n.º 5
0
        protected virtual FpPoint TwiceJacobianModified(bool calculateW)
        {
            ECFieldElement rawXCoord         = base.RawXCoord;
            ECFieldElement rawYCoord         = base.RawYCoord;
            ECFieldElement eCFieldElement    = base.RawZCoords[0];
            ECFieldElement jacobianModifiedW = GetJacobianModifiedW();
            ECFieldElement x = rawXCoord.Square();
            ECFieldElement eCFieldElement2 = Three(x).Add(jacobianModifiedW);
            ECFieldElement eCFieldElement3 = Two(rawYCoord);
            ECFieldElement eCFieldElement4 = eCFieldElement3.Multiply(rawYCoord);
            ECFieldElement eCFieldElement5 = Two(rawXCoord.Multiply(eCFieldElement4));
            ECFieldElement eCFieldElement6 = eCFieldElement2.Square().Subtract(Two(eCFieldElement5));
            ECFieldElement x2 = eCFieldElement4.Square();
            ECFieldElement eCFieldElement7 = Two(x2);
            ECFieldElement y = eCFieldElement2.Multiply(eCFieldElement5.Subtract(eCFieldElement6)).Subtract(eCFieldElement7);
            ECFieldElement eCFieldElement8 = (calculateW ? Two(eCFieldElement7.Multiply(jacobianModifiedW)) : null);
            ECFieldElement eCFieldElement9 = (eCFieldElement.IsOne ? eCFieldElement3 : eCFieldElement3.Multiply(eCFieldElement));

            return(new FpPoint(Curve, eCFieldElement6, y, new ECFieldElement[2] {
                eCFieldElement9, eCFieldElement8
            }, base.IsCompressed));
        }
Exemplo n.º 6
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.º 7
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.º 8
0
        public override ECPoint Add(ECPoint b)
        {
            //IL_04b2: Unknown result type (might be due to invalid IL or missing references)
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            if (this == b)
            {
                return(Twice());
            }
            ECCurve        curve            = Curve;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawYCoord        = base.RawYCoord;
            ECFieldElement rawXCoord2       = b.RawXCoord;
            ECFieldElement rawYCoord2       = b.RawYCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement eCFieldElement32 = rawXCoord2.Subtract(rawXCoord);
                ECFieldElement eCFieldElement33 = rawYCoord2.Subtract(rawYCoord);
                if (eCFieldElement32.IsZero)
                {
                    if (eCFieldElement33.IsZero)
                    {
                        return(Twice());
                    }
                    return(Curve.Infinity);
                }
                ECFieldElement eCFieldElement34 = eCFieldElement33.Divide(eCFieldElement32);
                ECFieldElement eCFieldElement35 = eCFieldElement34.Square().Subtract(rawXCoord).Subtract(rawXCoord2);
                ECFieldElement y3 = eCFieldElement34.Multiply(rawXCoord.Subtract(eCFieldElement35)).Subtract(rawYCoord);
                return(new FpPoint(Curve, eCFieldElement35, y3, base.IsCompressed));
            }

            case 1:
            {
                ECFieldElement eCFieldElement21 = base.RawZCoords[0];
                ECFieldElement eCFieldElement22 = b.RawZCoords[0];
                bool           isOne3           = eCFieldElement21.IsOne;
                bool           isOne4           = eCFieldElement22.IsOne;
                ECFieldElement eCFieldElement23 = (isOne3 ? rawYCoord2 : rawYCoord2.Multiply(eCFieldElement21));
                ECFieldElement eCFieldElement24 = (isOne4 ? rawYCoord : rawYCoord.Multiply(eCFieldElement22));
                ECFieldElement eCFieldElement25 = eCFieldElement23.Subtract(eCFieldElement24);
                ECFieldElement eCFieldElement26 = (isOne3 ? rawXCoord2 : rawXCoord2.Multiply(eCFieldElement21));
                ECFieldElement b6 = (isOne4 ? rawXCoord : rawXCoord.Multiply(eCFieldElement22));
                ECFieldElement eCFieldElement27 = eCFieldElement26.Subtract(b6);
                if (eCFieldElement27.IsZero)
                {
                    if (eCFieldElement25.IsZero)
                    {
                        return(Twice());
                    }
                    return(curve.Infinity);
                }
                ECFieldElement b7 = (isOne3 ? eCFieldElement22 : (isOne4 ? eCFieldElement21 : eCFieldElement21.Multiply(eCFieldElement22)));
                ECFieldElement eCFieldElement28 = eCFieldElement27.Square();
                ECFieldElement eCFieldElement29 = eCFieldElement28.Multiply(eCFieldElement27);
                ECFieldElement eCFieldElement30 = eCFieldElement28.Multiply(b6);
                ECFieldElement b8 = eCFieldElement25.Square().Multiply(b7).Subtract(eCFieldElement29)
                                    .Subtract(Two(eCFieldElement30));
                ECFieldElement x  = eCFieldElement27.Multiply(b8);
                ECFieldElement y2 = eCFieldElement30.Subtract(b8).MultiplyMinusProduct(eCFieldElement25, eCFieldElement24, eCFieldElement29);
                ECFieldElement eCFieldElement31 = eCFieldElement29.Multiply(b7);
                return(new FpPoint(curve, x, y2, new ECFieldElement[1] {
                        eCFieldElement31
                    }, base.IsCompressed));
            }

            case 2:
            case 4:
            {
                ECFieldElement eCFieldElement  = base.RawZCoords[0];
                ECFieldElement eCFieldElement2 = b.RawZCoords[0];
                bool           isOne           = eCFieldElement.IsOne;
                ECFieldElement zSquared        = null;
                ECFieldElement eCFieldElement7;
                ECFieldElement y;
                ECFieldElement eCFieldElement8;
                if (!isOne && eCFieldElement.Equals(eCFieldElement2))
                {
                    ECFieldElement eCFieldElement3 = rawXCoord.Subtract(rawXCoord2);
                    ECFieldElement eCFieldElement4 = rawYCoord.Subtract(rawYCoord2);
                    if (eCFieldElement3.IsZero)
                    {
                        if (eCFieldElement4.IsZero)
                        {
                            return(Twice());
                        }
                        return(curve.Infinity);
                    }
                    ECFieldElement eCFieldElement5 = eCFieldElement3.Square();
                    ECFieldElement eCFieldElement6 = rawXCoord.Multiply(eCFieldElement5);
                    ECFieldElement b2 = rawXCoord2.Multiply(eCFieldElement5);
                    ECFieldElement b3 = eCFieldElement6.Subtract(b2).Multiply(rawYCoord);
                    eCFieldElement7 = eCFieldElement4.Square().Subtract(eCFieldElement6).Subtract(b2);
                    y = eCFieldElement6.Subtract(eCFieldElement7).Multiply(eCFieldElement4).Subtract(b3);
                    eCFieldElement8 = eCFieldElement3;
                    if (isOne)
                    {
                        zSquared = eCFieldElement5;
                    }
                    else
                    {
                        eCFieldElement8 = eCFieldElement8.Multiply(eCFieldElement);
                    }
                }
                else
                {
                    ECFieldElement b4;
                    ECFieldElement b5;
                    if (isOne)
                    {
                        ECFieldElement eCFieldElement9 = eCFieldElement;
                        b4 = rawXCoord2;
                        b5 = rawYCoord2;
                    }
                    else
                    {
                        ECFieldElement eCFieldElement9 = eCFieldElement.Square();
                        b4 = eCFieldElement9.Multiply(rawXCoord2);
                        ECFieldElement eCFieldElement10 = eCFieldElement9.Multiply(eCFieldElement);
                        b5 = eCFieldElement10.Multiply(rawYCoord2);
                    }
                    bool           isOne2 = eCFieldElement2.IsOne;
                    ECFieldElement eCFieldElement12;
                    ECFieldElement eCFieldElement13;
                    if (isOne2)
                    {
                        ECFieldElement eCFieldElement11 = eCFieldElement2;
                        eCFieldElement12 = rawXCoord;
                        eCFieldElement13 = rawYCoord;
                    }
                    else
                    {
                        ECFieldElement eCFieldElement11 = eCFieldElement2.Square();
                        eCFieldElement12 = eCFieldElement11.Multiply(rawXCoord);
                        ECFieldElement eCFieldElement14 = eCFieldElement11.Multiply(eCFieldElement2);
                        eCFieldElement13 = eCFieldElement14.Multiply(rawYCoord);
                    }
                    ECFieldElement eCFieldElement15 = eCFieldElement12.Subtract(b4);
                    ECFieldElement eCFieldElement16 = eCFieldElement13.Subtract(b5);
                    if (eCFieldElement15.IsZero)
                    {
                        if (eCFieldElement16.IsZero)
                        {
                            return(Twice());
                        }
                        return(curve.Infinity);
                    }
                    ECFieldElement eCFieldElement17 = eCFieldElement15.Square();
                    ECFieldElement eCFieldElement18 = eCFieldElement17.Multiply(eCFieldElement15);
                    ECFieldElement eCFieldElement19 = eCFieldElement17.Multiply(eCFieldElement12);
                    eCFieldElement7 = eCFieldElement16.Square().Add(eCFieldElement18).Subtract(Two(eCFieldElement19));
                    y = eCFieldElement19.Subtract(eCFieldElement7).MultiplyMinusProduct(eCFieldElement16, eCFieldElement18, eCFieldElement13);
                    eCFieldElement8 = eCFieldElement15;
                    if (!isOne)
                    {
                        eCFieldElement8 = eCFieldElement8.Multiply(eCFieldElement);
                    }
                    if (!isOne2)
                    {
                        eCFieldElement8 = eCFieldElement8.Multiply(eCFieldElement2);
                    }
                    if (eCFieldElement8 == eCFieldElement15)
                    {
                        zSquared = eCFieldElement17;
                    }
                }
                ECFieldElement[] zs;
                if (coordinateSystem == 4)
                {
                    ECFieldElement eCFieldElement20 = CalculateJacobianModifiedW(eCFieldElement8, zSquared);
                    zs = new ECFieldElement[2] {
                        eCFieldElement8, eCFieldElement20
                    };
                }
                else
                {
                    zs = new ECFieldElement[1] {
                        eCFieldElement8
                    };
                }
                return(new FpPoint(curve, eCFieldElement7, y, zs, base.IsCompressed));
            }

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
        }
Exemplo n.º 9
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.º 10
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.º 11
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.º 12
0
        public override ECPoint Add(ECPoint b)
        {
            ECFieldElement element28;
            ECFieldElement element29;
            ECFieldElement element30;
            ECFieldElement element31;

            ECFieldElement[] elementArray;
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            if (this == b)
            {
                return(this.Twice());
            }
            ECCurve        curve            = this.Curve;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawYCoord        = base.RawYCoord;
            ECFieldElement element3         = b.RawXCoord;
            ECFieldElement element4         = b.RawYCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement element5 = element3.Subtract(rawXCoord);
                ECFieldElement element6 = element4.Subtract(rawYCoord);
                if (!element5.IsZero)
                {
                    ECFieldElement element7 = element6.Divide(element5);
                    ECFieldElement x        = element7.Square().Subtract(rawXCoord).Subtract(element3);
                    return(new FpPoint(this.Curve, x, element7.Multiply(rawXCoord.Subtract(x)).Subtract(rawYCoord), base.IsCompressed));
                }
                if (!element6.IsZero)
                {
                    return(this.Curve.Infinity);
                }
                return(this.Twice());
            }

            case 1:
            {
                ECFieldElement element10 = base.RawZCoords[0];
                ECFieldElement element11 = b.RawZCoords[0];
                bool           isOne     = element10.IsOne;
                bool           flag2     = element11.IsOne;
                ECFieldElement element12 = !isOne?element4.Multiply(element10) : element4;

                ECFieldElement element13 = !flag2?rawYCoord.Multiply(element11) : rawYCoord;

                ECFieldElement element14 = element12.Subtract(element13);
                ECFieldElement element15 = !isOne?element3.Multiply(element10) : element3;

                ECFieldElement element16 = !flag2?rawXCoord.Multiply(element11) : rawXCoord;

                ECFieldElement element17 = element15.Subtract(element16);
                if (!element17.IsZero)
                {
                    ECFieldElement element18 = !isOne ? (!flag2 ? element10.Multiply(element11) : element10) : element11;
                    ECFieldElement element19 = element17.Square();
                    ECFieldElement element20 = element19.Multiply(element17);
                    ECFieldElement x         = element19.Multiply(element16);
                    ECFieldElement element22 = element14.Square().Multiply(element18).Subtract(element20).Subtract(this.Two(x));
                    ECFieldElement element23 = element17.Multiply(element22);
                    ECFieldElement y         = x.Subtract(element22).MultiplyMinusProduct(element14, element13, element20);
                    ECFieldElement element25 = element20.Multiply(element18);
                    return(new FpPoint(curve, element23, y, new ECFieldElement[] { element25 }, base.IsCompressed));
                }
                if (!element14.IsZero)
                {
                    return(curve.Infinity);
                }
                return(this.Twice());
            }

            case 2:
            case 4:
            {
                ECFieldElement element26 = base.RawZCoords[0];
                ECFieldElement other     = b.RawZCoords[0];
                bool           isOne     = element26.IsOne;
                element31 = null;
                if (isOne || !element26.Equals(other))
                {
                    ECFieldElement element38;
                    ECFieldElement element39;
                    ECFieldElement element40;
                    ECFieldElement element42;
                    ECFieldElement element43;
                    ECFieldElement element44;
                    if (isOne)
                    {
                        element38 = element26;
                        element39 = element3;
                        element40 = element4;
                    }
                    else
                    {
                        element38 = element26.Square();
                        element39 = element38.Multiply(element3);
                        element40 = element38.Multiply(element26).Multiply(element4);
                    }
                    bool flag4 = other.IsOne;
                    if (flag4)
                    {
                        element42 = other;
                        element43 = rawXCoord;
                        element44 = rawYCoord;
                    }
                    else
                    {
                        element42 = other.Square();
                        element43 = element42.Multiply(rawXCoord);
                        element44 = element42.Multiply(other).Multiply(rawYCoord);
                    }
                    ECFieldElement element46 = element43.Subtract(element39);
                    ECFieldElement element47 = element44.Subtract(element40);
                    if (element46.IsZero)
                    {
                        if (element47.IsZero)
                        {
                            return(this.Twice());
                        }
                        return(curve.Infinity);
                    }
                    ECFieldElement element48 = element46.Square();
                    ECFieldElement element49 = element48.Multiply(element46);
                    ECFieldElement x         = element48.Multiply(element43);
                    element28 = element47.Square().Add(element49).Subtract(this.Two(x));
                    element29 = x.Subtract(element28).MultiplyMinusProduct(element47, element49, element44);
                    element30 = element46;
                    if (!isOne)
                    {
                        element30 = element30.Multiply(element26);
                    }
                    if (!flag4)
                    {
                        element30 = element30.Multiply(other);
                    }
                    if (element30 == element46)
                    {
                        element31 = element48;
                    }
                    break;
                }
                ECFieldElement element32 = rawXCoord.Subtract(element3);
                ECFieldElement element33 = rawYCoord.Subtract(element4);
                if (!element32.IsZero)
                {
                    ECFieldElement element34 = element32.Square();
                    ECFieldElement element35 = rawXCoord.Multiply(element34);
                    ECFieldElement element36 = element3.Multiply(element34);
                    ECFieldElement element37 = element35.Subtract(element36).Multiply(rawYCoord);
                    element28 = element33.Square().Subtract(element35).Subtract(element36);
                    element29 = element35.Subtract(element28).Multiply(element33).Subtract(element37);
                    element30 = element32;
                    if (isOne)
                    {
                        element31 = element34;
                    }
                    else
                    {
                        element30 = element30.Multiply(element26);
                    }
                    break;
                }
                if (!element33.IsZero)
                {
                    return(curve.Infinity);
                }
                return(this.Twice());
            }

            default:
                throw new InvalidOperationException("unsupported coordinate system");
            }
            if (coordinateSystem == 4)
            {
                ECFieldElement element51 = this.CalculateJacobianModifiedW(element30, element31);
                elementArray = new ECFieldElement[] { element30, element51 };
            }
            else
            {
                elementArray = new ECFieldElement[] { element30 };
            }
            return(new FpPoint(curve, element28, element29, elementArray, base.IsCompressed));
        }
Exemplo n.º 13
0
        public override ECPoint Add(ECPoint b)
        {
            if (base.IsInfinity)
            {
                return(b);
            }
            if (b.IsInfinity)
            {
                return(this);
            }
            if (this == b)
            {
                return(this.Twice());
            }
            ECCurve        curve            = this.Curve;
            int            coordinateSystem = curve.CoordinateSystem;
            ECFieldElement rawXCoord        = base.RawXCoord;
            ECFieldElement rawYCoord        = base.RawYCoord;
            ECFieldElement rawXCoord2       = b.RawXCoord;
            ECFieldElement rawYCoord2       = b.RawYCoord;

            switch (coordinateSystem)
            {
            case 0:
            {
                ECFieldElement eCFieldElement  = rawXCoord2.Subtract(rawXCoord);
                ECFieldElement eCFieldElement2 = rawYCoord2.Subtract(rawYCoord);
                if (!eCFieldElement.IsZero)
                {
                    ECFieldElement eCFieldElement3 = eCFieldElement2.Divide(eCFieldElement);
                    ECFieldElement eCFieldElement4 = eCFieldElement3.Square().Subtract(rawXCoord).Subtract(rawXCoord2);
                    ECFieldElement y = eCFieldElement3.Multiply(rawXCoord.Subtract(eCFieldElement4)).Subtract(rawYCoord);
                    return(new FpPoint(this.Curve, eCFieldElement4, y, base.IsCompressed));
                }
                if (eCFieldElement2.IsZero)
                {
                    return(this.Twice());
                }
                return(this.Curve.Infinity);
            }

            case 1:
            {
                ECFieldElement eCFieldElement5  = base.RawZCoords[0];
                ECFieldElement eCFieldElement6  = b.RawZCoords[0];
                bool           isOne            = eCFieldElement5.IsOne;
                bool           isOne2           = eCFieldElement6.IsOne;
                ECFieldElement eCFieldElement7  = isOne ? rawYCoord2 : rawYCoord2.Multiply(eCFieldElement5);
                ECFieldElement eCFieldElement8  = isOne2 ? rawYCoord : rawYCoord.Multiply(eCFieldElement6);
                ECFieldElement eCFieldElement9  = eCFieldElement7.Subtract(eCFieldElement8);
                ECFieldElement eCFieldElement10 = isOne ? rawXCoord2 : rawXCoord2.Multiply(eCFieldElement5);
                ECFieldElement b2 = isOne2 ? rawXCoord : rawXCoord.Multiply(eCFieldElement6);
                ECFieldElement eCFieldElement11 = eCFieldElement10.Subtract(b2);
                if (!eCFieldElement11.IsZero)
                {
                    ECFieldElement b3 = isOne ? eCFieldElement6 : (isOne2 ? eCFieldElement5 : eCFieldElement5.Multiply(eCFieldElement6));
                    ECFieldElement eCFieldElement12 = eCFieldElement11.Square();
                    ECFieldElement eCFieldElement13 = eCFieldElement12.Multiply(eCFieldElement11);
                    ECFieldElement eCFieldElement14 = eCFieldElement12.Multiply(b2);
                    ECFieldElement b4 = eCFieldElement9.Square().Multiply(b3).Subtract(eCFieldElement13).Subtract(this.Two(eCFieldElement14));
                    ECFieldElement x  = eCFieldElement11.Multiply(b4);
                    ECFieldElement y2 = eCFieldElement14.Subtract(b4).MultiplyMinusProduct(eCFieldElement9, eCFieldElement8, eCFieldElement13);
                    ECFieldElement eCFieldElement15 = eCFieldElement13.Multiply(b3);
                    return(new FpPoint(curve, x, y2, new ECFieldElement[]
                        {
                            eCFieldElement15
                        }, base.IsCompressed));
                }
                if (eCFieldElement9.IsZero)
                {
                    return(this.Twice());
                }
                return(curve.Infinity);
            }

            case 2:
            case 4:
            {
                ECFieldElement eCFieldElement16 = base.RawZCoords[0];
                ECFieldElement eCFieldElement17 = b.RawZCoords[0];
                bool           isOne3           = eCFieldElement16.IsOne;
                ECFieldElement zSquared         = null;
                ECFieldElement eCFieldElement22;
                ECFieldElement y3;
                ECFieldElement eCFieldElement23;
                if (!isOne3 && eCFieldElement16.Equals(eCFieldElement17))
                {
                    ECFieldElement eCFieldElement18 = rawXCoord.Subtract(rawXCoord2);
                    ECFieldElement eCFieldElement19 = rawYCoord.Subtract(rawYCoord2);
                    if (eCFieldElement18.IsZero)
                    {
                        if (eCFieldElement19.IsZero)
                        {
                            return(this.Twice());
                        }
                        return(curve.Infinity);
                    }
                    else
                    {
                        ECFieldElement eCFieldElement20 = eCFieldElement18.Square();
                        ECFieldElement eCFieldElement21 = rawXCoord.Multiply(eCFieldElement20);
                        ECFieldElement b5 = rawXCoord2.Multiply(eCFieldElement20);
                        ECFieldElement b6 = eCFieldElement21.Subtract(b5).Multiply(rawYCoord);
                        eCFieldElement22 = eCFieldElement19.Square().Subtract(eCFieldElement21).Subtract(b5);
                        y3 = eCFieldElement21.Subtract(eCFieldElement22).Multiply(eCFieldElement19).Subtract(b6);
                        eCFieldElement23 = eCFieldElement18;
                        if (isOne3)
                        {
                            zSquared = eCFieldElement20;
                        }
                        else
                        {
                            eCFieldElement23 = eCFieldElement23.Multiply(eCFieldElement16);
                        }
                    }
                }
                else
                {
                    ECFieldElement b7;
                    ECFieldElement b8;
                    if (isOne3)
                    {
                        b7 = rawXCoord2;
                        b8 = rawYCoord2;
                    }
                    else
                    {
                        ECFieldElement eCFieldElement24 = eCFieldElement16.Square();
                        b7 = eCFieldElement24.Multiply(rawXCoord2);
                        ECFieldElement eCFieldElement25 = eCFieldElement24.Multiply(eCFieldElement16);
                        b8 = eCFieldElement25.Multiply(rawYCoord2);
                    }
                    bool           isOne4 = eCFieldElement17.IsOne;
                    ECFieldElement eCFieldElement26;
                    ECFieldElement eCFieldElement27;
                    if (isOne4)
                    {
                        eCFieldElement26 = rawXCoord;
                        eCFieldElement27 = rawYCoord;
                    }
                    else
                    {
                        ECFieldElement eCFieldElement28 = eCFieldElement17.Square();
                        eCFieldElement26 = eCFieldElement28.Multiply(rawXCoord);
                        ECFieldElement eCFieldElement29 = eCFieldElement28.Multiply(eCFieldElement17);
                        eCFieldElement27 = eCFieldElement29.Multiply(rawYCoord);
                    }
                    ECFieldElement eCFieldElement30 = eCFieldElement26.Subtract(b7);
                    ECFieldElement eCFieldElement31 = eCFieldElement27.Subtract(b8);
                    if (eCFieldElement30.IsZero)
                    {
                        if (eCFieldElement31.IsZero)
                        {
                            return(this.Twice());
                        }
                        return(curve.Infinity);
                    }
                    else
                    {
                        ECFieldElement eCFieldElement32 = eCFieldElement30.Square();
                        ECFieldElement eCFieldElement33 = eCFieldElement32.Multiply(eCFieldElement30);
                        ECFieldElement eCFieldElement34 = eCFieldElement32.Multiply(eCFieldElement26);
                        eCFieldElement22 = eCFieldElement31.Square().Add(eCFieldElement33).Subtract(this.Two(eCFieldElement34));
                        y3 = eCFieldElement34.Subtract(eCFieldElement22).MultiplyMinusProduct(eCFieldElement31, eCFieldElement33, eCFieldElement27);
                        eCFieldElement23 = eCFieldElement30;
                        if (!isOne3)
                        {
                            eCFieldElement23 = eCFieldElement23.Multiply(eCFieldElement16);
                        }
                        if (!isOne4)
                        {
                            eCFieldElement23 = eCFieldElement23.Multiply(eCFieldElement17);
                        }
                        if (eCFieldElement23 == eCFieldElement30)
                        {
                            zSquared = eCFieldElement32;
                        }
                    }
                }
                ECFieldElement[] zs;
                if (coordinateSystem == 4)
                {
                    ECFieldElement eCFieldElement35 = this.CalculateJacobianModifiedW(eCFieldElement23, zSquared);
                    zs = new ECFieldElement[]
                    {
                        eCFieldElement23,
                        eCFieldElement35
                    };
                }
                else
                {
                    zs = new ECFieldElement[]
                    {
                        eCFieldElement23
                    };
                }
                return(new FpPoint(curve, eCFieldElement22, y3, zs, base.IsCompressed));
            }
            }
            throw new InvalidOperationException("unsupported coordinate system");
        }
Exemplo n.º 14
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");
        }