aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/main/java/org/spongycastle/math/ec/NafL2RMultiplier.java
blob: b2aac42c14d2f6b9200425bb63931126a36ee9df (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
package org.spongycastle.math.ec;

import java.math.BigInteger;

/**
 * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm (left-to-right).
 */
public class NafL2RMultiplier extends AbstractECMultiplier
{
    protected ECPoint multiplyPositive(ECPoint p, BigInteger k)
    {
        int[] naf = WNafUtil.generateCompactNaf(k);

        ECPoint addP = p.normalize(), subP = addP.negate();

        ECPoint R = p.getCurve().getInfinity();

        int i = naf.length;
        while (--i >= 0)
        {
            int ni = naf[i];
            int digit = ni >> 16, zeroes = ni & 0xFFFF;

            R = R.twicePlus(digit < 0 ? subP : addP);
            R = R.timesPow2(zeroes);
        }

        return R;
    }
}