Skip to content

Commit 3b1340c

Browse files
authored
Merge pull request #45 from wfus/decode
Decode
2 parents a0762ac + c189c0e commit 3b1340c

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

benchmark/decode.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from subprocess import call
2+
3+
CWD = "../"
4+
DEGREE = [8, 12, 16, 32, 48, 63]
5+
DELTA = [0.1, 0.2, 0.3, 0.4, 0.5]
6+
call(['make'], cwd=CWD)
7+
call(['./bin/decode_client', '-s'], cwd=CWD)
8+
for degree in DEGREE:
9+
for delta in DELTA:
10+
print(degree, delta)
11+
call(['./bin/decode_server', '--degree', str(degree), '--delta', str(delta)], cwd=CWD)
12+
call(['./bin/decode_client', '-r', '-o', 'decode_out/out_{}_{}.png'.format(degree, delta)], cwd=CWD)

homo/client_decode.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,12 @@ int main(int argc, const char** argv) {
222222
int pixel = encoder.decode(p);
223223
CLAMP(pixel, 0, 255)
224224
decrypted_image.push_back((uint8_t) pixel);
225-
std::cout << (int) pixel << std::endl;
226225
}
227226
instream.close();
228-
std::cout << std::endl;
229227

230228
// Calculate RMS Error
231229
// compare_resize_opencv(test_filename.c_str(), resized_width, resized_height, bicubic, decrypted_image);
232230

233-
std::cout << width << '\t' << height << '\t' << decrypted_image.size() << std::endl;
234231
#ifdef linux
235232
save_image_rgb(width, height, decrypted_image, test_output);
236233
#else

homo/client_resize.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ int main(int argc, const char** argv) {
228228
diff = std::chrono::steady_clock::now() - start;
229229
std::cout << chrono::duration<double, milli>(diff).count() << ',';
230230
// std::cout << i << '\t' << encoder.decode(p) << std::endl;
231-
uint8_t pixel = (uint8_t) encoder.decode(p);
231+
int pixel = encoder.decode(p);
232232
CLAMP(pixel, 0, 255)
233-
decrypted_image.push_back(pixel);
233+
decrypted_image.push_back((uint8_t) pixel);
234234
}
235235
instream.close();
236236
std::cout << std::endl;

homo/fhe_decode.h

+14-17
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,27 @@ Ciphertext homomorphic_cos(Ciphertext &x,
202202
void approximated_step(Ciphertext &amplitude,
203203
Ciphertext &index,
204204
Ciphertext &count,
205+
int order,
205206
int degree,
206207
double delta,
208+
int width,
209+
int height,
207210
std::vector<Ciphertext> &run,
208211
Evaluator &evaluator,
209212
FractionalEncoder &encoder,
210-
Encryptor &encryptor,
211-
Decryptor &decryptor) {
212-
Plaintext p1, p2, p3, p4, p5;
213+
Encryptor &encryptor) {
213214
Ciphertext b(count);
214215
evaluator.multiply_plain(b, encoder.encode(0.5));
215216
Ciphertext offset(index);
216217
evaluator.add(offset, b);
217218
evaluator.add_plain(offset, encoder.encode(-0.5));
218219
evaluator.negate(offset);
219220
evaluator.add_plain(b, encoder.encode(delta - 0.5));
220-
decryptor.decrypt(amplitude, p1);
221-
decryptor.decrypt(index, p2);
222-
decryptor.decrypt(count, p3);
223-
decryptor.decrypt(offset, p4);
224-
decryptor.decrypt(b, p5);
225-
std::cout << encoder.decode(p1) << '\t' << encoder.decode(p2) << '\t' << encoder.decode(p3) << '\t' << encoder.decode(p4) << '\t' << encoder.decode(p5) << std::endl;
226-
for (int i = 0; i < 16; i++) {
221+
for (int i = 0; i < width * height; i++) {
227222
Ciphertext c(b);
228-
evaluator.multiply_plain(c, encoder.encode(1.0 / 64.0));
223+
evaluator.multiply_plain(c, encoder.encode(1.0 / ((double) order)));
229224
for (int j = 1; j <= degree; j++) {
230-
double arg_factor = ((float) j) * M_PI / 64.0;
225+
double arg_factor = ((float) j) * M_PI / ((double) order);
231226
Ciphertext sin_arg(b);
232227
evaluator.multiply_plain(sin_arg, encoder.encode(arg_factor));
233228
Ciphertext cos_arg(offset);
@@ -246,11 +241,14 @@ void approximated_step(Ciphertext &amplitude,
246241
}
247242
}
248243

249-
void debug_approximated_step(Ciphertext &amplitude,
244+
void approximated_step(Ciphertext &amplitude,
250245
Ciphertext &index,
251246
Ciphertext &count,
247+
int order,
252248
int degree,
253249
double delta,
250+
int width,
251+
int height,
254252
std::vector<Ciphertext> &run,
255253
Evaluator &evaluator,
256254
FractionalEncoder &encoder,
@@ -266,11 +264,10 @@ void debug_approximated_step(Ciphertext &amplitude,
266264
double b = cnt / 2.0;
267265
double offset = -(ind + b - 0.5);
268266
b += delta - 0.5;
269-
std::cout << amp << '\t' << ind << '\t' << cnt << '\t' << b << '\t' << offset << std::endl;
270-
for (int i = 0; i < 16; i++) {
271-
double res = b / 64.0;
267+
for (int i = 0; i < width * height; i++) {
268+
double res = b / ((double) order);
272269
for (int j = 1; j <= degree; j++) {
273-
double arg_factor = ((float) j) * M_PI / 64.0;
270+
double arg_factor = ((float) j) * M_PI / ((double) order);
274271
double sin_factor = sin(arg_factor * b);
275272
double cos_factor = cos(arg_factor * (i + offset));
276273
double term = 2.0 / (M_PI * ((float) j)) * cos_factor * sin_factor;

homo/server_decode.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ int main(int argc, const char** argv) {
3434
int coeff_modulus = COEFF_MODULUS;
3535
int resized_width = 0;
3636
int resized_height = 0;
37+
int degree = 12;
38+
double delta = 0.5;
39+
int order = 64;
3740

3841

3942
try {
@@ -44,6 +47,9 @@ int main(int argc, const char** argv) {
4447
("f,file", "Filename for input file to be resized", cxxopts::value<std::string>())
4548
("v,verbose", "Verbose logging output", cxxopts::value<bool>(verbose))
4649
("o,outfile", "Filename for homomorphic ciphertext to be saved to", cxxopts::value<std::string>())
50+
("delta", "Delta for step function size", cxxopts::value<double>())
51+
("order", "Order of approximation for step function approximation Fourier series", cxxopts::value<int>())
52+
("degree", "Terms for step function approximation", cxxopts::value<int>())
4753
("ncoeff", "Number of coefficients for integer portion of encoding", cxxopts::value<int>())
4854
("fcoeff", "Number of coefficients for fractional portion of encoding", cxxopts::value<int>())
4955
("cmod", "Coefficient Modulus for polynomial encoding", cxxopts::value<int>())
@@ -64,6 +70,9 @@ int main(int argc, const char** argv) {
6470
if (result.count("fcoeff")) n_fractional_coeffs = result["fcoeff"].as<int>();
6571
if (result.count("pmod")) plain_modulus = result["pmod"].as<int>();
6672
if (result.count("cmod")) coeff_modulus = result["cmod"].as<int>();
73+
if (result.count("degree")) degree = result["degree"].as<int>();
74+
if (result.count("order")) order = result["order"].as<int>();
75+
if (result.count("delta")) delta = result["delta"].as<double>();
6776
if (result.count("n_poly_base")) n_poly_base = result["base"].as<int>();
6877
}
6978
catch (const cxxopts::OptionException& e) {
@@ -116,35 +125,26 @@ int main(int argc, const char** argv) {
116125
Ciphertext c, index, elem, count;
117126
std::vector<std::vector<Ciphertext>> res;
118127
for (int i = 0; i < 3; i++) {
119-
std::cout << "Color " << i << std::endl;
120128
encryptor.encrypt(encoder.encode(0), index);
121129
std::vector<Ciphertext> channel;
122130
for (int j = 0; j < width * height; j++) {
123131
encryptor.encrypt(encoder.encode(0), c);
124132
channel.push_back(c);
125133
}
126134
for (int j = 0; j < pairs[i]; j++) {
127-
std::cout << "Run " << j << std::endl;
128135
std::vector<Ciphertext> run;
129136
elem.load(myfile);
130137
count.load(myfile);
131-
debug_approximated_step(elem, index, count, 63, 0.5, run, evaluator, encoder, encryptor, decryptor);
138+
approximated_step(elem, index, count, order, degree, delta, width, height, run, evaluator, encoder, encryptor, decryptor);
132139
for (int k = 0; k < width * height; k++) {
133140
// Plaintext p;
134141
// decryptor.decrypt(run[k], p);
135142
// std::cout << encoder.decode(p) << '\t';
136143
evaluator.add(channel[k], run[k]);
137144
}
138-
std::cout << std::endl;
139145
evaluator.add(index, count);
140146
}
141147
res.push_back(channel);
142-
for (int j = 0; j < width * height; j++) {
143-
Plaintext p;
144-
decryptor.decrypt(res[i][j], p);
145-
std::cout << encoder.decode(p) << '\t';
146-
}
147-
std::cout << std::endl;
148148
}
149149
for (int i = 0; i < width * height; i++) {
150150
for (int j = 0; j < 3; j++) {

0 commit comments

Comments
 (0)