-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCipherSaveLoad.cpp
More file actions
124 lines (111 loc) · 3.59 KB
/
Copy pathCipherSaveLoad.cpp
File metadata and controls
124 lines (111 loc) · 3.59 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
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
#include "CipherSaveLoad.h"
void ciphertextSave2D(vector<vector<Ciphertext>> &x, string path) {
int rowSize = x.size();
int colSize = x[0].size();
// save the size for future loading
string sizeFile = path;
sizeFile.append("size.txt");
ofstream sizeOutput(sizeFile, std::ios::out);
sizeOutput << rowSize << endl;
sizeOutput << colSize << endl;
for (int row = 0; row < x.size(); row++)
for (int col = 0; col < x[0].size(); col++) {
string file = path;
file.append("r");
file.append(to_string(row));
file.append("c");
file.append(to_string(col));
ofstream output(file, std::ios::out | std::ofstream::binary);
x[row][col].save(output);
printf("Data at row%d, col%d saved\n", row, col);
}
}
void ciphertextSave3D(vector<vector<vector<Ciphertext>>> &x, string path) {
//input x[depth,x,y]
path.append("d");
for (int depth = 0; depth < x.size(); depth++) {
string file = path;
file.append(to_string(depth));
printf("Saving depth %d", depth);
ciphertextSave2D(x[depth], file);
}
}
void ciphertextLoad2D(vector<vector<Ciphertext>> &x, string path) {
// Load size from size.txt
string sizeFile = path;
sizeFile.append("size.txt");
ifstream sizeInput(sizeFile, std::ios::in);
int rowColSize[2];
int index = 0;
for (string line; getline(sizeInput, line);) {
rowColSize[index] = stoi(line);
index++;
}
for (int row = 0; row < rowColSize[0]; row++) {
vector<Ciphertext> cipher1D;
for (int col = 0; col < rowColSize[1]; col++) {
string file = path;
file.append("r");
file.append(to_string(row));
file.append("c");
file.append(to_string(col));
ifstream input(file, std::ios::in | std::ifstream::binary);
Ciphertext tmp = Ciphertext::Ciphertext(BigPolyArray());
tmp.load(input);
cipher1D.emplace_back(tmp);
printf("Data at row%d, col%d loaded\n", row, col);
}
x.emplace_back(cipher1D);
}
}
void ciphertextLoad3D(vector<vector<vector<Ciphertext>>> &x, string path, int depthSize) {
path.append("d");
for (int depth = 0; depth < depthSize; depth++) {
vector<vector<Ciphertext>> cipher2D;
string file = path;
file.append(to_string(depth));
printf("Loading depth %d", depth);
ciphertextLoad2D(cipher2D, file);
x.emplace_back(cipher2D);
}
}
void saveKeys(Ciphertext publicKey, Plaintext secretKey, EvaluationKeys evaluationKey, string path) {
string file1, file2, file3;
file1 = path;
file1.append("publickey");
ofstream output1(file1, std::ios::out | std::ofstream::binary);
publicKey.save(output1);
file2 = path;
file2.append("secretkey");
ofstream output2(file2, std::ios::out | std::ofstream::binary);
secretKey.save(output2);
file3 = path;
file3.append("evaluationkeys");
ofstream output3(file3, std::ios::out | std::ofstream::binary);
evaluationKey.save(output3);
}
void loadKeys(Ciphertext &publicKey, Plaintext &secretKey, EvaluationKeys &evaluationKey, string path) {
string file1, file2, file3;
file1 = path;
file1.append("publickey");
ifstream input1(file1, std::ios::in | std::ifstream::binary);
publicKey.load(input1);
file2 = path;
file2.append("secretkey");
ifstream input2(file2, std::ios::in | std::ifstream::binary);
secretKey.load(input2);
file3 = path;
file3.append("evaluationkeys");
ifstream input3(file3, std::ios::in | std::ifstream::binary);
evaluationKey.load(input3);
}
void saveParms(EncryptionParameters parms, string path) {
path.append("parms");
ofstream output(path, std::ios::out | std::ofstream::binary);
parms.save(output);
}
void loadParms(EncryptionParameters &parms, string path) {
path.append("parms");
ifstream input(path, std::ios::in | std::ifstream::binary);
parms.load(input);
}