|
| 1 | +from seal import * |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +def print_vector(vector): |
| 5 | + print('[ ', end='') |
| 6 | + for i in range(0, 8): |
| 7 | + print(vector[i], end=', ') |
| 8 | + print('... ]') |
| 9 | + |
| 10 | + |
| 11 | +def example_bgv_basics(): |
| 12 | + parms = EncryptionParameters (scheme_type.bgv) |
| 13 | + poly_modulus_degree = 8192 |
| 14 | + parms.set_poly_modulus_degree(poly_modulus_degree) |
| 15 | + parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree)) |
| 16 | + parms.set_plain_modulus(PlainModulus.Batching(poly_modulus_degree, 20)) |
| 17 | + context = SEALContext(parms) |
| 18 | + |
| 19 | + keygen = KeyGenerator(context) |
| 20 | + secret_key = keygen.secret_key() |
| 21 | + public_key = keygen.create_public_key() |
| 22 | + relin_keys = keygen.create_relin_keys() |
| 23 | + |
| 24 | + encryptor = Encryptor(context, public_key) |
| 25 | + evaluator = Evaluator(context) |
| 26 | + decryptor = Decryptor(context, secret_key) |
| 27 | + |
| 28 | + batch_encoder = BatchEncoder(context) |
| 29 | + slot_count = batch_encoder.slot_count() |
| 30 | + row_size = slot_count / 2 |
| 31 | + print(f'Plaintext matrix row size: {row_size}') |
| 32 | + |
| 33 | + pod_matrix = [0] * slot_count |
| 34 | + pod_matrix[0] = 1 |
| 35 | + pod_matrix[1] = 2 |
| 36 | + pod_matrix[2] = 3 |
| 37 | + pod_matrix[3] = 4 |
| 38 | + |
| 39 | + x_plain = batch_encoder.encode(pod_matrix) |
| 40 | + |
| 41 | + x_encrypted = encryptor.encrypt(x_plain) |
| 42 | + print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}') |
| 43 | + print('-'*50) |
| 44 | + |
| 45 | + x_squared = evaluator.square(x_encrypted) |
| 46 | + print(f'size of x_squared: {x_squared.size()}') |
| 47 | + evaluator.relinearize_inplace(x_squared, relin_keys) |
| 48 | + print(f'size of x_squared (after relinearization): {x_squared.size()}') |
| 49 | + print(f'noise budget in x_squared: {decryptor.invariant_noise_budget(x_squared)} bits') |
| 50 | + decrypted_result = decryptor.decrypt(x_squared) |
| 51 | + pod_result = batch_encoder.decode(decrypted_result) |
| 52 | + print_vector(pod_result) |
| 53 | + print('-'*50) |
| 54 | + |
| 55 | + x_4th = evaluator.square(x_squared) |
| 56 | + print(f'size of x_4th: {x_4th.size()}') |
| 57 | + evaluator.relinearize_inplace(x_4th, relin_keys) |
| 58 | + print(f'size of x_4th (after relinearization): { x_4th.size()}') |
| 59 | + print(f'noise budget in x_4th: {decryptor.invariant_noise_budget(x_4th)} bits') |
| 60 | + decrypted_result = decryptor.decrypt(x_4th) |
| 61 | + pod_result = batch_encoder.decode(decrypted_result) |
| 62 | + print_vector(pod_result) |
| 63 | + print('-'*50) |
| 64 | + |
| 65 | + x_8th = evaluator.square(x_4th) |
| 66 | + print(f'size of x_8th: {x_8th.size()}') |
| 67 | + evaluator.relinearize_inplace(x_8th, relin_keys) |
| 68 | + print(f'size of x_8th (after relinearization): { x_8th.size()}') |
| 69 | + print(f'noise budget in x_8th: {decryptor.invariant_noise_budget(x_8th)} bits') |
| 70 | + decrypted_result = decryptor.decrypt(x_8th) |
| 71 | + pod_result = batch_encoder.decode(decrypted_result) |
| 72 | + print_vector(pod_result) |
| 73 | + print('run out of noise budget') |
| 74 | + print('-'*100) |
| 75 | + |
| 76 | + x_encrypted = encryptor.encrypt(x_plain) |
| 77 | + print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}') |
| 78 | + print('-'*50) |
| 79 | + |
| 80 | + x_squared = evaluator.square(x_encrypted) |
| 81 | + print(f'size of x_squared: {x_squared.size()}') |
| 82 | + evaluator.relinearize_inplace(x_squared, relin_keys) |
| 83 | + evaluator.mod_switch_to_next_inplace(x_squared) |
| 84 | + print(f'noise budget in x_squared (with modulus switching): {decryptor.invariant_noise_budget(x_squared)} bits') |
| 85 | + decrypted_result = decryptor.decrypt(x_squared) |
| 86 | + pod_result = batch_encoder.decode(decrypted_result) |
| 87 | + print_vector(pod_result) |
| 88 | + print('-'*50) |
| 89 | + |
| 90 | + x_4th = evaluator.square(x_squared) |
| 91 | + print(f'size of x_4th: {x_4th.size()}') |
| 92 | + evaluator.relinearize_inplace(x_4th, relin_keys) |
| 93 | + evaluator.mod_switch_to_next_inplace(x_4th) |
| 94 | + print(f'size of x_4th (after relinearization): { x_4th.size()}') |
| 95 | + print(f'noise budget in x_4th (with modulus switching): {decryptor.invariant_noise_budget(x_4th)} bits') |
| 96 | + decrypted_result = decryptor.decrypt(x_4th) |
| 97 | + pod_result = batch_encoder.decode(decrypted_result) |
| 98 | + print_vector(pod_result) |
| 99 | + print('-'*50) |
| 100 | + |
| 101 | + x_8th = evaluator.square(x_4th) |
| 102 | + print(f'size of x_8th: {x_8th.size()}') |
| 103 | + evaluator.relinearize_inplace(x_8th, relin_keys) |
| 104 | + evaluator.mod_switch_to_next_inplace(x_8th) |
| 105 | + print(f'size of x_8th (after relinearization): { x_8th.size()}') |
| 106 | + print(f'noise budget in x_8th (with modulus switching): {decryptor.invariant_noise_budget(x_8th)} bits') |
| 107 | + decrypted_result = decryptor.decrypt(x_8th) |
| 108 | + pod_result = batch_encoder.decode(decrypted_result) |
| 109 | + print_vector(pod_result) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + example_bgv_basics() |
0 commit comments