-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_test_rotate_depth_8192_4_A.cpp
151 lines (114 loc) · 4.2 KB
/
20_test_rotate_depth_8192_4_A.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
/*
* test_rotate_depth_8192_4_A.cpp
*
* Created on: 28 Dec 2023
* Author: massimiliano
*/
#include <iostream>
#include "seal/seal.h"
#include "utils.h"
using namespace std;
using namespace seal;
/*
* calculate rotate(A,1) run times to test rotate depth
*/
void test_rotate_depth_8192_4_A(double range_limit = 100.0, int run = 10){
cout << "test_rotate_depth_8192_4_A(" << run << ")" << endl;
cout << "input data range [" << -range_limit << ", " << range_limit << "]" << endl;
EncryptionParameters parms(scheme_type::ckks);
/*
* poly_modulus degree 8192
* primes coeff_modulus {60,40,40,60} max_coeff_modulus 218
* scale 2^40 precision before point 60-40 = 20 bit, precision after point 40-20 = 20 Bit
*/
size_t poly_modulus_degree = 8192;
size_t scale_exp = 40;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {60,40,40,60}));
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);
cout << "Input A vector size " << input_A.size() << endl;
print_vector(input_A);
vector<double> expected_rotateA = input_A;
rotate(expected_rotateA.begin(), expected_rotateA.begin()+1, expected_rotateA.end());
cout << "--------------------------" << endl << endl;
/*
* encode input vector
*/
Plaintext plain_A;
encoder.encode(input_A, scale, plain_A);
cout << "Input A plaintext" << endl;
print_plaintext_info(plain_A,context);
cout << "--------------------------" << endl << endl;
/*
* encrypt plaintext
*/
Ciphertext encrypted_A;
encryptor.encrypt_symmetric(plain_A, encrypted_A);
cout << "Input A ciphertext" << endl;
print_ciphertext_info(encrypted_A,context);
cout << "--------------------------" << endl << endl;
/*
* perform A = rotate(A,1)
*/
Ciphertext encrypted_rotateA;
evaluator.rotate_vector(encrypted_A, 1, gal_keys, encrypted_rotateA);
cout << "Result Rotate 1A" << endl;
print_ciphertext_info(encrypted_rotateA,context);
check_chiphertext(&decryptor, &encoder, encrypted_rotateA, expected_rotateA);
/*
* perform run-1 times A = rotate(A,1)
*/
for(int i=0; i<run-1;i++){
/*
* check scale
*/
if(!check_operand_scale(encrypted_rotateA, pow(2.0, scale_exp))){
/*
* fix the scale of negA by forcing the expected value
*/
encrypted_rotateA.scale() = pow(2.0,scale_exp);
cout << "Result rotate A fix scale" << endl;
print_ciphertext_info(encrypted_rotateA,context);
check_chiphertext(&decryptor, &encoder, encrypted_rotateA, expected_rotateA);
}
evaluator.rotate_vector_inplace(encrypted_rotateA, 1, gal_keys);
rotate(expected_rotateA.begin(), expected_rotateA.begin()+1, expected_rotateA.end());
cout << "Result Rotate " << i+2 << "A" << endl;
print_ciphertext_info(encrypted_rotateA,context);
check_chiphertext(&decryptor, &encoder, encrypted_rotateA, expected_rotateA);
}
}