Divide() public abstract méthode

public abstract Divide ( ECFieldElement b ) : ECFieldElement
b ECFieldElement
Résultat ECFieldElement
Exemple #1
0
        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
        {
            ECFieldElement X = FromBigInteger(x), Y = FromBigInteger(y);

            switch (this.CoordinateSystem)
            {
            case COORD_LAMBDA_AFFINE:
            case COORD_LAMBDA_PROJECTIVE:
            {
                if (X.IsZero)
                {
                    if (!Y.Square().Equals(B))
                    {
                        throw new ArgumentException();
                    }
                }
                else
                {
                    // Y becomes Lambda (X + Y/X) here
                    Y = Y.Divide(X).Add(X);
                }
                break;
            }

            default:
            {
                break;
            }
            }

            return(CreateRawPoint(X, Y, withCompression));
        }
        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
        {
            //IL_0044: Unknown result type (might be due to invalid IL or missing references)
            ECFieldElement eCFieldElement  = FromBigInteger(x);
            ECFieldElement eCFieldElement2 = FromBigInteger(y);

            switch (CoordinateSystem)
            {
            case 5:
            case 6:
                if (eCFieldElement.IsZero)
                {
                    if (!eCFieldElement2.Square().Equals(B))
                    {
                        throw new ArgumentException();
                    }
                }
                else
                {
                    eCFieldElement2 = eCFieldElement2.Divide(eCFieldElement).Add(eCFieldElement);
                }
                break;
            }
            return(CreateRawPoint(eCFieldElement, eCFieldElement2, withCompression));
        }
        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
        {
            ECFieldElement eCFieldElement   = FromBigInteger(x);
            ECFieldElement eCFieldElement2  = FromBigInteger(y);
            int            coordinateSystem = CoordinateSystem;

            if (coordinateSystem == 5 || coordinateSystem == 6)
            {
                if (eCFieldElement.IsZero)
                {
                    if (!eCFieldElement2.Square().Equals(B))
                    {
                        throw new ArgumentException();
                    }
                }
                else
                {
                    eCFieldElement2 = eCFieldElement2.Divide(eCFieldElement).Add(eCFieldElement);
                }
            }
            return(CreateRawPoint(eCFieldElement, eCFieldElement2, withCompression));
        }
        public override ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompression)
        {
            ECFieldElement b        = this.FromBigInteger(x);
            ECFieldElement element2 = this.FromBigInteger(y);

            switch (this.CoordinateSystem)
            {
            case 5:
            case 6:
                if (b.IsZero)
                {
                    if (!element2.Square().Equals(this.B))
                    {
                        throw new ArgumentException();
                    }
                }
                else
                {
                    element2 = element2.Divide(b).Add(b);
                }
                break;
            }
            return(this.CreateRawPoint(b, element2, withCompression));
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
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");
            }
        }
Exemple #8
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));
        }
Exemple #9
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");
        }
Exemple #10
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");
        }
Exemple #11
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;
            }
        }
Exemple #12
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));
            }
            }
        }