TenSEAL icon indicating copy to clipboard operation
TenSEAL copied to clipboard

encrypted tensor add plain vector meets "ValueError: scale mismatch"

Open yoopaan opened this issue 3 years ago • 1 comments

Question


import numpy as np
import tenseal as ts

def keyGen(key_length=4096):
    bits_scale = 24
    coefficient_modulus = [30, 24, bits_scale, 30]
    if key_length == 8192:
        # bits_scale = 26
        # coefficient_modulus = [31,  26,  26, 26, 31]
        bits_scale = 40
        coefficient_modulus = [60, 40, 40, 60]
    elif key_length == 16384:
        bits_scale = 50
        coefficient_modulus = [60, 40, 40, 40, 40, 60]
    elif key_length == 32768:
        bits_scale = 60
        coefficient_modulus = [60, 60, 60, 60, 60, 60, 60, 60, 60]
    private_context = ts.context(
        ts.SCHEME_TYPE.CKKS, poly_modulus_degree=key_length,
        coeff_mod_bit_sizes=coefficient_modulus
    )
    private_context.auto_rescale = False
    private_context.auto_relin = True
    private_context.generate_galois_keys()
    private_context.global_scale = pow(2, bits_scale)
    public_context = ts.context_from(private_context.serialize())
    public_context.make_context_public()
    pk = public_context
    sk = private_context
    return pk, sk


def encrypt_tensor(pk, v):
    return ts.ckks_tensor(pk, v)


def decrypt_tensor(sk, enc_v):
    return ts.ckks_tensor_from(sk, enc_v.serialize()).decrypt().raw


def test_1():
    pk, sk = keyGen(4096)
    x = np.random.random((64, 31))
    weights = np.random.uniform(-0.5, 0.5, 31).astype(np.float32)
    encrypt_weights = encrypt_tensor(pk, weights)
    res = encrypt_weights.dot(x.transpose())
    print(type(res))
    print(res.shape)
    y = [1.0] * 64
    kk = res + y
    print(kk)


if __name__ == '__main__':
    test_1()

return ValueError: scale mismatch

System Information

  • Language Version: [Python 3.7]
  • Package Manager Version: [TenSeal 0.3.12]

yoopaan avatar Aug 22 '22 13:08 yoopaan

I think it is because you set auto_rescale = False

xuefeng-xu avatar Jul 26 '23 03:07 xuefeng-xu