mirror of
https://github.com/zama-ai/concrete.git
synced 2026-04-17 03:00:54 -04:00
feat(compiler): First draft to support FHE.eint up to 16bits
For now what it works are only levelled ops with user parameters. (take a look to the tests) Done: - Add parameters to the fhe parameters to support CRT-based large integers - Add command line options and tests options to allows the user to give those new parameters - Update the dialects and pipeline to handle new fhe parameters for CRT-based large integers - Update the client parameters and the client library to handle the CRT-based large integers Todo: - Plug the optimizer to compute the CRT-based large interger parameters - Plug the pbs for the CRT-based large integer
This commit is contained in:
@@ -11,17 +11,6 @@ namespace clientlib {
|
||||
|
||||
using StringError = concretelang::error::StringError;
|
||||
|
||||
size_t bitWidthAsWord(size_t exactBitWidth) {
|
||||
size_t sortedWordBitWidths[] = {8, 16, 32, 64};
|
||||
size_t previousWidth = 0;
|
||||
for (auto currentWidth : sortedWordBitWidths) {
|
||||
if (previousWidth < exactBitWidth && exactBitWidth <= currentWidth) {
|
||||
return currentWidth;
|
||||
}
|
||||
}
|
||||
return exactBitWidth;
|
||||
}
|
||||
|
||||
outcome::checked<std::unique_ptr<PublicArguments>, StringError>
|
||||
EncryptedArguments::exportPublicArguments(ClientParameters clientParameters,
|
||||
RuntimeContext runtimeContext) {
|
||||
@@ -33,7 +22,7 @@ outcome::checked<void, StringError>
|
||||
EncryptedArguments::pushArg(uint64_t arg, KeySet &keySet) {
|
||||
OUTCOME_TRYV(checkPushTooManyArgs(keySet));
|
||||
auto pos = currentPos++;
|
||||
CircuitGate input = keySet.inputGate(pos);
|
||||
OUTCOME_TRY(CircuitGate input, keySet.clientParameters().input(pos));
|
||||
if (input.shape.size != 0) {
|
||||
return StringError("argument #") << pos << " is not a scalar";
|
||||
}
|
||||
@@ -42,12 +31,11 @@ EncryptedArguments::pushArg(uint64_t arg, KeySet &keySet) {
|
||||
preparedArgs.push_back((void *)arg);
|
||||
return outcome::success();
|
||||
}
|
||||
ciphertextBuffers.resize(ciphertextBuffers.size() + 1); // Allocate empty
|
||||
// Allocate empty
|
||||
ciphertextBuffers.resize(ciphertextBuffers.size() + 1);
|
||||
TensorData &values_and_sizes = ciphertextBuffers.back();
|
||||
auto lweSize = keySet.getInputLweSecretKeyParam(pos).lweSize();
|
||||
values_and_sizes.sizes.push_back(lweSize);
|
||||
values_and_sizes.values.resize(lweSize);
|
||||
|
||||
values_and_sizes.sizes = keySet.clientParameters().bufferShape(input);
|
||||
values_and_sizes.values.resize(keySet.clientParameters().bufferSize(input));
|
||||
OUTCOME_TRYV(keySet.encrypt_lwe(pos, values_and_sizes.values.data(), arg));
|
||||
// Note: Since we bufferized lwe ciphertext take care of memref calling
|
||||
// convention
|
||||
@@ -57,97 +45,18 @@ EncryptedArguments::pushArg(uint64_t arg, KeySet &keySet) {
|
||||
preparedArgs.push_back((void *)values_and_sizes.values.data());
|
||||
// offset
|
||||
preparedArgs.push_back((void *)0);
|
||||
// size
|
||||
preparedArgs.push_back((void *)values_and_sizes.values.size());
|
||||
// stride
|
||||
preparedArgs.push_back((void *)1);
|
||||
return outcome::success();
|
||||
}
|
||||
|
||||
outcome::checked<void, StringError>
|
||||
EncryptedArguments::pushArg(std::vector<uint8_t> arg, KeySet &keySet) {
|
||||
return pushArg(8, (void *)arg.data(), {(int64_t)arg.size()}, keySet);
|
||||
}
|
||||
|
||||
outcome::checked<void, StringError>
|
||||
EncryptedArguments::pushArg(size_t width, const void *data,
|
||||
llvm::ArrayRef<int64_t> shape, KeySet &keySet) {
|
||||
OUTCOME_TRYV(checkPushTooManyArgs(keySet));
|
||||
auto pos = currentPos;
|
||||
CircuitGate input = keySet.inputGate(pos);
|
||||
// Check the width of data
|
||||
if (input.shape.width > 64) {
|
||||
return StringError("argument #")
|
||||
<< pos << " width > 64 bits is not supported";
|
||||
}
|
||||
auto roundedSize = bitWidthAsWord(input.shape.width);
|
||||
if (width != roundedSize) {
|
||||
return StringError("argument #") << pos << "width mismatch, got " << width
|
||||
<< " expected " << roundedSize;
|
||||
}
|
||||
// Check the shape of tensor
|
||||
if (input.shape.dimensions.empty()) {
|
||||
return StringError("argument #") << pos << "is not a tensor";
|
||||
}
|
||||
if (shape.size() != input.shape.dimensions.size()) {
|
||||
return StringError("argument #")
|
||||
<< pos << "has not the expected number of dimension, got "
|
||||
<< shape.size() << " expected " << input.shape.dimensions.size();
|
||||
}
|
||||
ciphertextBuffers.resize(ciphertextBuffers.size() + 1); // Allocate empty
|
||||
TensorData &values_and_sizes = ciphertextBuffers.back();
|
||||
for (size_t i = 0; i < shape.size(); i++) {
|
||||
values_and_sizes.sizes.push_back(shape[i]);
|
||||
if (shape[i] != input.shape.dimensions[i]) {
|
||||
return StringError("argument #")
|
||||
<< pos << " has not the expected dimension #" << i << " , got "
|
||||
<< shape[i] << " expected " << input.shape.dimensions[i];
|
||||
}
|
||||
}
|
||||
if (input.encryption.hasValue()) {
|
||||
auto lweSize = keySet.getInputLweSecretKeyParam(pos).lweSize();
|
||||
values_and_sizes.sizes.push_back(lweSize);
|
||||
|
||||
// Encrypted tensor: for now we support only 8 bits for encrypted tensor
|
||||
if (width != 8) {
|
||||
return StringError("argument #")
|
||||
<< pos << " width mismatch, expected 8 got " << width;
|
||||
}
|
||||
const uint8_t *data8 = (const uint8_t *)data;
|
||||
|
||||
// Allocate a buffer for ciphertexts of size of tensor
|
||||
values_and_sizes.values.resize(input.shape.size * lweSize);
|
||||
auto &values = values_and_sizes.values;
|
||||
// Allocate ciphertexts and encrypt, for every values in tensor
|
||||
for (size_t i = 0, offset = 0; i < input.shape.size;
|
||||
i++, offset += lweSize) {
|
||||
OUTCOME_TRYV(keySet.encrypt_lwe(pos, values.data() + offset, data8[i]));
|
||||
}
|
||||
} else {
|
||||
values_and_sizes.values.resize(input.shape.size);
|
||||
for (size_t i = 0; i < input.shape.size; i++) {
|
||||
values_and_sizes.values[i] = ((const uint64_t *)data)[i];
|
||||
}
|
||||
}
|
||||
// allocated
|
||||
preparedArgs.push_back(nullptr);
|
||||
// aligned
|
||||
preparedArgs.push_back((void *)values_and_sizes.values.data());
|
||||
// offset
|
||||
preparedArgs.push_back((void *)0);
|
||||
// sizes
|
||||
for (size_t size : values_and_sizes.sizes) {
|
||||
for (auto size : values_and_sizes.sizes) {
|
||||
preparedArgs.push_back((void *)size);
|
||||
}
|
||||
|
||||
// Set the stride for each dimension, equal to the product of the
|
||||
// following dimensions.
|
||||
// strides
|
||||
int64_t stride = values_and_sizes.length();
|
||||
for (size_t size : values_and_sizes.sizes) {
|
||||
for (size_t i = 0; i < values_and_sizes.sizes.size() - 1; i++) {
|
||||
auto size = values_and_sizes.sizes[i];
|
||||
stride = (size == 0 ? 0 : (stride / size));
|
||||
preparedArgs.push_back((void *)stride);
|
||||
}
|
||||
currentPos++;
|
||||
preparedArgs.push_back((void *)1);
|
||||
return outcome::success();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user