Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FixedPointCombMultiplier.java « ec « math « bouncycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 84fbf6a5a06dc4dd45faf73d510975b92603b145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.bouncycastle.math.ec;

import java.math.BigInteger;

public class FixedPointCombMultiplier extends AbstractECMultiplier
{
    protected ECPoint multiplyPositive(ECPoint p, BigInteger k)
    {
        ECCurve c = p.getCurve();
        int size = FixedPointUtil.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 IllegalStateException("fixed-point comb doesn't support scalars larger than the curve order");
        }

        // TODO Call method to let subclasses select width
        int width = size > 257 ? 6 : 5;

        FixedPointPreCompInfo info = FixedPointUtil.precompute(p, width);
        ECPoint[] lookupTable = info.getPreComp();

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

        ECPoint R = c.getInfinity();

        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;
    }
}