def test_bfv_naive_encryption_decryption(plain_vec): context = ts.context(ts.SCHEME_TYPE.BFV, 4096, 1024) bfv_vec = ts.bfv_naive_vector(context, plain_vec) decrypted_vec = bfv_vec.decrypt() assert decrypted_vec == plain_vec, "Decryption of vector is incorrect."
def ckks_test(): ctx = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60]) l, r = gen_values(200) scale = 2**40 return ts.ckks_vector(ctx, scale, l), ts.ckks_vector(ctx, scale, r)
def parallel_context(n_threads): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60], n_threads=n_threads) context.global_scale = pow(2, 40) return context
def test(): ## Make some fake data to regress to scale = 10**3 N = 100 beta_0 = 1 learning_rate = 0.005 learning_scale = np.sqrt(2 * learning_rate / N) x = np.tile(np.arange(10, dtype=float), 10) #10*10=N=100 y = learning_scale * (beta_0 * x + np.random.normal(size=N, scale=0.8)) x = learning_scale * x plt.scatter(x, y) plt.show() x_list = x.tolist() y_list = y.tolist() ## Encrypt context = ts.context(ts.SCHEME_TYPE.CKKS, poly_modulus_degree=8192 * 2, coeff_mod_bit_sizes=[60, 40, 40, 40, 40, 40, 40, 60]) context.generate_galois_keys() context.global_scale = 2**40 enc_x = ts.ckks_vector(context, x_list) enc_y = ts.ckks_vector(context, y_list) #lin_regressor = EncryptedLinReg(enc_x, enc_y, context, scale=10**3) lin_regressor = EncryptedLinReg(x, y, context) beta_g = lin_regressor.predict() return beta_g
def do(ct_size: int, batch_size: int) -> None: # third party import numpy as np import tenseal as ts # syft absolute import syft as sy sy.load("tenseal") sy.logger.add(sys.stderr, "ERROR") duet = sy.launch_duet(loopback=True, network_url=f"http://127.0.0.1:{PORT}/") duet.requests.add_handler(action="accept") context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60], n_threads=1) context.global_scale = pow(2, 40) data = np.random.uniform(-10, 10, 100) enc = [] for i in range(ct_size): enc.append(ts.ckks_vector(context, data)) start = time.time() _ = context.send(duet, pointable=True) for chunk in chunks(enc, batch_size): _ = sy.lib.python.List(chunk).send(duet, pointable=True) sys.stderr.write( f"[{ct_size}][{batch_size}] DO sending took {time.time() - start} sec\n" ) sy.core.common.event_loop.loop.run_forever()
def test_ckks_encryption_decryption_with_global_scale(plain_vec): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=COEFF_MOD_BIT_SIZES) context.global_scale = pow(2, 40) ckks_vec = ts.ckks_vector(context, plain_vec) decrypted_vec = ckks_vec.decrypt() assert _almost_equal(decrypted_vec, plain_vec, 1), "Decryption of vector is incorrect"
def test_generate_relin_keys(): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193) secret_key = context.secret_key() context.make_context_public(generate_galois_keys=False, generate_relin_keys=False) context.generate_relin_keys(secret_key) assert isinstance(context.relin_keys(), ts.RelinKeys), "Relin keys should be set"
def test_bfv_encryption_decryption(plain_vec, encryption_type): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193, [], encryption_type) bfv_vec = ts.bfv_vector(context, plain_vec) decrypted_vec = bfv_vec.decrypt() assert decrypted_vec == plain_vec, "Decryption of vector is incorrect."
def test_generate_relin_keys(encryption_type): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193, [], encryption_type) secret_key = context.secret_key() if encryption_type is ts.ENCRYPTION_TYPE.ASYMMETRIC: context.make_context_public(generate_galois_keys=False, generate_relin_keys=False) context.generate_relin_keys(secret_key) assert isinstance(context.relin_keys(), RelinKeys), "Relin keys should be set"
def test_global_scale(encryption_type): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, 0, [60, 40, 40, 60], encryption_type) # global scale shouldn't be set at first with pytest.raises(ValueError) as e: scale = context.global_scale for scale in [0, 1, 2, 2 ** 40]: context.global_scale = scale assert context.global_scale == scale
def create_ctx(): poly_mod_degree = 8192 coeff_mod_bit_sizes = [40, 21, 21, 21, 21, 21, 21, 40] ctx = ts.context(ts.SCHEME_TYPE.CKKS, poly_mod_degree, -1, coeff_mod_bit_sizes) ctx.global_scale = 2**21 ctx.generate_galois_keys() return ctx
def recalc_context(self): self.context = ts.context( ts.SCHEME_TYPE.CKKS, poly_modulus_degree=8192 * 2, coeff_mod_bit_sizes=[60, 40, 40, 40, 40, 40, 40, 60]) self.context.generate_galois_keys() self.context.global_scale = 2**40
def test_ckks_empty_encryption(plain_vec): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=COEFF_MOD_BIT_SIZES) scale = pow(2, 40) with pytest.raises(ValueError) as e: ckks_vec = ts.ckks_vector(context, plain_vec, scale) assert str(e.value) == "Attempting to encrypt an empty vector"
def test_polynomial_rescale_off(context, data, polynom): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, 0, [60, 40, 40, 60]) context.global_scale = 2**40 context.auto_rescale = False ct = ts.ckks_tensor(context, data) with pytest.raises(ValueError) as e: result = ct.polyval(polynom)
def test_make_context_public(encryption_type): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193, [], encryption_type) if encryption_type is ts.ENCRYPTION_TYPE.ASYMMETRIC: context.make_context_public(generate_galois_keys=False, generate_relin_keys=False) assert context.is_public() is True, "TenSEALContext should be public" else: with pytest.raises(ValueError) as e: context.make_context_public(generate_galois_keys=False, generate_relin_keys=False)
def test_ckks_encryption_decryption(): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60]) scale = pow(2, 40) plain_vec = [73, 81, 90] ckks_vec = ts.ckks_vector(context, scale, plain_vec) decrypted_vec = ckks_vec.decrypt() assert _almost_equal(decrypted_vec, plain_vec, 1), "Decryption of vector is incorrect"
def test_power_inplace(context, plain, power, precision): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193) tensor = ts.bfv_tensor(context, plain) expected = np.array([np.power(v, power) for v in plain.raw]).reshape(plain.shape).tolist() tensor **= power decrypted_result = tensor.decrypt().tolist() assert (decrypted_result == expected) == True, "Decryption of tensor is incorrect"
def test_bfv_secretkey_decryption(plain_vec): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193) bfv_vec = ts.bfv_vector(context, plain_vec) secret_key = context.secret_key() context.make_context_public() decrypted_vec = bfv_vec.decrypt(secret_key) assert decrypted_vec == plain_vec, "Decryption of vector is incorrect."
def test_simple_polynomial_modswitch_off(context, data, polynom): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, 0, [60, 40, 40, 60]) context.global_scale = 2**40 context.auto_mod_switch = False ct = ts.ckks_vector(context, data) with pytest.raises(ValueError) as e: result = polynom(ct) assert str(e.value) == "encrypted1 and encrypted2 parameter mismatch"
def ckks_context(n_threads=None): ctx = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60], n_threads=n_threads) ctx.generate_galois_keys() ctx.global_scale = 2**40 return ctx
def test_simple_polynomial_rescale_off(context, data, polynom): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, 0, [60, 40, 40, 60]) context.global_scale = 2**40 context.auto_rescale = False ct = ts.ckks_vector(context, data) with pytest.raises(ValueError) as e: result = polynom(ct) assert str(e.value) == "scale mismatch"
def test_ckks_secretkey_decryption(plain_vec): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=COEFF_MOD_BIT_SIZES) scale = pow(2, 40) ckks_vec = ts.ckks_vector(context, plain_vec, scale) secret_key = context.secret_key() context.make_context_public() decrypted_vec = ckks_vec.decrypt(secret_key) assert _almost_equal(decrypted_vec, plain_vec, 1), "Decryption of vector is incorrect"
def test_generate_galois_keys(): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193) secret_key = context.secret_key() context.make_context_public(generate_galois_keys=False, generate_relin_keys=False) context.generate_galois_keys(secret_key) assert type( context.galois_keys()) is ts.GaloisKeys, "Galois keys should be set"
def test_power_inplace(context, plain_vec, power, precision): context = ts.context(ts.SCHEME_TYPE.CKKS, 16384, coeff_mod_bit_sizes=[60, 40, 40, 40, 40, 60]) context.global_scale = pow(2, 40) ckks_vec = ts.ckks_vector(context, plain_vec) expected = [np.power(v, power) for v in plain_vec] ckks_vec **= power decrypted_result = ckks_vec.decrypt() assert _almost_equal(decrypted_result, expected, precision), "Decryption of vector is incorrect"
def test_context_creation(encryption_type): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193, [], encryption_type) assert context.is_private() is True, "TenSEALContext should be private" if encryption_type is ts.ENCRYPTION_TYPE.ASYMMETRIC: assert context.public_key() is not None, "TenSEALContext shouldn't be None" else: with pytest.raises(ValueError) as e: context.public_key()
def test_polynomial_modswitch_off(context, data, polynom): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, 0, [60, 40, 40, 60]) context.global_scale = 2**40 context.auto_mod_switch = False ct = ts.ckks_vector(context, data) with pytest.raises(ValueError) as e: result = ct.polyval(polynom) # encrypted1 and encrypted2 parameter mismatch (or encrypted_ntt and plain_ntt) assert "parameter mismatch" in str(e.value)
def test_ckks_secretkey_decryption(): context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60]) scale = pow(2, 40) plain_vec = [73, 81, 90] ckks_vec = ts.ckks_vector(context, scale, plain_vec) secret_key = context.secret_key() context.make_context_public() decrypted_vec = ckks_vec.decrypt(secret_key) assert _almost_equal(decrypted_vec, plain_vec, 1), "Decryption of vector is incorrect"
def __init__(self, poly_mod_degree=8192 * 2, coeff_mod_bit_sizes=[60, 40, 40, 40, 40, 40, 40, 60], global_scale=2**40): self.secret_context = ts.context(ts.SCHEME_TYPE.CKKS, poly_mod_degree, -1, coeff_mod_bit_sizes) self.secret_context.global_scale = global_scale self.secret_context.generate_galois_keys() self.public_context = self.secret_context.copy() self.public_context.make_context_public()
def test_bfv_naive_secretkey_decryption(): context = ts.context(ts.SCHEME_TYPE.BFV, 4096, 1024) plain_vec = [73, 81, 90] bfv_vec = ts.bfv_naive_vector(context, plain_vec) secret_key = context.secret_key() context.make_context_public() decrypted_vec = bfv_vec.decrypt(secret_key) assert decrypted_vec == plain_vec, "Decryption of vector is incorrect."
def test_bfv_secretkey_decryption(plain_vec, encryption_type): context = ts.context(ts.SCHEME_TYPE.BFV, 8192, 1032193, [], encryption_type) bfv_vec = ts.bfv_vector(context, plain_vec) secret_key = context.secret_key() if encryption_type is ts.ENCRYPTION_TYPE.ASYMMETRIC: context.make_context_public() decrypted_vec = bfv_vec.decrypt(secret_key) assert decrypted_vec == plain_vec, "Decryption of vector is incorrect."