Exemplo n.º 1
0
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."
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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"
Exemplo n.º 7
0
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"
Exemplo n.º 8
0
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."
Exemplo n.º 9
0
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"
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
    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
Exemplo n.º 13
0
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"
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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"
Exemplo n.º 17
0
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"
Exemplo n.º 18
0
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."
Exemplo n.º 19
0
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"
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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"
Exemplo n.º 22
0
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"
Exemplo n.º 23
0
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"
Exemplo n.º 24
0
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"
Exemplo n.º 25
0
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()
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
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"
Exemplo n.º 28
0
 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()
Exemplo n.º 29
0
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."
Exemplo n.º 30
0
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."