/** * Normalization ensures that any projective coordinate is 1, and therefore that the x, y * coordinates reflect those of the equivalent point in an affine coordinate system. Where more * than one point is to be normalized, this method will generally be more efficient than * normalizing each point separately. * * @param points * An array of points that will be updated in place with their normalized versions, * where necessary */ public void normalizeAll(ECPoint[] points) { normalizeAll(points, 0, points.length, null); }
/** * Normalization ensures that any projective coordinate is 1, and therefore that the x, y * coordinates reflect those of the equivalent point in an affine coordinate system. Where more * than one point is to be normalized, this method will generally be more efficient than * normalizing each point separately. * * @param points * An array of points that will be updated in place with their normalized versions, * where necessary */ public void normalizeAll(ECPoint[] points) { normalizeAll(points, 0, points.length, null); }
/** * Does the precomputation for WTNAF multiplication. * @param p The <code>ECPoint</code> for which to do the precomputation. * @param a The parameter <code>a</code> of the elliptic curve. * @return The precomputation array for <code>p</code>. */ public static ECPoint.AbstractF2m[] getPreComp(ECPoint.AbstractF2m p, byte a) { byte[][] alphaTnaf = (a == 0) ? Tnaf.alpha0Tnaf : Tnaf.alpha1Tnaf; ECPoint.AbstractF2m[] pu = new ECPoint.AbstractF2m[(alphaTnaf.length + 1) >>> 1]; pu[0] = p; int precompLen = alphaTnaf.length; for (int i = 3; i < precompLen; i += 2) { pu[i >>> 1] = Tnaf.multiplyFromTnaf(p, alphaTnaf[i]); } p.getCurve().normalizeAll(pu); return pu; } }
p.getCurve().normalizeAll(pu);
}; curve.normalizeAll(points);
c.normalizeAll(pow2Table); c.normalizeAll(lookupTable);
curve.normalizeAll(ghTilde);
curve.normalizeAll(ghTilde);
c.normalizeAll(pow2Table); c.normalizeAll(lookupTable);
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECFixedTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = this.k.mod(n); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(cipherText.getX()), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * 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"); } ECDomainParameters ec = key.getParameters(); BigInteger k = ECUtil.generateK(ec.getN(), random); ECMultiplier basePointMultiplier = createBasePointMultiplier(); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(point) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: the input cipherText will * need to be preserved in order to complete the transformation to the new public key. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewPublicKeyTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * 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"); } ECDomainParameters ec = key.getParameters(); BigInteger k = ECUtil.generateK(ec.getN(), random); ECMultiplier basePointMultiplier = createBasePointMultiplier(); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), point)) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECFixedTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = this.k.mod(n); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: the input cipherText will * need to be preserved in order to complete the transformation to the new public key. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewPublicKeyTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher test pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewRandomnessTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(cipherText.getX()), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); lastK = k; return new ECPair(gamma_phi[0], gamma_phi[1]); }
static ECPoint implShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) { ECCurve curve = P.getCurve(); ECPoint infinity = curve.getInfinity(); // TODO conjugate co-Z addition (ZADDC) can return both of these ECPoint PaddQ = P.add(Q); ECPoint PsubQ = P.subtract(Q); ECPoint[] points = new ECPoint[]{ Q, PsubQ, P, PaddQ }; curve.normalizeAll(points); ECPoint[] table = new ECPoint[] { points[3].negate(), points[2].negate(), points[1].negate(), points[0].negate(), infinity, points[0], points[1], points[2], points[3] }; byte[] jsf = WNafUtil.generateJSF(k, l); ECPoint R = infinity; int i = jsf.length; while (--i >= 0) { int jsfi = jsf[i]; // NOTE: The shifting ensures the sign is extended correctly int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28); int index = 4 + (kDigit * 3) + lDigit; R = R.twicePlus(table[index]); } return R; }
static ECPoint implShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) { ECCurve curve = P.getCurve(); ECPoint infinity = curve.getInfinity(); // TODO conjugate co-Z addition (ZADDC) can return both of these ECPoint PaddQ = P.add(Q); ECPoint PsubQ = P.subtract(Q); ECPoint[] points = new ECPoint[]{ Q, PsubQ, P, PaddQ }; curve.normalizeAll(points); ECPoint[] table = new ECPoint[] { points[3].negate(), points[2].negate(), points[1].negate(), points[0].negate(), infinity, points[0], points[1], points[2], points[3] }; byte[] jsf = WNafUtil.generateJSF(k, l); ECPoint R = infinity; int i = jsf.length; while (--i >= 0) { int jsfi = jsf[i]; // NOTE: The shifting ensures the sign is extended correctly int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28); int index = 4 + (kDigit * 3) + lDigit; R = R.twicePlus(table[index]); } return R; }
/** * Transform an existing cipher test pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewRandomnessTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); lastK = k; return new ECPair(gamma_phi[0], gamma_phi[1]); }