Skip to content

Commit 4d77e84

Browse files
authored
Merge pull request #4 from wfus/homo
Homo
2 parents 9ebbc61 + ebd9336 commit 4d77e84

File tree

6 files changed

+4791
-19
lines changed

6 files changed

+4791
-19
lines changed

homo/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LDFLAGS=-g
77

88
.PHONY : all clean
99

10-
all : raymond dct encode test
10+
all : raymond dct encode test sender
1111

1212

1313
raymond : raymond.cpp
@@ -25,5 +25,10 @@ test: test.cpp
2525
@-mkdir -p $(dir $@)
2626
$(CXX) $(CXXFLAGS) $(LDFLAGS) test.cpp $(addprefix -I,$(INCLUDE_DIR)) $(addprefix -L,$(LIB_DIR)) -lseal -o $(BIN_DIR)/test
2727

28+
sender: test.cpp
29+
@-mkdir -p $(dir $@)
30+
$(CXX) $(CXXFLAGS) $(LDFLAGS) sender.cpp $(addprefix -I,$(INCLUDE_DIR)) $(addprefix -L,$(LIB_DIR)) -lseal -o $(BIN_DIR)/sender
31+
32+
2833
clean :
2934
@-rm -f ../bin/*

homo/fhe_image.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
#include "seal/seal.h"
19+
1920
using namespace seal;
2021

2122

@@ -36,6 +37,14 @@ std::vector<std::vector<double>> split_image_eight_block(std::vector<double> im,
3637
return lst;
3738
}
3839

40+
void print_image(uint8_t *im, int w, int h) {
41+
std::cout << "Printing Image dim: (" << w << "," << h << ")" << std::endl;
42+
for (int i = 0; i < w*h; i++) {
43+
printf("%.2x ", im[i]);
44+
if ((i + 1) % w == 0) std::cout << std::endl;
45+
}
46+
}
47+
3948
void print_image(std::vector<double> &im, int w, int h) {
4049
std::cout << "Printing Image dim: (" << w << "," << h << ")" << std::endl;
4150
for (int i = 0; i < im.size(); i++) {
@@ -74,6 +83,24 @@ std::vector<double> read_image(std::string fname) {
7483
return im;
7584
}
7685

86+
std::vector<double> read_image(std::string fname, int* w, int *h) {
87+
88+
std::vector<double> im;
89+
std::ifstream myfile;
90+
91+
myfile.open(fname.c_str());
92+
myfile >> *w;
93+
myfile >> *h;
94+
std::cout << "Read in " << fname << "with dimensions: " << w << " x " << h << std::endl;
95+
96+
float tmp;
97+
for (int i = 0; i < (*w) * (*h); i++) {
98+
myfile >> tmp;
99+
im.push_back(tmp);
100+
}
101+
return im;
102+
}
103+
77104

78105
/* Recieves a 8x8 box of pixels, in ciphertext form.
79106
* Data should be laid out in a 64 element vector with
@@ -280,6 +307,21 @@ void dct_blocks(std::vector<std::vector<double>> &blocks) {
280307
for (int a = 0; a < blocks.size(); a++) {
281308
dct(&blocks[a][0]);
282309
}
310+
}
311+
312+
void print_parameters(const SEALContext &context) {
313+
std::cout << "/ Encryption parameters:" << std::endl;
314+
std::cout << "| poly_modulus: " << context.poly_modulus().to_string() << std::endl;
315+
316+
/*
317+
Print the size of the true (product) coefficient modulus
318+
*/
319+
std::cout << "| coeff_modulus size: "
320+
<< context.total_coeff_modulus().significant_bit_count() << " bits" << std::endl;
321+
322+
std::cout << "| plain_modulus: " << context.plain_modulus().value() << std::endl;
323+
std::cout << "\\ noise_standard_deviation: " << context.noise_standard_deviation() << std::endl;
324+
std::cout << std::endl;
283325
}
284326

285327
#endif

homo/raymond.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ const std::vector<double> S_STD_LUM_QUANT = { 16,11,12,14,12,10,16,14,13,14,18,1
2525
const std::vector<double> S_STD_CROMA_QUANT = { 17,18,18,24,21,24,47,26,26,47,99,66,56,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99 };
2626

2727
void raymond_average();
28-
std::vector<double> read_image(std::string fname);
29-
30-
void print_parameters(const SEALContext &context) {
31-
std::cout << "/ Encryption parameters:" << std::endl;
32-
std::cout << "| poly_modulus: " << context.poly_modulus().to_string() << std::endl;
33-
34-
/*
35-
Print the size of the true (product) coefficient modulus
36-
*/
37-
std::cout << "| coeff_modulus size: "
38-
<< context.total_coeff_modulus().significant_bit_count() << " bits" << std::endl;
39-
40-
std::cout << "| plain_modulus: " << context.plain_modulus().value() << std::endl;
41-
std::cout << "\\ noise_standard_deviation: " << context.noise_standard_deviation() << std::endl;
42-
std::cout << std::endl;
43-
}
4428

4529

4630
int main()
@@ -184,4 +168,4 @@ void raymond_average() {
184168
}
185169

186170
return;
187-
}
171+
}

homo/sender.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "seal/seal.h"
2+
#include "fhe_image.h"
3+
#include "jpge.h"
4+
#include "stb_image.c"
5+
6+
7+
using namespace seal;
8+
9+
auto start = std::chrono::steady_clock::now();
10+
const bool VERBOSE = true;
11+
12+
std::vector<double> read_image(std::string fname);
13+
14+
int main()
15+
{
16+
const char* test_filename = "../image/kung.jpg";
17+
const int requested_composition = 3;
18+
int width = 0, height = 0, actual_composition = 0;
19+
uint8_t *image_data = stbi_load(test_filename, &width, &height, &actual_composition, requested_composition);
20+
std::cout << width << " x " << height << std::endl;
21+
print_image(image_data, width, height);
22+
23+
24+
// Encryption Parameters
25+
EncryptionParameters params;
26+
params.set_poly_modulus("1x^32768 + 1");
27+
params.set_coeff_modulus(coeff_modulus_128(2048));
28+
params.set_plain_modulus(1 << 14);
29+
SEALContext context(params);
30+
print_parameters(context);
31+
32+
33+
// Generate keys
34+
start = std::chrono::steady_clock::now();
35+
KeyGenerator keygen(context);
36+
auto public_key = keygen.public_key();
37+
auto secret_key = keygen.secret_key();
38+
auto diff = std::chrono::steady_clock::now() - start;
39+
std::cout << "KeyGen: ";
40+
std::cout << chrono::duration<double, milli>(diff).count() << " ms" << std::endl;
41+
42+
// Encrytor and decryptor setup
43+
Encryptor encryptor(context, public_key);
44+
Evaluator evaluator(context);
45+
Decryptor decryptor(context, secret_key);
46+
47+
// Base + Number of coefficients used for encoding past the decimal point (both pos and neg)
48+
// Example: if poly_base = 11, and N_FRACTIONAL_COEFFS=3, then we will have
49+
// a1 * 11^-1 + a2 * 11^-2 + a3 * 11^-3
50+
const int POLY_BASE = 11;
51+
const int N_FRACTIONAL_COEFFS = 3;
52+
const int N_NUMBER_COEFFS = 10;
53+
54+
FractionalEncoder encoder(context.plain_modulus(), context.poly_modulus(), N_NUMBER_COEFFS, N_FRACTIONAL_COEFFS, POLY_BASE);
55+
56+
std::ofstream myfile;
57+
myfile.open("../image/nothingpersonnel.txt");
58+
start = std::chrono::steady_clock::now();
59+
for (int i = 0; i < width * height; i++) {
60+
Ciphertext c;
61+
double conv = (double)(image_data[i]);
62+
std::cout<< conv <<std::endl;
63+
encryptor.encrypt(encoder.encode(conv), c);
64+
c.save(myfile);
65+
}
66+
67+
68+
myfile.close();
69+
return 0;
70+
}

0 commit comments

Comments
 (0)