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>
98 lines
2.3 KiB
C++
98 lines
2.3 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 <cstddef>
|
|
#include <stdio.h>
|
|
|
|
#include "concretelang/Common/CRT.h"
|
|
|
|
namespace concretelang {
|
|
namespace crt {
|
|
uint64_t productOfModuli(std::vector<int64_t> moduli) {
|
|
uint64_t product = 1;
|
|
for (auto modulus : moduli) {
|
|
product *= modulus;
|
|
}
|
|
return product;
|
|
}
|
|
|
|
std::vector<int64_t> crt(std::vector<int64_t> moduli, uint64_t val) {
|
|
std::vector<int64_t> remainders(moduli.size(), 0);
|
|
|
|
for (size_t i = 0; i < moduli.size(); i++) {
|
|
remainders[i] = val % moduli[i];
|
|
}
|
|
return remainders;
|
|
}
|
|
|
|
// https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
|
|
// Returns modulo inverse of a with respect
|
|
// to m using extended Euclid Algorithm
|
|
// Assumption: a and m are coprimes, i.e.,
|
|
// gcd(a, m) = 1
|
|
int64_t modInverse(int64_t a, int64_t m) {
|
|
int64_t m0 = m;
|
|
int64_t y = 0, x = 1;
|
|
|
|
if (m == 1)
|
|
return 0;
|
|
|
|
while (a > 1) {
|
|
// q is quotient
|
|
int64_t q = a / m;
|
|
int64_t t = m;
|
|
|
|
// m is remainder now, process same as
|
|
// Euclid's algo
|
|
m = a % m;
|
|
a = t;
|
|
t = y;
|
|
|
|
// Update y and x
|
|
y = x - q * y;
|
|
x = t;
|
|
}
|
|
|
|
// Make x positive
|
|
if (x < 0)
|
|
x += m0;
|
|
|
|
return x;
|
|
}
|
|
|
|
uint64_t iCrt(std::vector<int64_t> moduli, std::vector<int64_t> remainders) {
|
|
// Compute the product of moduli
|
|
int64_t product = productOfModuli(moduli);
|
|
|
|
int64_t result = 0;
|
|
|
|
// Apply above formula
|
|
for (size_t i = 0; i < remainders.size(); i++) {
|
|
int tmp = product / moduli[i];
|
|
result += remainders[i] * modInverse(tmp, moduli[i]) * tmp;
|
|
}
|
|
|
|
return result % product;
|
|
}
|
|
|
|
uint64_t encode(int64_t plaintext, uint64_t modulus, uint64_t product) {
|
|
// values are represented on the interval [0; product[ so we represent
|
|
// plantext on this interval
|
|
if (plaintext < 0) {
|
|
plaintext = product + plaintext;
|
|
}
|
|
__uint128_t m = plaintext % modulus;
|
|
return m * ((__uint128_t)(1) << 64) / modulus;
|
|
}
|
|
|
|
uint64_t decode(uint64_t val, uint64_t modulus) {
|
|
auto result = (__uint128_t)val * (__uint128_t)modulus;
|
|
result = result + ((result & ((__uint128_t)(1) << 63)) << 1);
|
|
result = result / ((__uint128_t)(1) << 64);
|
|
return (uint64_t)result % modulus;
|
|
}
|
|
} // namespace crt
|
|
} // namespace concretelang
|