Precompute() public static method

public static Precompute ( ECPoint p, int width, bool includeNegated ) : WNafPreCompInfo
p ECPoint
width int
includeNegated bool
return WNafPreCompInfo
Example #1
0
        public static ECPoint MapPointWithPrecomp(ECPoint p, int width, bool includeNegated, ECPointMap pointMap)
        {
            ECCurve         curve            = p.Curve;
            WNafPreCompInfo wNafPreCompInfo  = WNafUtilities.Precompute(p, width, includeNegated);
            ECPoint         eCPoint          = pointMap.Map(p);
            WNafPreCompInfo wNafPreCompInfo2 = WNafUtilities.GetWNafPreCompInfo(curve.GetPreCompInfo(eCPoint, WNafUtilities.PRECOMP_NAME));
            ECPoint         twice            = wNafPreCompInfo.Twice;

            if (twice != null)
            {
                ECPoint twice2 = pointMap.Map(twice);
                wNafPreCompInfo2.Twice = twice2;
            }
            ECPoint[] preComp = wNafPreCompInfo.PreComp;
            ECPoint[] array   = new ECPoint[preComp.Length];
            for (int i = 0; i < preComp.Length; i++)
            {
                array[i] = pointMap.Map(preComp[i]);
            }
            wNafPreCompInfo2.PreComp = array;
            if (includeNegated)
            {
                ECPoint[] array2 = new ECPoint[array.Length];
                for (int j = 0; j < array2.Length; j++)
                {
                    array2[j] = array[j].Negate();
                }
                wNafPreCompInfo2.PreCompNeg = array2;
            }
            curve.SetPreCompInfo(eCPoint, WNafUtilities.PRECOMP_NAME, wNafPreCompInfo2);
            return(eCPoint);
        }
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int             num             = System.Math.Max(2, System.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   = System.Math.Abs(num4);
                ECPoint[] array2 = (num4 >= 0) ? preComp : preCompNeg;
                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  = System.Math.Abs(num13);
                ECPoint[] array3 = (num13 >= 0) ? preComp : preCompNeg;
                ECPoint   b      = array3[num14 >> 1];
                eCPoint = eCPoint.TwicePlus(b);
                eCPoint = eCPoint.TimesPow2(e);
            }
            return(eCPoint);
        }
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int             num             = Math.Max(2, Math.Min(16, this.GetWindowSize(k.BitLength)));
            WNafPreCompInfo wNafPreCompInfo = WNafUtilities.Precompute(p, num, true);

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

            if (i > 1)
            {
                int       num2   = array[--i];
                int       num3   = num2 >> 16;
                int       num4   = num2 & 65535;
                int       num5   = Math.Abs(num3);
                ECPoint[] array2 = (num3 < 0) ? preCompNeg : preComp;
                if (num5 << 2 < 1 << num)
                {
                    int num6  = (int)LongArray.BitLengths[num5];
                    int num7  = num - num6;
                    int num8  = num5 ^ 1 << num6 - 1;
                    int num9  = (1 << num - 1) - 1;
                    int num10 = (num8 << num7) + 1;
                    eCPoint = array2[num9 >> 1].Add(array2[num10 >> 1]);
                    num4   -= num7;
                }
                else
                {
                    eCPoint = array2[num5 >> 1];
                }
                eCPoint = eCPoint.TimesPow2(num4);
            }
            while (i > 0)
            {
                int       num11  = array[--i];
                int       num12  = num11 >> 16;
                int       e      = num11 & 65535;
                int       num13  = Math.Abs(num12);
                ECPoint[] array3 = (num12 < 0) ? preCompNeg : preComp;
                ECPoint   b      = array3[num13 >> 1];
                eCPoint = eCPoint.TwicePlus(b);
                eCPoint = eCPoint.TimesPow2(e);
            }
            return(eCPoint);
        }
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int             width = Math.Max(2, Math.Min(0x10, this.GetWindowSize(k.BitLength)));
            WNafPreCompInfo info  = WNafUtilities.Precompute(p, width, true);

            ECPoint[] preComp    = info.PreComp;
            ECPoint[] preCompNeg = info.PreCompNeg;
            int[]     numArray   = WNafUtilities.GenerateCompactWindowNaf(width, k);
            ECPoint   infinity   = p.Curve.Infinity;
            int       length     = numArray.Length;

            if (length > 1)
            {
                int       num3        = numArray[--length];
                int       num4        = num3 >> 0x10;
                int       e           = num3 & 0xffff;
                int       index       = Math.Abs(num4);
                ECPoint[] pointArray3 = (num4 >= 0) ? preComp : preCompNeg;
                if ((index << 2) < (((int)1) << width))
                {
                    int num7  = LongArray.BitLengths[index];
                    int num8  = width - num7;
                    int num9  = index ^ (((int)1) << (num7 - 1));
                    int num10 = (((int)1) << (width - 1)) - 1;
                    int num11 = (num9 << num8) + 1;
                    infinity = pointArray3[num10 >> 1].Add(pointArray3[num11 >> 1]);
                    e       -= num8;
                }
                else
                {
                    infinity = pointArray3[index >> 1];
                }
                infinity = infinity.TimesPow2(e);
            }
            while (length > 0)
            {
                int       num12       = numArray[--length];
                int       num13       = num12 >> 0x10;
                int       e           = num12 & 0xffff;
                int       num15       = Math.Abs(num13);
                ECPoint[] pointArray4 = (num13 >= 0) ? preComp : preCompNeg;
                ECPoint   b           = pointArray4[num15 >> 1];
                infinity = infinity.TwicePlus(b).TimesPow2(e);
            }
            return(infinity);
        }
        /**
         * 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;

                    //Console.WriteLine("Optimized: 2^" + scale + " * " + n + " = " + i1 + " + " + i2);
                }
                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);
        }