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