From 3e70eb4dd279c62767a9635de2857555d6830218 Mon Sep 17 00:00:00 2001 From: nindanaoto Date: Mon, 4 Oct 2021 19:11:53 +0900 Subject: [PATCH] clang-formatted --- include/cloudkey.hpp | 20 +++++------ include/gatebootstrapping.hpp | 63 +++++++++++++++++++++++++---------- include/keyswitch.hpp | 26 +++++++++------ src/circuitbootstrapping.cpp | 11 +++--- src/cloudkey.cpp | 21 +++++++----- src/gate.cpp | 12 ++++--- test/combinedmem.cpp | 4 +-- test/ram.cpp | 5 +-- test/write.cpp | 5 +-- 9 files changed, 104 insertions(+), 63 deletions(-) diff --git a/include/cloudkey.hpp b/include/cloudkey.hpp index 4fab3bf..183ee76 100644 --- a/include/cloudkey.hpp +++ b/include/cloudkey.hpp @@ -49,7 +49,9 @@ template void ikskgen(KeySwitchingKey

&ksk, const SecretKey &sk); template -inline void privkskgen(PrivateKeySwitchingKey

&privksk, Polynomial func,const SecretKey &sk) +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]; @@ -57,17 +59,15 @@ inline void privkskgen(PrivateKeySwitchingKey

&privksk, Polynomial c = trlweSymEncryptZero( - P::α, - sk.key.get()); - for(int k = 0; k::digits - - (j + 1) * P::basebit); + P::α, sk.key.get()); + for (int k = 0; k < P::targetP::n; k++) + c[1][k] += + (u + 1) * func[k] * key[i] + << (numeric_limits::digits - + (j + 1) * P::basebit); privksk[i][j][u] = c; } } diff --git a/include/gatebootstrapping.hpp b/include/gatebootstrapping.hpp index 544aa9d..93e56f6 100644 --- a/include/gatebootstrapping.hpp +++ b/include/gatebootstrapping.hpp @@ -21,17 +21,26 @@ void BlindRotate(TRLWE &res, const Polynomial &testvector) { constexpr uint32_t bitwidth = bits_needed(); - const uint32_t b̄ = - 2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth) ) << bitwidth); + const uint32_t b̄ = 2 * P::targetP::n - + ((tlwe[P::domainP::n] >> + (std::numeric_limits::digits - + 1 - P::targetP::nbit + bitwidth)) + << bitwidth); res[0] = {}; PolynomialMulByXai(res[1], testvector, b̄); for (int i = 0; i < P::domainP::n; i++) { - constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits::digits - 2 - P::targetP::nbit + bitwidth); - const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth; + constexpr typename P::domainP::T roundoffset = + 1ULL << (std::numeric_limits::digits - 2 - + P::targetP::nbit + bitwidth); + const uint32_t ā = + (tlwe[i] + roundoffset) >> + (std::numeric_limits::digits - 1 - + P::targetP::nbit + bitwidth) + << bitwidth; if (ā == 0) continue; // Do not use CMUXFFT to avoid unnecessary copy. - CMUXFFTwithPolynomialMulByXaiMinusOne( - res, bkfft[i], ā); + CMUXFFTwithPolynomialMulByXaiMinusOne(res, + bkfft[i], ā); } } @@ -42,17 +51,26 @@ void BlindRotate(TRLWE &res, const TRLWE &testvector) { constexpr uint32_t bitwidth = bits_needed(); - const uint32_t b̄ = - 2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth) ) << bitwidth); + const uint32_t b̄ = 2 * P::targetP::n - + ((tlwe[P::domainP::n] >> + (std::numeric_limits::digits - + 1 - P::targetP::nbit + bitwidth)) + << bitwidth); PolynomialMulByXai(res[0], testvector[0], b̄); PolynomialMulByXai(res[1], testvector[1], b̄); for (int i = 0; i < P::domainP::n; i++) { - constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits::digits - 2 - P::targetP::nbit + bitwidth); - const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth; + constexpr typename P::domainP::T roundoffset = + 1ULL << (std::numeric_limits::digits - 2 - + P::targetP::nbit + bitwidth); + const uint32_t ā = + (tlwe[i] + roundoffset) >> + (std::numeric_limits::digits - 1 - + P::targetP::nbit + bitwidth) + << bitwidth; if (ā == 0) continue; // Do not use CMUXFFT to avoid unnecessary copy. - CMUXFFTwithPolynomialMulByXaiMinusOne( - res, bkfft[i], ā); + CMUXFFTwithPolynomialMulByXaiMinusOne(res, + bkfft[i], ā); } } @@ -63,17 +81,26 @@ void BlindRotate(TRLWE &res, const Polynomial &testvector) { constexpr uint32_t bitwidth = bits_needed(); - const uint32_t b̄ = - 2 * P::targetP::n - ((tlwe[P::domainP::n] >>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth))<> + (std::numeric_limits::digits - + 1 - P::targetP::nbit + bitwidth)) + << bitwidth); res[0] = {}; PolynomialMulByXai(res[1], testvector, b̄); for (int i = 0; i < P::domainP::n; i++) { - constexpr typename P::domainP::T roundoffset = 1ULL << (std::numeric_limits::digits - 2 - P::targetP::nbit + bitwidth); - const uint32_t ā = (tlwe[i]+roundoffset)>>(std::numeric_limits::digits - 1 - P::targetP::nbit + bitwidth) << bitwidth; + constexpr typename P::domainP::T roundoffset = + 1ULL << (std::numeric_limits::digits - 2 - + P::targetP::nbit + bitwidth); + const uint32_t ā = + (tlwe[i] + roundoffset) >> + (std::numeric_limits::digits - 1 - + P::targetP::nbit + bitwidth) + << bitwidth; if (ā == 0) continue; // Do not use CMUXNTT to avoid unnecessary copy. - CMUXNTTwithPolynomialMulByXaiMinusOne( - res, bkntt[i], ā); + CMUXNTTwithPolynomialMulByXaiMinusOne(res, + bkntt[i], ā); } } diff --git a/include/keyswitch.hpp b/include/keyswitch.hpp index b30ea68..96af64b 100644 --- a/include/keyswitch.hpp +++ b/include/keyswitch.hpp @@ -25,20 +25,26 @@ void AnnihilateKeySwitching(TRLWE

&res, const TRLWE

&trlwe, const AnnihilateKey

&ahk); template -void AnnihilatePrivateKeySwitching(std::array,num_func> &res, const TRLWE

&trlwe, - const AnnihilateKey

&ahk, const std::array,num_func> &privks) +void AnnihilatePrivateKeySwitching( + std::array, num_func> &res, const TRLWE

&trlwe, + const AnnihilateKey

&ahk, + const std::array, 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

evaledauto; - EvalAuto

(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

(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 evaledauto; - EvalAuto

(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

(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]; } } diff --git a/src/circuitbootstrapping.cpp b/src/circuitbootstrapping.cpp index 789c698..116a5b9 100644 --- a/src/circuitbootstrapping.cpp +++ b/src/circuitbootstrapping.cpp @@ -11,14 +11,13 @@ void CircuitBootstrappingPartial(TRLWE &trgswupper, const CircuitKey &ck, const uint32_t digit) { - const typename bkP::targetP::T μs2 = 1ULL << (std::numeric_limits::digits - + const typename bkP::targetP::T μs2 = + 1ULL << (std::numeric_limits::digits - (digit + 1) * privksP::targetP::Bgbit - 1); Polynomial testvec; - for(int i = 0; i tlwemiddle; - GateBootstrappingTLWE2TLWEFFT( - tlwemiddle, tlwe, ck.bkfft, - testvec); + for (int i = 0; i < bkP::targetP::n; i++) testvec[i] = μs2; + TLWE tlwemiddle; + GateBootstrappingTLWE2TLWEFFT(tlwemiddle, tlwe, ck.bkfft, testvec); tlwemiddle[bkP::targetP::n] += μs2; PrivKeySwitch(trgswupper, tlwemiddle, ck.privksk[0]); PrivKeySwitch(trgswlower, tlwemiddle, ck.privksk[1]); diff --git a/src/cloudkey.cpp b/src/cloudkey.cpp index 0c472f6..171b0e4 100644 --- a/src/cloudkey.cpp +++ b/src/cloudkey.cpp @@ -12,8 +12,8 @@ void bkgen(BootstrappingKey

&bk, const SecretKey &sk) plainpoly, P::targetP::α, sk.key.get()); } } -#define INST(P) \ - template void bkgen

(BootstrappingKey

&bk, const SecretKey &sk) +#define INST(P) \ + template void bkgen

(BootstrappingKey

& bk, const SecretKey &sk) TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) #undef INST @@ -27,8 +27,9 @@ void bkfftgen(BootstrappingKeyFFT

&bkfft, const SecretKey &sk) plainpoly, P::targetP::α, sk.key.get()); } } -#define INST(P) \ - template void bkfftgen

(BootstrappingKeyFFT

&bkfft, const SecretKey &sk) +#define INST(P) \ + template void bkfftgen

(BootstrappingKeyFFT

& bkfft, \ + const SecretKey &sk) TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) #undef INST @@ -42,8 +43,9 @@ void bknttgen(BootstrappingKeyNTT

&bkntt, const SecretKey &sk) plainpoly, P::targetP::α, sk.key.get()); } } -#define INST(P) \ - template void bknttgen

(BootstrappingKeyNTT

&bkntt, const SecretKey &sk) +#define INST(P) \ + template void bknttgen

(BootstrappingKeyNTT

& bkntt, \ + const SecretKey &sk) TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) #undef INST @@ -60,8 +62,8 @@ void ikskgen(KeySwitchingKey

&ksk, const SecretKey &sk) (j + 1) * P::basebit)), P::α, sk.key.get()); } -#define INST(P) \ - template void ikskgen

(KeySwitchingKey

&ksk, const SecretKey &sk) +#define INST(P) \ + template void ikskgen

(KeySwitchingKey

& ksk, const SecretKey &sk) TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH(INST) #undef INST @@ -106,7 +108,8 @@ CircuitKey::CircuitKey(const SecretKey &sk) // Generate privksk TFHEpp::Polynomial poly = {1}; privkskgen(privksk[1], poly, sk); - for(int i = 0; i()[i]; + for (int i = 0; i < privksP::targetP::n; i++) + poly[i] = -sk.key.get()[i]; privkskgen(privksk[0], poly, sk); } #define INST(bsP, privksP) \ diff --git a/src/gate.cpp b/src/gate.cpp index e5f7d2a..b2912b4 100644 --- a/src/gate.cpp +++ b/src/gate.cpp @@ -155,8 +155,10 @@ void HomMUXwoSE(TRLWE &res, temp1[lvl0param::n] -= P::domainP::μ; temp0[lvl0param::n] -= P::domainP::μ; TRLWE and0; - BlindRotate

(res, temp1, bkfft, μpolygen()); - BlindRotate

(and0, temp0, bkfft, μpolygen()); + BlindRotate

(res, temp1, bkfft, + μpolygen()); + BlindRotate

(and0, temp0, bkfft, + μpolygen()); for (int i = 0; i < P::targetP::n; i++) { res[0][i] += and0[0][i]; @@ -191,8 +193,10 @@ void ExtractSwitchAndHomMUX(TRLWE &res, const TRLWE &csr, c1[lvl0param::n] -= lvl0param::μ; c0[lvl0param::n] -= lvl0param::μ; TRLWE and0; - BlindRotate(res, c1, gk.bkfftlvl01, μpolygen()); - BlindRotate(and0, c0, gk.bkfftlvl01, μpolygen()); + BlindRotate(res, c1, gk.bkfftlvl01, + μpolygen()); + BlindRotate(and0, c0, gk.bkfftlvl01, + μpolygen()); for (int i = 0; i < lvl1param::n; i++) { res[0][i] += and0[0][i]; diff --git a/test/combinedmem.cpp b/test/combinedmem.cpp index ecfaab4..ada1693 100644 --- a/test/combinedmem.cpp +++ b/test/combinedmem.cpp @@ -133,8 +133,8 @@ void combWRAM( SampleExtractIndex(temp2, temp, 0); TLWE temp3; IdentityKeySwitch(temp3, temp2, gk.ksk); - BlindRotate(encram[j][i], temp3, - gk.bkfftlvl01, μpolygen()); + BlindRotate(encram[j][i], temp3, gk.bkfftlvl01, + μpolygen()); } } } diff --git a/test/ram.cpp b/test/ram.cpp index ce742a0..e9d8876 100644 --- a/test/ram.cpp +++ b/test/ram.cpp @@ -90,8 +90,9 @@ int main() SampleExtractIndex(temp2, temp, 0); TLWE temp3; IdentityKeySwitch(temp3, temp2, (*ck).ksk); - BlindRotate((*encmemory)[i], temp3, - (*ck).ck.bkfft,μpolygen()); + BlindRotate( + (*encmemory)[i], temp3, (*ck).ck.bkfft, + μpolygen()); } end = chrono::system_clock::now(); diff --git a/test/write.cpp b/test/write.cpp index bfccd0b..b285c76 100644 --- a/test/write.cpp +++ b/test/write.cpp @@ -77,8 +77,9 @@ int main() } } TRLWE msbaddress; - BlindRotate( - msbaddress, encaddress[address_bit - 1], (*ck).gk.bkfftlvl01, μpolygen()); + BlindRotate(msbaddress, encaddress[address_bit - 1], + (*ck).gk.bkfftlvl01, + μpolygen()); trlweaddress[memsize >> 1] = msbaddress; trlweaddress[memsize >> 1][1][0] += lvl1param::μ;