def helper_decode(scheme, ctx, test): if scheme == sealapi.SCHEME_TYPE.CKKS: encoder = sealapi.CKKSEncoder(ctx) return encoder.decode_double(test) encoder = sealapi.BatchEncoder(ctx) return encoder.decode_int64(test)
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) public_key = keygen.public_key() secret_key = keygen.secret_key() decryptor = sealapi.Decryptor(ctx, secret_key) encryptor = sealapi.Encryptor(ctx, public_key, 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_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 helper_encode(scheme, ctx, test): plaintext = sealapi.Plaintext() if scheme == sealapi.SCHEME_TYPE.CKKS: encoder = sealapi.CKKSEncoder(ctx) encoder.encode(test, 2 ** 40, plaintext) else: encoder = sealapi.BatchEncoder(ctx) encoder.encode(test, plaintext) return plaintext
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