package org.bouncycastle.crypto.ec;

import java.math.BigInteger;
import java.security.SecureRandom;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.math.ec.ECPoint;

/**
 * this does your basic ElGamal encryption algorithm using EC
 */
public class ECElGamalEncryptor
    implements ECEncryptor
{
    private ECPublicKeyParameters key;
    private SecureRandom          random;

    /**
     * initialise the encryptor.
     *
     * @param param the necessary EC key parameters.
     */
    public void init(
        CipherParameters    param)
    {
        if (param instanceof ParametersWithRandom)
        {
            ParametersWithRandom    p = (ParametersWithRandom)param;

            if (!(p.getParameters() instanceof ECPublicKeyParameters))
            {
                throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
            }
            this.key = (ECPublicKeyParameters)p.getParameters();
            this.random = p.getRandom();
        }
        else
        {
            if (!(param instanceof ECPublicKeyParameters))
            {
                throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
            }

            this.key = (ECPublicKeyParameters)param;
            this.random = new SecureRandom();
        }
    }

    /**
     * Process a single EC point using the basic ElGamal algorithm.
     *
     * @param point the EC point to process.
     * @return the result of the Elgamal process.
     */
    public ECPair encrypt(ECPoint point)
    {
        if (key == null)
        {
            throw new IllegalStateException("ECElGamalEncryptor not initialised");
        }

        BigInteger             n = key.getParameters().getN();
        BigInteger             k = ECUtil.generateK(n, random);

        ECPoint  g = key.getParameters().getG();
        ECPoint  gamma = g.multiply(k);
        ECPoint  phi = key.getQ().multiply(k).add(point);

        return new ECPair(gamma.normalize(), phi.normalize());
    }
}

@@ -136,32 +136,90 @@
 pherParameters;%0A
+import org.bouncycastle.crypto.params.ECDomainParameters;%0A
 import org.bounc
@@ -1909,32 +1909,29 @@
     
-BigInteger             n
+ECDomainParameters ec
  = k
@@ -1952,15 +1952,8 @@
 rs()
-.getN()
 ;%0A  
@@ -1969,28 +1969,16 @@
 Integer 
-            
 k = ECUt
@@ -1990,17 +1990,25 @@
 nerateK(
-n
+ec.getN()
 , random
@@ -2030,68 +2030,59 @@
 oint
-  g = key.getParameters().getG();%0A        ECPoint  gamma = g
+%5B%5D gamma_phi = new ECPoint%5B%5D%7B%0A            ec.getG()
 .mul
@@ -2089,17 +2089,17 @@
 tiply(k)
-;
+,
 %0A       
@@ -2103,22 +2103,11 @@
     
-ECPoint  phi =
+   
  key
@@ -2136,16 +2136,74 @@
 d(point)
+%0A        %7D;%0A%0A        ec.getCurve().normalizeAll(gamma_phi)
 ;%0A%0A     
@@ -2232,37 +2232,29 @@
 amma
-.normalize(), phi.normalize()
+_phi%5B0%5D, gamma_phi%5B1%5D
 );%0A 

Normalize both points at once (saving a field inversion)