Files
concrete/frontends/concrete-python/tests/compilation/test_keys.py
Alexandre Péré e8ef48ffd8 feat(compiler): introduce concrete-protocol
This commit:
 + Adds support for a protocol which enables inter-op between concrete,
   tfhe-rs and potentially other contributors to the fhe ecosystem.
 + Gets rid of hand-made serialization in the compiler, and
   client/server libs.
 + Refactors client/server libs to allow more pre/post processing of
   circuit inputs/outputs.

The protocol is supported by a definition in the shape of a capnp file,
which defines different types of objects among which:
 + ProgramInfo object, which is a precise description of a set of fhe
   circuit coming from the same compilation (understand function type
   information), and the associated key set.
 + *Key objects, which represent secret/public keys used to
   encrypt/execute fhe circuits.
 + Value object, which represent values that can be transferred between
   client and server to support calls to fhe circuits.

The hand-rolled serialization that was previously used is completely
dropped in favor of capnp in the whole codebase.

The client/server libs, are refactored to introduce a modular design for
pre-post processing. Reading the ProgramInfo file associated with a
compilation, the client and server libs assemble a pipeline of
transformers (functions) for pre and post processing of values coming in
and out of a circuit. This design properly decouples various aspects of
the processing, and allows these capabilities to be safely extended.

In practice this commit includes the following:
 + Defines the specification in a concreteprotocol package
 + Integrate the compilation of this package as a compiler dependency
   via cmake
 + Modify the compiler to use the Encodings objects defined in the
   protocol
 + Modify the compiler to emit ProgramInfo files as compilation
   artifact, and gets rid of the bloated ClientParameters.
 + Introduces a new Common library containing the functionalities shared
   between the compiler and the client/server libs.
 + Introduces a functional pre-post processing pipeline to this common
   library
 + Modify the client/server libs to support loading ProgramInfo objects,
   and calling circuits using Value messages.
 + Drops support of JIT.
 + Drops support of C-api.
 + Drops support of Rust bindings.

Co-authored-by: Nikita Frolov <nf@mkmks.org>
2023-11-09 17:09:04 +01:00

167 lines
4.5 KiB
Python

"""
Tests of `Keys` class.
"""
import tempfile
from pathlib import Path
import pytest
from concrete import fhe
def test_keys_save_load(helpers):
"""
Test saving and loading keys.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x**2
inputset = range(10)
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir_path = Path(tmp_dir)
keys_path = tmp_dir_path / "keys"
circuit1 = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
circuit1.keygen()
sample = circuit1.encrypt(5)
evaluation = circuit1.run(sample)
circuit1.keys.save(str(keys_path))
circuit2 = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
circuit2.keys.load(str(keys_path))
assert circuit2.decrypt(evaluation) == 25
def test_keys_bad_save_load(helpers):
"""
Test saving/loading keys where location is (not) empty.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x**2
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir_path = Path(tmp_dir)
keys_path = tmp_dir_path / "keys"
inputset = range(10)
circuit = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
with pytest.raises(ValueError) as excinfo:
circuit.keys.load(keys_path)
expected_message = f"Unable to load keys from {keys_path} because it doesn't exist"
helpers.check_str(expected_message, str(excinfo.value))
with open(keys_path, "w", encoding="utf-8") as f:
f.write("foo")
circuit.keys.generate()
with pytest.raises(ValueError) as excinfo:
circuit.keys.save(keys_path)
expected_message = f"Unable to save keys to {keys_path} because it already exists"
helpers.check_str(expected_message, str(excinfo.value))
def test_keys_load_if_exists_generate_and_save_otherwise(helpers):
"""
Test saving and loading keys using `load_if_exists_generate_and_save_otherwise`.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x**2
inputset = range(10)
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir_path = Path(tmp_dir)
keys_path = tmp_dir_path / "keys"
circuit1 = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
circuit1.keys.load_if_exists_generate_and_save_otherwise(str(keys_path))
sample = circuit1.encrypt(5)
evaluation = circuit1.run(sample)
circuit2 = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
circuit2.keys.load_if_exists_generate_and_save_otherwise(str(keys_path))
assert circuit2.decrypt(evaluation) == 25
def test_keys_serialize_deserialize(helpers):
"""
Test serializing and deserializing keys.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x**2
inputset = range(10)
circuit = f.compile(inputset, helpers.configuration())
server = circuit.server
client1 = fhe.Client(server.client_specs)
client1.keys.generate()
sample = client1.encrypt(5)
evaluation = server.run(sample, evaluation_keys=client1.evaluation_keys)
client2 = fhe.Client(server.client_specs)
client2.keys = fhe.Keys.deserialize(client1.keys.serialize())
assert client2.decrypt(evaluation) == 25
def test_keys_serialize_before_generation(helpers):
"""
Test serialization of keys before their generation.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x + 42
inputset = range(10)
circuit = f.compile(inputset, configuration=helpers.configuration())
with pytest.raises(RuntimeError) as excinfo:
circuit.keys.serialize()
expected_message = "Keys cannot be serialized before they are generated"
helpers.check_str(expected_message, str(excinfo.value))
def test_keys_generate_manual_seed(helpers):
"""
Test key generation with custom seed.
"""
@fhe.compiler({"x": "encrypted"})
def f(x):
return x**2
inputset = range(10)
circuit = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
circuit.keygen(seed=42)
sample = circuit.encrypt(5)
evaluation = circuit.run(sample)
same_circuit = f.compile(inputset, helpers.configuration().fork(use_insecure_key_cache=False))
same_circuit.keygen(seed=42)
assert same_circuit.decrypt(evaluation) == 25