From 31ffcf6ec0fd3bf7e9aaf493734f6c4020cc0d3d Mon Sep 17 00:00:00 2001 From: nindanaoto Date: Mon, 4 Oct 2021 19:03:28 +0900 Subject: [PATCH] Added privkskgen --- include/cloudkey.hpp | 25 ++++++++++++++++++++++++- include/keyswitch.hpp | 2 +- include/params.hpp | 2 +- src/cloudkey.cpp | 23 ++++------------------- src/keyswitch.cpp | 4 ++-- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/include/cloudkey.hpp b/include/cloudkey.hpp index bce2b9b..7e7f888 100644 --- a/include/cloudkey.hpp +++ b/include/cloudkey.hpp @@ -82,6 +82,29 @@ inline void ikskgen(KeySwitchingKey

&ksk, const SecretKey &sk) (j + 1) * P::basebit)), P::α, sk.key.get()); } +template +inline void privkskgen(PrivateKeySwitchingKey

&privksk, Polynomial func,const SecretKey &sk) +{ + std::array key; + for (int i = 0; i < P::domainP::n; i++) key[i] = sk.key.lvl2[i]; + key[P::domainP::n] = -1; +#pragma omp parallel for collapse(3) + for (int i = 0; i <= P::domainP::n; i++) + for (int j = 0; j < P::t; j++) + for (typename P::targetP::T u = 0; + u < (1 << P::basebit) - 1; u++) { + TRLWE c = + trlweSymEncryptZero( + P::α, + sk.key.get()); + for(int k = 0; k::digits - + (j + 1) * P::basebit); + privksk[i][j][u] = c; + } +} template inline relinKey

relinKeygen(const Key

&key) @@ -151,7 +174,7 @@ struct GateKeyNTT { template struct CircuitKey { BootstrappingKeyFFT bkfft; - std::array, 2> privksk; + std::array, 2> privksk; CircuitKey(const SecretKey &sk); CircuitKey() {} template diff --git a/include/keyswitch.hpp b/include/keyswitch.hpp index 7bf1bf8..b30ea68 100644 --- a/include/keyswitch.hpp +++ b/include/keyswitch.hpp @@ -45,5 +45,5 @@ void AnnihilatePrivateKeySwitching(std::array,num_func> &res, const TRL template void PrivKeySwitch(TRLWE &res, const TLWE &tlwe, - const PrivKeySwitchKey

&privksk); + const PrivateKeySwitchingKey

&privksk); } // namespace TFHEpp \ No newline at end of file diff --git a/include/params.hpp b/include/params.hpp index 0593dca..9360f09 100644 --- a/include/params.hpp +++ b/include/params.hpp @@ -87,7 +87,7 @@ using TLWE2TRLWEIKSKey = std::array< template using AnnihilateKey = std::array, P::nbit>; template -using PrivKeySwitchKey = std::array< +using PrivateKeySwitchingKey = std::array< std::array, (1 << P::basebit) - 1>, P::t>, P::domainP::n + 1>; diff --git a/src/cloudkey.cpp b/src/cloudkey.cpp index bfe16b5..7661937 100644 --- a/src/cloudkey.cpp +++ b/src/cloudkey.cpp @@ -41,25 +41,10 @@ CircuitKey::CircuitKey(const SecretKey &sk) bkfftgen(bkfft, sk); // Generate privksk - array key; - for (int i = 0; i < privksP::domainP::n; i++) key[i] = sk.key.lvl2[i]; - key[privksP::domainP::n] = -1; -#pragma omp parallel for collapse(4) - for (int z = 0; z < 2; z++) - for (int i = 0; i <= privksP::domainP::n; i++) - for (int j = 0; j < privksP::t; j++) - for (typename privksP::targetP::T u = 0; - u < (1 << privksP::basebit) - 1; u++) { - TRLWE c = - trlweSymEncryptZero( - privksP::α, - sk.key.get()); - c[z][0] += (u + 1) * key[i] - << (numeric_limits< - typename privksP::targetP::T>::digits - - (j + 1) * privksP::basebit); - privksk[z][i][j][u] = c; - } + TFHEpp::Polynomial poly = {1}; + privkskgen(privksk[1], poly, sk); + for(int i = 0; i()[i]; + privkskgen(privksk[0], poly, sk); } #define INST(bsP, privksP) \ template CircuitKey::CircuitKey(const SecretKey &sk) diff --git a/src/keyswitch.cpp b/src/keyswitch.cpp index f06610b..1eedb08 100644 --- a/src/keyswitch.cpp +++ b/src/keyswitch.cpp @@ -135,7 +135,7 @@ TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) template void PrivKeySwitch(TRLWE &res, const TLWE &tlwe, - const PrivKeySwitchKey

&privksk) + const PrivateKeySwitchingKey

&privksk) { constexpr uint32_t mask = (1 << P::basebit) - 1; constexpr uint64_t prec_offset = @@ -164,7 +164,7 @@ void PrivKeySwitch(TRLWE &res, #define INST(P) \ template void PrivKeySwitch

(TRLWE & res, \ const TLWE &tlwe, \ - const PrivKeySwitchKey

&privksk) + const PrivateKeySwitchingKey

&privksk) TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH(INST) #undef INST