Skip to content

Commit 3729c45

Browse files
committed
clang-formatted
1 parent ca7099f commit 3729c45

5 files changed

Lines changed: 184 additions & 109 deletions

File tree

benchmark/bench.cpp

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,79 @@
1-
#include "google-benchmark/include/benchmark/benchmark.h"
2-
#include "../include/tfhe++.hpp"
3-
#include <random>
41
#include <memory>
2+
#include <random>
53

6-
void BM_HomGate(benchmark::State& state){
4+
#include "../include/tfhe++.hpp"
5+
#include "google-benchmark/include/benchmark/benchmark.h"
6+
7+
void BM_HomGate(benchmark::State& state)
8+
{
79
std::random_device seed_gen;
810
std::default_random_engine engine(seed_gen());
911
std::uniform_int_distribution<uint32_t> binary(0, 1);
1012
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
1113
const std::unique_ptr<TFHEpp::GateKey> gk(new TFHEpp::GateKey(*sk));
12-
TFHEpp::TLWE<TFHEpp::lvl0param> ca = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
13-
TFHEpp::TLWE<TFHEpp::lvl0param> cb = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
14+
TFHEpp::TLWE<TFHEpp::lvl0param> ca =
15+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
16+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
17+
TFHEpp::TLWE<TFHEpp::lvl0param> cb =
18+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
19+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
1420
TFHEpp::TLWE<TFHEpp::lvl0param> res;
1521
for (auto _ : state) TFHEpp::HomNAND(res, ca, cb, *gk);
1622
}
1723

18-
void BM_HomMUX(benchmark::State& state){
24+
void BM_HomMUX(benchmark::State& state)
25+
{
1926
std::random_device seed_gen;
2027
std::default_random_engine engine(seed_gen());
2128
std::uniform_int_distribution<uint32_t> binary(0, 1);
2229
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
2330
const std::unique_ptr<TFHEpp::GateKey> gk(new TFHEpp::GateKey(*sk));
24-
TFHEpp::TLWE<TFHEpp::lvl0param> ca = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
25-
TFHEpp::TLWE<TFHEpp::lvl0param> cb = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
26-
TFHEpp::TLWE<TFHEpp::lvl0param> cs = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
31+
TFHEpp::TLWE<TFHEpp::lvl0param> ca =
32+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
33+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
34+
TFHEpp::TLWE<TFHEpp::lvl0param> cb =
35+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
36+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
37+
TFHEpp::TLWE<TFHEpp::lvl0param> cs =
38+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
39+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
2740
TFHEpp::TLWE<TFHEpp::lvl0param> res;
2841
for (auto _ : state) TFHEpp::HomMUX(res, cs, ca, cb, *gk);
2942
}
3043

31-
void BM_TLWE2TRLWE(benchmark::State& state){
44+
void BM_TLWE2TRLWE(benchmark::State& state)
45+
{
3246
std::random_device seed_gen;
3347
std::default_random_engine engine(seed_gen());
3448
std::uniform_int_distribution<uint32_t> binary(0, 1);
3549
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
3650
const std::unique_ptr<TFHEpp::GateKey> gk(new TFHEpp::GateKey(*sk));
37-
TFHEpp::TLWE<TFHEpp::lvl0param> ca = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
51+
TFHEpp::TLWE<TFHEpp::lvl0param> ca =
52+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
53+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
3854
TFHEpp::TRLWE<TFHEpp::lvl1param> res;
39-
for (auto _ : state) TFHEpp::GateBootstrappingTLWE2TRLWEFFT<TFHEpp::lvl01param>(res, ca, gk->bkfftlvl01);
55+
for (auto _ : state)
56+
TFHEpp::GateBootstrappingTLWE2TRLWEFFT<TFHEpp::lvl01param>(
57+
res, ca, gk->bkfftlvl01);
4058
}
4159

42-
void BM_IKS(benchmark::State& state){
60+
void BM_IKS(benchmark::State& state)
61+
{
4362
std::random_device seed_gen;
4463
std::default_random_engine engine(seed_gen());
4564
std::uniform_int_distribution<uint32_t> binary(0, 1);
4665
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
4766
const std::unique_ptr<TFHEpp::GateKey> gk(new TFHEpp::GateKey(*sk));
48-
TFHEpp::TLWE<TFHEpp::lvl1param> ca = TFHEpp::tlweSymEncrypt<TFHEpp::lvl1param>(binary(engine),TFHEpp::lvl1param::α,sk->key.lvl1);
67+
TFHEpp::TLWE<TFHEpp::lvl1param> ca =
68+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl1param>(
69+
binary(engine), TFHEpp::lvl1param::α, sk->key.lvl1);
4970
TFHEpp::TLWE<TFHEpp::lvl0param> res;
50-
for (auto _ : state) TFHEpp::IdentityKeySwitch<TFHEpp::lvl10param>(res, ca, gk->ksk);
71+
for (auto _ : state)
72+
TFHEpp::IdentityKeySwitch<TFHEpp::lvl10param>(res, ca, gk->ksk);
5173
}
5274

53-
void BM_SEI(benchmark::State& state){
75+
void BM_SEI(benchmark::State& state)
76+
{
5477
std::random_device seed_gen;
5578
std::default_random_engine engine(seed_gen());
5679
std::uniform_int_distribution<uint32_t> binary(0, 1);
@@ -59,61 +82,92 @@ void BM_SEI(benchmark::State& state){
5982
std::array<typename TFHEpp::lvl1param::T, TFHEpp::lvl1param::n> pmu;
6083
for (int j = 0; j < TFHEpp::lvl1param::n; j++)
6184
pmu[j] = binary(engine) ? TFHEpp::lvl1param::μ : -TFHEpp::lvl1param::μ;
62-
TFHEpp::TRLWE<TFHEpp::lvl1param> ca = TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu,TFHEpp::lvl1param::α,sk->key.lvl1);
85+
TFHEpp::TRLWE<TFHEpp::lvl1param> ca =
86+
TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu, TFHEpp::lvl1param::α,
87+
sk->key.lvl1);
6388
TFHEpp::TLWE<TFHEpp::lvl1param> res;
64-
for (auto _ : state) TFHEpp::SampleExtractIndex<TFHEpp::lvl1param>(res, ca, 0);
89+
for (auto _ : state)
90+
TFHEpp::SampleExtractIndex<TFHEpp::lvl1param>(res, ca, 0);
6591
}
6692

67-
void BM_CMUX(benchmark::State& state){
93+
void BM_CMUX(benchmark::State& state)
94+
{
6895
std::random_device seed_gen;
6996
std::default_random_engine engine(seed_gen());
7097
std::uniform_int_distribution<uint32_t> binary(0, 1);
7198
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
72-
std::array<typename TFHEpp::lvl1param::T, TFHEpp::lvl1param::n> pmu1,pmu0;
99+
std::array<typename TFHEpp::lvl1param::T, TFHEpp::lvl1param::n> pmu1, pmu0;
73100
for (int j = 0; j < TFHEpp::lvl1param::n; j++)
74101
pmu1[j] = binary(engine) ? TFHEpp::lvl1param::μ : -TFHEpp::lvl1param::μ;
75102
for (int j = 0; j < TFHEpp::lvl1param::n; j++)
76103
pmu0[j] = binary(engine) ? TFHEpp::lvl1param::μ : -TFHEpp::lvl1param::μ;
77-
TFHEpp::TRLWE<TFHEpp::lvl1param> c0 = TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu0,TFHEpp::lvl1param::α,sk->key.lvl1);
78-
TFHEpp::TRLWE<TFHEpp::lvl1param> c1 = TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu1,TFHEpp::lvl1param::α,sk->key.lvl1);
104+
TFHEpp::TRLWE<TFHEpp::lvl1param> c0 =
105+
TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu0, TFHEpp::lvl1param::α,
106+
sk->key.lvl1);
107+
TFHEpp::TRLWE<TFHEpp::lvl1param> c1 =
108+
TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu1, TFHEpp::lvl1param::α,
109+
sk->key.lvl1);
79110
const TFHEpp::Polynomial<TFHEpp::lvl1param> plainpoly = {binary(engine)};
80-
TFHEpp::TRGSWFFT<TFHEpp::lvl1param> cs = TFHEpp::trgswfftSymEncrypt<TFHEpp::lvl1param>(plainpoly, TFHEpp::lvl1param::α, sk->key.lvl1);
111+
TFHEpp::TRGSWFFT<TFHEpp::lvl1param> cs =
112+
TFHEpp::trgswfftSymEncrypt<TFHEpp::lvl1param>(
113+
plainpoly, TFHEpp::lvl1param::α, sk->key.lvl1);
81114
TFHEpp::TRLWE<TFHEpp::lvl1param> res;
82115
for (auto _ : state) TFHEpp::CMUXFFT<TFHEpp::lvl1param>(res, cs, c1, c0);
83116
}
84117

85-
void BM_ExternalProduct(benchmark::State& state){
118+
void BM_ExternalProduct(benchmark::State& state)
119+
{
86120
std::random_device seed_gen;
87121
std::default_random_engine engine(seed_gen());
88122
std::uniform_int_distribution<uint32_t> binary(0, 1);
89123
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
90-
std::array<typename TFHEpp::lvl1param::T, TFHEpp::lvl1param::n> pmu1,pmu0;
124+
std::array<typename TFHEpp::lvl1param::T, TFHEpp::lvl1param::n> pmu1, pmu0;
91125
for (int j = 0; j < TFHEpp::lvl1param::n; j++)
92126
pmu1[j] = binary(engine) ? TFHEpp::lvl1param::μ : -TFHEpp::lvl1param::μ;
93-
TFHEpp::TRLWE<TFHEpp::lvl1param> c0 = TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu0,TFHEpp::lvl1param::α,sk->key.lvl1);
127+
TFHEpp::TRLWE<TFHEpp::lvl1param> c0 =
128+
TFHEpp::trlweSymEncrypt<TFHEpp::lvl1param>(pmu0, TFHEpp::lvl1param::α,
129+
sk->key.lvl1);
94130
const TFHEpp::Polynomial<TFHEpp::lvl1param> plainpoly = {binary(engine)};
95-
TFHEpp::TRGSWFFT<TFHEpp::lvl1param> cs = TFHEpp::trgswfftSymEncrypt<TFHEpp::lvl1param>(plainpoly, TFHEpp::lvl1param::α, sk->key.lvl1);
131+
TFHEpp::TRGSWFFT<TFHEpp::lvl1param> cs =
132+
TFHEpp::trgswfftSymEncrypt<TFHEpp::lvl1param>(
133+
plainpoly, TFHEpp::lvl1param::α, sk->key.lvl1);
96134
TFHEpp::TRLWE<TFHEpp::lvl1param> res;
97-
for (auto _ : state) TFHEpp::trgswfftExternalProduct<TFHEpp::lvl1param>(res, c0, cs);
135+
for (auto _ : state)
136+
TFHEpp::trgswfftExternalProduct<TFHEpp::lvl1param>(res, c0, cs);
98137
}
99138

100-
void BM_CB(benchmark::State& state){
139+
void BM_CB(benchmark::State& state)
140+
{
101141
std::random_device seed_gen;
102142
std::default_random_engine engine(seed_gen());
103143
std::uniform_int_distribution<uint32_t> binary(0, 1);
104-
const std::unique_ptr<TFHEpp::SecretKey> sk (new TFHEpp::SecretKey());
105-
const std::unique_ptr<TFHEpp::CircuitKey<TFHEpp::lvl02param,TFHEpp::lvl22param>> ck(new TFHEpp::CircuitKey<TFHEpp::lvl02param,TFHEpp::lvl22param>(*sk));
106-
TFHEpp::TLWE<TFHEpp::lvl0param> ca = TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(binary(engine),TFHEpp::lvl0param::α,sk->key.lvl0);
144+
const std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
145+
const std::unique_ptr<
146+
TFHEpp::CircuitKey<TFHEpp::lvl02param, TFHEpp::lvl22param>>
147+
ck(new TFHEpp::CircuitKey<TFHEpp::lvl02param, TFHEpp::lvl22param>(*sk));
148+
TFHEpp::TLWE<TFHEpp::lvl0param> ca =
149+
TFHEpp::tlweSymEncrypt<TFHEpp::lvl0param>(
150+
binary(engine), TFHEpp::lvl0param::α, sk->key.lvl0);
107151
TFHEpp::TRGSWFFT<TFHEpp::lvl2param> res;
108-
for (auto _ : state) TFHEpp::CircuitBootstrappingFFT(res, ca, *ck);
152+
for (auto _ : state) TFHEpp::CircuitBootstrappingFFT(res, ca, *ck);
109153
}
110154

111-
BENCHMARK(BM_HomGate)->Iterations(1)->Repetitions(100)->DisplayAggregatesOnly(true);
112-
BENCHMARK(BM_HomMUX)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
113-
BENCHMARK(BM_TLWE2TRLWE)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
155+
BENCHMARK(BM_HomGate)
156+
->Iterations(1)
157+
->Repetitions(100)
158+
->DisplayAggregatesOnly(true);
159+
BENCHMARK(BM_HomMUX)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(
160+
true);
161+
BENCHMARK(BM_TLWE2TRLWE)
162+
->Iterations(1)
163+
->Repetitions(10)
164+
->DisplayAggregatesOnly(true);
114165
BENCHMARK(BM_IKS)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
115166
BENCHMARK(BM_SEI)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
116167
BENCHMARK(BM_CMUX)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
117-
BENCHMARK(BM_ExternalProduct)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
168+
BENCHMARK(BM_ExternalProduct)
169+
->Iterations(1)
170+
->Repetitions(10)
171+
->DisplayAggregatesOnly(true);
118172
// BENCHMARK(BM_CB)->Iterations(1)->Repetitions(10)->DisplayAggregatesOnly(true);
119173
BENCHMARK_MAIN();

format.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
clang-format -style=file -i ./src/*.cpp
44
clang-format -style=file -i ./include/*.hpp
5+
clang-format -style=file -i ./include/params/*.hpp
56
clang-format -style=file -i ./test/*.cpp
7+
clang-format -style=file -i ./benchmark/*.cpp
68
clang-format -style=file -i ./tutorial/*.cpp

include/params/128bit.hpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,89 @@
11
#pragma once
22

3-
#include <cstdint>
43
#include <cmath>
4+
#include <cstdint>
55

66
struct lvl0param {
7-
static constexpr std::uint32_t n = 635; //dimension
8-
static const inline double α = std::pow(2.0, -15); //fresh noise
9-
using T = uint32_t; //Torus representation
7+
static constexpr std::uint32_t n = 635; // dimension
8+
static const inline double α = std::pow(2.0, -15); // fresh noise
9+
using T = uint32_t; // Torus representation
1010
static constexpr T μ = 1U << 29;
1111
};
1212

1313
struct lvl1param {
14-
static constexpr std::uint32_t nbit = 10; //dimension must be a power of 2 for ease of polynomial multiplication.
15-
static constexpr std::uint32_t n = 1<<nbit; //dimension
14+
static constexpr std::uint32_t nbit =
15+
10; // dimension must be a power of 2 for ease of polynomial
16+
// multiplication.
17+
static constexpr std::uint32_t n = 1 << nbit; // dimension
1618
static constexpr std::uint32_t l = 3;
1719
static constexpr std::uint32_t Bgbit = 6;
18-
static constexpr std::uint32_t Bg = 1<<Bgbit;
19-
static const inline double α = std::pow(2.0, -25); //fresh noise
20-
using T = uint32_t; //Torus representation
20+
static constexpr std::uint32_t Bg = 1 << Bgbit;
21+
static const inline double α = std::pow(2.0, -25); // fresh noise
22+
using T = uint32_t; // Torus representation
2123
static constexpr T μ = 1U << 29;
2224
static constexpr uint32_t plain_modulus = 2;
23-
static constexpr double Δ = static_cast<double>(1ULL << std::numeric_limits<T>::digits) / plain_modulus;
25+
static constexpr double Δ =
26+
static_cast<double>(1ULL << std::numeric_limits<T>::digits) /
27+
plain_modulus;
2428
};
2529

2630
struct lvl2param {
27-
static const std::uint32_t nbit = 11; //dimension must be a power of 2 for ease of polynomial multiplication.
28-
static constexpr std::uint32_t n = 1<<nbit; //dimension
31+
static const std::uint32_t nbit = 11; // dimension must be a power of 2 for
32+
// ease of polynomial multiplication.
33+
static constexpr std::uint32_t n = 1 << nbit; // dimension
2934
static constexpr std::uint32_t l = 4;
3035
static constexpr std::uint32_t Bgbit = 9;
31-
static constexpr std::uint32_t Bg = 1<<Bgbit;
32-
static const inline double α = std::pow(2.0, -44); //fresh noise
33-
using T = uint64_t; //Torus representation
36+
static constexpr std::uint32_t Bg = 1 << Bgbit;
37+
static const inline double α = std::pow(2.0, -44); // fresh noise
38+
using T = uint64_t; // Torus representation
3439
static constexpr T μ = 1ULL << 61;
3540
static constexpr uint32_t plain_modulus = 8;
3641
static constexpr double Δ = μ;
3742
};
3843

39-
//Key Switching parameters
44+
// Key Switching parameters
4045
struct lvl10param {
41-
static constexpr std::uint32_t t = 8; //number of addition in keyswitching
42-
static constexpr std::uint32_t basebit = 2; //how many bit should be encrypted in keyswitching key
43-
static const inline double α = lvl0param::α; //key noise
46+
static constexpr std::uint32_t t = 8; // number of addition in keyswitching
47+
static constexpr std::uint32_t basebit =
48+
2; // how many bit should be encrypted in keyswitching key
49+
static const inline double α = lvl0param::α; // key noise
4450
using domainP = lvl1param;
4551
using targetP = lvl0param;
4652
};
4753

4854
struct lvl11param {
49-
static constexpr std::uint32_t t = 8; //number of addition in keyswitching
50-
static constexpr std::uint32_t basebit = 2; //how many bit should be encrypted in keyswitching key
51-
static const inline double α = lvl1param::α; //key noise
55+
static constexpr std::uint32_t t = 8; // number of addition in keyswitching
56+
static constexpr std::uint32_t basebit =
57+
2; // how many bit should be encrypted in keyswitching key
58+
static const inline double α = lvl1param::α; // key noise
5259
using domainP = lvl1param;
5360
using targetP = lvl1param;
5461
};
5562

56-
struct lvl20param{
57-
static constexpr std::uint32_t t = 7; //number of addition in keyswitching
58-
static constexpr std::uint32_t basebit = 2; //how many bit should be encrypted in keyswitching key
59-
static const inline double α = lvl0param::α; //key noise
63+
struct lvl20param {
64+
static constexpr std::uint32_t t = 7; // number of addition in keyswitching
65+
static constexpr std::uint32_t basebit =
66+
2; // how many bit should be encrypted in keyswitching key
67+
static const inline double α = lvl0param::α; // key noise
6068
using domainP = lvl2param;
6169
using targetP = lvl0param;
6270
};
6371

64-
struct lvl21param{
65-
static constexpr std::uint32_t t = 10; //number of addition in keyswitching
66-
static constexpr std::uint32_t basebit = 3; //how many bit should be encrypted in keyswitching key
67-
static const inline double α = lvl1param::α; //key noise
72+
struct lvl21param {
73+
static constexpr std::uint32_t t = 10; // number of addition in
74+
// keyswitching
75+
static constexpr std::uint32_t basebit =
76+
3; // how many bit should be encrypted in keyswitching key
77+
static const inline double α = lvl1param::α; // key noise
6878
using domainP = lvl2param;
6979
using targetP = lvl1param;
7080
};
7181

72-
struct lvl22param{
73-
static constexpr std::uint32_t t = 8; //number of addition in keyswitching
74-
static constexpr std::uint32_t basebit = 4; //how many bit should be encrypted in keyswitching key
75-
static const inline double α = lvl2param::α; //key noise
82+
struct lvl22param {
83+
static constexpr std::uint32_t t = 8; // number of addition in keyswitching
84+
static constexpr std::uint32_t basebit =
85+
4; // how many bit should be encrypted in keyswitching key
86+
static const inline double α = lvl2param::α; // key noise
7687
using domainP = lvl2param;
7788
using targetP = lvl2param;
7889
};

0 commit comments

Comments
 (0)