Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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));
            }
            }
        }