Class PaillierCipher

java.lang.Object
edu.fiu.adwise.homomorphic_encryption.paillier.PaillierCipher
All Implemented Interfaces:
CipherConstants

public final class PaillierCipher extends Object implements CipherConstants
This class implements the Paillier cryptosystem, which supports homomorphic encryption operations on BigInteger values. It provides methods for encryption, decryption, addition, subtraction, scalar multiplication, and division (only works under VERY specific circumstances) of encrypted values, as well as utility methods for summation and sum-product calculations.

The Paillier cryptosystem is a probabilistic asymmetric encryption scheme that allows homomorphic addition and scalar multiplication of ciphertexts.

Note: This class is final and cannot be extended. It implements the CipherConstants interface.

For more details on the Paillier cryptosystem, refer to the original paper by Pascal Paillier: "Public-Key Cryptosystems Based on Composite Degree Residuosity Classes".

  • Constructor Details

    • PaillierCipher

      public PaillierCipher()
  • Method Details

    • encrypt

      public static BigInteger encrypt(BigInteger plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Encrypts a plaintext using the provided Paillier public key. The ciphertext is computed as g^m * r^n mod n^2.
      Parameters:
      plaintext - The plaintext to encrypt as a BigInteger.
      public_key - The PaillierPublicKey used for encryption.
      Returns:
      The encrypted ciphertext as a BigInteger.
      Throws:
      HomomorphicException - If the plaintext is negative or exceeds the supported range.
    • encrypt

      public static BigInteger encrypt(long plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Encrypts a plaintext represented as a long using the provided Paillier public key.
      Parameters:
      plaintext - The plaintext to encrypt as a long.
      public_key - The PaillierPublicKey used for encryption.
      Returns:
      The encrypted ciphertext as a BigInteger.
      Throws:
      HomomorphicException - If the plaintext is negative or exceeds the supported range.
    • decrypt

      public static BigInteger decrypt(BigInteger ciphertext, PaillierPrivateKey private_key) throws HomomorphicException
      Decrypts a ciphertext using the provided Paillier private key. The plaintext is computed as L(c^lambda mod n^2) * rho mod n.
      Parameters:
      ciphertext - The ciphertext to decrypt as a BigInteger.
      private_key - The PaillierPrivateKey used for decryption.
      Returns:
      The decrypted plaintext as a BigInteger.
      Throws:
      HomomorphicException - If the ciphertext is negative or exceeds the supported range.
    • add

      public static BigInteger add(BigInteger ciphertext1, BigInteger ciphertext2, PaillierPublicKey public_key) throws HomomorphicException
      Performs homomorphic addition of two Paillier encrypted values. The result is still encrypted and computed as the product of the two ciphertexts modulo n^2.

      Note: If the sum exceeds n, it is reduced modulo n.

      Parameters:
      ciphertext1 - The first encrypted Paillier value as a BigInteger.
      ciphertext2 - The second encrypted Paillier value as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt both ciphertexts.
      Returns:
      The encrypted sum of the two ciphertexts as a BigInteger.
      Throws:
      HomomorphicException - If either ciphertext is negative or exceeds n^2.
    • add_plaintext

      public static BigInteger add_plaintext(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Performs homomorphic addition of a Paillier encrypted value and a plaintext value. The result is encrypted and computed as the product of the ciphertext and g^plaintext modulo n^2.

      Note: If the sum exceeds n, it is reduced modulo n.

      Parameters:
      ciphertext - The encrypted Paillier value as a BigInteger.
      plaintext - The plaintext value to add as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt the ciphertext.
      Returns:
      The encrypted sum of the ciphertext and plaintext as a BigInteger.
      Throws:
      HomomorphicException - If the ciphertext is negative or exceeds n^2, or if the plaintext is negative or exceeds n.
    • subtract

      public static BigInteger subtract(BigInteger ciphertext1, BigInteger ciphertext2, PaillierPublicKey public_key) throws HomomorphicException
      Performs homomorphic subtraction of two Paillier encrypted values. The result is encrypted and computed as the product of the first ciphertext and the modular inverse of the second ciphertext modulo n^2.
      Parameters:
      ciphertext1 - The first encrypted Paillier value as a BigInteger.
      ciphertext2 - The second encrypted Paillier value as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt both ciphertexts.
      Returns:
      The encrypted result of ciphertext1 - ciphertext2 as a BigInteger.
      Throws:
      HomomorphicException - If either ciphertext is negative or exceeds n^2.
    • subtract_plaintext

      public static BigInteger subtract_plaintext(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Computes the encrypted Paillier value of the ciphertext subtracted by the plaintext. If the difference is negative, the result is adjusted by adding N.
      Parameters:
      ciphertext - The encrypted Paillier value as a BigInteger.
      plaintext - The plaintext value to subtract as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt the ciphertext.
      Returns:
      The encrypted result of ciphertext - plaintext as a BigInteger.
      Throws:
      HomomorphicException - If an error occurs during the operation.
    • subtract_ciphertext

      public static BigInteger subtract_ciphertext(BigInteger plaintext, BigInteger ciphertext, PaillierPublicKey public_key) throws HomomorphicException
      Computes the encrypted Paillier value of the plaintext subtracted by the ciphertext. This is equivalent to y - [x] = y + [-x] = [-x] + y.
      Parameters:
      plaintext - The plaintext value to subtract as a BigInteger.
      ciphertext - The encrypted Paillier value as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt the ciphertext.
      Returns:
      The encrypted result of plaintext - ciphertext as a BigInteger.
      Throws:
      HomomorphicException - If an error occurs during the operation.
    • multiply

      public static BigInteger multiply(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Computes the Paillier encrypted value of a ciphertext multiplied by a plaintext.
      Parameters:
      ciphertext - The Paillier encrypted value as a BigInteger.
      plaintext - The plaintext value to multiply the ciphertext with as a BigInteger.
      public_key - The PaillierPublicKey used to encrypt the ciphertext.
      Returns:
      The encrypted result of ciphertext * plaintext as a BigInteger.
      Throws:
      HomomorphicException - If the ciphertext is negative, exceeds n^2, or if the plaintext is negative or exceeds n.
    • multiply

      public static BigInteger multiply(BigInteger ciphertext1, long scalar, PaillierPublicKey public_key) throws HomomorphicException
      Computes the Paillier encrypted value of a ciphertext multiplied by a scalar.
      Parameters:
      ciphertext1 - The Paillier encrypted value as a BigInteger.
      scalar - The scalar value to multiply the ciphertext with as a long.
      public_key - The PaillierPublicKey used to encrypt the ciphertext.
      Returns:
      The encrypted result of ciphertext * scalar as a BigInteger.
      Throws:
      HomomorphicException - If the ciphertext is negative, exceeds n^2, or if the scalar is negative or exceeds n.
    • divide

      public static BigInteger divide(BigInteger ciphertext, BigInteger divisor, PaillierPublicKey public_key) throws HomomorphicException
      Compute the division of the Paillier cipher-text and a plaintext. Warning: Divide will only work correctly on perfect divisor like 2|20, it will work. If you try 3|20, it will NOT work, and you will get a wrong answer! If you want to do 3|20, you MUST use a division protocol from Veugen paper
      Parameters:
      ciphertext - - Paillier ciphertext
      divisor - - plaintext value
      public_key - - was used to encrypt ciphertext
      Returns:
      product - Encrypted Paillier value equal to ciphertext/plaintext
      Throws:
      HomomorphicException - - If an invalid input was found
    • sum

      public static BigInteger sum(BigInteger[] values, PaillierPublicKey public_key) throws HomomorphicException
      Compute the sum of the encrypted Paillier values
      Parameters:
      values - - Array of Encrypted Paillier values
      public_key - - PaillierPublicKey used to encrypt all the values
      Returns:
      sum - the encrypted sum of all values in the array
      Throws:
      HomomorphicException - - If an invalid input was found
    • sum

      public static BigInteger sum(BigInteger[] values, PaillierPublicKey public_key, int limit) throws HomomorphicException
      Compute the sum of the encrypted Paillier values
      Parameters:
      values - - Array of Encrypted Paillier values
      public_key - - PaillierPublicKey used to encrypt the values
      limit - - Sum values up to this index value in the array
      Returns:
      sum - the encrypted sum of all values in the array
      Throws:
      HomomorphicException - - If an invalid input was found
    • sum

      public static BigInteger sum(List<BigInteger> values, PaillierPublicKey public_key) throws HomomorphicException
      Compute the encrypted sum of all Paillier values
      Parameters:
      values - - List of Paillier encrypted values by PaillierPublicKey public_key
      public_key - - PaillierPublicKey used to encrypt every element in values list.
      Returns:
      sum - the encrypted sum of all values in the list
      Throws:
      HomomorphicException - - If an invalid input was found
    • sum

      public static BigInteger sum(List<BigInteger> values, PaillierPublicKey public_key, int limit) throws HomomorphicException
      Note: Compute the sum of all values in the list of Paillier Encrypted values.
      Parameters:
      values - - List of Encrypted Paillier values
      public_key - - PaillierPublicKey used to encrypt the list of values
      limit - - maximum index to sum up to in the area
      Returns:
      sum - the encrypted sum of all values in the list
      Throws:
      HomomorphicException - - If an invalid input was found
    • sum_product

      public static BigInteger sum_product(List<BigInteger> ciphertext, List<Long> plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Compute the sum-product. It computes the scalar multiplication between the array of Encrypted and plaintext values. Then it computes the encrypted sum.
      Parameters:
      public_key - - Paillier Public Key used to encrypt list of ciphertext
      ciphertext - - List of Paillier ciphertext
      plaintext - - List of plaintext
      Returns:
      Encrypted sum product
      Throws:
      HomomorphicException - - If the lists of encrypted values and plaintext values are not equal
    • sum_product

      public static BigInteger sum_product(BigInteger[] ciphertext, Long[] plaintext, PaillierPublicKey public_key) throws HomomorphicException
      Compute the sum-product. It computes the scalar multiplication between the array of Encrypted and plaintext values. Then it computes the encrypted sum.
      Parameters:
      ciphertext - - Array of Encrypted Paillier values
      plaintext - - Array of plaintext values
      public_key - - Paillier Public Key used to encrypt values in ciphertext list
      Returns:
      Encrypted sum-product
      Throws:
      HomomorphicException - - If the size of plaintext array and ciphertext array isn't equal