-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path3_levels.cpp
151 lines (130 loc) · 5.38 KB
/
3_levels.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
#include "seal/seal.h"
#include <iostream>
using namespace std;
using namespace seal;
int main()
{
cout << "\n--------- Levels Demo ---------\n"
<< endl;
EncryptionParameters params(scheme_type::BFV);
size_t poly_modulus_degree = 8192;
params.set_poly_modulus_degree(poly_modulus_degree);
// Use a of coeff_modulus of 5 primes of sizes 50, 30, 30, 50 and bits
params.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {50, 30, 30, 50, 50}));
// 20 bit poly mod degree
params.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));
auto context = SEALContext::Create(params);
cout << "Print the modulus switching chain" << endl;
// Print the key level parameter info
auto context_data = context->key_context_data();
cout << "\tLevel (chain index): " << context_data->chain_index() << endl;
// cout << "\tparms_id: " << context_data->parms_id() << endl;
cout << "\tcoeff_modulus primes: ";
cout << hex;
for (const auto &prime : context_data->parms().coeff_modulus())
{
cout << prime.value() << " ";
}
cout << dec << endl;
cout << "\\" << endl;
cout << " \\-->";
// Iterate over the remaining levels
context_data = context->first_context_data();
while (context_data)
{
cout << " Level (chain index): " << context_data->chain_index();
if (context_data->parms_id() == context->first_parms_id())
{
cout << " ...... first_context_data()" << endl;
}
else if (context_data->parms_id() == context->last_parms_id())
{
cout << " ...... last_context_data()" << endl;
}
else
{
cout << endl;
}
// cout << " parms_id: " << context_data->parms_id() << endl;
cout << " coeff_modulus primes: ";
cout << hex;
for (const auto &prime : context_data->parms().coeff_modulus())
{
cout << prime.value() << " ";
}
cout << dec << endl;
cout << "\\" << endl;
cout << " \\-->";
/*
Step forward in the chain.
*/
context_data = context_data->next_context_data();
}
cout << "End of chain reached\n"
<< endl;
// Generate keys
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
SecretKey sk = keygen.secret_key();
RelinKeys relin_keys = keygen.relin_keys();
GaloisKeys gal_keys = keygen.galois_keys();
Encryptor encryptor(context, pk);
Decryptor decryptor(context, sk);
Evaluator evaluator(context);
Plaintext plain("1x^3 + 2x^2 + 3x^1 + 4");
Ciphertext cipher;
encryptor.encrypt(plain, cipher);
cout << "Perform modulus switching on cipher" << endl;
context_data = context->first_context_data();
cout << "---->";
while (context_data->next_context_data())
{
cout << " Level (chain index): " << context_data->chain_index() << endl;
// cout << " parms_id of encrypted: " << cipher.parms_id() << endl;
cout << " Noise budget at this level: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
cout << "\\" << endl;
cout << " \\-->";
evaluator.mod_switch_to_next_inplace(cipher);
context_data = context_data->next_context_data();
}
cout << " Level (chain index): " << context_data->chain_index() << endl;
// cout << " parms_id of encrypted: " << cipher.parms_id() << endl;
cout << " Noise budget at this level: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
cout << "\\" << endl;
cout << " \\-->";
cout << " End of chain reached" << endl
<< endl;
cout << "Decrypt still works after modulus switching." << endl;
decryptor.decrypt(cipher, plain);
cout << " + Decryption of encrypted: " << plain.to_string() << endl;
cout << "\tComputation is more efficient with modulus switching." << endl;
cout << "Compute the 8th power." << endl;
encryptor.encrypt(plain, cipher);
cout << " + Noise budget fresh: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
evaluator.square_inplace(cipher);
evaluator.relinearize_inplace(cipher, relin_keys);
cout << " + Noise budget of the 2nd power: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
evaluator.square_inplace(cipher);
evaluator.relinearize_inplace(cipher, relin_keys);
cout << " + Noise budget of the 4th power: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
evaluator.mod_switch_to_next_inplace(cipher);
cout << " + Noise budget after modulus switching: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
evaluator.square_inplace(cipher);
evaluator.relinearize_inplace(cipher, relin_keys);
cout << " + Noise budget of the 8th power: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
evaluator.mod_switch_to_next_inplace(cipher);
cout << " + Noise budget after modulus switching: "
<< decryptor.invariant_noise_budget(cipher) << " bits" << endl;
decryptor.decrypt(cipher, plain);
cout << " + Decryption of the 8th power (hexadecimal) ...... Correct." << endl;
cout << " " << plain.to_string() << endl
<< endl;
return 0;
}