Class ElGamalCipher

java.lang.Object
edu.fiu.adwise.homomorphic_encryption.elgamal.ElGamalCipher

public class ElGamalCipher extends Object
This class provides methods for encryption, decryption, and homomorphic operations using the ElGamal cryptosystem. It supports both additive and multiplicative modes.

Reference: Based on this ElGamal implementation in Python

  • Constructor Details

    • ElGamalCipher

      public ElGamalCipher()
  • Method Details

    • encrypt

      public static ElGamal_Ciphertext encrypt(BigInteger plaintext, ElGamalPublicKey public_key)
      Encrypts a plaintext message using the ElGamal public key.
      Parameters:
      plaintext - The plaintext message to encrypt.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The encrypted ciphertext.
    • encrypt

      public static ElGamal_Ciphertext encrypt(long plaintext, ElGamalPublicKey public_key)
      Encrypts a plaintext message (long) using the ElGamal public key.
      Parameters:
      plaintext - The plaintext message to encrypt.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The encrypted ciphertext.
    • decrypt

      public static BigInteger decrypt(ElGamal_Ciphertext ciphertext, ElGamalPrivateKey private_key)
      Decrypts a ciphertext using the ElGamal private key.
      Parameters:
      ciphertext - The ciphertext to decrypt.
      private_key - The ElGamal private key used for decryption.
      Returns:
      The decrypted plaintext message.
    • multiply_scalar

      public static ElGamal_Ciphertext multiply_scalar(ElGamal_Ciphertext ciphertext1, BigInteger scalar, ElGamalPublicKey public_key)
      Multiplies a ciphertext by a scalar value in additive mode.
      Parameters:
      ciphertext1 - The ciphertext to be multiplied.
      scalar - The scalar value to multiply the ciphertext by.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after multiplication.
      Throws:
      IllegalArgumentException - If the cipher is in multiplicative mode.
    • multiply_scalar

      public static ElGamal_Ciphertext multiply_scalar(ElGamal_Ciphertext ciphertext1, long scalar, ElGamalPublicKey public_key)
      Multiplies a ciphertext by a scalar value (long) in additive mode.
      Parameters:
      ciphertext1 - The ciphertext to be multiplied.
      scalar - The scalar value to multiply the ciphertext by.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after multiplication.
    • multiply

      public static ElGamal_Ciphertext multiply(ElGamal_Ciphertext ciphertext1, ElGamal_Ciphertext ciphertext2, ElGamalPublicKey public_key)
      Multiplies two ciphertexts in multiplicative mode.
      Parameters:
      ciphertext1 - The first ciphertext.
      ciphertext2 - The second ciphertext.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after multiplication.
      Throws:
      IllegalArgumentException - If the cipher is in additive mode.
    • divide

      public static ElGamal_Ciphertext divide(ElGamal_Ciphertext ciphertext1, ElGamal_Ciphertext ciphertext2, ElGamalPublicKey public_key)
      Divides one ciphertext by another in multiplicative mode.
      Parameters:
      ciphertext1 - The dividend ciphertext.
      ciphertext2 - The divisor ciphertext.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after division.
      Throws:
      IllegalArgumentException - If the cipher is in additive mode.
    • add

      public static ElGamal_Ciphertext add(ElGamal_Ciphertext ciphertext1, ElGamal_Ciphertext ciphertext2, ElGamalPublicKey public_key)
      Adds two ciphertexts in additive mode.
      Parameters:
      ciphertext1 - The first ciphertext.
      ciphertext2 - The second ciphertext.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after addition.
      Throws:
      IllegalArgumentException - If the cipher is in multiplicative mode.
    • subtract

      public static ElGamal_Ciphertext subtract(ElGamal_Ciphertext ciphertext1, ElGamal_Ciphertext ciphertext2, ElGamalPublicKey public_key)
      Subtracts one ciphertext from another in additive mode.
      Parameters:
      ciphertext1 - The ciphertext to subtract from.
      ciphertext2 - The ciphertext to subtract.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after subtraction.
      Throws:
      IllegalArgumentException - If the cipher is in multiplicative mode.
    • sum

      public static ElGamal_Ciphertext sum(List<ElGamal_Ciphertext> values, ElGamalPublicKey public_key, int limit) throws HomomorphicException
      Computes the sum of a list of ciphertexts in additive mode.
      Parameters:
      values - The list of ciphertexts to sum.
      public_key - The ElGamal public key used for encryption.
      limit - The maximum number of ciphertexts to sum.
      Returns:
      The resulting ciphertext after summation.
      Throws:
      HomomorphicException - If the cipher is not in additive mode.
    • sum

      public static ElGamal_Ciphertext sum(ElGamal_Ciphertext[] values, ElGamalPublicKey public_key, int limit) throws HomomorphicException
      Computes the sum of an array of ciphertexts in additive mode.
      Parameters:
      values - The array of ciphertexts to sum.
      public_key - The ElGamal public key used for encryption.
      limit - The maximum number of ciphertexts to sum.
      Returns:
      The resulting ciphertext after summation.
      Throws:
      HomomorphicException - If the cipher is not in additive mode.
    • sum_product

      public static ElGamal_Ciphertext sum_product(ElGamal_Ciphertext[] cipher, Long[] plain, ElGamalPublicKey public_key) throws HomomorphicException
      Computes the sum of products of ciphertexts and plaintexts in additive mode.
      Parameters:
      cipher - The array of ciphertexts.
      plain - The array of plaintexts.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after summation of products.
      Throws:
      HomomorphicException - If the cipher is not in additive mode.
      IllegalArgumentException - If the arrays are not of the same size.
    • sum_product

      public static ElGamal_Ciphertext sum_product(List<ElGamal_Ciphertext> cipher, List<Long> plain, ElGamalPublicKey public_key) throws HomomorphicException
      Computes the sum of products of the ciphertexts and plaintexts in additive mode.
      Parameters:
      cipher - The list of ciphertexts.
      plain - The list of plaintexts.
      public_key - The ElGamal public key used for encryption.
      Returns:
      The resulting ciphertext after summation of products.
      Throws:
      HomomorphicException - If the cipher is not in additive mode.
      IllegalArgumentException - If the lists are not of the same size.