Exemplo n.º 1
0
        public static FixedPointPreCompInfo Precompute(ECPoint p, int minWidth)
        {
            ECCurve c = p.Curve;

            int n = 1 << minWidth;
            FixedPointPreCompInfo info = GetFixedPointPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME));

            ECPoint[] lookupTable = info.PreComp;

            if (lookupTable == null || lookupTable.Length < n)
            {
                int bits = GetCombSize(c);
                int d    = (bits + minWidth - 1) / minWidth;

                ECPoint[] pow2Table = new ECPoint[minWidth];
                pow2Table[0] = p;
                for (int i = 1; i < minWidth; ++i)
                {
                    pow2Table[i] = pow2Table[i - 1].TimesPow2(d);
                }

                c.NormalizeAll(pow2Table);

                lookupTable    = new ECPoint[n];
                lookupTable[0] = c.Infinity;

                for (int bit = minWidth - 1; bit >= 0; --bit)
                {
                    ECPoint pow2 = pow2Table[bit];

                    int step = 1 << bit;
                    for (int i = step; i < n; i += (step << 1))
                    {
                        lookupTable[i] = lookupTable[i - step].Add(pow2);
                    }
                }

                c.NormalizeAll(lookupTable);

                info.PreComp = lookupTable;
                info.Width   = minWidth;

                c.SetPreCompInfo(p, PRECOMP_NAME, info);
            }

            return(info);
        }
Exemplo n.º 2
0
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            ECCurve c    = p.Curve;
            int     size = FixedPointUtilities.GetCombSize(c);

            if (k.BitLength > size)
            {
                /*
                 * TODO The comb works best when the scalars are less than the (possibly unknown) order.
                 * Still, if we want to handle larger scalars, we could allow customization of the comb
                 * size, or alternatively we could deal with the 'extra' bits either by running the comb
                 * multiple times as necessary, or by using an alternative multiplier as prelude.
                 */
                throw new InvalidOperationException("fixed-point comb doesn't support scalars larger than the curve order");
            }

            int minWidth = GetWidthForCombSize(size);

            FixedPointPreCompInfo info = FixedPointUtilities.Precompute(p, minWidth);

            ECPoint[] lookupTable = info.PreComp;
            int       width       = info.Width;

            int d = (size + width - 1) / width;

            ECPoint R = c.Infinity;

            int top = d * width - 1;

            for (int i = 0; i < d; ++i)
            {
                int index = 0;

                for (int j = top - i; j >= 0; j -= d)
                {
                    index <<= 1;
                    if (k.TestBit(j))
                    {
                        index |= 1;
                    }
                }

                R = R.TwicePlus(lookupTable[index]);
            }

            return(R);
        }