-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
195 lines (181 loc) · 9.28 KB
/
common.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include <cstddef>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <mutex>
#include <memory>
#include <limits>
#include <algorithm>
#include <numeric>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include "seal/seal.h"
using namespace std;
using namespace seal;
struct thread_para{
int fd;
shared_ptr<SEALContext> context;
};
#define nation_flag "\
+---------------------------------------------------------------------+\n\
| _______ ________________.___________ _______ _________ |\n\
| \\ \\ / _ \\__ ___/| \\_____ \\ \\ \\ / _____/ |\n\
| / | \\ / /_\\ \\| | | |/ | \\ / | \\ \\_____ \\ |\n\
| / | \\/ | \\ | | / | \\/ | \\/ \\ |\n\
| \\____|__ /\\____|__ /____| |___\\_______ /\\____|__ /_______ / |\n\
| \\/ \\/ \\/ \\/ \\/ |\n\
+---------------------------------------------------------------------+\n"
inline void add_plain_helper(int op, shared_ptr<SEALContext> context);
inline void mul_helper(int op, shared_ptr<SEALContext> context);
inline void square_helper(int op, shared_ptr<SEALContext> context);
void calc_bfv_basic();
int muti_core_runner();
/*
Helper function: Prints the name of the example in a fancy banner.
*/
inline void print_example_banner(std::string title){
if (!title.empty())
{
std::size_t title_length = title.length();
std::size_t banner_length = title_length + 2 * 10;
std::string banner_top = "+" + std::string(banner_length - 2, '-') + "+";
std::string banner_middle =
"|" + std::string(9, ' ') + title + std::string(9, ' ') + "|";
std::cout << std::endl
<< banner_top << std::endl
<< banner_middle << std::endl
<< banner_top << std::endl;
}
}
/*
Helper function: Prints the parameters in a SEALContext.
*/
inline void print_parameters(std::shared_ptr<seal::SEALContext> context){
// Verify parameters
if (!context){
throw std::invalid_argument("context is not set");
}
auto &context_data = *context->key_context_data();
/*
Which scheme are we using?
*/
std::string scheme_name;
switch (context_data.parms().scheme())
{
case seal::scheme_type::BFV:
scheme_name = "BFV";
break;
case seal::scheme_type::CKKS:
scheme_name = "CKKS";
break;
default:
throw std::invalid_argument("unsupported scheme");
}
std::cout << "/" << std::endl;
std::cout << "| Encryption parameters :" << std::endl;
std::cout << "| scheme: " << scheme_name << std::endl;
std::cout << "| poly_modulus_degree: " <<
context_data.parms().poly_modulus_degree() << std::endl;
/*
Print the size of the true (product) coefficient modulus.
*/
std::cout << "| coeff_modulus size: ";
std::cout << context_data.total_coeff_modulus_bit_count() << " (";
auto coeff_modulus = context_data.parms().coeff_modulus();
std::size_t coeff_mod_count = coeff_modulus.size();
for (std::size_t i = 0; i < coeff_mod_count - 1; i++){
std::cout << coeff_modulus[i].bit_count() << " + ";
}
std::cout << coeff_modulus.back().bit_count();
std::cout << ") bits" << std::endl;
/*
For the BFV scheme print the plain_modulus parameter.
*/
if (context_data.parms().scheme() == seal::scheme_type::BFV)
{
std::cout << "| plain_modulus: " << context_data.
parms().plain_modulus().value() << std::endl;
}
std::cout << "\\" << std::endl;
}
inline void print_result(int op, std::shared_ptr<seal::SEALContext> context, int num1, int num2, size_t size_en, size_t noise_en, string polynomial, size_t res, size_t en_time, size_t reli_time, size_t de_time, size_t op_time){
auto &context_data = *context->key_context_data();
std::string scheme_name;
std::string op_name;
switch(op){
case 1: op_name = "Add"; break;
case 2: op_name = "Mul"; break;
case 3: op_name = "Square"; break;
default: printf("Unsupported Compute Mode!!!\n"); return;
}
switch (context_data.parms().scheme())
{
case seal::scheme_type::BFV:
scheme_name = "BFV";
break;
case seal::scheme_type::CKKS:
scheme_name = "CKKS";
break;
default:
throw std::invalid_argument("unsupported scheme");
}
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| TEST RESULT |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Task Type | %-58s |\n", op_name.c_str());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Number 1 | %-58d |\n", num1);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
if (num2 != -1){
fprintf(stdout, "| Number 2 | %-58d |\n", num2);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
}
fprintf(stdout, "| |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Encryption Parameters |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Scheme | %-56s |\n", scheme_name.c_str());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Poly_modulus_degree | %-56d |\n", context_data.parms().poly_modulus_degree());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Coeff_modulus size | %-56d |\n", context_data.total_coeff_modulus_bit_count());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Plain_modulus | %-56d |\n", context_data.parms().plain_modulus().value());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Speed(microsecond) |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Encryption Time | %-56lu |\n", en_time);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
if (reli_time != -1){
fprintf(stdout, "| Relinearize Time | %-56lu |\n", reli_time);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
}
fprintf(stdout, "| Decryption Time | %-56lu |\n", de_time);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Operation Time | %-56lu |\n", op_time);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Result |\n");
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Polynomial | %-56s |\n", polynomial.c_str());
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Size_encrypt | %-56lu |\n", size_en);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Noise_budget | %-56lu |\n", noise_en);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "| Output | %-56lu |\n", res);
fprintf(stdout, "+----------------------------------------------------------------------------------------+\n");
fprintf(stdout, "\n");
}