Skip to content

Commit

Permalink
Apply Koga's opt to CB
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Aug 14, 2024
1 parent 6923ec7 commit 5daa838
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ 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
22 changes: 14 additions & 8 deletions include/keyswitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,32 @@ void PrivKeySwitch(TRLWE<typename P::targetP> &res,
const TLWE<typename P::domainP> &tlwe,
const PrivateKeySwitchingKey<P> &privksk)
{
constexpr uint32_t mask = (1 << P::basebit) - 1;
constexpr uint64_t prec_offset =
constexpr typename P::domainP::T roundoffset =
1ULL << (std::numeric_limits<typename P::domainP::T>::digits -
(1 + P::basebit * P::t));

//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 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] + prec_offset;
const typename P::domainP::T aibar = tlwe[i] + offset + roundoffset;

for (int j = 0; j < P::t; j++) {
const typename P::domainP::T aij =
(aibar >> (std::numeric_limits<typename P::domainP::T>::digits -
const int32_t aij =
((aibar >> (std::numeric_limits<typename P::domainP::T>::digits -
(j + 1) * P::basebit)) &
mask;
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)
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
2 changes: 1 addition & 1 deletion include/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 5daa838

Please sign in to comment.