-
Notifications
You must be signed in to change notification settings - Fork 189
/
simple-integers-serial-bgvrns.cpp
315 lines (263 loc) · 13.4 KB
/
simple-integers-serial-bgvrns.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: [email protected]
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
/*
Simple example for BGVrns (integer arithmetic) with serialization. Refer to
simple-real-numbers-serial file for an example of how to use this in a "client-server" setup
*/
#include "openfhe.h"
// header files needed for serialization
#include "ciphertext-ser.h"
#include "cryptocontext-ser.h"
#include "key/key-ser.h"
#include "scheme/bgvrns/bgvrns-ser.h"
using namespace lbcrypto;
const std::string DATAFOLDER = "demoData";
int main() {
std::cout << "This program requres the subdirectory `" << DATAFOLDER << "' to exist, otherwise you will get "
<< "an error writing serializations." << std::endl;
// Sample Program: Step 1 - Set CryptoContext
CCParams<CryptoContextBGVRNS> parameters;
parameters.SetMultiplicativeDepth(2);
parameters.SetPlaintextModulus(65537);
CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
// Enable features that you wish to use
cryptoContext->Enable(PKE);
cryptoContext->Enable(KEYSWITCH);
cryptoContext->Enable(LEVELEDSHE);
std::cout << "\nThe cryptocontext has been generated." << std::endl;
// Serialize cryptocontext
if (!Serial::SerializeToFile(DATAFOLDER + "/cryptocontext.txt", cryptoContext, SerType::BINARY)) {
std::cerr << "Error writing serialization of the crypto context to "
"cryptocontext.txt"
<< std::endl;
return 1;
}
std::cout << "The cryptocontext has been serialized." << std::endl;
// Sample Program: Step 2 - Key Generation
// Initialize Public Key Containers
KeyPair<DCRTPoly> keyPair;
// Generate a public/private key pair
keyPair = cryptoContext->KeyGen();
std::cout << "The key pair has been generated." << std::endl;
// Serialize the public key
if (!Serial::SerializeToFile(DATAFOLDER + "/key-public.txt", keyPair.publicKey, SerType::BINARY)) {
std::cerr << "Error writing serialization of public key to key-public.txt" << std::endl;
return 1;
}
std::cout << "The public key has been serialized." << std::endl;
// Serialize the secret key
if (!Serial::SerializeToFile(DATAFOLDER + "/key-private.txt", keyPair.secretKey, SerType::BINARY)) {
std::cerr << "Error writing serialization of private key to key-private.txt" << std::endl;
return 1;
}
std::cout << "The secret key has been serialized." << std::endl;
// Generate the relinearization key
cryptoContext->EvalMultKeyGen(keyPair.secretKey);
std::cout << "The eval mult keys have been generated." << std::endl;
// Serialize the relinearization (evaluation) key for homomorphic
// multiplication
std::ofstream emkeyfile(DATAFOLDER + "/" + "key-eval-mult.txt", std::ios::out | std::ios::binary);
if (emkeyfile.is_open()) {
if (cryptoContext->SerializeEvalMultKey(emkeyfile, SerType::BINARY) == false) {
std::cerr << "Error writing serialization of the eval mult keys to "
"key-eval-mult.txt"
<< std::endl;
return 1;
}
std::cout << "The eval mult keys have been serialized." << std::endl;
emkeyfile.close();
}
else {
std::cerr << "Error serializing eval mult keys" << std::endl;
return 1;
}
// Generate the rotation evaluation keys
cryptoContext->EvalRotateKeyGen(keyPair.secretKey, {1, 2, -1, -2});
std::cout << "The rotation keys have been generated." << std::endl;
// Serialize the rotation keyhs
std::ofstream erkeyfile(DATAFOLDER + "/" + "key-eval-rot.txt", std::ios::out | std::ios::binary);
if (erkeyfile.is_open()) {
if (cryptoContext->SerializeEvalAutomorphismKey(erkeyfile, SerType::BINARY) == false) {
std::cerr << "Error writing serialization of the eval rotation keys to "
"key-eval-rot.txt"
<< std::endl;
return 1;
}
std::cout << "The eval rotation keys have been serialized." << std::endl;
erkeyfile.close();
}
else {
std::cerr << "Error serializing eval rotation keys" << std::endl;
return 1;
}
// Sample Program: Step 3: Encryption
// First plaintext vector is encoded
std::vector<int64_t> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext1 = cryptoContext->MakePackedPlaintext(vectorOfInts1);
// Second plaintext vector is encoded
std::vector<int64_t> vectorOfInts2 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext2 = cryptoContext->MakePackedPlaintext(vectorOfInts2);
// Third plaintext vector is encoded
std::vector<int64_t> vectorOfInts3 = {1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext3 = cryptoContext->MakePackedPlaintext(vectorOfInts3);
std::cout << "Plaintext #1: " << plaintext1 << std::endl;
std::cout << "Plaintext #2: " << plaintext2 << std::endl;
std::cout << "Plaintext #3: " << plaintext3 << std::endl;
// The encoded vectors are encrypted
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
auto ciphertext2 = cryptoContext->Encrypt(keyPair.publicKey, plaintext2);
auto ciphertext3 = cryptoContext->Encrypt(keyPair.publicKey, plaintext3);
std::cout << "The plaintexts have been encrypted." << std::endl;
if (!Serial::SerializeToFile(DATAFOLDER + "/" + "ciphertext1.txt", ciphertext1, SerType::BINARY)) {
std::cerr << "Error writing serialization of ciphertext 1 to ciphertext1.txt" << std::endl;
return 1;
}
std::cout << "The first ciphertext has been serialized." << std::endl;
if (!Serial::SerializeToFile(DATAFOLDER + "/" + "ciphertext2.txt", ciphertext2, SerType::BINARY)) {
std::cerr << "Error writing serialization of ciphertext 2 to ciphertext2.txt" << std::endl;
return 1;
}
std::cout << "The second ciphertext has been serialized." << std::endl;
if (!Serial::SerializeToFile(DATAFOLDER + "/" + "ciphertext3.txt", ciphertext3, SerType::BINARY)) {
std::cerr << "Error writing serialization of ciphertext 3 to ciphertext3.txt" << std::endl;
return 1;
}
std::cout << "The third ciphertext has been serialized." << std::endl;
// Sample Program: Step 4 - Evaluation
// OpenFHE maintains an internal map of CryptoContext objects which are
// indexed by a tag and the tag is applied to both the CryptoContext and some
// of the keys. When deserializing a context, OpenFHE checks for the tag and
// if it finds it in the CryptoContext map, it will return the stored version.
// Hence, we need to clear the context and clear the keys.
cryptoContext->ClearEvalMultKeys();
cryptoContext->ClearEvalAutomorphismKeys();
lbcrypto::CryptoContextFactory<lbcrypto::DCRTPoly>::ReleaseAllContexts();
// Deserialize the crypto context
CryptoContext<DCRTPoly> cc;
if (!Serial::DeserializeFromFile(DATAFOLDER + "/cryptocontext.txt", cc, SerType::BINARY)) {
std::cerr << "I cannot read serialization from " << DATAFOLDER + "/cryptocontext.txt" << std::endl;
return 1;
}
std::cout << "The cryptocontext has been deserialized." << std::endl;
PublicKey<DCRTPoly> pk;
if (Serial::DeserializeFromFile(DATAFOLDER + "/key-public.txt", pk, SerType::BINARY) == false) {
std::cerr << "Could not read public key" << std::endl;
return 1;
}
std::cout << "The public key has been deserialized." << std::endl;
std::ifstream emkeys(DATAFOLDER + "/key-eval-mult.txt", std::ios::in | std::ios::binary);
if (!emkeys.is_open()) {
std::cerr << "I cannot read serialization from " << DATAFOLDER + "/key-eval-mult.txt" << std::endl;
return 1;
}
if (cc->DeserializeEvalMultKey(emkeys, SerType::BINARY) == false) {
std::cerr << "Could not deserialize the eval mult key file" << std::endl;
return 1;
}
std::cout << "Deserialized the eval mult keys." << std::endl;
std::ifstream erkeys(DATAFOLDER + "/key-eval-rot.txt", std::ios::in | std::ios::binary);
if (!erkeys.is_open()) {
std::cerr << "I cannot read serialization from " << DATAFOLDER + "/key-eval-rot.txt" << std::endl;
return 1;
}
if (cc->DeserializeEvalAutomorphismKey(erkeys, SerType::BINARY) == false) {
std::cerr << "Could not deserialize the eval rotation key file" << std::endl;
return 1;
}
std::cout << "Deserialized the eval rotation keys." << std::endl;
// deserializing ciphertexts
Ciphertext<DCRTPoly> ct1;
if (Serial::DeserializeFromFile(DATAFOLDER + "/ciphertext1.txt", ct1, SerType::BINARY) == false) {
std::cerr << "Could not read the ciphertext" << std::endl;
return 1;
}
std::cout << "The first ciphertext has been deserialized." << std::endl;
Ciphertext<DCRTPoly> ct2;
if (Serial::DeserializeFromFile(DATAFOLDER + "/ciphertext2.txt", ct2, SerType::BINARY) == false) {
std::cerr << "Could not read the ciphertext" << std::endl;
return 1;
}
std::cout << "The second ciphertext has been deserialized." << std::endl;
Ciphertext<DCRTPoly> ct3;
if (Serial::DeserializeFromFile(DATAFOLDER + "/ciphertext3.txt", ct3, SerType::BINARY) == false) {
std::cerr << "Could not read the ciphertext" << std::endl;
return 1;
}
std::cout << "The third ciphertext has been deserialized." << std::endl;
// Homomorphic additions
auto ciphertextAdd12 = cc->EvalAdd(ct1, ct2); // iphertext2);
auto ciphertextAddResult = cc->EvalAdd(ciphertextAdd12, ct3); // iphertext3);
// Homomorphic multiplications
auto ciphertextMul12 = cc->EvalMult(ct1, ct2); // iphertext2);
auto ciphertextMultResult = cc->EvalMult(ciphertextMul12, ct3); // iphertext3);
// Homomorphic rotations
auto ciphertextRot1 = cc->EvalRotate(ct1, 1);
auto ciphertextRot2 = cc->EvalRotate(ct1, 2);
auto ciphertextRot3 = cc->EvalRotate(ct1, -1);
auto ciphertextRot4 = cc->EvalRotate(ct1, -2);
// Sample Program: Step 5 - Decryption
PrivateKey<DCRTPoly> sk;
if (Serial::DeserializeFromFile(DATAFOLDER + "/key-private.txt", sk, SerType::BINARY) == false) {
std::cerr << "Could not read secret key" << std::endl;
return 1;
}
std::cout << "The secret key has been deserialized." << std::endl;
// Decrypt the result of additions
Plaintext plaintextAddResult;
cc->Decrypt(sk, ciphertextAddResult, &plaintextAddResult);
// Decrypt the result of multiplications
Plaintext plaintextMultResult;
cc->Decrypt(sk, ciphertextMultResult, &plaintextMultResult);
// Decrypt the result of rotations
Plaintext plaintextRot1;
cc->Decrypt(sk, ciphertextRot1, &plaintextRot1);
Plaintext plaintextRot2;
cc->Decrypt(sk, ciphertextRot2, &plaintextRot2);
Plaintext plaintextRot3;
cc->Decrypt(sk, ciphertextRot3, &plaintextRot3);
Plaintext plaintextRot4;
cc->Decrypt(sk, ciphertextRot4, &plaintextRot4);
// Shows only the same number of elements as in the original plaintext vector
// By default it will show all coefficients in the BFV-encoded polynomial
plaintextRot1->SetLength(vectorOfInts1.size());
plaintextRot2->SetLength(vectorOfInts1.size());
plaintextRot3->SetLength(vectorOfInts1.size());
plaintextRot4->SetLength(vectorOfInts1.size());
// Output results
std::cout << "\nResults of homomorphic computations" << std::endl;
std::cout << "#1 + #2 + #3: " << plaintextAddResult << std::endl;
std::cout << "#1 * #2 * #3: " << plaintextMultResult << std::endl;
std::cout << "Left rotation of #1 by 1: " << plaintextRot1 << std::endl;
std::cout << "Left rotation of #1 by 2: " << plaintextRot2 << std::endl;
std::cout << "Right rotation of #1 by 1: " << plaintextRot3 << std::endl;
std::cout << "Right rotation of #1 by 2: " << plaintextRot4 << std::endl;
return 0;
}