Lightweight Partially Homomorphic Encryption Library for Python
LightPHE is a lightweight partially homomorphic encryption library for Python. It is a hybrid homomorphic encryption library wrapping many schemes such as RSA, ElGamal, Paillier, and more. It offers a more efficient and practical choice compared to fully homomorphic encryption for tasks that demand partial homomorphic capabilities, being faster, demanding fewer computational resources, and generating smaller ciphertexts. The library is actively maintained, with its latest release in March 2026.
Common errors
-
TypeError: unsupported operand type(s) for *: 'Ciphertext' and 'Ciphertext'
cause You are attempting to multiply two `Ciphertext` objects using an algorithm (e.g., Paillier) that is not multiplicatively homomorphic for ciphertexts.fixEnsure the chosen algorithm supports the desired homomorphic operation. For multiplicative operations on ciphertexts, consider algorithms like RSA. For scalar multiplication (ciphertext by a known plain constant), many additively homomorphic schemes like Paillier support it. Refer to the 'Summary of Homomorphic Features' in the LightPHE documentation. -
Message does not get decrypted in Python with LightPHE
cause This generic error often occurs if the cryptosystem was built with one algorithm but operations or decryption are attempted in a way incompatible with that algorithm's properties, or if the keys are mismatched/corrupted. Another common cause is attempting an operation that, while syntactically correct, breaks the homomorphic properties for the specific scheme.fixDouble-check that the `algorithm_name` used during `LightPHE` initialization is appropriate for your desired operations. Ensure that the `encrypt` and `decrypt` calls use the same `LightPHE` instance (or correctly serialized/deserialized keys). Verify that intermediate homomorphic operations respect the limitations of the chosen scheme (e.g., not performing multiplication if only addition is supported).
Warnings
- gotcha Attempting homomorphic operations not supported by the chosen algorithm will raise an exception. For example, Paillier is additively homomorphic but not multiplicatively homomorphic for two ciphertexts.
- gotcha Decryption for some algorithms, like Exponential ElGamal or Elliptic Curve ElGamal, involves solving the discrete logarithm problem. While the library handles this, very large plaintexts might impact practical decryption times or even theoretical feasibility if not carefully considered.
Install
-
pip install lightphe
Imports
- LightPHE
from lightphe.schemes import Paillier
from lightphe import LightPHE
Quickstart
from lightphe import LightPHE
# Build an additively homomorphic cryptosystem (e.g., Paillier)
# Other options: 'RSA', 'ElGamal', 'Exponential-ElGamal', 'Damgard-Jurik', etc.
cs = LightPHE(algorithm_name = "Paillier")
# Define plaintexts
m1 = 17
m2 = 21
# Encrypt plaintexts (private key is not required for encryption)
c1 = cs.encrypt(m1)
c2 = cs.encrypt(m2)
# Perform homomorphic addition on ciphertexts
c3 = c1 + c2
# Decrypt the result to get the sum of plaintexts
d3 = cs.decrypt(c3)
print(f"Plaintext 1: {m1}")
print(f"Plaintext 2: {m2}")
print(f"Ciphertext 1: {c1}")
print(f"Ciphertext 2: {c2}")
print(f"Homomorphic Sum (c1 + c2): {c3}")
print(f"Decrypted Sum: {d3}")
assert d3 == m1 + m2, "Decrypted sum does not match plaintext sum"
print("Assertion successful: Decrypted sum matches expected value.")