mirror of
https://github.com/zama-ai/concrete.git
synced 2026-02-18 08:31:31 -05:00
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>
115 lines
3.4 KiB
Python
115 lines
3.4 KiB
Python
import numpy as np
|
|
import pytest
|
|
import shutil
|
|
import tempfile
|
|
|
|
from concrete.compiler import (
|
|
ClientSupport,
|
|
EvaluationKeys,
|
|
LibrarySupport,
|
|
PublicArguments,
|
|
PublicResult,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mlir, args, expected_result",
|
|
[
|
|
pytest.param(
|
|
"""
|
|
|
|
func.func @main(%arg0: !FHE.eint<5>, %arg1: i6) -> !FHE.eint<5> {
|
|
%1 = "FHE.add_eint_int"(%arg0, %arg1): (!FHE.eint<5>, i6) -> (!FHE.eint<5>)
|
|
return %1: !FHE.eint<5>
|
|
}
|
|
|
|
""",
|
|
(5, 7),
|
|
12,
|
|
id="enc_plain_int_args",
|
|
marks=pytest.mark.xfail,
|
|
),
|
|
pytest.param(
|
|
"""
|
|
|
|
func.func @main(%arg0: !FHE.eint<5>, %arg1: !FHE.eint<5>) -> !FHE.eint<5> {
|
|
%1 = "FHE.add_eint"(%arg0, %arg1): (!FHE.eint<5>, !FHE.eint<5>) -> (!FHE.eint<5>)
|
|
return %1: !FHE.eint<5>
|
|
}
|
|
|
|
""",
|
|
(5, 7),
|
|
12,
|
|
id="enc_enc_int_args",
|
|
),
|
|
pytest.param(
|
|
"""
|
|
|
|
func.func @main(%arg0: tensor<4x!FHE.eint<5>>, %arg1: tensor<4xi6>) -> !FHE.eint<5> {
|
|
%ret = "FHELinalg.dot_eint_int"(%arg0, %arg1) : (tensor<4x!FHE.eint<5>>, tensor<4xi6>) -> !FHE.eint<5>
|
|
return %ret : !FHE.eint<5>
|
|
}
|
|
|
|
""",
|
|
(
|
|
np.array([1, 2, 3, 4], dtype=np.uint64),
|
|
np.array([4, 3, 2, 1], dtype=np.uint8),
|
|
),
|
|
20,
|
|
id="enc_plain_ndarray_args",
|
|
marks=pytest.mark.xfail,
|
|
),
|
|
pytest.param(
|
|
"""
|
|
|
|
func.func @main(%a0: tensor<4x!FHE.eint<5>>, %a1: tensor<4x!FHE.eint<5>>) -> tensor<4x!FHE.eint<5>> {
|
|
%res = "FHELinalg.add_eint"(%a0, %a1) : (tensor<4x!FHE.eint<5>>, tensor<4x!FHE.eint<5>>) -> tensor<4x!FHE.eint<5>>
|
|
return %res : tensor<4x!FHE.eint<5>>
|
|
}
|
|
|
|
""",
|
|
(
|
|
np.array([1, 2, 3, 4], dtype=np.uint64),
|
|
np.array([7, 0, 1, 5], dtype=np.uint64),
|
|
),
|
|
np.array([8, 2, 4, 9]),
|
|
id="enc_enc_ndarray_args",
|
|
),
|
|
],
|
|
)
|
|
def test_client_server_end_to_end(mlir, args, expected_result, keyset_cache):
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
support = LibrarySupport.new(str(tmpdirname))
|
|
compilation_result = support.compile(mlir)
|
|
server_lambda = support.load_server_lambda(compilation_result, False)
|
|
|
|
client_parameters = support.load_client_parameters(compilation_result)
|
|
keyset = ClientSupport.key_set(client_parameters, keyset_cache)
|
|
|
|
evaluation_keys = keyset.get_evaluation_keys()
|
|
evaluation_keys_serialized = evaluation_keys.serialize()
|
|
evaluation_keys_deserialized = EvaluationKeys.deserialize(
|
|
evaluation_keys_serialized
|
|
)
|
|
|
|
args = ClientSupport.encrypt_arguments(client_parameters, keyset, args)
|
|
args_serialized = args.serialize()
|
|
args_deserialized = PublicArguments.deserialize(
|
|
client_parameters, args_serialized
|
|
)
|
|
|
|
result = support.server_call(
|
|
server_lambda,
|
|
args_deserialized,
|
|
evaluation_keys_deserialized,
|
|
)
|
|
result_serialized = result.serialize()
|
|
result_deserialized = PublicResult.deserialize(
|
|
client_parameters, result_serialized
|
|
)
|
|
|
|
output = ClientSupport.decrypt_result(
|
|
client_parameters, keyset, result_deserialized
|
|
)
|
|
assert np.array_equal(output, expected_result)
|