forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.h
232 lines (205 loc) · 6.04 KB
/
examples.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 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 <random>
#include <thread>
#include <mutex>
#include <memory>
#include <limits>
#include <algorithm>
#include <numeric>
#include "seal/seal.h"
void example_bfv_basics();
void example_encoders();
void example_levels();
void example_ckks_basics();
void example_rotation();
void example_performance_test();
/*
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;
}
/*
Helper function: Prints the `parms_id' to std::ostream.
*/
inline std::ostream &operator <<(std::ostream &stream, seal::parms_id_type parms_id)
{
/*
Save the formatting information for std::cout.
*/
std::ios old_fmt(nullptr);
old_fmt.copyfmt(std::cout);
stream << std::hex << std::setfill('0')
<< std::setw(16) << parms_id[0] << " "
<< std::setw(16) << parms_id[1] << " "
<< std::setw(16) << parms_id[2] << " "
<< std::setw(16) << parms_id[3] << " ";
/*
Restore the old std::cout formatting.
*/
std::cout.copyfmt(old_fmt);
return stream;
}
/*
Helper function: Prints a vector of floating-point values.
*/
template<typename T>
inline void print_vector(std::vector<T> vec, std::size_t print_size = 4, int prec = 3)
{
/*
Save the formatting information for std::cout.
*/
std::ios old_fmt(nullptr);
old_fmt.copyfmt(std::cout);
std::size_t slot_count = vec.size();
std::cout << std::fixed << std::setprecision(prec);
std::cout << std::endl;
if(slot_count <= 2 * print_size)
{
std::cout << " [";
for (std::size_t i = 0; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
else
{
vec.resize(std::max(vec.size(), 2 * print_size));
std::cout << " [";
for (std::size_t i = 0; i < print_size; i++)
{
std::cout << " " << vec[i] << ",";
}
if(vec.size() > 2 * print_size)
{
std::cout << " ...,";
}
for (std::size_t i = slot_count - print_size; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
std::cout << std::endl;
/*
Restore the old std::cout formatting.
*/
std::cout.copyfmt(old_fmt);
}
/*
Helper function: Prints a matrix of values.
*/
template<typename T>
inline void print_matrix(std::vector<T> matrix, std::size_t row_size)
{
/*
We're not going to print every column of the matrix (there are 2048). Instead
print this many slots from beginning and end of the matrix.
*/
std::size_t print_size = 5;
std::cout << std::endl;
std::cout << " [";
for (std::size_t i = 0; i < print_size; i++)
{
std::cout << std::setw(3) << std::right << matrix[i] << ",";
}
std::cout << std::setw(3) << " ...,";
for (std::size_t i = row_size - print_size; i < row_size; i++)
{
std::cout << std::setw(3) << matrix[i]
<< ((i != row_size - 1) ? "," : " ]\n");
}
std::cout << " [";
for (std::size_t i = row_size; i < row_size + print_size; i++)
{
std::cout << std::setw(3) << matrix[i] << ",";
}
std::cout << std::setw(3) << " ...,";
for (std::size_t i = 2 * row_size - print_size; i < 2 * row_size; i++)
{
std::cout << std::setw(3) << matrix[i]
<< ((i != 2 * row_size - 1) ? "," : " ]\n");
}
std::cout << std::endl;
};
/*
Helper function: Print line number.
*/
inline void print_line(int line_number)
{
std::cout << "Line " << std::setw(3) << line_number << " --> ";
}