Skip to content

Commit

Permalink
clang formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Aug 14, 2024
1 parent 415e998 commit 6cb21e2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void ikskgen(KeySwitchingKey<P>& ksk, const Key<typename P::domainP>& 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<typename P::targetP>(
domainkey[l * P::domainP::n + i] * (k + 1) *
Expand Down Expand Up @@ -223,7 +223,8 @@ void privkskgen(PrivateKeySwitchingKey<P>& 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<typename P::targetP> c =
trlweSymEncryptZero<typename P::targetP>(targetkey);
for (int k = 0; k < P::targetP::n; k++)
Expand Down
51 changes: 29 additions & 22 deletions include/keyswitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<P::basebit) / 2 *
(1ULL << (std::numeric_limits<typename P::domainP::T>::digits -
i * P::basebit));
offset +=
(1ULL << P::basebit) / 2 *
(1ULL << (std::numeric_limits<typename P::domainP::T>::digits -
i * P::basebit));
return offset;
}

Expand Down Expand Up @@ -47,22 +48,24 @@ void IdentityKeySwitch(TLWE<typename P::targetP> &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<P>();
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<typename P::domainP::T>::digits -
(j + 1) * P::basebit)) &
mask)-halfbase;
if(aij > 0)
((aibar >>
(std::numeric_limits<typename P::domainP::T>::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];
}
Expand Down Expand Up @@ -101,9 +104,9 @@ void CatIdentityKeySwitch(
<< (target_digit - domain_digit);
}

//Koga's Optimization
// Koga's Optimization
constexpr typename P::domainP::T offset = iksoffsetgen<P>();
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<typename P::domainP::T, numcat> aibarcat;
Expand All @@ -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<typename P::domainP::T>::digits -
(j + 1) * P::basebit)) &
mask)-halfbase;
((aibarcat[cat] >>
(std::numeric_limits<typename P::domainP::T>::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];
Expand Down Expand Up @@ -276,25 +281,27 @@ void PrivKeySwitch(TRLWE<typename P::targetP> &res,
1ULL << (std::numeric_limits<typename P::domainP::T>::digits -
(1 + P::basebit * P::t));

//Koga's Optimization
// Koga's Optimization
constexpr typename P::domainP::T offset = iksoffsetgen<P>();
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++) {
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<typename P::domainP::T>::digits -
(j + 1) * P::basebit)) &
mask)-halfbase;
((aibar >>
(std::numeric_limits<typename P::domainP::T>::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];
Expand Down
4 changes: 2 additions & 2 deletions include/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ using BootstrappingKeyRAINTT =

template <class P>
using KeySwitchingKey = std::array<
std::array<std::array<TLWE<typename P::targetP>, (1 << (P::basebit-1))>,
std::array<std::array<TLWE<typename P::targetP>, (1 << (P::basebit - 1))>,
P::t>,
P::domainP::k * P::domainP::n>;
template <class P>
Expand All @@ -164,7 +164,7 @@ template <class P>
using AnnihilateKey = std::array<TRGSWFFT<P>, P::nbit>;
template <class P>
using PrivateKeySwitchingKey = std::array<
std::array<std::array<TRLWE<typename P::targetP>, (1 << (P::basebit-1))>,
std::array<std::array<TRLWE<typename P::targetP>, (1 << (P::basebit - 1))>,
P::t>,
P::domainP::k * P::domainP::n + 1>;
template <class P>
Expand Down

0 comments on commit 6cb21e2

Please sign in to comment.