-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1200 Seems like openfhe does expose an overload to EvalSub that allows ct-pt. PiperOrigin-RevId: 719367130
- Loading branch information
1 parent
68edaa0
commit dd91785
Showing
9 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: heir-translate %s --emit-openfhe-pke | FileCheck %s | ||
|
||
!cc = !openfhe.crypto_context | ||
|
||
!Z1095233372161_i64_ = !mod_arith.int<1095233372161 : i64> | ||
!Z65537_i64_ = !mod_arith.int<65537 : i64> | ||
#key = #lwe.key<> | ||
#modulus_chain_L5_C0_ = #lwe.modulus_chain<elements = <1095233372161 : i64, 1032955396097 : i64, 1005037682689 : i64, 998595133441 : i64, 972824936449 : i64, 959939837953 : i64>, current = 0> | ||
!rns_L0_ = !rns.rns<!Z1095233372161_i64_> | ||
#ring_rns_L0_1_x8_ = #polynomial.ring<coefficientType = !rns_L0_, polynomialModulus = <1 + x**8>> | ||
#ring_Z65537_i64_1_x8_ = #polynomial.ring<coefficientType = !Z65537_i64_, polynomialModulus = <1 + x**8>> | ||
#inverse_canonical_encoding = #lwe.inverse_canonical_encoding<scaling_factor = 1024> | ||
#plaintext_space = #lwe.plaintext_space<ring = #ring_Z65537_i64_1_x8_, encoding = #inverse_canonical_encoding> | ||
#ciphertext_space_L0_ = #lwe.ciphertext_space<ring = #ring_rns_L0_1_x8_, encryption_type = lsb> | ||
!pt = !lwe.new_lwe_plaintext<application_data = <message_type = f16>, plaintext_space = #plaintext_space> | ||
!ct = !lwe.new_lwe_ciphertext<application_data = <message_type = f16>, plaintext_space = #plaintext_space, ciphertext_space = #ciphertext_space_L0_, key = #key, modulus_chain = #modulus_chain_L5_C0_> | ||
|
||
// [(input1 + input2) - input3] * input4 | ||
module attributes {scheme.bgv} { | ||
func.func @test_ciphertext_plaintext_ops(%cc : !cc, %input1 : !ct, %input2 : !pt, %input3 : !pt, %input4 : !pt) -> !ct { | ||
%add_res = openfhe.add_plain %cc, %input1, %input2 : (!cc, !ct, !pt) -> !ct | ||
%sub_res = openfhe.sub_plain %cc, %add_res, %input3 : (!cc, !ct, !pt) -> !ct | ||
%mul_res = openfhe.mul_plain %cc, %sub_res, %input4 : (!cc, !ct, !pt) -> !ct | ||
return %mul_res : !ct | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "gmock/gmock.h" // from @googletest | ||
#include "gtest/gtest.h" // from @googletest | ||
#include "src/pke/include/openfhe.h" // from @openfhe | ||
|
||
// Generated headers (block clang-format from messing up order) | ||
#include "tests/Examples/openfhe/ciphertext_plaintext_ops_lib.h" | ||
|
||
using namespace lbcrypto; | ||
using ::testing::ElementsAre; | ||
|
||
namespace mlir { | ||
namespace heir { | ||
namespace openfhe { | ||
|
||
TEST(CiphertextPlaintextOpsTest, TestInput1) { | ||
CCParams<CryptoContextBGVRNS> parameters; | ||
parameters.SetMultiplicativeDepth(2); | ||
parameters.SetPlaintextModulus(65537); | ||
CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters); | ||
cryptoContext->Enable(PKE); | ||
cryptoContext->Enable(KEYSWITCH); | ||
cryptoContext->Enable(LEVELEDSHE); | ||
|
||
KeyPair<DCRTPoly> keyPair; | ||
keyPair = cryptoContext->KeyGen(); | ||
cryptoContext->EvalMultKeyGen(keyPair.secretKey); | ||
cryptoContext->EvalRotateKeyGen(keyPair.secretKey, {1, 2, -1, -2}); | ||
|
||
std::vector<int64_t> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; | ||
Plaintext plaintext1 = cryptoContext->MakePackedPlaintext(vectorOfInts1); | ||
std::vector<int64_t> vectorOfInts2 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12}; | ||
Plaintext plaintext2 = cryptoContext->MakePackedPlaintext(vectorOfInts2); | ||
std::vector<int64_t> vectorOfInts3 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12}; | ||
Plaintext plaintext3 = cryptoContext->MakePackedPlaintext(vectorOfInts3); | ||
std::vector<int64_t> vectorOfInts4 = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2}; | ||
Plaintext plaintext4 = cryptoContext->MakePackedPlaintext(vectorOfInts4); | ||
|
||
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1); | ||
|
||
// Computes [(ciphertext1 + plaintext2) - plaintext3] * plaintext4 | ||
auto ciphertextActual = test_ciphertext_plaintext_ops( | ||
cryptoContext, ciphertext1, plaintext2, plaintext3, plaintext4); | ||
|
||
Plaintext plaintextActual; | ||
cryptoContext->Decrypt(keyPair.secretKey, ciphertextActual, &plaintextActual); | ||
auto actual = plaintextActual->GetPackedValue(); | ||
actual.resize(12); | ||
|
||
EXPECT_THAT(actual, ElementsAre(1, 4, 3, 8, 5, 12, 7, 16, 9, 20, 11, 24)); | ||
} | ||
|
||
} // namespace openfhe | ||
} // namespace heir | ||
} // namespace mlir |