Files
concrete/compilers/concrete-compiler/compiler/lib/ClientLib/ClientLib.cpp
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

133 lines
4.5 KiB
C++

// Part of the Concrete Compiler Project, under the BSD3 License with Zama
// Exceptions. See
// https://github.com/zama-ai/concrete-compiler-internal/blob/main/LICENSE.txt
// for license information.
#include <cassert>
#include <cstdint>
#include <cstring>
#include <functional>
#include <optional>
#include <string>
#include <variant>
#include "boost/outcome.h"
#include "concrete-cpu.h"
#include "concrete-protocol.capnp.h"
#include "concretelang/ClientLib/ClientLib.h"
#include "concretelang/Common/Csprng.h"
#include "concretelang/Common/Error.h"
#include "concretelang/Common/Keysets.h"
#include "concretelang/Common/Protocol.h"
#include "concretelang/Common/Transformers.h"
#include "concretelang/Common/Values.h"
using concretelang::error::Result;
using concretelang::keysets::ClientKeyset;
using concretelang::transformers::InputTransformer;
using concretelang::transformers::OutputTransformer;
using concretelang::transformers::TransformerFactory;
using concretelang::values::TransportValue;
using concretelang::values::Value;
namespace concretelang {
namespace clientlib {
Result<ClientCircuit>
ClientCircuit::create(const Message<concreteprotocol::CircuitInfo> &info,
const ClientKeyset &keyset,
std::shared_ptr<CSPRNG> csprng, bool useSimulation) {
auto inputTransformers = std::vector<InputTransformer>();
for (auto gateInfo : info.asReader().getInputs()) {
InputTransformer transformer;
if (gateInfo.getTypeInfo().hasIndex()) {
OUTCOME_TRY(transformer,
TransformerFactory::getIndexInputTransformer(gateInfo));
} else if (gateInfo.getTypeInfo().hasPlaintext()) {
OUTCOME_TRY(transformer,
TransformerFactory::getPlaintextInputTransformer(gateInfo));
} else if (gateInfo.getTypeInfo().hasLweCiphertext()) {
OUTCOME_TRY(transformer,
TransformerFactory::getLweCiphertextInputTransformer(
keyset, gateInfo, csprng, useSimulation));
} else {
return StringError("Malformed input gate info.");
}
inputTransformers.push_back(transformer);
}
auto outputTransformers = std::vector<OutputTransformer>();
for (auto gateInfo : info.asReader().getOutputs()) {
OutputTransformer transformer;
if (gateInfo.getTypeInfo().hasIndex()) {
OUTCOME_TRY(transformer,
TransformerFactory::getIndexOutputTransformer(gateInfo));
} else if (gateInfo.getTypeInfo().hasPlaintext()) {
OUTCOME_TRY(transformer,
TransformerFactory::getPlaintextOutputTransformer(gateInfo));
} else if (gateInfo.getTypeInfo().hasLweCiphertext()) {
OUTCOME_TRY(transformer,
TransformerFactory::getLweCiphertextOutputTransformer(
keyset, gateInfo, useSimulation));
} else {
return StringError("Malformed output gate info.");
}
outputTransformers.push_back(transformer);
}
return ClientCircuit(info, inputTransformers, outputTransformers);
}
Result<TransportValue> ClientCircuit::prepareInput(Value arg, size_t pos) {
if (pos >= inputTransformers.size()) {
return StringError("Tried to prepare a Value for incorrect position.");
}
return inputTransformers[pos](arg);
}
Result<Value> ClientCircuit::processOutput(TransportValue result, size_t pos) {
if (pos >= outputTransformers.size()) {
return StringError(
"Tried to process a TransportValue for incorrect position.");
}
return outputTransformers[pos](result);
}
std::string ClientCircuit::getName() {
return circuitInfo.asReader().getName();
}
const Message<concreteprotocol::CircuitInfo> &ClientCircuit::getCircuitInfo() {
return circuitInfo;
}
Result<ClientProgram>
ClientProgram::create(const Message<concreteprotocol::ProgramInfo> &info,
const ClientKeyset &keyset,
std::shared_ptr<CSPRNG> csprng, bool useSimulation) {
ClientProgram output;
for (auto circuitInfo : info.asReader().getCircuits()) {
OUTCOME_TRY(
ClientCircuit clientCircuit,
ClientCircuit::create(circuitInfo, keyset, csprng, useSimulation));
output.circuits.push_back(clientCircuit);
}
return output;
}
Result<ClientCircuit> ClientProgram::getClientCircuit(std::string circuitName) {
for (auto circuit : circuits) {
if (circuit.getName() == circuitName) {
return circuit;
}
}
return StringError("Tried to get unknown client circuit: `" + circuitName +
"`");
}
} // namespace clientlib
} // namespace concretelang