Created
July 24, 2022 15:12
-
-
Save Keridos/abc81b44bf0de9bbf804ba0ec0ea7237 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import numpy as np | |
import seal | |
from seal import * | |
def print_vector(vector): | |
print('[ ', end='') | |
for i in range(0, 8): | |
print(vector[i], end=', ') | |
print('... ]') | |
def ckks_test(): | |
parms = EncryptionParameters(scheme_type.ckks) | |
poly_modulus_degree = 8192 | |
modulus = seal.Modulus(8192) | |
parms.set_poly_modulus_degree(poly_modulus_degree) | |
parms.set_coeff_modulus(CoeffModulus.Create(poly_modulus_degree, [60, 40, 40, 60])) | |
context = SEALContext(parms) | |
keygen = KeyGenerator(context) | |
secret_key = keygen.secret_key() | |
galois_keys = keygen.create_galois_keys() | |
public_key = keygen.create_public_key() | |
relin_keys = keygen.create_relin_keys() | |
encryptor = Encryptor(context, public_key) | |
evaluator = Evaluator(context) | |
decryptor = Decryptor(context, secret_key) | |
encoder = CKKSEncoder(context) | |
scale = np.power(2., 40) | |
x = 2. | |
y = 12. | |
x_plain = encoder.encode(x, scale) | |
y_plain = encoder.encode(y, scale) | |
pi_plain = encoder.encode(numpy.pi, scale) | |
x_encrypted = encryptor.encrypt(x_plain) | |
y_encrypted = encryptor.encrypt(y_plain) | |
x_squared_encrypted = evaluator.multiply(x_encrypted, x_encrypted) | |
evaluator.relinearize_inplace(x_squared_encrypted, relin_keys) | |
evaluator.rescale_to_next_inplace(x_squared_encrypted) | |
pi_x_encrypted = evaluator.multiply_plain(x_encrypted, pi_plain) | |
evaluator.rescale_to_next_inplace(pi_x_encrypted) | |
intermediate_encrypted = evaluator.multiply(pi_x_encrypted, x_squared_encrypted) | |
evaluator.relinearize_inplace(intermediate_encrypted, relin_keys) | |
evaluator.rescale_to_next_inplace(intermediate_encrypted) | |
last_parms_id = intermediate_encrypted.parms_id() | |
evaluator.mod_switch_to_inplace(y_encrypted, last_parms_id) | |
intermediate_encrypted.scale(np.power(2, 40)) | |
result_encrypted = evaluator.add(y_encrypted, intermediate_encrypted) | |
evaluator.relinearize_inplace(result_encrypted, relin_keys) | |
decrypted_result = decryptor.decrypt(result_encrypted) | |
pod_result = encoder.decode(decrypted_result) | |
print_vector(pod_result) | |
print('-' * 50) | |
if __name__ == "__main__": | |
ckks_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment