summary refs log tree commit diff
path: root/crypto/src/math/ec/multiplier/FixedPointCombMultiplier.cs
blob: 51ed5d7c52dddce16795270e7258f9a2fc6b6329 (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
31
32
33
34
35
36
37
38
using System;

namespace Org.BouncyCastle.Math.EC.Multiplier
{
    public class FixedPointCombMultiplier
        : AbstractECMultiplier
    {
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int width = 4;

            FixedPointPreCompInfo info = FixedPointUtilities.Precompute(p, width);
            ECPoint[] lookupTable = info.PreComp;

            ECCurve c = p.Curve;
            int d = (c.Order.BitLength + width - 1) / width;

            ECPoint R = c.Infinity;

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

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

            return R;
        }
    }
}