Esempio n. 1
0
    protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
    {
        int             num             = Math.Max(2, Math.Min(16, GetWindowSize(k.BitLength)));
        WNafPreCompInfo wNafPreCompInfo = WNafUtilities.Precompute(p, num, includeNegated: true);

        ECPoint[] preComp    = wNafPreCompInfo.PreComp;
        ECPoint[] preCompNeg = wNafPreCompInfo.PreCompNeg;
        int[]     array      = WNafUtilities.GenerateCompactWindowNaf(num, k);
        ECPoint   eCPoint    = p.Curve.Infinity;
        int       num2       = array.Length;

        if (num2 > 1)
        {
            int       num3   = array[--num2];
            int       num4   = num3 >> 16;
            int       num5   = num3 & 0xFFFF;
            int       num6   = Math.Abs(num4);
            ECPoint[] array2 = (num4 < 0) ? preCompNeg : preComp;
            if (num6 << 2 < 1 << num)
            {
                int num7  = LongArray.BitLengths[num6];
                int num8  = num - num7;
                int num9  = num6 ^ (1 << num7 - 1);
                int num10 = (1 << num - 1) - 1;
                int num11 = (num9 << num8) + 1;
                eCPoint = array2[num10 >> 1].Add(array2[num11 >> 1]);
                num5   -= num8;
            }
            else
            {
                eCPoint = array2[num6 >> 1];
            }
            eCPoint = eCPoint.TimesPow2(num5);
        }
        while (num2 > 0)
        {
            int       num12  = array[--num2];
            int       num13  = num12 >> 16;
            int       e      = num12 & 0xFFFF;
            int       num14  = Math.Abs(num13);
            ECPoint[] array3 = (num13 < 0) ? preCompNeg : preComp;
            ECPoint   b      = array3[num14 >> 1];
            eCPoint = eCPoint.TwicePlus(b);
            eCPoint = eCPoint.TimesPow2(e);
        }
        return(eCPoint);
    }
        /**
         * Multiplies <code>this</code> by an integer <code>k</code> using the
         * Window NAF method.
         * @param k The integer by which <code>this</code> is multiplied.
         * @return A new <code>ECPoint</code> which equals <code>this</code>
         * multiplied by <code>k</code>.
         */
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            // Clamp the window width in the range [2, 16]
            int width = System.Math.Max(2, System.Math.Min(16, GetWindowSize(k.BitLength)));

            WNafPreCompInfo wnafPreCompInfo = WNafUtilities.Precompute(p, width, true);

            ECPoint[] preComp    = wnafPreCompInfo.PreComp;
            ECPoint[] preCompNeg = wnafPreCompInfo.PreCompNeg;

            int[] wnaf = WNafUtilities.GenerateCompactWindowNaf(width, k);

            ECPoint R = p.Curve.Infinity;

            int i = wnaf.Length;

            /*
             * NOTE: We try to optimize the first window using the precomputed points to substitute an
             * addition for 2 or more doublings.
             */
            if (i > 1)
            {
                int wi = wnaf[--i];
                int digit = wi >> 16, zeroes = wi & 0xFFFF;

                int       n     = System.Math.Abs(digit);
                ECPoint[] table = digit < 0 ? preCompNeg : preComp;

                // Optimization can only be used for values in the lower half of the table
                if ((n << 2) < (1 << width))
                {
                    int highest = LongArray.BitLengths[n];

                    // TODO Get addition/doubling cost ratio from curve and compare to 'scale' to see if worth substituting?
                    int scale   = width - highest;
                    int lowBits = n ^ (1 << (highest - 1));

                    int i1 = ((1 << (width - 1)) - 1);
                    int i2 = (lowBits << scale) + 1;
                    R = table[i1 >> 1].Add(table[i2 >> 1]);

                    zeroes -= scale;
                }
                else
                {
                    R = table[n >> 1];
                }

                R = R.TimesPow2(zeroes);
            }

            while (i > 0)
            {
                int wi = wnaf[--i];
                int digit = wi >> 16, zeroes = wi & 0xFFFF;

                int       n     = System.Math.Abs(digit);
                ECPoint[] table = digit < 0 ? preCompNeg : preComp;
                ECPoint   r     = table[n >> 1];

                R = R.TwicePlus(r);
                R = R.TimesPow2(zeroes);
            }

            return(R);
        }