Skip to content

Commit 9068a47

Browse files
committed
Added ManyLUT packing
1 parent dd638ab commit 9068a47

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

include/aes.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ void AESInvSbox(std::array<TLWE<typename brP::targetP>, 2> &res,
180180
IdentityKeySwitch<iksP>(shifted, tlwe[1], ek.getiksk<iksP>());
181181
shifted[iksP::targetP::k * iksP::targetP::n] +=
182182
1ULL << (std::numeric_limits<typename iksP::targetP::T>::digits - 6);
183-
for (int i = 0; i < 2; i++) {
183+
{
184184
TRLWE<typename brP::targetP> trlwe;
185-
std::array<TLWE<typename iksP::domainP>, 16> tabletlwe;
186-
for (int j = 0; j < 16; j++) tabletlwe[j] = midtlwes[j][i];
187-
TLWE2TablePacking<typename brP::targetP, 16>(
185+
std::array<std::array<TLWE<typename iksP::domainP>, 16>, 2> tabletlwe;
186+
for(int i = 0; i <2; i++) for (int j = 0; j < 16; j++) tabletlwe[i][j] = midtlwes[j][i];
187+
TLWE2TablePackingManyLUT<typename brP::targetP, 16, 2>(
188188
trlwe, tabletlwe, ek.getahk<typename brP::targetP>());
189-
GateBootstrappingTLWE2TLWEFFT<brP>(res[i], shifted, ek.getbkfft<brP>(),
189+
GateBootstrappingManyLUT<brP,2>(res, shifted, ek.getbkfft<brP>(),
190190
trlwe);
191191
}
192192
}

include/gatebootstrapping.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ void GateBootstrappingManyLUT(
240240
SampleExtractIndex<typename P::targetP>(res[i], acc, i);
241241
}
242242

243+
template <class P, uint32_t num_out>
244+
void GateBootstrappingManyLUT(
245+
std::array<TLWE<typename P::targetP>, num_out> &res,
246+
const TLWE<typename P::domainP> &tlwe, const BootstrappingKeyFFT<P> &bkfft,
247+
const TRLWE<typename P::targetP> &testvector)
248+
{
249+
alignas(64) TRLWE<typename P::targetP> acc;
250+
BlindRotate<P, num_out>(acc, tlwe, bkfft, testvector);
251+
for (int i = 0; i < num_out; i++)
252+
SampleExtractIndex<typename P::targetP>(res[i], acc, i);
253+
}
254+
243255
template <class P, typename P::T μ>
244256
constexpr Polynomial<P> μpolygen()
245257
{

include/keyswitch.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,52 @@ void TLWE2TablePacking(TRLWE<P> &res, std::array<TLWE<P>, num_tlwe> &tlwe,
455455
}
456456
}
457457

458+
template <class P, uint num_tlwe, uint num_func>
459+
void TLWE2TablePackingManyLUT(TRLWE<P> &res, std::array<std::array<TLWE<P>, num_tlwe>,num_func> &tlwe,
460+
const AnnihilateKey<P> &ahk)
461+
{
462+
static_assert(std::has_single_bit(num_tlwe),
463+
"Currently, num_tlwe must be power of 2");
464+
constexpr uint l = std::countr_zero(num_tlwe);
465+
static_assert(num_func == 2,
466+
"Currently, num_func must be 2");
467+
constexpr uint f = std::countr_zero(num_func);
468+
std::array<TRLWE<P>, num_func> temptrlwe;
469+
for(int index = 0; index < num_func; index++){
470+
PackLWEs<P>(temptrlwe[index], tlwe[index], ahk, l, 0, 1);
471+
for (int i = l; i < P::nbit-f; i++) {
472+
TRLWE<P> tempmul;
473+
for (int j = 0; j < P::k + 1; j++)
474+
PolynomialMulByXai<P>(tempmul[j], temptrlwe[index][j], P::n >> (i + 1));
475+
TRLWE<P> tempsub;
476+
for (int j = 0; j < (P::k + 1) * P::n; j++) {
477+
temptrlwe[index][0][j] /= 2;
478+
tempmul[0][j] /= 2;
479+
tempsub[0][j] = temptrlwe[index][0][j] - tempmul[0][j];
480+
temptrlwe[index][0][j] += tempmul[0][j];
481+
}
482+
// reuse tempmul
483+
EvalAuto<P>(tempmul, tempsub, (1 << (i + 1)) + 1, ahk[i]);
484+
for (int j = 0; j < (P::k + 1) * P::n; j++) temptrlwe[index][0][j] += tempmul[0][j];
485+
}
486+
}
487+
{
488+
TRLWE<P> tempoddmul;
489+
for (int i = 0; i < P::k + 1; i++) {
490+
PolynomialMulByXai<P>(tempoddmul[i], temptrlwe[1][i],1);
491+
for (int j = 0; j < P::n; j++) {
492+
temptrlwe[0][i][j] /= 2;
493+
tempoddmul[i][j] /= 2;
494+
temptrlwe[1][i][j] = temptrlwe[0][i][j] - tempoddmul[i][j];
495+
}
496+
}
497+
EvalAuto<P>(res, temptrlwe[1], (1 << P::nbit) + 1, ahk[P::nbit - 1]);
498+
for (int i = 0; i < P::k + 1; i++)
499+
for (int j = 0; j < P::n; j++)
500+
res[i][j] += temptrlwe[0][i][j] + tempoddmul[i][j];
501+
}
502+
}
503+
458504
template <class P>
459505
void PackLWEsLSB(TRLWE<P> &res, const std::vector<TLWE<P>> &tlwe,
460506
const AnnihilateKey<P> &ahk, const uint l, const uint offset,

0 commit comments

Comments
 (0)