1package org.bouncycastle.math.ec;
2
3import java.math.BigInteger;
4
5/**
6 * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm.
7 */
8class FpNafMultiplier implements ECMultiplier
9{
10    /**
11     * D.3.2 pg 101
12     * @see org.bouncycastle.math.ec.ECMultiplier#multiply(org.bouncycastle.math.ec.ECPoint, java.math.BigInteger)
13     */
14    public ECPoint multiply(ECPoint p, BigInteger k, PreCompInfo preCompInfo)
15    {
16        // TODO Probably should try to add this
17        // BigInteger e = k.mod(n); // n == order of p
18        BigInteger e = k;
19        BigInteger h = e.multiply(BigInteger.valueOf(3));
20
21        ECPoint neg = p.negate();
22        ECPoint R = p;
23
24        for (int i = h.bitLength() - 2; i > 0; --i)
25        {
26            R = R.twice();
27
28            boolean hBit = h.testBit(i);
29            boolean eBit = e.testBit(i);
30
31            if (hBit != eBit)
32            {
33                R = R.add(hBit ? p : neg);
34            }
35        }
36
37        return R;
38    }
39}
40