Skip to content

Commit 4adc331

Browse files
committed
formatted
1 parent fdd4b86 commit 4adc331

File tree

10 files changed

+106
-83
lines changed

10 files changed

+106
-83
lines changed

include/aes.hpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
#pragma once
22
#include <AES.h>
3-
//Transciphering by AES
4-
// Based on Hippogryph
5-
namespace TFHEpp
6-
{
3+
// Transciphering by AES
4+
// Based on Hippogryph
5+
namespace TFHEpp {
76

87
template <class P>
9-
inline Polynomial<P> AESInvSboxPoly(const uint8_t upperindex){
8+
inline Polynomial<P> AESInvSboxPoly(const uint8_t upperindex)
9+
{
1010
Polynomial<P> poly;
11-
constexpr uint segment = P::n/16;
12-
for(int i = 0; i < 16; i++)
13-
for(int j = 0; j < segment/2; j++){
14-
poly[i*segment+2*j] = (inv_sbox[upperindex][i]&0xF)*(1ULL<<(std::numeric_limits<typename P::T>::digits-5));
15-
poly[i*segment+2*j+1] = (inv_sbox[upperindex][i]>>4)*(1ULL<<(std::numeric_limits<typename P::T>::digits-5));
11+
constexpr uint segment = P::n / 16;
12+
for (int i = 0; i < 16; i++)
13+
for (int j = 0; j < segment / 2; j++) {
14+
poly[i * segment + 2 * j] =
15+
(inv_sbox[upperindex][i] & 0xF) *
16+
(1ULL << (std::numeric_limits<typename P::T>::digits - 5));
17+
poly[i * segment + 2 * j + 1] =
18+
(inv_sbox[upperindex][i] >> 4) *
19+
(1ULL << (std::numeric_limits<typename P::T>::digits - 5));
1620
}
1721
return poly;
1822
}
1923

2024
template <class iksP, class brP>
2125
void AESInvSbox(std::array<TLWE<typename brP::targetP>, 2> &res,
22-
const std::array<TLWE<typename iksP::domainP>, 2> &tlwe,
23-
const EvalKey &ek)
26+
const std::array<TLWE<typename iksP::domainP>, 2> &tlwe,
27+
const EvalKey &ek)
2428
{
25-
std::array<std::array<TLWE<typename brP::targetP>,2>,16> midtlwes;
29+
std::array<std::array<TLWE<typename brP::targetP>, 2>, 16> midtlwes;
2630
TLWE<typename iksP::targetP> shifted;
2731
IdentityKeySwitch<iksP>(shifted, tlwe[0], ek.getiksk<iksP>());
28-
shifted[iksP::targetP::k*iksP::targetP::n] += 1ULL<<(std::numeric_limits<typename iksP::targetP::T>::digits-6);
32+
shifted[iksP::targetP::k * iksP::targetP::n] +=
33+
1ULL << (std::numeric_limits<typename iksP::targetP::T>::digits - 6);
2934
for (int i = 0; i < 16; i++)
30-
GateBootstrappingManyLUT<brP, 2>(midtlwes[i], shifted, ek.getbkfft<brP>(), AESInvSboxPoly<typename brP::targetP>(i));
35+
GateBootstrappingManyLUT<brP, 2>(
36+
midtlwes[i], shifted, ek.getbkfft<brP>(),
37+
AESInvSboxPoly<typename brP::targetP>(i));
3138
IdentityKeySwitch<iksP>(shifted, tlwe[1], ek.getiksk<iksP>());
32-
shifted[iksP::targetP::k*iksP::targetP::n] += 1ULL<<(std::numeric_limits<typename iksP::targetP::T>::digits-6);
33-
for(int i = 0; i < 2; i++){
39+
shifted[iksP::targetP::k * iksP::targetP::n] +=
40+
1ULL << (std::numeric_limits<typename iksP::targetP::T>::digits - 6);
41+
for (int i = 0; i < 2; i++) {
3442
TRLWE<typename brP::targetP> trlwe;
3543
std::array<TLWE<typename iksP::domainP>, 16> tabletlwe;
36-
for(int j = 0; j < 16; j++) tabletlwe[j] = midtlwes[j][i];
37-
TLWE2TablePacking<typename brP::targetP, 16>(trlwe, tabletlwe, ek.getahk<typename brP::targetP>());
38-
GateBootstrappingTLWE2TLWEFFT<brP>(res[i], shifted, ek.getbkfft<brP>(), trlwe);
44+
for (int j = 0; j < 16; j++) tabletlwe[j] = midtlwes[j][i];
45+
TLWE2TablePacking<typename brP::targetP, 16>(
46+
trlwe, tabletlwe, ek.getahk<typename brP::targetP>());
47+
GateBootstrappingTLWE2TLWEFFT<brP>(res[i], shifted, ek.getbkfft<brP>(),
48+
trlwe);
3949
}
4050
}
41-
}
51+
} // namespace TFHEpp

include/evalkeygens.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ template <class P>
177177
void annihilatekeygen(AnnihilateKey<P>& ahk, const Key<P>& key)
178178
{
179179
for (int i = 0; i < P::nbit; i++)
180-
evalautokeygen<P>(ahk[i], (1 << (i+1)) + 1, key);
180+
evalautokeygen<P>(ahk[i], (1 << (i + 1)) + 1, key);
181181
}
182182

183183
template <class P>

include/gatebootstrapping.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ void BlindRotate(TRLWE<typename P::targetP> &res,
119119
<< bitwidth;
120120
if (ā == 0) continue;
121121
// Do not use CMUXFFT to avoid unnecessary copy.
122-
CMUXFFTwithPolynomialMulByXaiMinusOne<P>(res,
123-
bkfft[i], ā);
122+
CMUXFFTwithPolynomialMulByXaiMinusOne<P>(res, bkfft[i], ā);
124123
}
125124
#endif
126125
}
@@ -197,10 +196,10 @@ void GateBootstrappingTLWE2TLWEFFT(
197196
}
198197

199198
template <class P>
200-
void GateBootstrappingTLWE2TLWEFFT(
201-
TLWE<typename P::targetP> &res, const TLWE<typename P::domainP> &tlwe,
202-
const BootstrappingKeyFFT<P> &bkfft,
203-
const TRLWE<typename P::targetP> &testvector)
199+
void GateBootstrappingTLWE2TLWEFFT(TLWE<typename P::targetP> &res,
200+
const TLWE<typename P::domainP> &tlwe,
201+
const BootstrappingKeyFFT<P> &bkfft,
202+
const TRLWE<typename P::targetP> &testvector)
204203
{
205204
alignas(64) TRLWE<typename P::targetP> acc;
206205
BlindRotate<P>(acc, tlwe, bkfft, testvector);

include/keyswitch.hpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include <array>
4-
#include <span>
54
#include <bit>
5+
#include <span>
66

77
#include "params.hpp"
88
#include "trgsw.hpp"
@@ -307,7 +307,7 @@ void EvalAuto(TRLWE<P> &res, const TRLWE<P> &trlwe, const int d,
307307
}
308308

309309
// https://eprint.iacr.org/2024/1318
310-
// Reversed order but this is easily proved by packing trivial all 0 TRLWE.
310+
// Reversed order but this is easily proved by packing trivial all 0 TRLWE.
311311
// TODO: They says we should divide by N first, not by 2 for each step. Why?
312312
template <class P>
313313
void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
@@ -318,7 +318,7 @@ void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
318318
for (int i = 0; i < P::nbit; i++) {
319319
for (int j = 0; j < (P::k + 1) * P::n; j++) res[0][j] /= 2;
320320
TRLWE<P> evaledauto;
321-
EvalAuto<P>(evaledauto, res, (1 << (i+1)) + 1, ahk[i]);
321+
EvalAuto<P>(evaledauto, res, (1 << (i + 1)) + 1, ahk[i]);
322322
for (int j = 0; j < (P::k + 1) * P::n; j++)
323323
res[0][j] += evaledauto[0][j];
324324
}
@@ -349,13 +349,13 @@ void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
349349
// }
350350

351351
// template <class P, uint num_tlwe>
352-
// void AnnihilatePacking(TRLWE<P> &res, const std::array<TLWE<P>, num_tlwe> &tlwes,
352+
// void AnnihilatePacking(TRLWE<P> &res, const std::array<TLWE<P>, num_tlwe>
353+
// &tlwes,
353354
// const AnnihilateKey<P> &ahk)
354355
// {
355-
// static_assert(std::has_single_bit(num_tlwe), "Currently, num_tlwe must be power of 2");
356-
// std::array<TRLWE<P>, num_tlwe> trlwes;
357-
// constexpr uint l = std::count_zero(num_tlwe);
358-
// for (int i = 0; i < num_tlwe; i++) {
356+
// static_assert(std::has_single_bit(num_tlwe), "Currently, num_tlwe must be
357+
// power of 2"); std::array<TRLWE<P>, num_tlwe> trlwes; constexpr uint l =
358+
// std::count_zero(num_tlwe); for (int i = 0; i < num_tlwe; i++) {
359359
// InvSampleExtractIndex<P>(trlwes[i], tlwes[i], 0);
360360
// for (int j = 0; j <= P::k * P::n; j++)//rest are known to be 0
361361
// trlwes[i][0][j] /= P::n;
@@ -369,8 +369,8 @@ void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
369369
// trlwes[stride+j][k] = trlwes[j][k] - res[k];
370370
// for(int k = 0; i < (P::k+1) * P::n; k++)
371371
// trlwes[j][k] += res[k];
372-
// EvalAuto<P>(res, trlwes[stride+j], (1 << (P::nbit - i)) + 1, ahk[i]);
373-
// for(int k = 0; i < (P::k+1) * P::n; k++)
372+
// EvalAuto<P>(res, trlwes[stride+j], (1 << (P::nbit - i)) + 1,
373+
// ahk[i]); for(int k = 0; i < (P::k+1) * P::n; k++)
374374
// trlwes[j][k] += res[k];
375375
// }
376376
// }
@@ -385,9 +385,8 @@ void AnnihilateKeySwitching(TRLWE<P> &res, const TRLWE<P> &trlwe,
385385
// }
386386

387387
template <class P, class Container>
388-
void PackLWEs(TRLWE<P> &res, const Container &tlwe,
389-
const AnnihilateKey<P> &ahk, const uint l, const uint offset,
390-
const uint interval)
388+
void PackLWEs(TRLWE<P> &res, const Container &tlwe, const AnnihilateKey<P> &ahk,
389+
const uint l, const uint offset, const uint interval)
391390
{
392391
if (l == 0)
393392
InvSampleExtractIndex<P>(res, tlwe[offset], 0);
@@ -405,7 +404,7 @@ void PackLWEs(TRLWE<P> &res, const Container &tlwe,
405404
tempodd[i][j] = tempeven[i][j] - tempoddmul[i][j];
406405
}
407406
}
408-
EvalAuto<P>(res, tempodd, (1 << l) + 1, ahk[l-1]);
407+
EvalAuto<P>(res, tempodd, (1 << l) + 1, ahk[l - 1]);
409408
for (int i = 0; i < P::k + 1; i++)
410409
for (int j = 0; j < P::n; j++)
411410
res[i][j] += tempeven[i][j] + tempoddmul[i][j];
@@ -414,7 +413,7 @@ void PackLWEs(TRLWE<P> &res, const Container &tlwe,
414413

415414
template <class P>
416415
void TLWE2TRLWEChensPacking(TRLWE<P> &res, std::vector<TLWE<P>> &tlwe,
417-
const AnnihilateKey<P> &ahk)
416+
const AnnihilateKey<P> &ahk)
418417
{
419418
uint l = std::bit_width(tlwe.size()) - 1;
420419
if (!std::has_single_bit(tlwe.size())) {
@@ -425,34 +424,34 @@ void TLWE2TRLWEChensPacking(TRLWE<P> &res, std::vector<TLWE<P>> &tlwe,
425424
for (int i = l; i < P::nbit; i++) {
426425
TRLWE<P> evaledauto;
427426
for (int j = 0; j < (P::k + 1) * P::n; j++) res[0][j] /= 2;
428-
EvalAuto<P>(evaledauto, res, (1 << (i+1)) + 1, ahk[i]);
427+
EvalAuto<P>(evaledauto, res, (1 << (i + 1)) + 1, ahk[i]);
429428
for (int j = 0; j < (P::k + 1) * P::n; j++)
430429
res[0][j] += evaledauto[0][j];
431430
}
432431
}
433432

434433
template <class P, uint num_tlwe>
435-
void TLWE2TablePacking(TRLWE<P> &res, std::array<TLWE<P>,num_tlwe> &tlwe,
436-
const AnnihilateKey<P> &ahk)
434+
void TLWE2TablePacking(TRLWE<P> &res, std::array<TLWE<P>, num_tlwe> &tlwe,
435+
const AnnihilateKey<P> &ahk)
437436
{
438-
static_assert(std::has_single_bit(num_tlwe), "Currently, num_tlwe must be power of 2");
437+
static_assert(std::has_single_bit(num_tlwe),
438+
"Currently, num_tlwe must be power of 2");
439439
constexpr uint l = std::countr_zero(num_tlwe);
440440
PackLWEs<P>(res, tlwe, ahk, l, 0, 1);
441441
for (int i = l; i < P::nbit; i++) {
442442
TRLWE<P> tempmul;
443-
for (int j = 0; j < P::k + 1; j++)
444-
PolynomialMulByXai<P>(tempmul[j], res[j], P::n >> (i+1));
443+
for (int j = 0; j < P::k + 1; j++)
444+
PolynomialMulByXai<P>(tempmul[j], res[j], P::n >> (i + 1));
445445
TRLWE<P> tempsub;
446-
for (int j = 0; j < (P::k + 1) * P::n; j++){
446+
for (int j = 0; j < (P::k + 1) * P::n; j++) {
447447
res[0][j] /= 2;
448448
tempmul[0][j] /= 2;
449449
tempsub[0][j] = res[0][j] - tempmul[0][j];
450450
res[0][j] += tempmul[0][j];
451451
}
452-
//reuse tempmul
453-
EvalAuto<P>(tempmul, tempsub, (1 << (i+1)) + 1, ahk[i]);
454-
for (int j = 0; j < (P::k + 1) * P::n; j++)
455-
res[0][j] += tempmul[0][j];
452+
// reuse tempmul
453+
EvalAuto<P>(tempmul, tempsub, (1 << (i + 1)) + 1, ahk[i]);
454+
for (int j = 0; j < (P::k + 1) * P::n; j++) res[0][j] += tempmul[0][j];
456455
}
457456
}
458457

@@ -480,7 +479,7 @@ void PackLWEsLSB(TRLWE<P> &res, const std::vector<TLWE<P>> &tlwe,
480479
tempodd[i][j] = tempeven[i][j] - tempoddmul[i][j];
481480
}
482481
}
483-
EvalAuto<P>(res, tempodd, (1 << l) + 1, ahk[l-1]);
482+
EvalAuto<P>(res, tempodd, (1 << l) + 1, ahk[l - 1]);
484483
for (int i = 0; i < P::k + 1; i++)
485484
for (int j = 0; j < P::n; j++)
486485
res[i][j] += tempeven[i][j] + tempoddmul[i][j];

include/params/concrete.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct lvl2param {
7575
static constexpr int32_t key_value_max = 1;
7676
static constexpr int32_t key_value_min = -1;
7777
static const std::uint32_t nbit = 10; // dimension must be a power of 2 for
78-
// ease of polynomial multiplication.
78+
// ease of polynomial multiplication.
7979
static constexpr std::uint32_t n = 1 << nbit; // dimension
8080
static constexpr std::uint32_t k = 2;
8181
static constexpr std::uint32_t lₐ = 3;

include/tlwe.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ TLWE<P> tlweSymEncrypt(const typename P::T p, const Key<P> &key)
5454
template <class P, uint plain_modulus = P::plain_modulus>
5555
TLWE<P> tlweSymIntEncrypt(const typename P::T p, const double α,
5656
const Key<P> &key)
57-
{ constexpr double Δ =
57+
{
58+
constexpr double Δ =
5859
std::pow(2.0, std::numeric_limits<typename P::T>::digits) /
5960
plain_modulus;
6061
return tlweSymEncrypt<P>(static_cast<typename P::T>(p * Δ), α, key);
@@ -82,7 +83,7 @@ TLWE<P> tlweSymIntEncrypt(const typename P::T p, const Key<P> &key)
8283
template <class P, uint plain_modulus = P::plain_modulus>
8384
TLWE<P> tlweSymIntEncrypt(const typename P::T p, const SecretKey &sk)
8485
{
85-
return tlweSymIntEncrypt<P,plain_modulus>(p, sk.key.get<P>());
86+
return tlweSymIntEncrypt<P, plain_modulus>(p, sk.key.get<P>());
8687
}
8788

8889
template <class P>
@@ -117,7 +118,7 @@ typename P::T tlweSymIntDecrypt(const TLWE<P> &c, const Key<P> &key)
117118
return res >= plain_modulus / 2 ? res - plain_modulus : res;
118119
}
119120

120-
template <class P, uint plain_modulus=P::plain_modulus>
121+
template <class P, uint plain_modulus = P::plain_modulus>
121122
typename P::T tlweSymIntDecrypt(const TLWE<P> &c, const SecretKey &sk)
122123
{
123124
return tlweSymIntDecrypt<P, plain_modulus>(c, sk.key.get<P>());

test/aesinvsbox.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
#include <random>
66
#include <tfhe++.hpp>
77

8-
int main(){
8+
int main()
9+
{
910
using brP = TFHEpp::lvlh2param;
1011
using iksP = TFHEpp::lvl2hparam;
1112
std::random_device seed_gen;
1213
std::default_random_engine engine(seed_gen());
13-
constexpr uint32_t plain_modulus = 1<<(4+1);
14+
constexpr uint32_t plain_modulus = 1 << (4 + 1);
1415
std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
15-
constexpr uint num_test = 1U<<8;
16+
constexpr uint num_test = 1U << 8;
1617
// constexpr uint num_test = 1U<<4;
17-
std::vector<std::array<TFHEpp::TLWE<typename iksP::domainP>,2>> cin(num_test);
18+
std::vector<std::array<TFHEpp::TLWE<typename iksP::domainP>, 2>> cin(
19+
num_test);
1820

19-
for (int i = 0; i < num_test; i++){
20-
cin[i][0] = TFHEpp::tlweSymIntEncrypt<typename iksP::domainP, plain_modulus>(i&0xF, *sk);
21-
cin[i][1] = TFHEpp::tlweSymIntEncrypt<typename iksP::domainP, plain_modulus>((i>>4), *sk);
21+
for (int i = 0; i < num_test; i++) {
22+
cin[i][0] =
23+
TFHEpp::tlweSymIntEncrypt<typename iksP::domainP, plain_modulus>(
24+
i & 0xF, *sk);
25+
cin[i][1] =
26+
TFHEpp::tlweSymIntEncrypt<typename iksP::domainP, plain_modulus>(
27+
(i >> 4), *sk);
2228
}
23-
std::vector<std::array<TFHEpp::TLWE<typename brP::targetP>,2>> cres(num_test);
29+
std::vector<std::array<TFHEpp::TLWE<typename brP::targetP>, 2>> cres(
30+
num_test);
2431
TFHEpp::EvalKey ek;
2532
ek.emplacebkfft<brP>(*sk);
2633
ek.emplaceiksk<iksP>(*sk);
@@ -29,8 +36,8 @@ int main(){
2936
std::chrono::system_clock::time_point start, end;
3037
start = std::chrono::system_clock::now();
3138
for (int test = 0; test < num_test; test++) {
32-
std::cout<<"test: " << test << std::endl;
33-
TFHEpp::AESInvSbox<iksP,brP>(cres[test], cin[test], ek);
39+
std::cout << "test: " << test << std::endl;
40+
TFHEpp::AESInvSbox<iksP, brP>(cres[test], cin[test], ek);
3441
}
3542

3643
end = std::chrono::system_clock::now();
@@ -39,9 +46,15 @@ int main(){
3946
.count();
4047
std::cout << elapsed / num_test << "ms" << std::endl;
4148
for (int i = 0; i < num_test; i++) {
42-
const uint8_t pres = (TFHEpp::tlweSymIntDecrypt<typename brP::targetP, plain_modulus>(cres[i][1], *sk)<<4)+TFHEpp::tlweSymIntDecrypt<typename brP::targetP, plain_modulus>(cres[i][0], *sk);
43-
// std::cout << "test: " << i << " pres: " << (int)pres << " expected: " << (int)inv_sbox[i>>4][i&0xF] << std::endl;
44-
assert(pres == inv_sbox[i>>4][i&0xF]);
49+
const uint8_t pres =
50+
(TFHEpp::tlweSymIntDecrypt<typename brP::targetP, plain_modulus>(
51+
cres[i][1], *sk)
52+
<< 4) +
53+
TFHEpp::tlweSymIntDecrypt<typename brP::targetP, plain_modulus>(
54+
cres[i][0], *sk);
55+
// std::cout << "test: " << i << " pres: " << (int)pres << " expected: "
56+
// << (int)inv_sbox[i>>4][i&0xF] << std::endl;
57+
assert(pres == inv_sbox[i >> 4][i & 0xF]);
4558
}
4659
std::cout << "Passed" << std::endl;
4760
}

test/annihilate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ int main()
5151
pres[i] = TFHEpp::trlweSymDecrypt<P>(cres[i], sk->key.get<P>());
5252
for (int i = 0; i < num_test; i++) assert(pres[i][0] == (pin[i][0] > 0));
5353
// TFHEpp::Polynomial<P> phase =
54-
// TFHEpp::trlwePhase<P>(cres[0], sk->key.get<P>());
54+
// TFHEpp::trlwePhase<P>(cres[0], sk->key.get<P>());
5555
// for (int i = 0; i < P::n; i++)
56-
// std::cout << static_cast<int64_t>(phase[i]) << ":";
56+
// std::cout << static_cast<int64_t>(phase[i]) << ":";
5757
// std::cout << std::endl;
5858
std::cout << "Passed" << std::endl;
5959
double elapsed =

test/chenspacking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main()
3737

3838
for (int test = 0; test < num_test; test++) {
3939
TFHEpp::TLWE2TRLWEChensPacking<TFHEpp::lvl1param>(cres[test], ca[test],
40-
*ahk);
40+
*ahk);
4141
}
4242

4343
end = std::chrono::system_clock::now();

0 commit comments

Comments
 (0)