def test_evaluator_galois(): parms = sealapi.EncryptionParameters(sealapi.SCHEME_TYPE.BFV) parms.set_poly_modulus_degree(8) parms.set_plain_modulus(257) coeff = sealapi.CoeffModulus.Create(8, [40, 40]) parms.set_coeff_modulus(coeff) ctx = sealapi.SEALContext(parms, False, sealapi.SEC_LEVEL_TYPE.NONE) keygen = sealapi.KeyGenerator(ctx) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) secret_key = keygen.secret_key() galois_keys = sealapi.GaloisKeys() keygen.create_galois_keys([1, 3, 5], galois_keys) decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key) evaluator = sealapi.Evaluator(ctx) plain = sealapi.Plaintext("1x^2") encrypted = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, encrypted) evaluator.apply_galois_inplace(encrypted, 1, galois_keys) decryptor.decrypt(encrypted, plain) assert plain.to_string() == "1x^2" out = sealapi.Ciphertext(ctx) evaluator.apply_galois(encrypted, 3, galois_keys, out) decryptor.decrypt(out, plain) assert plain.to_string() == "1x^6"
def test_evaluator_rotate_vector(): testcase = [complex(i, i) for i in range(4)] slot_size = len(testcase) delta = 1 << 30 parms = sealapi.EncryptionParameters(sealapi.SCHEME_TYPE.CKKS) poly_modulus = 2 * slot_size parms.set_poly_modulus_degree(poly_modulus) parms.set_plain_modulus(0) coeff = sealapi.CoeffModulus.Create(poly_modulus, [40, 40, 40, 40]) parms.set_coeff_modulus(coeff) ctx = sealapi.SEALContext(parms, False, sealapi.SEC_LEVEL_TYPE.NONE) ctx = helper_context_ckks() keygen = sealapi.KeyGenerator(ctx) galois_keys = sealapi.GaloisKeys() keygen.create_galois_keys(galois_keys) pk = sealapi.PublicKey() keygen.create_public_key(pk) decryptor = sealapi.Decryptor(ctx, keygen.secret_key()) encryptor = sealapi.Encryptor(ctx, pk) evaluator = sealapi.Evaluator(ctx) encoder = sealapi.CKKSEncoder(ctx) plain = sealapi.Plaintext() encoder.encode(testcase, ctx.first_parms_id(), delta, plain) encrypted = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, encrypted) # inplace steps = 1 evaluator.rotate_vector_inplace(encrypted, steps, galois_keys) decryptor.decrypt(encrypted, plain) decoded = encoder.decode_complex(plain)[:slot_size] for idx in range(slot_size): off = (idx + steps) % slot_size assert abs(testcase[off].real - decoded[idx].real) < 0.1 assert abs(testcase[off].imag - decoded[idx].imag) < 0.1 # to another ciphertext steps = -steps out = sealapi.Ciphertext(ctx) evaluator.rotate_vector(encrypted, steps, galois_keys, out) decryptor.decrypt(out, plain) decoded = encoder.decode_complex(plain)[:slot_size] for idx in range(slot_size): assert abs(testcase[idx].real - decoded[idx].real) < 0.1 assert abs(testcase[idx].imag - decoded[idx].imag) < 0.1
def test_evaluator_rotate_bfv(): parms = sealapi.EncryptionParameters(sealapi.SCHEME_TYPE.BFV) parms.set_poly_modulus_degree(8) parms.set_plain_modulus(257) coeff = sealapi.CoeffModulus.Create(8, [40, 40]) parms.set_coeff_modulus(coeff) ctx = sealapi.SEALContext.Create(parms, False, sealapi.SEC_LEVEL_TYPE.NONE) keygen = sealapi.KeyGenerator(ctx) galois_keys = keygen.galois_keys_local() decryptor = sealapi.Decryptor(ctx, keygen.secret_key()) encryptor = sealapi.Encryptor(ctx, keygen.public_key()) evaluator = sealapi.Evaluator(ctx) encoder = sealapi.BatchEncoder(ctx) testcase = [1, 2, 3, 4, 5, 6, 7, 8] # Input # 1, 2, 3, 4, # 5, 6, 7, 8 plain = sealapi.Plaintext() encoder.encode(testcase, plain) encrypted = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, encrypted) evaluator.rotate_columns_inplace(encrypted, galois_keys) decryptor.decrypt(encrypted, plain) # Rotate columns # 5, 6, 7, 8, # 1, 2, 3, 4 assert encoder.decode_int64(plain) == [5, 6, 7, 8, 1, 2, 3, 4] evaluator.rotate_rows_inplace(encrypted, -1, galois_keys) decryptor.decrypt(encrypted, plain) # Shift rows -1 # 8, 5, 6, 7, # 4, 1, 2, 3, assert encoder.decode_int64(plain) == [8, 5, 6, 7, 4, 1, 2, 3] cout = sealapi.Ciphertext(ctx) evaluator.rotate_rows(encrypted, 2, galois_keys, cout) decryptor.decrypt(cout, plain) # Shift rows +2 # 6, 7, 8, 5, # 2, 3, 4, 1, assert encoder.decode_int64(plain) == [6, 7, 8, 5, 2, 3, 4, 1] evaluator.rotate_columns(cout, galois_keys, encrypted) decryptor.decrypt(encrypted, plain) # Rotate columns # 2, 3, 4, 1, # 6, 7, 8, 5, assert encoder.decode_int64(plain) == [2, 3, 4, 1, 6, 7, 8, 5]
def helper_generate_evaluator(ctx): evaluator = sealapi.Evaluator(ctx) keygen = sealapi.KeyGenerator(ctx) public_key = keygen.public_key() secret_key = keygen.secret_key() relin_keys = keygen.relin_keys_local() decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key, secret_key) return evaluator, encryptor, decryptor, relin_keys
def helper_generate_evaluator(ctx): evaluator = sealapi.Evaluator(ctx) keygen = sealapi.KeyGenerator(ctx) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) secret_key = keygen.secret_key() relin_keys = sealapi.RelinKeys() keygen.create_relin_keys(relin_keys) decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key, secret_key) return evaluator, encryptor, decryptor, relin_keys
def test_evaluator_conjugate(): testcase = [complex(i, i) for i in range(32)] slot_size = len(testcase) delta = 1 << 30 parms = sealapi.EncryptionParameters(sealapi.SCHEME_TYPE.CKKS) poly_modulus = 2 * slot_size parms.set_poly_modulus_degree(poly_modulus) parms.set_plain_modulus(0) coeff = sealapi.CoeffModulus.Create(poly_modulus, [40, 40, 40, 40]) parms.set_coeff_modulus(coeff) ctx = sealapi.SEALContext.Create(parms, False, sealapi.SEC_LEVEL_TYPE.NONE) ctx = helper_context_ckks() keygen = sealapi.KeyGenerator(ctx) galois_keys = keygen.galois_keys_local() decryptor = sealapi.Decryptor(ctx, keygen.secret_key()) encryptor = sealapi.Encryptor(ctx, keygen.public_key()) evaluator = sealapi.Evaluator(ctx) encoder = sealapi.CKKSEncoder(ctx) plain = sealapi.Plaintext() encoder.encode(testcase, ctx.first_parms_id(), delta, plain) encrypted = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, encrypted) evaluator.complex_conjugate_inplace(encrypted, galois_keys) decryptor.decrypt(encrypted, plain) decoded = encoder.decode_complex(plain)[:slot_size] for idx in range(slot_size): assert abs(testcase[idx].real - decoded[idx].real) < 0.1 assert abs(testcase[idx].imag + decoded[idx].imag) < 0.1 out = sealapi.Ciphertext(ctx) evaluator.complex_conjugate(encrypted, galois_keys, out) decryptor.decrypt(out, plain) decoded = encoder.decode_complex(plain)[:slot_size] for idx in range(slot_size): assert abs(testcase[idx].real - decoded[idx].real) < 0.1 assert abs(testcase[idx].imag - decoded[idx].imag) < 0.1
def test_evaluator_mod_switch(): poly_modulus_degree = 8192 plain_modulus = 1032193 ctx = helper_context_bfv(poly_modulus_degree, plain_modulus) batchenc = sealapi.BatchEncoder(ctx) keygen = sealapi.KeyGenerator(ctx) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) secret_key = keygen.secret_key() decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key) evaluator = sealapi.Evaluator(ctx) # cphertext mod switch to next expected_value = [1, 2, 3, 4, 5] plain = sealapi.Plaintext() batchenc.encode(expected_value, plain) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_next_inplace(enc) after = decryptor.invariant_noise_budget(enc) assert before > after decryptor.decrypt(enc, out) assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value # ciphertext mod switch to next expected_value = [1, 2, 3, 4, 5] plain = sealapi.Plaintext() batchenc.encode(expected_value, plain) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_next(enc, cout) after = decryptor.invariant_noise_budget(cout) assert before > after decryptor.decrypt(cout, out) assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value # cphertext mod switch to inplace parms_id = ctx.last_parms_id() expected_value = [1, 2, 3, 4, 5] plain = sealapi.Plaintext() batchenc.encode(expected_value, plain) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_inplace(enc, parms_id) after = decryptor.invariant_noise_budget(enc) assert before > after decryptor.decrypt(enc, out) assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value assert enc.parms_id() == parms_id # ciphertext mod switch to parms_id = ctx.last_parms_id() expected_value = [1, 2, 3, 4, 5] plain = sealapi.Plaintext() batchenc.encode(expected_value, plain) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to(enc, parms_id, cout) after = decryptor.invariant_noise_budget(cout) assert before > after decryptor.decrypt(cout, out) assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value assert cout.parms_id() == parms_id pol_str = "1x^3 + 1x^1 + 3" # plaintext mod switch to next inplace plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id()) assert plain.is_ntt_form() is True evaluator.mod_switch_to_next_inplace(plain) assert plain.parms_id() != ctx.first_parms_id() # plaintext mod switch to next inplace failure plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.last_parms_id()) assert plain.is_ntt_form() is True with pytest.raises(BaseException): evaluator.mod_switch_to_next_inplace(plain) # plaintext mod switch to inplace plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id()) assert plain.is_ntt_form() is True evaluator.mod_switch_to_inplace(plain, ctx.last_parms_id()) assert plain.parms_id() == ctx.last_parms_id() # plaintext mod switch to next plain = sealapi.Plaintext(pol_str) plain_out = sealapi.Plaintext() evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out) assert plain_out.is_ntt_form() is True plain_next = sealapi.Plaintext() evaluator.mod_switch_to_next(plain_out, plain_next) assert plain_out.parms_id() == ctx.first_parms_id() assert plain_next.parms_id() != ctx.first_parms_id() # plaintext mod switch to plain = sealapi.Plaintext(pol_str) plain_out = sealapi.Plaintext() evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out) assert plain_out.is_ntt_form() is True plain_next = sealapi.Plaintext() evaluator.mod_switch_to(plain_out, ctx.last_parms_id(), plain_next) assert plain_out.parms_id() == ctx.first_parms_id() assert plain_next.parms_id() == ctx.last_parms_id()
def test_evaluator_mod_switch(): scheme = sealapi.SCHEME_TYPE.BFV parms = sealapi.EncryptionParameters(scheme) parms.set_poly_modulus_degree(128) parms.set_plain_modulus(1 << 6) coeff = sealapi.CoeffModulus.Create(128, [30, 30, 30]) parms.set_coeff_modulus(coeff) ctx = sealapi.SEALContext.Create(parms, True, sealapi.SEC_LEVEL_TYPE.NONE) intenc = sealapi.IntegerEncoder(ctx) keygen = sealapi.KeyGenerator(ctx) public_key = keygen.public_key() secret_key = keygen.secret_key() decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key) evaluator = sealapi.Evaluator(ctx) # cphertext mod switch to next expected_value = 1234 plain = intenc.encode(expected_value) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_next_inplace(enc) after = decryptor.invariant_noise_budget(enc) assert before > after decryptor.decrypt(enc, out) assert intenc.decode_int64(out) == expected_value # ciphertext mod switch to next plain = intenc.encode(expected_value) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_next(enc, cout) after = decryptor.invariant_noise_budget(cout) assert before > after decryptor.decrypt(cout, out) assert intenc.decode_int64(out) == expected_value # cphertext mod switch to inplace parms_id = ctx.last_parms_id() plain = intenc.encode(expected_value) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to_inplace(enc, parms_id) after = decryptor.invariant_noise_budget(enc) assert before > after decryptor.decrypt(enc, out) assert intenc.decode_int64(out) == expected_value assert enc.parms_id() == parms_id # ciphertext mod switch to parms_id = ctx.last_parms_id() plain = intenc.encode(expected_value) out = sealapi.Plaintext() enc = sealapi.Ciphertext(ctx) cout = sealapi.Ciphertext(ctx) encryptor.encrypt(plain, enc) before = decryptor.invariant_noise_budget(enc) evaluator.mod_switch_to(enc, parms_id, cout) after = decryptor.invariant_noise_budget(cout) assert before > after decryptor.decrypt(cout, out) assert intenc.decode_int64(out) == expected_value assert cout.parms_id() == parms_id pol_str = "1x^3 + 1x^1 + 3" # plaintext mod switch to next inplace plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id()) assert plain.is_ntt_form() is True evaluator.mod_switch_to_next_inplace(plain) assert plain.parms_id() != ctx.first_parms_id() # plaintext mod switch to next inplace failure plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.last_parms_id()) assert plain.is_ntt_form() is True with pytest.raises(BaseException): evaluator.mod_switch_to_next_inplace(plain) # plaintext mod switch to inplace plain = sealapi.Plaintext(pol_str) evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id()) assert plain.is_ntt_form() is True evaluator.mod_switch_to_inplace(plain, ctx.last_parms_id()) assert plain.parms_id() == ctx.last_parms_id() # plaintext mod switch to next plain = sealapi.Plaintext(pol_str) plain_out = sealapi.Plaintext() evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out) assert plain_out.is_ntt_form() is True plain_next = sealapi.Plaintext() evaluator.mod_switch_to_next(plain_out, plain_next) assert plain_out.parms_id() == ctx.first_parms_id() assert plain_next.parms_id() != ctx.first_parms_id() # plaintext mod switch to plain = sealapi.Plaintext(pol_str) plain_out = sealapi.Plaintext() evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out) assert plain_out.is_ntt_form() is True plain_next = sealapi.Plaintext() evaluator.mod_switch_to(plain_out, ctx.last_parms_id(), plain_next) assert plain_out.parms_id() == ctx.first_parms_id() assert plain_next.parms_id() == ctx.last_parms_id()