From cd0f02d99c5c84c2c32e666ab4514dcf6970673d Mon Sep 17 00:00:00 2001 From: youben11 Date: Tue, 18 Jul 2023 10:56:03 +0100 Subject: [PATCH] feat(compiler): support crt encoding in scalars during simulation --- .../concretelang/ClientLib/ValueDecrypter.h | 20 +++++++++++++++---- .../concretelang/ClientLib/ValueExporter.h | 15 +++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueDecrypter.h b/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueDecrypter.h index 77cf62096..4fc85ddd3 100644 --- a/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueDecrypter.h +++ b/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueDecrypter.h @@ -54,10 +54,22 @@ public: return value.getScalar().getValue(); if (isSimulated()) { - // value is a scalar in simulation - auto ciphertext = value.getScalar().getValue(); - OUTCOME_TRY(auto decrypted, decryptValue(pos, &ciphertext)); - return (T)decrypted; + OUTCOME_TRY(auto gate, outputGate(pos)); + auto crtVec = gate.encryption->encoding.crt; + if (crtVec.empty()) { + // value is a scalar + auto ciphertext = value.getScalar().getValue(); + OUTCOME_TRY(auto decrypted, decryptValue(pos, &ciphertext)); + return (T)decrypted; + } else { + // value is a tensor (crt) + auto &buffer = value.getTensor(); + auto ciphertext = buffer.getOpaqueElementPointer(0); + OUTCOME_TRY( + auto decrypted, + decryptValue(pos, reinterpret_cast(ciphertext))); + return (T)decrypted; + } } auto &buffer = value.getTensor(); diff --git a/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueExporter.h b/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueExporter.h index 6e01f728e..50d66dfa5 100644 --- a/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueExporter.h +++ b/compilers/concrete-compiler/compiler/include/concretelang/ClientLib/ValueExporter.h @@ -238,9 +238,18 @@ protected: /// Simulate encrypt and export a 64bits integer to a serializale value outcome::checked exportEncryptValue(uint64_t arg, CircuitGate &gate, size_t argPos) override { - uint64_t encValue = 0; - OUTCOME_TRYV(encryptValue(gate, argPos, &encValue, arg)); - return ScalarData(encValue); + auto crtVec = gate.encryption->encoding.crt; + if (crtVec.empty()) { + uint64_t encValue = 0; + OUTCOME_TRYV(encryptValue(gate, argPos, &encValue, arg)); + return ScalarData(encValue); + } else { + TensorData td(bufferShape(gate), clientlib::EncryptedScalarElementType, + clientlib::EncryptedScalarElementWidth); + OUTCOME_TRYV( + encryptValue(gate, argPos, td.getElementPointer(0), arg)); + return std::move(td); + } } outcome::checked inputGate(size_t argPos) override {