Class PaillierCipher
java.lang.Object
edu.fiu.adwise.homomorphic_encryption.paillier.PaillierCipher
- All Implemented Interfaces:
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".
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BigIntegeradd(BigInteger ciphertext1, BigInteger ciphertext2, PaillierPublicKey public_key) Performs homomorphic addition of two Paillier encrypted values.static BigIntegeradd_plaintext(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) Performs homomorphic addition of a Paillier encrypted value and a plaintext value.static BigIntegerdecrypt(BigInteger ciphertext, PaillierPrivateKey private_key) Decrypts a ciphertext using the provided Paillier private key.static BigIntegerdivide(BigInteger ciphertext, BigInteger divisor, PaillierPublicKey public_key) Compute the division of the Paillier cipher-text and a plaintext.static BigIntegerencrypt(long plaintext, PaillierPublicKey public_key) Encrypts a plaintext represented as alongusing the provided Paillier public key.static BigIntegerencrypt(BigInteger plaintext, PaillierPublicKey public_key) Encrypts a plaintext using the provided Paillier public key.static BigIntegermultiply(BigInteger ciphertext1, long scalar, PaillierPublicKey public_key) Computes the Paillier encrypted value of a ciphertext multiplied by a scalar.static BigIntegermultiply(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) Computes the Paillier encrypted value of a ciphertext multiplied by a plaintext.static BigIntegersubtract(BigInteger ciphertext1, BigInteger ciphertext2, PaillierPublicKey public_key) Performs homomorphic subtraction of two Paillier encrypted values.static BigIntegersubtract_ciphertext(BigInteger plaintext, BigInteger ciphertext, PaillierPublicKey public_key) Computes the encrypted Paillier value of the plaintext subtracted by the ciphertext.static BigIntegersubtract_plaintext(BigInteger ciphertext, BigInteger plaintext, PaillierPublicKey public_key) Computes the encrypted Paillier value of the ciphertext subtracted by the plaintext.static BigIntegersum(BigInteger[] values, PaillierPublicKey public_key) Compute the sum of the encrypted Paillier valuesstatic BigIntegersum(BigInteger[] values, PaillierPublicKey public_key, int limit) Compute the sum of the encrypted Paillier valuesstatic BigIntegersum(List<BigInteger> values, PaillierPublicKey public_key) Compute the encrypted sum of all Paillier valuesstatic BigIntegersum(List<BigInteger> values, PaillierPublicKey public_key, int limit) Note: Compute the sum of all values in the list of Paillier Encrypted values.static BigIntegersum_product(BigInteger[] ciphertext, Long[] plaintext, PaillierPublicKey public_key) Compute the sum-product.static BigIntegersum_product(List<BigInteger> ciphertext, List<Long> plaintext, PaillierPublicKey public_key) Compute the sum-product.
-
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 asg^m * r^n mod n^2.- Parameters:
plaintext- The plaintext to encrypt as aBigInteger.public_key- ThePaillierPublicKeyused 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 alongusing the provided Paillier public key.- Parameters:
plaintext- The plaintext to encrypt as along.public_key- ThePaillierPublicKeyused 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 asL(c^lambda mod n^2) * rho mod n.- Parameters:
ciphertext- The ciphertext to decrypt as aBigInteger.private_key- ThePaillierPrivateKeyused 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 aBigInteger.ciphertext2- The second encrypted Paillier value as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.plaintext- The plaintext value to add as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.ciphertext2- The second encrypted Paillier value as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.plaintext- The plaintext value to subtract as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.ciphertext- The encrypted Paillier value as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.plaintext- The plaintext value to multiply the ciphertext with as aBigInteger.public_key- ThePaillierPublicKeyused 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 aBigInteger.scalar- The scalar value to multiply the ciphertext with as along.public_key- ThePaillierPublicKeyused 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 ciphertextdivisor- - plaintext valuepublic_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 valuespublic_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 valuespublic_key- - PaillierPublicKey used to encrypt the valueslimit- - 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_keypublic_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 valuespublic_key- - PaillierPublicKey used to encrypt the list of valueslimit- - 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 ciphertextciphertext- - List of Paillier ciphertextplaintext- - 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 valuesplaintext- - Array of plaintext valuespublic_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
-