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_ckks_encoder(testcase): ctx = helper_context_ckks() encoder = sealapi.CKKSEncoder(ctx) plaintext = sealapi.Plaintext() encoder.encode(testcase, 2**40, plaintext) out = encoder.decode_double(plaintext) is_close_enough(out, testcase) keygen = sealapi.KeyGenerator(ctx) pk = sealapi.PublicKey() public_key = keygen.create_public_key(pk) secret_key = keygen.secret_key() decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, pk, secret_key) plaintext = sealapi.Plaintext() ciphertext = sealapi.Ciphertext(ctx) encoder.encode(testcase, 2**40, plaintext) encryptor.encrypt(plaintext, ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) decrypted = encoder.decode_double(plaintext) is_close_enough(decrypted, testcase)
def test_valcheck(check): ctx = helper_context_bfv(8192) other_ctx = helper_context_ckks(8192) invalid_ctx = helper_context_invalid() ciphertext = sealapi.Ciphertext(ctx) assert check(ciphertext, ctx) is True assert check(ciphertext, other_ctx) is False assert check(ciphertext, invalid_ctx) is False assert sealapi.is_buffer_valid(ciphertext) is True plaintext = sealapi.Plaintext() assert check(plaintext, ctx) is True assert check(plaintext, other_ctx) is True assert check(plaintext, invalid_ctx) is False keygen = sealapi.KeyGenerator(ctx) pk = sealapi.PublicKey() keygen.create_public_key(pk) rk = sealapi.RelinKeys() keygen.create_relin_keys(rk) gk = sealapi.GaloisKeys() keygen.create_galois_keys(gk) for key in [pk, keygen.secret_key(), gk, rk]: assert check(key, ctx) is True assert check(key, other_ctx) is False assert check(key, invalid_ctx) is False assert sealapi.is_buffer_valid(key) is True
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_keygenerator_publickey(ctx): keygen = sealapi.KeyGenerator(ctx) public_key = keygen.public_key() assert public_key.data().parms_id() == public_key.parms_id() assert public_key.data().poly_modulus_degree() == helper_poly_modulus_degree(ctx) tmp = NamedTemporaryFile() public_key.save(tmp.name) save_test = sealapi.PublicKey() save_test.load(ctx, tmp.name) assert save_test.parms_id() == public_key.parms_id()
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_keygenerator_publickey(ctx): keygen = sealapi.KeyGenerator(ctx) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) assert public_key.data().parms_id() == public_key.parms_id() assert public_key.data().poly_modulus_degree( ) == helper_poly_modulus_degree(ctx) def save_load(path): public_key.save(path) save_test = sealapi.PublicKey() save_test.load(ctx, path) assert save_test.parms_id() == public_key.parms_id() tmp_file(save_load)
def test_ciphertext(testcase, scheme, ctx): poly_modulus_degree = helper_poly_modulus_degree(ctx) ctx_data = ctx.key_context_data() parms = ctx_data.parms() coeff_mod_count = len(parms.coeff_modulus()) keygen = sealapi.KeyGenerator(ctx) ciphertext = sealapi.Ciphertext(ctx) plaintext = helper_encode(scheme, ctx, testcase) pk = sealapi.PublicKey() keygen.create_public_key(pk) encryptor = sealapi.Encryptor(ctx, pk) decryptor = sealapi.Decryptor(ctx, keygen.secret_key()) encryptor.encrypt(plaintext, ciphertext) assert len(ciphertext.parms_id()) > 0 assert ciphertext.scale > 0 assert ciphertext.coeff_modulus_size() == coeff_mod_count - 1 assert ciphertext.poly_modulus_degree() == poly_modulus_degree assert ciphertext.dyn_array().size() > 0 assert ciphertext.size() == 2 assert ciphertext.size_capacity() == 2 assert ciphertext.is_transparent() is False assert ciphertext.is_ntt_form() is (scheme == sealapi.SCHEME_TYPE.CKKS) def save_load(path): ciphertext.save(path) save_test = sealapi.Ciphertext(ctx) save_test.load(ctx, path) decryptor.decrypt(save_test, plaintext) decoded = helper_decode(scheme, ctx, plaintext) is_close_enough(decoded[: len(testcase)], testcase) tmp_file(save_load) ciphertext.resize(ctx, 10) assert ciphertext.size() == 10 assert ciphertext.size_capacity() == 10 ciphertext.reserve(15) assert ciphertext.size() == 10 assert ciphertext.size_capacity() == 15
def save_load(path): public_key.save(path) save_test = sealapi.PublicKey() save_test.load(ctx, path) assert save_test.parms_id() == public_key.parms_id()
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(parms, False, sealapi.SEC_LEVEL_TYPE.NONE) keygen = sealapi.KeyGenerator(ctx) galois_keys = sealapi.GaloisKeys() keygen.create_galois_keys(galois_keys) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) decryptor = sealapi.Decryptor(ctx, keygen.secret_key()) encryptor = sealapi.Encryptor(ctx, 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 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_encryptor_bfv(): batch = [1, 2, 3, 4, 5] poly_modulus_degree = 8192 plain_modulus = 1032193 ctx = helper_context_bfv(poly_modulus_degree, plain_modulus) keygen = sealapi.KeyGenerator(ctx) batchenc = sealapi.BatchEncoder(ctx) public_key = sealapi.PublicKey() keygen.create_public_key(public_key) secret_key = keygen.secret_key() decryptor = sealapi.Decryptor(ctx, secret_key) plaintext = sealapi.Plaintext() batchenc.encode(batch, plaintext) def _test_encryptor_symmetric_setup(encryptor): # encrypt symmetric ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt_symmetric(plaintext, ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == batch plaintext_out.set_zero() def save_load(path): serial = encryptor.encrypt_symmetric(plaintext) serial.save(path) assert Path(path).stat().st_size > 0 tmp_file(save_load) plaintext_out.set_zero() # zero symmetric ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt_zero_symmetric(ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch) plaintext_out.set_zero() # zero symmetric parms_id ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt_zero_symmetric(ctx.last_parms_id(), ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch) plaintext_out.set_zero() def _test_encryptor_pk_setup(encryptor): ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt(plaintext, ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == batch plaintext_out.set_zero() ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt_zero(ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch) plaintext_out.set_zero() ciphertext = sealapi.Ciphertext(ctx) encryptor.encrypt_zero(ctx.last_parms_id(), ciphertext) plaintext_out = sealapi.Plaintext() decryptor.decrypt(ciphertext, plaintext_out) assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch) plaintext_out.set_zero() encryptor = sealapi.Encryptor(ctx, public_key) _test_encryptor_pk_setup(encryptor) encryptor = sealapi.Encryptor(ctx, public_key, secret_key) _test_encryptor_symmetric_setup(encryptor) _test_encryptor_pk_setup(encryptor) encryptor = sealapi.Encryptor(ctx, secret_key) _test_encryptor_symmetric_setup(encryptor) encryptor = sealapi.Encryptor(ctx, public_key) encryptor.set_secret_key(secret_key) _test_encryptor_pk_setup(encryptor) _test_encryptor_symmetric_setup(encryptor) encryptor = sealapi.Encryptor(ctx, secret_key) encryptor.set_public_key(public_key) _test_encryptor_pk_setup(encryptor) _test_encryptor_symmetric_setup(encryptor)