package org.bouncycastle.math.ec;

import java.math.BigInteger;

public class ECAlgorithms
{
    public static ECPoint sumOfTwoMultiplies(ECPoint P, BigInteger a,
        ECPoint Q, BigInteger b)
    {
        ECCurve cp = P.getCurve();
        Q = importPoint(cp, Q);

        // Point multiplication for Koblitz curves (using WTNAF) beats Shamir's trick
        if (cp instanceof ECCurve.F2m)
        {
            ECCurve.F2m f2mCurve = (ECCurve.F2m)cp;
            if (f2mCurve.isKoblitz())
            {
                return P.multiply(a).add(Q.multiply(b));
            }
        }

        return implShamirsTrick(P, a, Q, b);
    }

    /*
     * "Shamir's Trick", originally due to E. G. Straus
     * (Addition chains of vectors. American Mathematical Monthly,
     * 71(7):806-808, Aug./Sept. 1964)
     * <pre>
     * Input: The points P, Q, scalar k = (km?, ... , k1, k0)
     * and scalar l = (lm?, ... , l1, l0).
     * Output: R = k * P + l * Q.
     * 1: Z <- P + Q
     * 2: R <- O
     * 3: for i from m-1 down to 0 do
     * 4:        R <- R + R        {point doubling}
     * 5:        if (ki = 1) and (li = 0) then R <- R + P end if
     * 6:        if (ki = 0) and (li = 1) then R <- R + Q end if
     * 7:        if (ki = 1) and (li = 1) then R <- R + Z end if
     * 8: end for
     * 9: return R
     * </pre>
     */
    public static ECPoint shamirsTrick(ECPoint P, BigInteger k,
        ECPoint Q, BigInteger l)
    {
        ECCurve cp = P.getCurve();
        Q = importPoint(cp, Q);

        return implShamirsTrick(P, k, Q, l);
    }

    static ECPoint importPoint(ECCurve c, ECPoint Q)
    {
        ECCurve cq = Q.getCurve();
        if (!c.equals(cq))
        {
            throw new IllegalArgumentException("P and Q must be on same curve");
        }
        return c.importPoint(Q);
    }

    static void implMontgomeryTrick(ECFieldElement[] zs, int off, int len)
    {
        /*
         * Uses the "Montgomery Trick" to invert many field elements, with only a single actual
         * field inversion. See e.g. the paper:
         * "Fast Multi-scalar Multiplication Methods on Elliptic Curves with Precomputation Strategy Using Montgomery Trick"
         * by Katsuyuki Okeya, Kouichi Sakurai.
         */

        ECFieldElement[] c = new ECFieldElement[len];
        c[0] = zs[off];

        int i = 0;
        while (++i < len)
        {
            c[i] = c[i - 1].multiply(zs[off + i]);
        }

        ECFieldElement u = c[--i].invert();

        while (i > 0)
        {
            int j = off + i--;
            ECFieldElement tmp = zs[j];
            zs[j] = c[i].multiply(u);
            u = u.multiply(tmp);
        }

        zs[off] = u;
    }

    static ECPoint implShamirsTrick(ECPoint P, BigInteger k,
        ECPoint Q, BigInteger l)
    {
        P = P.normalize();
        Q = Q.normalize();

        ECPoint infinity = P.getCurve().getInfinity();

        // TODO conjugate co-Z addition (ZADDC) can return both of these
        ECPoint PaddQ = P.add(Q).normalize();
        ECPoint PsubQ = P.subtract(Q).normalize();

        ECPoint[] points = new ECPoint[] {
            PaddQ.negate(), P.negate(), PsubQ.negate(),
            Q.negate(), infinity, Q,
            PsubQ, P, PaddQ };

        byte[] kNaf = WNafUtil.generateNaf(k);
        byte[] lNaf = WNafUtil.generateNaf(l);

        ECPoint R = infinity;

        int i = Math.max(kNaf.length, lNaf.length);
        while (--i >= 0)
        {
            int kni = i < kNaf.length ? kNaf[i] + 1 : 1;
            int lni = i < lNaf.length ? lNaf[i] + 1 : 1;

            int index = kni * 3 + lni;
            R = R.twicePlus(points[index]);
        }

        return R;
    }
}

@@ -2812,55 +2812,38 @@
     
-P = P.normalize();%0A        Q = Q.normaliz
+ECCurve curve = P.getCurv
 e();%0A
-%0A
     
@@ -2865,28 +2865,21 @@
 inity = 
-P.getC
+c
 urve
-()
 .getInfi
@@ -2992,28 +2992,16 @@
 P.add(Q)
-.normalize()
 ;%0A      
@@ -3035,19 +3035,108 @@
 t(Q)
-.normalize(
+;%0A%0A        ECPoint%5B%5D points = new ECPoint%5B%5D%7B Q, PsubQ, P, PaddQ %7D;%0A        curve.normalizeAll(points
 );%0A%0A
@@ -3145,38 +3145,37 @@
       ECPoint%5B%5D 
-points
+table
  = new ECPoint%5B%5D
@@ -3189,21 +3189,25 @@
         
-PaddQ
+points%5B3%5D
 .negate(
@@ -3209,17 +3209,25 @@
 gate(), 
-P
+points%5B2%5D
 .negate(
@@ -3229,21 +3229,25 @@
 gate(), 
-PsubQ
+points%5B1%5D
 .negate(
@@ -3261,17 +3261,25 @@
         
-Q
+points%5B0%5D
 .negate(
@@ -3291,17 +3291,25 @@
 finity, 
-Q
+points%5B0%5D
 ,%0A      
@@ -3314,31 +3314,47 @@
         
-PsubQ, P, PaddQ
+points%5B1%5D, points%5B2%5D, points%5B3%5D
  %7D;%0A%0A   
@@ -3745,22 +3745,21 @@
 icePlus(
-points
+table
 %5Bindex%5D)

Use normalizeAll for table normalization in Shamir's Trick