4.3.1 Homomorphic Encryption
Last updated
Last updated
Flowchart:
Code example:
from seal import EncryptionParameters, SEALContext, KeyGenerator, Encryptor, Decryptor, Plaintext, Ciphertext
# Set homomorphic encryption parameters
params = EncryptionParameters()
params.set_poly_modulus("1x^4096 + 1")
params.set_coeff_modulus(seal.coeff_modulus_128(4096))
params.set_plain_modulus(1 << 8)
# Create homomorphic encryption context
context = SEALContext(params)
# Create key generator
keygen = KeyGenerator(context)
public_key = keygen.public_key()
secret_key = keygen.secret_key()
# Create encryptor and decryptor
encryptor = Encryptor(context, public_key)
decryptor = Decryptor(context, secret_key)
# Encrypt Data
plaintext = Plaintext("42")
ciphertext = Ciphertext()
encryptor.encrypt(plaintext, ciphertext)
# Perform homomorphic addition
addend = Plaintext("15")
encryptor.encrypt(addend, ciphertext)
ciphertext += ciphertext
# Perform homomorphic multiplication
multiplier = Plaintext("7")
encryptor.encrypt(multiplier, ciphertext)
ciphertext *= ciphertext
# Decrypted Result
result = Plaintext()
decryptor.decrypt(ciphertext, result)
print("Decrypted result:", result.to_string())