コード例 #1
0
 public X9ECParameters(ECCurve curve, X9ECPoint g, BigInteger n, BigInteger h, byte[] seed)
 {
     //IL_00a5: Unknown result type (might be due to invalid IL or missing references)
     //IL_00b0: Unknown result type (might be due to invalid IL or missing references)
     this.curve = curve;
     this.g     = g;
     this.n     = n;
     this.h     = h;
     this.seed  = seed;
     if (ECAlgorithms.IsFpCurve(curve))
     {
         fieldID = new X9FieldID(curve.Field.Characteristic);
         return;
     }
     if (ECAlgorithms.IsF2mCurve(curve))
     {
         IPolynomialExtensionField polynomialExtensionField = (IPolynomialExtensionField)curve.Field;
         int[] exponentsPresent = polynomialExtensionField.MinimalPolynomial.GetExponentsPresent();
         if (exponentsPresent.Length == 3)
         {
             fieldID = new X9FieldID(exponentsPresent[2], exponentsPresent[1]);
             return;
         }
         if (exponentsPresent.Length == 5)
         {
             fieldID = new X9FieldID(exponentsPresent[4], exponentsPresent[1], exponentsPresent[2], exponentsPresent[3]);
             return;
         }
         throw new ArgumentException("Only trinomial and pentomial curves are supported");
     }
     throw new ArgumentException("'curve' is of an unsupported type");
 }
コード例 #2
0
        public X9Curve(
            ECCurve curve,
            byte[] seed)
        {
            if (curve == null)
            {
                throw new ArgumentNullException("curve");
            }

            this.curve = curve;
            this.seed  = Arrays.Clone(seed);

            if (ECAlgorithms.IsFpCurve(curve))
            {
                this.fieldIdentifier = X9ObjectIdentifiers.PrimeField;
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                this.fieldIdentifier = X9ObjectIdentifiers.CharacteristicTwoField;
            }
            else
            {
                throw new ArgumentException("This type of ECCurve is not implemented");
            }
        }
コード例 #3
0
        private void ImplSqrtTest(ECCurve c)
        {
            if (ECAlgorithms.IsFpCurve(c))
            {
                BigInteger p                = c.Field.Characteristic;
                BigInteger pMinusOne        = p.Subtract(BigInteger.One);
                BigInteger legendreExponent = p.ShiftRight(1);

                int count = 0;
                while (count < 10)
                {
                    BigInteger nonSquare = BigIntegers.CreateRandomInRange(BigInteger.Two, pMinusOne, Random);
                    if (!nonSquare.ModPow(legendreExponent, p).Equals(BigInteger.One))
                    {
                        ECFieldElement root = c.FromBigInteger(nonSquare).Sqrt();
                        Assert.IsNull(root);
                        ++count;
                    }
                }
            }
            else if (ECAlgorithms.IsF2mCurve(c))
            {
                int            m  = c.FieldSize;
                BigInteger     x  = new BigInteger(m, Random);
                ECFieldElement fe = c.FromBigInteger(x);
                for (int i = 0; i < 100; ++i)
                {
                    ECFieldElement sq    = fe.Square();
                    ECFieldElement check = sq.Sqrt();
                    Assert.AreEqual(fe, check);
                    fe = sq;
                }
            }
        }
コード例 #4
0
 public X9ECParameters(ECCurve curve, X9ECPoint g, BigInteger n, BigInteger h, byte[] seed)
 {
     this.curve = curve;
     this.g     = g;
     this.n     = n;
     this.h     = h;
     this.seed  = seed;
     if (ECAlgorithms.IsFpCurve(curve))
     {
         this.fieldID = new X9FieldID(curve.Field.Characteristic);
     }
     else
     {
         if (!ECAlgorithms.IsF2mCurve(curve))
         {
             throw new ArgumentException("'curve' is of an unsupported type");
         }
         IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
         int[] exponentsPresent          = field.MinimalPolynomial.GetExponentsPresent();
         if (exponentsPresent.Length == 3)
         {
             this.fieldID = new X9FieldID(exponentsPresent[2], exponentsPresent[1]);
         }
         else
         {
             if (exponentsPresent.Length != 5)
             {
                 throw new ArgumentException("Only trinomial and pentomial curves are supported");
             }
             this.fieldID = new X9FieldID(exponentsPresent[4], exponentsPresent[1], exponentsPresent[2], exponentsPresent[3]);
         }
     }
 }
コード例 #5
0
ファイル: TlsEccUtilities.cs プロジェクト: zeilja/bc-csharp
        public static void WriteExplicitECParameters(byte[] ecPointFormats, ECDomainParameters ecParameters,
                                                     Stream output)
        {
            ECCurve curve = ecParameters.Curve;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                TlsUtilities.WriteUint8(ECCurveType.explicit_prime, output);

                WriteECParameter(curve.Field.Characteristic, output);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
                int[] exponents = field.MinimalPolynomial.GetExponentsPresent();

                TlsUtilities.WriteUint8(ECCurveType.explicit_char2, output);

                int m = exponents[exponents.Length - 1];
                TlsUtilities.CheckUint16(m);
                TlsUtilities.WriteUint16(m, output);

                if (exponents.Length == 3)
                {
                    TlsUtilities.WriteUint8(ECBasisType.ec_basis_trinomial, output);
                    WriteECExponent(exponents[1], output);
                }
                else if (exponents.Length == 5)
                {
                    TlsUtilities.WriteUint8(ECBasisType.ec_basis_pentanomial, output);
                    WriteECExponent(exponents[1], output);
                    WriteECExponent(exponents[2], output);
                    WriteECExponent(exponents[3], output);
                }
                else
                {
                    throw new ArgumentException("Only trinomial and pentomial curves are supported");
                }
            }
            else
            {
                throw new ArgumentException("'ecParameters' not a known curve type");
            }

            WriteECFieldElement(curve.A, output);
            WriteECFieldElement(curve.B, output);
            TlsUtilities.WriteOpaque8(SerializeECPoint(ecPointFormats, ecParameters.G), output);
            WriteECParameter(ecParameters.N, output);
            WriteECParameter(ecParameters.H, output);
        }
コード例 #6
0
ファイル: TlsEccUtilities.cs プロジェクト: zeilja/bc-csharp
        public static ECPoint DeserializeECPoint(byte[] ecPointFormats, ECCurve curve, byte[] encoding)
        {
            if (encoding == null || encoding.Length < 1)
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }

            byte actualFormat;

            switch (encoding[0])
            {
            case 0x02: // compressed
            case 0x03: // compressed
            {
                if (ECAlgorithms.IsF2mCurve(curve))
                {
                    actualFormat = ECPointFormat.ansiX962_compressed_char2;
                }
                else if (ECAlgorithms.IsFpCurve(curve))
                {
                    actualFormat = ECPointFormat.ansiX962_compressed_prime;
                }
                else
                {
                    throw new TlsFatalAlert(AlertDescription.illegal_parameter);
                }
                break;
            }

            case 0x04: // uncompressed
            {
                actualFormat = ECPointFormat.uncompressed;
                break;
            }

            case 0x00: // infinity
            case 0x06: // hybrid
            case 0x07: // hybrid
            default:
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }

            if (actualFormat != ECPointFormat.uncompressed &&
                (ecPointFormats == null || !Arrays.Contains(ecPointFormats, actualFormat)))
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }

            return(curve.DecodePoint(encoding));
        }
コード例 #7
0
        public static byte[] SerializeECPoint(byte[] ecPointFormats, ECPoint point)
        {
            ECCurve curve      = point.Curve;
            bool    compressed = false;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, 1);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, 2);
            }
            return(point.GetEncoded(compressed));
        }
コード例 #8
0
        public static void WriteExplicitECParameters(byte[] ecPointFormats, ECDomainParameters ecParameters, Stream output)
        {
            //IL_00b2: Unknown result type (might be due to invalid IL or missing references)
            //IL_00bd: Unknown result type (might be due to invalid IL or missing references)
            ECCurve curve = ecParameters.Curve;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                TlsUtilities.WriteUint8(1, output);
                WriteECParameter(curve.Field.Characteristic, output);
            }
            else
            {
                if (!ECAlgorithms.IsF2mCurve(curve))
                {
                    throw new ArgumentException("'ecParameters' not a known curve type");
                }
                IPolynomialExtensionField polynomialExtensionField = (IPolynomialExtensionField)curve.Field;
                int[] exponentsPresent = polynomialExtensionField.MinimalPolynomial.GetExponentsPresent();
                TlsUtilities.WriteUint8(2, output);
                int i = exponentsPresent[exponentsPresent.Length - 1];
                TlsUtilities.CheckUint16(i);
                TlsUtilities.WriteUint16(i, output);
                if (exponentsPresent.Length == 3)
                {
                    TlsUtilities.WriteUint8(1, output);
                    WriteECExponent(exponentsPresent[1], output);
                }
                else
                {
                    if (exponentsPresent.Length != 5)
                    {
                        throw new ArgumentException("Only trinomial and pentomial curves are supported");
                    }
                    TlsUtilities.WriteUint8(2, output);
                    WriteECExponent(exponentsPresent[1], output);
                    WriteECExponent(exponentsPresent[2], output);
                    WriteECExponent(exponentsPresent[3], output);
                }
            }
            WriteECFieldElement(curve.A, output);
            WriteECFieldElement(curve.B, output);
            TlsUtilities.WriteOpaque8(SerializeECPoint(ecPointFormats, ecParameters.G), output);
            WriteECParameter(ecParameters.N, output);
            WriteECParameter(ecParameters.H, output);
        }
コード例 #9
0
        private void ImplSqrtTest(ECCurve c)
        {
            if (ECAlgorithms.IsFpCurve(c))
            {
                BigInteger p                = c.Field.Characteristic;
                BigInteger pMinusOne        = p.Subtract(BigInteger.One);
                BigInteger legendreExponent = p.ShiftRight(1);

                int count = 0;
                while (count < 10)
                {
                    BigInteger nonSquare = BigIntegers.CreateRandomInRange(BigInteger.Two, pMinusOne, secRand);
                    if (!nonSquare.ModPow(legendreExponent, p).Equals(BigInteger.One))
                    {
                        ECFieldElement root = c.FromBigInteger(nonSquare).Sqrt();
                        Assert.IsNull(root);
                        ++count;
                    }
                }
            }
        }
コード例 #10
0
 public X9Curve(ECCurve curve, byte[] seed)
 {
     //IL_000e: Unknown result type (might be due to invalid IL or missing references)
     //IL_0054: Unknown result type (might be due to invalid IL or missing references)
     if (curve == null)
     {
         throw new ArgumentNullException("curve");
     }
     this.curve = curve;
     this.seed  = Arrays.Clone(seed);
     if (ECAlgorithms.IsFpCurve(curve))
     {
         fieldIdentifier = X9ObjectIdentifiers.PrimeField;
         return;
     }
     if (ECAlgorithms.IsF2mCurve(curve))
     {
         fieldIdentifier = X9ObjectIdentifiers.CharacteristicTwoField;
         return;
     }
     throw new ArgumentException("This type of ECCurve is not implemented");
 }
コード例 #11
0
ファイル: TlsEccUtilities.cs プロジェクト: zeilja/bc-csharp
        public static byte[] SerializeECPoint(byte[] ecPointFormats, ECPoint point)
        {
            ECCurve curve = point.Curve;

            /*
             * RFC 4492 5.7. ...an elliptic curve point in uncompressed or compressed format. Here, the
             * format MUST conform to what the server has requested through a Supported Point Formats
             * Extension if this extension was used, and MUST be uncompressed if this extension was not
             * used.
             */
            bool compressed = false;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, ECPointFormat.ansiX962_compressed_prime);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, ECPointFormat.ansiX962_compressed_char2);
            }
            return(point.GetEncoded(compressed));
        }
コード例 #12
0
        public static ECPoint DeserializeECPoint(byte[] ecPointFormats, ECCurve curve, byte[] encoding)
        {
            if (encoding == null || encoding.Length < 1)
            {
                throw new TlsFatalAlert(47);
            }
            byte b;

            switch (encoding[0])
            {
            case 2:
            case 3:
                if (ECAlgorithms.IsF2mCurve(curve))
                {
                    b = 2;
                }
                else
                {
                    if (!ECAlgorithms.IsFpCurve(curve))
                    {
                        throw new TlsFatalAlert(47);
                    }
                    b = 1;
                }
                break;

            case 4:
                b = 0;
                break;

            default:
                throw new TlsFatalAlert(47);
            }
            if (b != 0 && (ecPointFormats == null || !Arrays.Contains(ecPointFormats, b)))
            {
                throw new TlsFatalAlert(47);
            }
            return(curve.DecodePoint(encoding));
        }
コード例 #13
0
        public static ECPoint DeserializeECPoint(byte[] ecPointFormats, ECCurve curve, byte[] encoding)
        {
            byte num;

            if ((encoding == null) || (encoding.Length < 1))
            {
                throw new TlsFatalAlert(0x2f);
            }
            switch (encoding[0])
            {
            case 2:
            case 3:
                if (!ECAlgorithms.IsF2mCurve(curve))
                {
                    if (!ECAlgorithms.IsFpCurve(curve))
                    {
                        throw new TlsFatalAlert(0x2f);
                    }
                    num = 1;
                }
                else
                {
                    num = 2;
                }
                break;

            case 4:
                num = 0;
                break;

            default:
                throw new TlsFatalAlert(0x2f);
            }
            if ((num != 0) && ((ecPointFormats == null) || !Arrays.Contains(ecPointFormats, num)))
            {
                throw new TlsFatalAlert(0x2f);
            }
            return(curve.DecodePoint(encoding));
        }
コード例 #14
0
        public X9ECParameters(
            ECCurve curve,
            X9ECPoint g,
            BigInteger n,
            BigInteger h,
            byte[] seed)
        {
            this.Curve     = curve;
            this.BaseEntry = g;
            this.N         = n;
            this.H         = h;
            this.seed      = seed;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                this.FieldIDEntry = new X9FieldID(curve.Field.Characteristic);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                var field     = (IPolynomialExtensionField)curve.Field;
                var exponents = field.MinimalPolynomial.GetExponentsPresent();
                if (exponents.Length == 3)
                {
                    this.FieldIDEntry = new X9FieldID(exponents[2], exponents[1]);
                }
                else if (exponents.Length == 5)
                {
                    this.FieldIDEntry = new X9FieldID(exponents[4], exponents[1], exponents[2], exponents[3]);
                }
                else
                {
                    throw new ArgumentException("Only trinomial and pentomial curves are supported");
                }
            }
            else
            {
                throw new ArgumentException("'curve' is of an unsupported type");
            }
        }
コード例 #15
0
ファイル: WNafUtilities.cs プロジェクト: shojayxt/NStratis
        public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated)
        {
            ECCurve         c = p.Curve;
            WNafPreCompInfo wnafPreCompInfo = GetWNafPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME));

            int iniPreCompLen = 0, reqPreCompLen = 1 << System.Math.Max(0, width - 2);

            ECPoint[] preComp = wnafPreCompInfo.PreComp;
            if (preComp == null)
            {
                preComp = EMPTY_POINTS;
            }
            else
            {
                iniPreCompLen = preComp.Length;
            }

            if (iniPreCompLen < reqPreCompLen)
            {
                preComp = ResizeTable(preComp, reqPreCompLen);

                if (reqPreCompLen == 1)
                {
                    preComp[0] = p.Normalize();
                }
                else
                {
                    int curPreCompLen = iniPreCompLen;
                    if (curPreCompLen == 0)
                    {
                        preComp[0]    = p;
                        curPreCompLen = 1;
                    }

                    ECFieldElement iso = null;

                    if (reqPreCompLen == 2)
                    {
                        preComp[1] = p.ThreeTimes();
                    }
                    else
                    {
                        ECPoint twiceP = wnafPreCompInfo.Twice, last = preComp[curPreCompLen - 1];
                        if (twiceP == null)
                        {
                            twiceP = preComp[0].Twice();
                            wnafPreCompInfo.Twice = twiceP;

                            /*
                             * For Fp curves with Jacobian projective coordinates, use a (quasi-)isomorphism
                             * where 'twiceP' is "affine", so that the subsequent additions are cheaper. This
                             * also requires scaling the initial point's X, Y coordinates, and reversing the
                             * isomorphism as part of the subsequent normalization.
                             *
                             *  NOTE: The correctness of this optimization depends on:
                             *      1) additions do not use the curve's A, B coefficients.
                             *      2) no special cases (i.e. Q +/- Q) when calculating 1P, 3P, 5P, ...
                             */
                            if (ECAlgorithms.IsFpCurve(c) && c.FieldSize >= 64)
                            {
                                switch (c.CoordinateSystem)
                                {
                                case ECCurve.COORD_JACOBIAN:
                                case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
                                case ECCurve.COORD_JACOBIAN_MODIFIED:
                                {
                                    iso    = twiceP.GetZCoord(0);
                                    twiceP = c.CreatePoint(twiceP.XCoord.ToBigInteger(),
                                                           twiceP.YCoord.ToBigInteger());

                                    ECFieldElement iso2 = iso.Square(), iso3 = iso2.Multiply(iso);
                                    last = last.ScaleX(iso2).ScaleY(iso3);

                                    if (iniPreCompLen == 0)
                                    {
                                        preComp[0] = last;
                                    }
                                    break;
                                }
                                }
                            }
                        }

                        while (curPreCompLen < reqPreCompLen)
                        {
                            /*
                             * Compute the new ECPoints for the precomputation array. The values 1, 3,
                             * 5, ..., 2^(width-1)-1 times p are computed
                             */
                            preComp[curPreCompLen++] = last = last.Add(twiceP);
                        }
                    }

                    /*
                     * Having oft-used operands in affine form makes operations faster.
                     */
                    c.NormalizeAll(preComp, iniPreCompLen, reqPreCompLen - iniPreCompLen, iso);
                }
            }

            wnafPreCompInfo.PreComp = preComp;

            if (includeNegated)
            {
                ECPoint[] preCompNeg = wnafPreCompInfo.PreCompNeg;

                int pos;
                if (preCompNeg == null)
                {
                    pos        = 0;
                    preCompNeg = new ECPoint[reqPreCompLen];
                }
                else
                {
                    pos = preCompNeg.Length;
                    if (pos < reqPreCompLen)
                    {
                        preCompNeg = ResizeTable(preCompNeg, reqPreCompLen);
                    }
                }

                while (pos < reqPreCompLen)
                {
                    preCompNeg[pos] = preComp[pos].Negate();
                    ++pos;
                }

                wnafPreCompInfo.PreCompNeg = preCompNeg;
            }

            c.SetPreCompInfo(p, PRECOMP_NAME, wnafPreCompInfo);

            return(wnafPreCompInfo);
        }
コード例 #16
0
ファイル: WNafUtilities.cs プロジェクト: Siegema/socket-test
            public PreCompInfo Precompute(PreCompInfo existing)
            {
                WNafPreCompInfo existingWNaf = existing as WNafPreCompInfo;

                int width         = System.Math.Max(2, System.Math.Min(MAX_WIDTH, m_minWidth));
                int reqPreCompLen = 1 << (width - 2);

                if (CheckExisting(existingWNaf, width, reqPreCompLen, m_includeNegated))
                {
                    existingWNaf.DecrementPromotionCountdown();
                    return(existingWNaf);
                }

                WNafPreCompInfo result = new WNafPreCompInfo();

                ECCurve c = m_p.Curve;

                ECPoint[] preComp = null, preCompNeg = null;
                ECPoint   twiceP  = null;

                if (null != existingWNaf)
                {
                    int promotionCountdown = existingWNaf.DecrementPromotionCountdown();
                    result.PromotionCountdown = promotionCountdown;

                    int confWidth = existingWNaf.ConfWidth;
                    result.ConfWidth = confWidth;

                    preComp    = existingWNaf.PreComp;
                    preCompNeg = existingWNaf.PreCompNeg;
                    twiceP     = existingWNaf.Twice;
                }

                width         = System.Math.Min(MAX_WIDTH, System.Math.Max(result.ConfWidth, width));
                reqPreCompLen = 1 << (width - 2);

                int iniPreCompLen = 0;

                if (null == preComp)
                {
                    preComp = EMPTY_POINTS;
                }
                else
                {
                    iniPreCompLen = preComp.Length;
                }

                if (iniPreCompLen < reqPreCompLen)
                {
                    preComp = WNafUtilities.ResizeTable(preComp, reqPreCompLen);

                    if (reqPreCompLen == 1)
                    {
                        preComp[0] = m_p.Normalize();
                    }
                    else
                    {
                        int curPreCompLen = iniPreCompLen;
                        if (curPreCompLen == 0)
                        {
                            preComp[0]    = m_p;
                            curPreCompLen = 1;
                        }

                        ECFieldElement iso = null;

                        if (reqPreCompLen == 2)
                        {
                            preComp[1] = m_p.ThreeTimes();
                        }
                        else
                        {
                            ECPoint isoTwiceP = twiceP, last = preComp[curPreCompLen - 1];
                            if (null == isoTwiceP)
                            {
                                isoTwiceP = preComp[0].Twice();
                                twiceP    = isoTwiceP;

                                /*
                                 * For Fp curves with Jacobian projective coordinates, use a (quasi-)isomorphism
                                 * where 'twiceP' is "affine", so that the subsequent additions are cheaper. This
                                 * also requires scaling the initial point's X, Y coordinates, and reversing the
                                 * isomorphism as part of the subsequent normalization.
                                 *
                                 *  NOTE: The correctness of this optimization depends on:
                                 *      1) additions do not use the curve's A, B coefficients.
                                 *      2) no special cases (i.e. Q +/- Q) when calculating 1P, 3P, 5P, ...
                                 */
                                if (!twiceP.IsInfinity && ECAlgorithms.IsFpCurve(c) && c.FieldSize >= 64)
                                {
                                    switch (c.CoordinateSystem)
                                    {
                                    case ECCurve.COORD_JACOBIAN:
                                    case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
                                    case ECCurve.COORD_JACOBIAN_MODIFIED:
                                    {
                                        iso       = twiceP.GetZCoord(0);
                                        isoTwiceP = c.CreatePoint(twiceP.XCoord.ToBigInteger(),
                                                                  twiceP.YCoord.ToBigInteger());

                                        ECFieldElement iso2 = iso.Square(), iso3 = iso2.Multiply(iso);
                                        last = last.ScaleX(iso2).ScaleY(iso3);

                                        if (iniPreCompLen == 0)
                                        {
                                            preComp[0] = last;
                                        }
                                        break;
                                    }
                                    }
                                }
                            }

                            while (curPreCompLen < reqPreCompLen)
                            {
                                /*
                                 * Compute the new ECPoints for the precomputation array. The values 1, 3,
                                 * 5, ..., 2^(width-1)-1 times p are computed
                                 */
                                preComp[curPreCompLen++] = last = last.Add(isoTwiceP);
                            }
                        }

                        /*
                         * Having oft-used operands in affine form makes operations faster.
                         */
                        c.NormalizeAll(preComp, iniPreCompLen, reqPreCompLen - iniPreCompLen, iso);
                    }
                }

                if (m_includeNegated)
                {
                    int pos;
                    if (null == preCompNeg)
                    {
                        pos        = 0;
                        preCompNeg = new ECPoint[reqPreCompLen];
                    }
                    else
                    {
                        pos = preCompNeg.Length;
                        if (pos < reqPreCompLen)
                        {
                            preCompNeg = WNafUtilities.ResizeTable(preCompNeg, reqPreCompLen);
                        }
                    }

                    while (pos < reqPreCompLen)
                    {
                        preCompNeg[pos] = preComp[pos].Negate();
                        ++pos;
                    }
                }

                result.PreComp    = preComp;
                result.PreCompNeg = preCompNeg;
                result.Twice      = twiceP;
                result.Width      = width;
                return(result);
            }
コード例 #17
0
        public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated)
        {
            ECCurve         curve           = p.Curve;
            WNafPreCompInfo wNafPreCompInfo = WNafUtilities.GetWNafPreCompInfo(curve.GetPreCompInfo(p, WNafUtilities.PRECOMP_NAME));
            int             num             = 0;
            int             num2            = 1 << Math.Max(0, width - 2);

            ECPoint[] array = wNafPreCompInfo.PreComp;
            if (array == null)
            {
                array = WNafUtilities.EMPTY_POINTS;
            }
            else
            {
                num = array.Length;
            }
            if (num < num2)
            {
                array = WNafUtilities.ResizeTable(array, num2);
                if (num2 == 1)
                {
                    array[0] = p.Normalize();
                }
                else
                {
                    int i = num;
                    if (i == 0)
                    {
                        array[0] = p;
                        i        = 1;
                    }
                    ECFieldElement eCFieldElement = null;
                    if (num2 == 2)
                    {
                        array[1] = p.ThreeTimes();
                    }
                    else
                    {
                        ECPoint eCPoint  = wNafPreCompInfo.Twice;
                        ECPoint eCPoint2 = array[i - 1];
                        if (eCPoint == null)
                        {
                            eCPoint = array[0].Twice();
                            wNafPreCompInfo.Twice = eCPoint;
                            if (ECAlgorithms.IsFpCurve(curve) && curve.FieldSize >= 64)
                            {
                                switch (curve.CoordinateSystem)
                                {
                                case 2:
                                case 3:
                                case 4:
                                {
                                    eCFieldElement = eCPoint.GetZCoord(0);
                                    eCPoint        = curve.CreatePoint(eCPoint.XCoord.ToBigInteger(), eCPoint.YCoord.ToBigInteger());
                                    ECFieldElement eCFieldElement2 = eCFieldElement.Square();
                                    ECFieldElement scale           = eCFieldElement2.Multiply(eCFieldElement);
                                    eCPoint2 = eCPoint2.ScaleX(eCFieldElement2).ScaleY(scale);
                                    if (num == 0)
                                    {
                                        array[0] = eCPoint2;
                                    }
                                    break;
                                }
                                }
                            }
                        }
                        while (i < num2)
                        {
                            eCPoint2 = (array[i++] = eCPoint2.Add(eCPoint));
                        }
                    }
                    curve.NormalizeAll(array, num, num2 - num, eCFieldElement);
                }
            }
            wNafPreCompInfo.PreComp = array;
            if (includeNegated)
            {
                ECPoint[] array2 = wNafPreCompInfo.PreCompNeg;
                int       j;
                if (array2 == null)
                {
                    j      = 0;
                    array2 = new ECPoint[num2];
                }
                else
                {
                    j = array2.Length;
                    if (j < num2)
                    {
                        array2 = WNafUtilities.ResizeTable(array2, num2);
                    }
                }
                while (j < num2)
                {
                    array2[j] = array[j].Negate();
                    j++;
                }
                wNafPreCompInfo.PreCompNeg = array2;
            }
            curve.SetPreCompInfo(p, WNafUtilities.PRECOMP_NAME, wNafPreCompInfo);
            return(wNafPreCompInfo);
        }
コード例 #18
0
        public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated)
        {
            ECCurve         c = p.Curve;
            WNafPreCompInfo wNafPreCompInfo = GetWNafPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME));
            int             off             = 0;
            int             length          = ((int)1) << Math.Max(0, width - 2);

            ECPoint[] preComp = wNafPreCompInfo.PreComp;
            if (preComp == null)
            {
                preComp = EMPTY_POINTS;
            }
            else
            {
                off = preComp.Length;
            }
            if (off < length)
            {
                preComp = ResizeTable(preComp, length);
                if (length == 1)
                {
                    preComp[0] = p.Normalize();
                }
                else
                {
                    int num3 = off;
                    if (num3 == 0)
                    {
                        preComp[0] = p;
                        num3       = 1;
                    }
                    ECFieldElement b = null;
                    if (length == 2)
                    {
                        preComp[1] = p.ThreeTimes();
                    }
                    else
                    {
                        ECPoint twice  = wNafPreCompInfo.Twice;
                        ECPoint point2 = preComp[num3 - 1];
                        if (twice == null)
                        {
                            twice = preComp[0].Twice();
                            wNafPreCompInfo.Twice = twice;
                            if (ECAlgorithms.IsFpCurve(c) && (c.FieldSize >= 0x40))
                            {
                                switch (c.CoordinateSystem)
                                {
                                case 2:
                                case 3:
                                case 4:
                                {
                                    b     = twice.GetZCoord(0);
                                    twice = c.CreatePoint(twice.XCoord.ToBigInteger(), twice.YCoord.ToBigInteger());
                                    ECFieldElement scale    = b.Square();
                                    ECFieldElement element3 = scale.Multiply(b);
                                    point2 = point2.ScaleX(scale).ScaleY(element3);
                                    if (off == 0)
                                    {
                                        preComp[0] = point2;
                                    }
                                    break;
                                }
                                }
                            }
                        }
                        while (num3 < length)
                        {
                            preComp[num3++] = point2 = point2.Add(twice);
                        }
                    }
                    c.NormalizeAll(preComp, off, length - off, b);
                }
            }
            wNafPreCompInfo.PreComp = preComp;
            if (includeNegated)
            {
                int       num5;
                ECPoint[] preCompNeg = wNafPreCompInfo.PreCompNeg;
                if (preCompNeg == null)
                {
                    num5       = 0;
                    preCompNeg = new ECPoint[length];
                }
                else
                {
                    num5 = preCompNeg.Length;
                    if (num5 < length)
                    {
                        preCompNeg = ResizeTable(preCompNeg, length);
                    }
                }
                while (num5 < length)
                {
                    preCompNeg[num5] = preComp[num5].Negate();
                    num5++;
                }
                wNafPreCompInfo.PreCompNeg = preCompNeg;
            }
            c.SetPreCompInfo(p, PRECOMP_NAME, wNafPreCompInfo);
            return(wNafPreCompInfo);
        }