package org.bouncycastle.math.ec;

import java.math.BigInteger;

/**
 * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm.
 */
public class FpNafMultiplier implements ECMultiplier
{
    /**
     * D.3.2 pg 101
     * @see org.bouncycastle.math.ec.ECMultiplier#multiply(org.bouncycastle.math.ec.ECPoint, java.math.BigInteger)
     */
    public ECPoint multiply(ECPoint p, BigInteger k, PreCompInfo preCompInfo)
    {
        // TODO Probably should try to add this
        // BigInteger e = k.mod(n); // n == order of p
        BigInteger e = k;
        BigInteger h = e.shiftLeft(1).add(e);

        p = p.normalize();

        ECPoint neg = p.negate();
        ECPoint R = p;

        for (int i = h.bitLength() - 2; i > 0; --i)
        {             
            boolean hBit = h.testBit(i);
            boolean eBit = e.testBit(i);

            if (hBit != eBit)
            {
                R = R.twicePlus(hBit ? p : neg);
            }
            else
            {
                R = R.twice();
            }
        }

        return R;
    }
}

@@ -444,172 +444,263 @@
     
-// TODO Probably should try to add this%0A        // BigInteger e = k.mod(n); // n == order of p%0A        BigInteger e = k;%0A        BigInteger h = e.shiftLeft(1).add(e
+if (k.signum() %3C 0)%0A        %7B%0A            throw new IllegalArgumentException(%22'k' cannot be negative%22);%0A        %7D%0A        if (k.signum() == 0)%0A        %7B%0A            return p.getCurve().getInfinity();%0A        %7D%0A%0A        byte%5B%5D wnaf = WNafUtil.generateNaf(k
 );%0A%0A
@@ -746,16 +746,17 @@
 oint neg
+P
  = p.neg
@@ -783,16 +783,41 @@
 nt R = p
+.getCurve().getInfinity()
 ;%0A%0A     
@@ -823,21 +823,16 @@
     
-for (
 int i = 
 h.bi
@@ -831,144 +831,85 @@
 i = 
-h.bitLength() - 2; i %3E 0; --i)%0A        %7B             %0A            boolean hBit = h.testBit(i);%0A            boolean eBit = e.testBit(i);%0A
+wnaf.length;%0A        while (--i %3E= 0)%0A        %7B%0A            int wi = wnaf%5Bi%5D;
 %0A   
@@ -925,20 +925,15 @@
 if (
-hBit != eBit
+wi == 0
 )%0A  
@@ -975,27 +975,9 @@
 wice
-Plus(hBit ? p : neg
+(
 );%0A 
@@ -1012,32 +1012,79 @@
 e%0A            %7B%0A
+                ECPoint r = wi %3E 0 ? p : negP;%0A
                 
@@ -1086,33 +1086,38 @@
      R = R.twice
-(
+Plus(r
 );%0A            %7D

Rewrite using WNafUtil.generateNaf instead of the inline Naf generation