def test_fv_encryption_decrption_without_changing_parameters(): ctx = Context(EncryptionParams(1024, CoeffModulus().create(1024, [30, 30]), 1024)) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key values = [0, 1, -1, 100, -100, 1000] for value in values: # Checking simple encryption-decryption with same parameters. assert value == encoder.decode(decryptor.decrypt(encryptor.encrypt(encoder.encode(value)))) # Checking the decryption of same ciphertext 3 times (checking for ciphertext deepcopy). ct = encryptor.encrypt(encoder.encode(value)) for _ in range(3): assert value == encoder.decode(decryptor.decrypt(ct))
def test_fv_mul_cipher_plain(int1, int2): ctx = Context(EncryptionParams(64, CoeffModulus().create(64, [30, 30]), 64)) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key evaluator = Evaluator(ctx) op1 = encryptor.encrypt(encoder.encode(int1)) op2 = encoder.encode(int2) assert (int1 * int2 == encoder.decode( decryptor.decrypt(evaluator._mul_cipher_plain(op1, op2))) == encoder.decode(decryptor.decrypt(evaluator.mul(op1, op2))) == encoder.decode(decryptor.decrypt(evaluator.mul(op2, op1))))
def test_fv_sub_cipher_cipher(int1, int2): ctx = Context(EncryptionParams(1024, CoeffModulus().create(1024, [30, 30]), 1024)) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key evaluator = Evaluator(ctx) op1 = encryptor.encrypt(encoder.encode(int1)) op2 = encryptor.encrypt(encoder.encode(int2)) assert ( int1 - int2 == encoder.decode(decryptor.decrypt(evaluator._sub_cipher_cipher(op1, op2))) == encoder.decode(decryptor.decrypt(evaluator.sub(op1, op2))) == -encoder.decode(decryptor.decrypt(evaluator.sub(op2, op1))) )
def test_fv_negate_cipher(val): ctx = Context(EncryptionParams(1024, CoeffModulus().create(1024, [30, 30]), 1024)) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) evaluator = Evaluator(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key op = encryptor.encrypt(encoder.encode(val)) assert -val == encoder.decode(decryptor.decrypt(evaluator.negate(op)))
def test_fv_encryption_decrption_symmetric(poly_modulus, plain_modulus, coeff_bit_sizes, integer): ctx = Context( EncryptionParams( poly_modulus, CoeffModulus().create(poly_modulus, coeff_bit_sizes), plain_modulus ) ) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[0]) # keys[0] = secret_key decryptor = Decryptor(ctx, keys[0]) assert integer == encoder.decode(decryptor.decrypt(encryptor.encrypt(encoder.encode(integer))))
def test_fv_encryption_decrption_standard_seq_level(poly_modulus, plain_modulus, seq_level, integer): ctx = Context( EncryptionParams(poly_modulus, CoeffModulus().bfv_default(poly_modulus, seq_level), plain_modulus)) keys = KeyGenerator(ctx).keygen() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key assert integer == encoder.decode( decryptor.decrypt(encryptor.encrypt(encoder.encode(integer))))
def test_fv_relin(val1, val2): ctx = Context(EncryptionParams(128, CoeffModulus().create(128, [40, 40]), 64)) keygenerator = KeyGenerator(ctx) keys = keygenerator.keygen() relin_key = keygenerator.get_relin_keys() encoder = IntegerEncoder(ctx) encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key evaluator = Evaluator(ctx) op1 = encryptor.encrypt(encoder.encode(val1)) op2 = encryptor.encrypt(encoder.encode(val2)) temp_prod = evaluator.mul(op1, op2) relin_prod = evaluator.relin(temp_prod, relin_key) assert len(temp_prod.data) - 1 == len(relin_prod.data) assert val1 * val2 == encoder.decode(decryptor.decrypt(relin_prod))