diff --git a/include/cloudkey.hpp b/include/cloudkey.hpp index cad1862..adf8933 100644 --- a/include/cloudkey.hpp +++ b/include/cloudkey.hpp @@ -193,7 +193,7 @@ void ikskgen(KeySwitchingKey

& ksk, const Key& domainkey, for (int l = 0; l < P::domainP::k; l++) for (int i = 0; i < P::domainP::n; i++) for (int j = 0; j < P::t; j++) - for (uint32_t k = 0; k < 1U << (P::basebit-1); k++) + for (uint32_t k = 0; k < 1U << (P::basebit - 1); k++) ksk[l * P::domainP::n + i][j][k] = tlweSymEncrypt( domainkey[l * P::domainP::n + i] * (k + 1) * @@ -223,7 +223,8 @@ void privkskgen(PrivateKeySwitchingKey

& privksk, #pragma omp parallel for collapse(3) for (int i = 0; i <= P::domainP::k * 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 c = trlweSymEncryptZero(targetkey); for (int k = 0; k < P::targetP::n; k++) diff --git a/include/keyswitch.hpp b/include/keyswitch.hpp index fd5c921..7cbf138 100644 --- a/include/keyswitch.hpp +++ b/include/keyswitch.hpp @@ -13,9 +13,10 @@ constexpr typename P::domainP::T iksoffsetgen() { typename P::domainP::T offset = 0; for (int i = 1; i <= P::t; i++) - offset += (1ULL<::digits - - i * P::basebit)); + offset += + (1ULL << P::basebit) / 2 * + (1ULL << (std::numeric_limits::digits - + i * P::basebit)); return offset; } @@ -47,22 +48,24 @@ void IdentityKeySwitch(TLWE &res, tlwe[P::domainP::k * P::domainP::n]) << (target_digit - domain_digit); -//Koga's Optimization + // Koga's Optimization constexpr typename P::domainP::T offset = iksoffsetgen

(); - constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; + constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; constexpr typename P::domainP::T halfbase = 1ULL << (P::basebit - 1); for (int i = 0; i < P::domainP::k * P::domainP::n; i++) { const typename P::domainP::T aibar = tlwe[i] + offset + roundoffset; for (int j = 0; j < P::t; j++) { const int32_t aij = - ((aibar >> (std::numeric_limits::digits - - (j + 1) * P::basebit)) & - mask)-halfbase; - if(aij > 0) + ((aibar >> + (std::numeric_limits::digits - + (j + 1) * P::basebit)) & + mask) - + halfbase; + if (aij > 0) for (int k = 0; k <= P::targetP::k * P::targetP::n; k++) res[k] -= ksk[i][j][aij - 1][k]; - else if(aij < 0) + else if (aij < 0) for (int k = 0; k <= P::targetP::k * P::targetP::n; k++) res[k] += ksk[i][j][-aij - 1][k]; } @@ -101,9 +104,9 @@ void CatIdentityKeySwitch( << (target_digit - domain_digit); } - //Koga's Optimization + // Koga's Optimization constexpr typename P::domainP::T offset = iksoffsetgen

(); - constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; + constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; constexpr typename P::domainP::T halfbase = 1ULL << (P::basebit - 1); for (int i = 0; i < P::domainP::k * P::domainP::n; i++) { std::array aibarcat; @@ -112,9 +115,11 @@ void CatIdentityKeySwitch( for (int j = 0; j < P::t; j++) { for (int cat = 0; cat < numcat; cat++) { const int32_t aij = - ((aibarcat[cat] >> (std::numeric_limits::digits - - (j + 1) * P::basebit)) & - mask)-halfbase; + ((aibarcat[cat] >> + (std::numeric_limits::digits - + (j + 1) * P::basebit)) & + mask) - + halfbase; if (aij > 0) for (int k = 0; k <= P::targetP::k * P::targetP::n; k++) res[cat][k] -= ksk[i][j][aij - 1][k]; @@ -276,9 +281,9 @@ void PrivKeySwitch(TRLWE &res, 1ULL << (std::numeric_limits::digits - (1 + P::basebit * P::t)); - //Koga's Optimization + // Koga's Optimization constexpr typename P::domainP::T offset = iksoffsetgen

(); - constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; + constexpr typename P::domainP::T mask = (1ULL << P::basebit) - 1; constexpr typename P::domainP::T halfbase = 1ULL << (P::basebit - 1); res = {}; for (int i = 0; i <= P::domainP::k * P::domainP::n; i++) { @@ -286,15 +291,17 @@ void PrivKeySwitch(TRLWE &res, for (int j = 0; j < P::t; j++) { const int32_t aij = - ((aibar >> (std::numeric_limits::digits - - (j + 1) * P::basebit)) & - mask)-halfbase; + ((aibar >> + (std::numeric_limits::digits - + (j + 1) * P::basebit)) & + mask) - + halfbase; - if(aij > 0) + if (aij > 0) for (int k = 0; k < P::targetP::k + 1; k++) for (int p = 0; p < P::targetP::n; p++) res[k][p] -= privksk[i][j][aij - 1][k][p]; - else if(aij < 0) + else if (aij < 0) for (int k = 0; k < P::targetP::k + 1; k++) for (int p = 0; p < P::targetP::n; p++) res[k][p] += privksk[i][j][abs(aij) - 1][k][p]; diff --git a/include/params.hpp b/include/params.hpp index 0d115df..6a73e2f 100644 --- a/include/params.hpp +++ b/include/params.hpp @@ -147,7 +147,7 @@ using BootstrappingKeyRAINTT = template using KeySwitchingKey = std::array< - std::array, (1 << (P::basebit-1))>, + std::array, (1 << (P::basebit - 1))>, P::t>, P::domainP::k * P::domainP::n>; template @@ -164,7 +164,7 @@ template using AnnihilateKey = std::array, P::nbit>; template using PrivateKeySwitchingKey = std::array< - std::array, (1 << (P::basebit-1))>, + std::array, (1 << (P::basebit - 1))>, P::t>, P::domainP::k * P::domainP::n + 1>; template