|
| 1 | +import numpy as np |
| 2 | +from seal import * |
| 3 | +from seal_helper import print_example_banner, print_parameters, print_vector |
| 4 | + |
| 5 | + |
| 6 | +def bfv_batch_encoder_example(): |
| 7 | + print_example_banner("Example: Encoders / BFV Batching") |
| 8 | + |
| 9 | + parms = EncryptionParameters(scheme_type.bfv) |
| 10 | + poly_modulus_degree = 8192 |
| 11 | + parms.set_poly_modulus_degree(poly_modulus_degree) |
| 12 | + parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree)) |
| 13 | + parms.set_plain_modulus(PlainModulus.Batching(poly_modulus_degree, 20)) |
| 14 | + |
| 15 | + context = SEALContext(parms) |
| 16 | + print_parameters(context) |
| 17 | + |
| 18 | + keygen = KeyGenerator(context) |
| 19 | + encryptor = Encryptor(context, keygen.create_public_key()) |
| 20 | + evaluator = Evaluator(context) |
| 21 | + decryptor = Decryptor(context, keygen.secret_key()) |
| 22 | + batch_encoder = BatchEncoder(context) |
| 23 | + |
| 24 | + slot_count = batch_encoder.slot_count() |
| 25 | + pod_matrix = [0] * slot_count |
| 26 | + pod_matrix[0:8] = [0, 1, 2, 3, 4, 5, 6, 7] |
| 27 | + |
| 28 | + plain_matrix = batch_encoder.encode(pod_matrix) |
| 29 | + encrypted_matrix = encryptor.encrypt(plain_matrix) |
| 30 | + |
| 31 | + pod_matrix2 = [((i & 1) + 1) for i in range(slot_count)] |
| 32 | + plain_matrix2 = batch_encoder.encode(pod_matrix2) |
| 33 | + |
| 34 | + encrypted_matrix = evaluator.add_plain(encrypted_matrix, plain_matrix2) |
| 35 | + evaluator.square_inplace(encrypted_matrix) |
| 36 | + evaluator.relinearize_inplace(encrypted_matrix, keygen.create_relin_keys()) |
| 37 | + |
| 38 | + plain_result = decryptor.decrypt(encrypted_matrix) |
| 39 | + pod_result = batch_encoder.decode(plain_result) |
| 40 | + print_vector(pod_result.astype(np.float64), 8, 0) |
| 41 | + |
| 42 | + |
| 43 | +def ckks_encoder_example(): |
| 44 | + print_example_banner("Example: Encoders / CKKS") |
| 45 | + |
| 46 | + parms = EncryptionParameters(scheme_type.ckks) |
| 47 | + poly_modulus_degree = 8192 |
| 48 | + parms.set_poly_modulus_degree(poly_modulus_degree) |
| 49 | + parms.set_coeff_modulus(CoeffModulus.Create(poly_modulus_degree, [40, 40, 40, 40, 40])) |
| 50 | + context = SEALContext(parms) |
| 51 | + |
| 52 | + encoder = CKKSEncoder(context) |
| 53 | + keygen = KeyGenerator(context) |
| 54 | + encryptor = Encryptor(context, keygen.create_public_key()) |
| 55 | + evaluator = Evaluator(context) |
| 56 | + decryptor = Decryptor(context, keygen.secret_key()) |
| 57 | + relin_keys = keygen.create_relin_keys() |
| 58 | + |
| 59 | + input_vec = [0.0, 1.1, 2.2, 3.3] |
| 60 | + scale = 2.0 ** 30 |
| 61 | + plain = encoder.encode(input_vec, scale) |
| 62 | + encrypted = encryptor.encrypt(plain) |
| 63 | + |
| 64 | + evaluator.square_inplace(encrypted) |
| 65 | + evaluator.relinearize_inplace(encrypted, relin_keys) |
| 66 | + |
| 67 | + output_plain = decryptor.decrypt(encrypted) |
| 68 | + output = encoder.decode(output_plain) |
| 69 | + print_vector(output, 4, 4) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + bfv_batch_encoder_example() |
| 74 | + ckks_encoder_example() |
0 commit comments