-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_test_muladd_depth_4096_3_AB_plus_accC.cpp
252 lines (193 loc) · 7.3 KB
/
9_test_muladd_depth_4096_3_AB_plus_accC.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* test_muladd_depth_4096_3_AB_plus_accC.cpp
*
* Created on: 27 Dec 2023
* Author: massimiliano
*/
#include <iostream>
#include "seal/seal.h"
#include "utils.h"
using namespace std;
using namespace seal;
/*
* calculate (A * B) + C + ... + C to test multiply and add depth
*/
void test_muladd_depth_4096_3_AB_plus_accC(double range_limit = 100.0, int run = 10){
cout << "test_muladd_depth_4096_3_AB_plus_accC(" << run << ")" << endl;
cout << "input data range [" << -range_limit << ", " << range_limit << "]" << endl;
EncryptionParameters parms(scheme_type::ckks);
/*
* poly_modulus degree 4096
* primes coeff_modulus {35,25,35} max_coeff_modulus 109
* scale 2^25 precision before point 35-25 = 10 bit, precision after point 25-10 = 15 Bit
*/
size_t poly_modulus_degree = 4096;
size_t scale_exp = 25;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {35,25,35}));
double scale = pow(2.0, scale_exp);
/*
* SEAL context
*/
SEALContext context(parms);
print_parameters(context);
cout << "context.using_keyswitching()? " << context.using_keyswitching() << endl;
cout << endl;
print_modulus_switching_chain(context);
/*
* key generation
*/
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
RelinKeys relin_keys;
keygen.create_relin_keys(relin_keys);
GaloisKeys gal_keys;
keygen.create_galois_keys(gal_keys);
cout << "Print the parameter IDs of generated keys." << endl;
cout << " + secret_key: " << secret_key.parms_id() << endl;
cout << " + relin_keys: " << relin_keys.parms_id() << endl << endl;
/*
* encryptor, decryptor, evaluator and encoder
*/
Encryptor encryptor(context, secret_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
CKKSEncoder encoder(context);
size_t slot_count = encoder.slot_count();
cout << "Encoder number of slots: " << slot_count << endl;
cout << "Scale 2^" << scale_exp << endl << endl;
/*
* random input data and expected result
*/
const vector<double> input_A = generate_random_data(10, -range_limit, range_limit);
const vector<double> input_B = generate_random_data(10, -range_limit, range_limit);
const vector<double> input_C = generate_random_data(10, -range_limit, range_limit);
cout << "Input A vector size " << input_A.size() << endl;
print_vector(input_A);
cout << "Input B vector size " << input_B.size() << endl;
print_vector(input_B);
cout << "Input C vector size " << input_C.size() << endl;
print_vector(input_C);
vector<double> expected_AB;
vector<double> expected_accC;
for(int i=0;i<input_A.size();i++){
expected_AB.push_back(input_A[i]*input_B[i]);
expected_accC.push_back(input_C[i]+ input_C[i]);
}
cout << "--------------------------" << endl << endl;
/*
* encode input vector
*/
Plaintext plain_A, plain_B, plain_C;
encoder.encode(input_A, scale, plain_A);
encoder.encode(input_B, scale, plain_B);
encoder.encode(input_C, scale, plain_C);
cout << "Input A plaintext" << endl;
print_plaintext_info(plain_A,context);
cout << "Input B plaintext" << endl;
print_plaintext_info(plain_B,context);
cout << "Input C plaintext" << endl;
print_plaintext_info(plain_C,context);
cout << "--------------------------" << endl << endl;
/*
* encrypt plaintext
*/
Ciphertext encrypted_A, encrypted_B, encrypted_C;
encryptor.encrypt_symmetric(plain_A, encrypted_A);
encryptor.encrypt_symmetric(plain_B, encrypted_B);
encryptor.encrypt_symmetric(plain_C, encrypted_C);
cout << "Input A ciphertext" << endl;
print_ciphertext_info(encrypted_A,context);
cout << "Input B ciphertext" << endl;
print_ciphertext_info(encrypted_B,context);
cout << "Input C ciphertext" << endl;
print_ciphertext_info(encrypted_C,context);
cout << "--------------------------" << endl << endl;
/*
* perform multiplication A*B
*/
Ciphertext encrypted_AB;
evaluator.multiply(encrypted_A, encrypted_B, encrypted_AB);
cout << "Result AB" << endl;
print_ciphertext_info(encrypted_AB,context);
check_chiphertext(&decryptor, &encoder, encrypted_AB, expected_AB);
cout << "--------------------------" << endl << endl;
/*
* relinearize A*B to return to size = 2
*/
evaluator.relinearize_inplace(encrypted_AB,relin_keys);
cout << "Result AB relin" << endl;
print_ciphertext_info(encrypted_AB,context);
check_chiphertext(&decryptor, &encoder, encrypted_AB, expected_AB);
/*
* rescale A*B to next prime in the chain
*/
evaluator.rescale_to_next_inplace(encrypted_AB);
cout << "Result AB rescale" << endl;
print_ciphertext_info(encrypted_AB,context);
check_chiphertext(&decryptor, &encoder, encrypted_AB, expected_AB);
/*
* perform accC = C + C
*/
Ciphertext encrypted_accC;
evaluator.add(encrypted_C, encrypted_C, encrypted_accC);
cout << "Result Acc 2C" << endl;
print_ciphertext_info(encrypted_accC,context);
check_chiphertext(&decryptor, &encoder, encrypted_accC, expected_accC);
/*
* perform run-2 times accC += C
*/
for(int i=0; i<run-2;i++){
/*
* check scale
*/
if(!check_operand_scale(encrypted_accC, encrypted_C)){
/*
* fix the scale of accC by forcing the expected value
*/
encrypted_accC.scale() = pow(2.0,scale_exp);
cout << "Result accC fix scale" << endl;
print_ciphertext_info(encrypted_accC,context);
check_chiphertext(&decryptor, &encoder, encrypted_accC, expected_accC);
}
evaluator.add_inplace(encrypted_accC, encrypted_C);
for(int j=0;j<input_A.size();j++){
expected_accC[j] += input_C[j];
}
cout << "Result Acc " << i+3 << "C" << endl;
print_ciphertext_info(encrypted_accC,context);
check_chiphertext(&decryptor, &encoder, encrypted_accC, expected_accC);
}
cout << "--------------------------" << endl << endl;
/*
* switch accC to the same prime as A*B
*/
evaluator.mod_switch_to_inplace(encrypted_accC, encrypted_AB.parms_id());
cout << "accC mod switch" << endl;
print_ciphertext_info(encrypted_C,context);
cout << "--------------------------" << endl << endl;
/*
* check scale
*/
if(!check_operand_scale(encrypted_AB, encrypted_accC)){
/*
* fix the scale of A*B by forcing the expected value
*/
encrypted_AB.scale() = pow(2.0,scale_exp);
cout << "Result AB fix scale" << endl;
print_ciphertext_info(encrypted_AB,context);
check_chiphertext(&decryptor, &encoder, encrypted_AB, expected_AB);
}
/*
* perform addition (A*B) + accC
*/
Ciphertext encrypted_AB_plus_accC;
evaluator.add(encrypted_AB, encrypted_accC, encrypted_AB_plus_accC);
cout << "Result AB + accC" << endl;
print_ciphertext_info(encrypted_AB_plus_accC,context);
vector<double> expected_AB_plus_accC;
for(int i=0;i<expected_AB.size();i++){
expected_AB_plus_accC.push_back(expected_AB[i]+expected_accC[i]);
}
check_chiphertext(&decryptor, &encoder, encrypted_AB_plus_accC, expected_AB_plus_accC);
}