summary refs log tree commit diff
path: root/crypto/src/math/ec/multiplier/NafL2RMultiplier.cs
blob: ac80cf905d2bd2aed77401ae63cc5e203d8df703 (plain) (blame)
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
namespace Org.BouncyCastle.Math.EC.Multiplier
{
    /**
     * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm (left-to-right).
     */
    public class NafL2RMultiplier
        : AbstractECMultiplier
    {
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int[] naf = WNafUtilities.GenerateCompactNaf(k);

            ECPoint addP = p.Normalize(), subP = addP.Negate();

            ECPoint R = p.Curve.Infinity;

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