Skip to content

Commit

Permalink
clang-formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Oct 4, 2021
1 parent 625eda8 commit 3e70eb4
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 63 deletions.
20 changes: 10 additions & 10 deletions include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ template <class P>
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)
inline void privkskgen(PrivateKeySwitchingKey<P> &privksk,
Polynomial<typename P::targetP> func,
const SecretKey &sk)
{
std::array<typename P::domainP::T, P::domainP::n + 1> 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++) {
for (typename P::targetP::T u = 0; u < (1 << P::basebit) - 1; u++) {
TRLWE<typename P::targetP> c =
trlweSymEncryptZero<typename P::targetP>(
P::α,
sk.key.get<typename P::targetP>());
for(int k = 0; k<P::targetP::n;k++)
c[1][k] += (u + 1) * func[k]*key[i]
<< (numeric_limits<
typename P::targetP::T>::digits -
(j + 1) * P::basebit);
P::α, sk.key.get<typename P::targetP>());
for (int k = 0; k < P::targetP::n; k++)
c[1][k] +=
(u + 1) * func[k] * key[i]
<< (numeric_limits<typename P::targetP::T>::digits -
(j + 1) * P::basebit);
privksk[i][j][u] = c;
}
}
Expand Down
63 changes: 45 additions & 18 deletions include/gatebootstrapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ void BlindRotate(TRLWE<typename P::targetP> &res,
const Polynomial<typename P::targetP> &testvector)
{
constexpr uint32_t bitwidth = bits_needed<num_out - 1>();
const uint32_t b̄ =
2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth) ) << bitwidth);
const uint32_t b̄ = 2 * P::targetP::n -
((tlwe[P::domainP::n] >>
(std::numeric_limits<typename P::domainP::T>::digits -
1 - P::targetP::nbit + bitwidth))
<< bitwidth);
res[0] = {};
PolynomialMulByXai<typename P::targetP>(res[1], testvector, b̄);
for (int i = 0; i < P::domainP::n; i++) {
constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 - P::targetP::nbit + bitwidth);
const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth;
constexpr typename P::domainP::T roundoffset =
1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 -
P::targetP::nbit + bitwidth);
const uint32_t ā =
(tlwe[i] + roundoffset) >>
(std::numeric_limits<typename P::domainP::T>::digits - 1 -
P::targetP::nbit + bitwidth)
<< bitwidth;
if (ā == 0) continue;
// Do not use CMUXFFT to avoid unnecessary copy.
CMUXFFTwithPolynomialMulByXaiMinusOne<typename P::targetP>(
res, bkfft[i], ā);
CMUXFFTwithPolynomialMulByXaiMinusOne<typename P::targetP>(res,
bkfft[i], ā);
}
}

Expand All @@ -42,17 +51,26 @@ void BlindRotate(TRLWE<typename P::targetP> &res,
const TRLWE<typename P::targetP> &testvector)
{
constexpr uint32_t bitwidth = bits_needed<num_out - 1>();
const uint32_t b̄ =
2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth) ) << bitwidth);
const uint32_t b̄ = 2 * P::targetP::n -
((tlwe[P::domainP::n] >>
(std::numeric_limits<typename P::domainP::T>::digits -
1 - P::targetP::nbit + bitwidth))
<< bitwidth);
PolynomialMulByXai<typename P::targetP>(res[0], testvector[0], b̄);
PolynomialMulByXai<typename P::targetP>(res[1], testvector[1], b̄);
for (int i = 0; i < P::domainP::n; i++) {
constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 - P::targetP::nbit + bitwidth);
const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth;
constexpr typename P::domainP::T roundoffset =
1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 -
P::targetP::nbit + bitwidth);
const uint32_t ā =
(tlwe[i] + roundoffset) >>
(std::numeric_limits<typename P::domainP::T>::digits - 1 -
P::targetP::nbit + bitwidth)
<< bitwidth;
if (ā == 0) continue;
// Do not use CMUXFFT to avoid unnecessary copy.
CMUXFFTwithPolynomialMulByXaiMinusOne<typename P::targetP>(
res, bkfft[i], ā);
CMUXFFTwithPolynomialMulByXaiMinusOne<typename P::targetP>(res,
bkfft[i], ā);
}
}

Expand All @@ -63,17 +81,26 @@ void BlindRotate(TRLWE<typename P::targetP> &res,
const Polynomial<typename P::targetP> &testvector)
{
constexpr uint32_t bitwidth = bits_needed<num_out - 1>();
const uint32_t b̄ =
2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth))<<bitwidth);
const uint32_t b̄ = 2 * P::targetP::n -
((tlwe[P::domainP::n] >>
(std::numeric_limits<typename P::domainP::T>::digits -
1 - P::targetP::nbit + bitwidth))
<< bitwidth);
res[0] = {};
PolynomialMulByXai<typename P::targetP>(res[1], testvector, b̄);
for (int i = 0; i < P::domainP::n; i++) {
constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 - P::targetP::nbit + bitwidth);
const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits<typename P::domainP::T>::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth;
constexpr typename P::domainP::T roundoffset =
1ULL << (std::numeric_limits<typename P::domainP::T>::digits - 2 -
P::targetP::nbit + bitwidth);
const uint32_t ā =
(tlwe[i] + roundoffset) >>
(std::numeric_limits<typename P::domainP::T>::digits - 1 -
P::targetP::nbit + bitwidth)
<< bitwidth;
if (ā == 0) continue;
// Do not use CMUXNTT to avoid unnecessary copy.
CMUXNTTwithPolynomialMulByXaiMinusOne<typename P::targetP>(
res, bkntt[i], ā);
CMUXNTTwithPolynomialMulByXaiMinusOne<typename P::targetP>(res,
bkntt[i], ā);
}
}

Expand Down
26 changes: 16 additions & 10 deletions include/keyswitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@ void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
const AnnihilateKey<P> &ahk);

template <class P, uint num_func>
void AnnihilatePrivateKeySwitching(std::array<TRLWE<P>,num_func> &res, const TRLWE<P> &trlwe,
const AnnihilateKey<P> &ahk, const std::array<TRGSWFFT<P>,num_func> &privks)
void AnnihilatePrivateKeySwitching(
std::array<TRLWE<P>, num_func> &res, const TRLWE<P> &trlwe,
const AnnihilateKey<P> &ahk,
const std::array<TRGSWFFT<P>, num_func> &privks)
{
static_assert(num_func>0, "num_func must be bigger than 0");
res[num_func-1] = trlwe;
for (int i = 0; i < P::nbit-1; i++) {
static_assert(num_func > 0, "num_func must be bigger than 0");
res[num_func - 1] = trlwe;
for (int i = 0; i < P::nbit - 1; i++) {
TRLWE<P> evaledauto;
EvalAuto<P>(evaledauto, res[num_func-1], (1 << (P::nbit - i)) + 1, ahk[i]);
for (int j = 0; j < 2 * P::n; j++) res[num_func-1][0][j] += evaledauto[0][j];
EvalAuto<P>(evaledauto, res[num_func - 1], (1 << (P::nbit - i)) + 1,
ahk[i]);
for (int j = 0; j < 2 * P::n; j++)
res[num_func - 1][0][j] += evaledauto[0][j];
}
for(int i = 0; i<num_func;i++){
for (int i = 0; i < num_func; i++) {
TRLWE<P> evaledauto;
EvalAuto<P>(evaledauto, res[num_func-1], (1 << (P::nbit - i)) + 1, privks[i]);
for (int j = 0; j < 2 * P::n; j++) res[i][0][j] += res[num_func-1][0][j] + evaledauto[0][j];
EvalAuto<P>(evaledauto, res[num_func - 1], (1 << (P::nbit - i)) + 1,
privks[i]);
for (int j = 0; j < 2 * P::n; j++)
res[i][0][j] += res[num_func - 1][0][j] + evaledauto[0][j];
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/circuitbootstrapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ void CircuitBootstrappingPartial(TRLWE<typename privksP::targetP> &trgswupper,
const CircuitKey<bkP, privksP> &ck,
const uint32_t digit)
{
const typename bkP::targetP::T μs2 = 1ULL << (std::numeric_limits<typename privksP::domainP::T>::digits -
const typename bkP::targetP::T μs2 =
1ULL << (std::numeric_limits<typename privksP::domainP::T>::digits -
(digit + 1) * privksP::targetP::Bgbit - 1);
Polynomial<typename bkP::targetP> testvec;
for(int i = 0; i<bkP::targetP::n;i++) testvec[i] = μs2;
TLWE<typename bkP::targetP> tlwemiddle;
GateBootstrappingTLWE2TLWEFFT<bkP>(
tlwemiddle, tlwe, ck.bkfft,
testvec);
for (int i = 0; i < bkP::targetP::n; i++) testvec[i] = μs2;
TLWE<typename bkP::targetP> tlwemiddle;
GateBootstrappingTLWE2TLWEFFT<bkP>(tlwemiddle, tlwe, ck.bkfft, testvec);
tlwemiddle[bkP::targetP::n] += μs2;
PrivKeySwitch<privksP>(trgswupper, tlwemiddle, ck.privksk[0]);
PrivKeySwitch<privksP>(trgswlower, tlwemiddle, ck.privksk[1]);
Expand Down
21 changes: 12 additions & 9 deletions src/cloudkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ void bkgen(BootstrappingKey<P> &bk, const SecretKey &sk)
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bkgen<P>(BootstrappingKey<P> &bk, const SecretKey &sk)
#define INST(P) \
template void bkgen<P>(BootstrappingKey<P> & bk, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

Expand All @@ -27,8 +27,9 @@ void bkfftgen(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk)
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bkfftgen<P>(BootstrappingKeyFFT<P> &bkfft, const SecretKey &sk)
#define INST(P) \
template void bkfftgen<P>(BootstrappingKeyFFT<P> & bkfft, \
const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

Expand All @@ -42,8 +43,9 @@ void bknttgen(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk)
plainpoly, P::targetP::α, sk.key.get<typename P::targetP>());
}
}
#define INST(P) \
template void bknttgen<P>(BootstrappingKeyNTT<P> &bkntt, const SecretKey &sk)
#define INST(P) \
template void bknttgen<P>(BootstrappingKeyNTT<P> & bkntt, \
const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST

Expand All @@ -60,8 +62,8 @@ void ikskgen(KeySwitchingKey<P> &ksk, const SecretKey &sk)
(j + 1) * P::basebit)),
P::α, sk.key.get<typename P::targetP>());
}
#define INST(P) \
template void ikskgen<P>(KeySwitchingKey<P> &ksk, const SecretKey &sk)
#define INST(P) \
template void ikskgen<P>(KeySwitchingKey<P> & ksk, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH(INST)
#undef INST

Expand Down Expand Up @@ -106,7 +108,8 @@ CircuitKey<bsP, privksP>::CircuitKey(const SecretKey &sk)
// Generate privksk
TFHEpp::Polynomial<typename privksP::targetP> poly = {1};
privkskgen<privksP>(privksk[1], poly, sk);
for(int i = 0; i<privksP::targetP::n; i++) poly[i] = -sk.key.get<typename privksP::targetP>()[i];
for (int i = 0; i < privksP::targetP::n; i++)
poly[i] = -sk.key.get<typename privksP::targetP>()[i];
privkskgen<privksP>(privksk[0], poly, sk);
}
#define INST(bsP, privksP) \
Expand Down
12 changes: 8 additions & 4 deletions src/gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ void HomMUXwoSE(TRLWE<typename P::targetP> &res,
temp1[lvl0param::n] -= P::domainP::μ;
temp0[lvl0param::n] -= P::domainP::μ;
TRLWE<typename P::targetP> and0;
BlindRotate<P>(res, temp1, bkfft, μpolygen<typename P::targetP,P::targetP::μ>());
BlindRotate<P>(and0, temp0, bkfft, μpolygen<typename P::targetP,P::targetP::μ>());
BlindRotate<P>(res, temp1, bkfft,
μpolygen<typename P::targetP, P::targetP::μ>());
BlindRotate<P>(and0, temp0, bkfft,
μpolygen<typename P::targetP, P::targetP::μ>());

for (int i = 0; i < P::targetP::n; i++) {
res[0][i] += and0[0][i];
Expand Down Expand Up @@ -191,8 +193,10 @@ void ExtractSwitchAndHomMUX(TRLWE<lvl1param> &res, const TRLWE<lvl1param> &csr,
c1[lvl0param::n] -= lvl0param::μ;
c0[lvl0param::n] -= lvl0param::μ;
TRLWE<lvl1param> and0;
BlindRotate<lvl01param>(res, c1, gk.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
BlindRotate<lvl01param>(and0, c0, gk.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
BlindRotate<lvl01param>(res, c1, gk.bkfftlvl01,
μpolygen<lvl1param, lvl1param::μ>());
BlindRotate<lvl01param>(and0, c0, gk.bkfftlvl01,
μpolygen<lvl1param, lvl1param::μ>());

for (int i = 0; i < lvl1param::n; i++) {
res[0][i] += and0[0][i];
Expand Down
4 changes: 2 additions & 2 deletions test/combinedmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void combWRAM(
SampleExtractIndex<lvl1param>(temp2, temp, 0);
TLWE<lvl0param> temp3;
IdentityKeySwitch<lvl10param>(temp3, temp2, gk.ksk);
BlindRotate<lvl01param>(encram[j][i], temp3,
gk.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
BlindRotate<lvl01param>(encram[j][i], temp3, gk.bkfftlvl01,
μpolygen<lvl1param, lvl1param::μ>());
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions test/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ int main()
SampleExtractIndex<typename ksP::domainP>(temp2, temp, 0);
TLWE<typename ksP::targetP> temp3;
IdentityKeySwitch<ksP>(temp3, temp2, (*ck).ksk);
BlindRotate<CBbsP>((*encmemory)[i], temp3,
(*ck).ck.bkfft,μpolygen<typename CBbsP::targetP, CBbsP::targetP::μ>());
BlindRotate<CBbsP>(
(*encmemory)[i], temp3, (*ck).ck.bkfft,
μpolygen<typename CBbsP::targetP, CBbsP::targetP::μ>());
}

end = chrono::system_clock::now();
Expand Down
5 changes: 3 additions & 2 deletions test/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ int main()
}
}
TRLWE<lvl1param> msbaddress;
BlindRotate<lvl01param>(
msbaddress, encaddress[address_bit - 1], (*ck).gk.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
BlindRotate<lvl01param>(msbaddress, encaddress[address_bit - 1],
(*ck).gk.bkfftlvl01,
μpolygen<lvl1param, lvl1param::μ>());

trlweaddress[memsize >> 1] = msbaddress;
trlweaddress[memsize >> 1][1][0] += lvl1param::μ;
Expand Down

0 comments on commit 3e70eb4

Please sign in to comment.