-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cc.cpp
75 lines (61 loc) · 2.27 KB
/
test_cc.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
// Copyright © 2018 yukiymd. All rights reserved.
#include <iostream>
#include <fstream>
#include "palisade.h"
#include "cryptocontexthelper.h"
#include "utils/serializablehelper.h"
using namespace lbcrypto;
int main() {
std::cout << "STEP 1: Contexts Creation" << "\n";
CryptoContext<Poly> cc1 = CryptoContextHelper::getNewContext("BFV1");
cc1->Enable(ENCRYPTION);
cc1->Enable(SHE);
cc1->Enable(PRE);
cc1->Enable(MULTIPARTY);
std::cout << "STEP 2: Key-generation" << "\n";
std::string ccPath = "tmp/cc";
LPKeyPair<Poly> kp1 = cc1->KeyGen();
std::cout << "STEP 3: Context Duplication" << "\n";
Serialized ser1;
if (!cc1->Serialize(&ser1)) {
std::cout << " Serialization failed ..." << "\n";
return 1;
}
if (!SerializableHelper::WriteSerializationToFile(ser1, ccPath)) {
std::cout << " Writing failed ..." << "\n";
return 1;
}
Serialized ser2;
if (!SerializableHelper::ReadSerializationFromFile(ccPath, &ser2)) {
std::cout << " Reading failed ..." << "\n";
return 1;
}
CryptoContext<Poly> cc2 =
CryptoContextFactory<Poly>::DeserializeAndCreateContext(ser2);
std::cout << "STEP 4: Encryption" << "\n";
int num1 = 1;
Plaintext plaintext1 = cc1->MakeIntegerPlaintext(num1);
Ciphertext<Poly> ciphertext1 = cc1->Encrypt(kp1.publicKey, plaintext1);
std::cout << "STEP 5: Calculation" << "\n";
int num2 = 2;
Plaintext plaintext2 = cc2->MakeIntegerPlaintext(num2);
Ciphertext<Poly> ciphertext3 = cc2->EvalAdd(ciphertext1, plaintext2);
std::cout << "STEP 6: Decryption" << "\n";
Plaintext plaintext3;
cc1->Decrypt(kp1.secretKey, ciphertext3, &plaintext3);
std::cout << " " << num1 << " + " << num2 << " = " << plaintext3 << "\n";
std::cout << "STEP 7: Security Check" << "\n";
LPKeyPair<Poly> kp2 = cc2->KeyGen();
Plaintext plaintext4;
std::cout << " !!! Expected to fail !!!" << "\n";
try {
cc2->Decrypt(kp2.secretKey, ciphertext3, &plaintext4);
std::cout << " " << num1 << " + " << num2 << " = " << plaintext4 << "\n";
cc2->Decrypt(kp2.secretKey, ciphertext3, &plaintext4);
std::cout << " " << num1 << " + " << num2 << " = " << plaintext4 << "\n";
} catch (exception e) {
std::cout << " Decryption failed ..." << "\n";
}
std::cout << "--- DONE ---" << "\n";
return 0;
}