-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathannihilate.cpp
More file actions
63 lines (52 loc) · 2.18 KB
/
annihilate.cpp
File metadata and controls
63 lines (52 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cassert>
#include <chrono>
#include <iostream>
#include <memory>
#include <random>
#include <tfhe++.hpp>
int main()
{
constexpr uint32_t num_test = 1000;
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
std::uniform_int_distribution<uint32_t> binary(0, 1);
using P = TFHEpp::lvl2param;
std::unique_ptr<TFHEpp::SecretKey> sk(new TFHEpp::SecretKey());
std::vector<uint8_t> pa(num_test);
for (int i = 0; i < num_test; i++) pa[i] = binary(engine) > 0;
std::vector<TFHEpp::TLWE<P>> ca(num_test);
std::vector<std::array<uint8_t, P::n>> pin(num_test);
std::vector<std::array<typename P::T, P::n>> pmu(num_test);
for (std::array<uint8_t, P::n> &i : pin)
for (uint8_t &p : i) p = binary(engine);
for (int i = 0; i < num_test; i++)
for (int j = 0; j < P::n; j++)
pmu[i][j] = (pin[i][j] > 0) ? (P::μ) : -(P::μ);
std::vector<TFHEpp::TRLWE<P>> cin(num_test);
for (int i = 0; i < num_test; i++)
TFHEpp::trlweSymEncrypt<P>(cin[i], pmu[i], sk->key.get<P>());
std::vector<TFHEpp::TRLWE<P>> cres(num_test);
std::unique_ptr<TFHEpp::AnnihilateKey<P>> ahk(
new TFHEpp::AnnihilateKey<P>());
TFHEpp::annihilatekeygen<P>(*ahk, *sk);
std::chrono::system_clock::time_point start, end;
start = std::chrono::system_clock::now();
for (int test = 0; test < num_test; test++) {
TFHEpp::AnnihilateKeySwitching<P>(cres[test], cin[test], *ahk);
}
end = std::chrono::system_clock::now();
std::vector<std::array<bool, P::n>> pres(num_test);
for (int i = 0; i < num_test; i++)
pres[i] = TFHEpp::trlweSymDecrypt<P>(cres[i], sk->key.get<P>());
for (int i = 0; i < num_test; i++) assert(pres[i][0] == (pin[i][0] > 0));
// TFHEpp::Polynomial<P> phase =
// TFHEpp::trlwePhase<P>(cres[0], sk->key.get<P>());
// for (int i = 0; i < P::n; i++)
// std::cout << static_cast<int64_t>(phase[i]) << ":";
// std::cout << std::endl;
std::cout << "Passed" << std::endl;
double elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
std::cout << elapsed / num_test << "ms" << std::endl;
}