Skip to content

Commit

Permalink
move some keygen to cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Oct 4, 2021
1 parent 31ffcf6 commit 625eda8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 40 deletions.
46 changes: 6 additions & 40 deletions include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,16 @@
namespace TFHEpp {

template <class P>
inline void bkgen(BootstrappingKey<P> &bk, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bk[i] = trgswSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
void bkgen(BootstrappingKey<P> &bk, const SecretKey &sk);

template <class P>
inline void bkfftgen(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bkfft[i] = trgswfftSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
void bkfftgen(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk);

template <class P>
inline void bknttgen(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bkntt[i] = trgswnttSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
void bknttgen(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk);

template <class P>
inline void tlwe2trlweikskkgen(TLWE2TRLWEIKSKey<P> &iksk, const SecretKey &sk)
void tlwe2trlweikskkgen(TLWE2TRLWEIKSKey<P> &iksk, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++)
for (int j = 0; j < P::t; j++)
Expand All @@ -70,18 +46,8 @@ inline void annihilatekeyegen(AnnihilateKey<P> &ahk, const SecretKey &sk)
}

template <class P>
inline void ikskgen(KeySwitchingKey<P> &ksk, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++)
for (int j = 0; j < P::t; j++)
for (uint32_t k = 0; k < (1 << P::basebit) - 1; k++)
ksk[i][j][k] = tlweSymEncrypt<typename P::targetP>(
sk.key.get<typename P::domainP>()[i] * (k + 1) *
(1ULL
<< (numeric_limits<typename P::targetP::T>::digits -
(j + 1) * P::basebit)),
P::α, sk.key.get<typename P::targetP>());
}
void ikskgen(KeySwitchingKey<P> &ksk, const SecretKey &sk);

template <class P>
inline void privkskgen(PrivateKeySwitchingKey<P> &privksk, Polynomial<typename P::targetP> func,const SecretKey &sk)
{
Expand Down
63 changes: 63 additions & 0 deletions src/cloudkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,69 @@

namespace TFHEpp {

template <class P>
void bkgen(BootstrappingKey<P> &bk, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bk[i] = trgswSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bkgen<P>(BootstrappingKey<P> &bk, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

template <class P>
void bkfftgen(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bkfft[i] = trgswfftSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bkfftgen<P>(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

template <class P>
void bknttgen(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = sk.key.get<typename P::domainP>()[i];
bkntt[i] = trgswnttSymEncrypt<typename P::targetP>(
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bknttgen<P>(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

template <class P>
void ikskgen(KeySwitchingKey<P> &ksk, const SecretKey &sk)
{
for (int i = 0; i < P::domainP::n; i++)
for (int j = 0; j < P::t; j++)
for (uint32_t k = 0; k < (1 << P::basebit) - 1; k++)
ksk[i][j][k] = tlweSymEncrypt<typename P::targetP>(
sk.key.get<typename P::domainP>()[i] * (k + 1) *
(1ULL
<< (numeric_limits<typename P::targetP::T>::digits -
(j + 1) * P::basebit)),
P::α, sk.key.get<typename P::targetP>());
}
#define INST(P) \
template void ikskgen<P>(KeySwitchingKey<P> &ksk, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH(INST)
#undef INST

GateKeywoFFT::GateKeywoFFT(const SecretKey &sk)
{
// Generete bkfft
Expand Down

0 comments on commit 625eda8

Please sign in to comment.