-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_noise.cpp
72 lines (59 loc) · 2.36 KB
/
test_noise.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
// Copyright © 2018 yukiymd. All rights reserved.
#include <iostream>
#include <fstream>
#include <vector>
#include "palisade.h"
#include "cryptocontexthelper.h"
#include "utils/serializablehelper.h"
#define ERRLOG(msg) std::cerr << "!!!!! " \
<< __FILE__ << ", " \
<< __LINE__ << ", " \
<< __FUNCTION__ << ": " \
<< msg << "\n";
using namespace lbcrypto;
int main() {
std::cout << "STEP 1: Contexts Creation" << "\n";
uint64_t ptm = 15361;
float securityLevel = 0.1;
float dist = 1.006;
// One of these value should be non-zero, and the others should be zero.
int numAdds = 1, numMults = 0, numKeySwitches = 0;
std::cerr << "Generating cc ..." << std::endl
<< " plaintextModulus = " << ptm << "\n"
<< " securityLevel = " << securityLevel << "\n"
<< " dist = " << dist << "\n"
<< " numAdds = " << numAdds << "\n"
<< " numMults = " << numMults << "\n"
<< " numKeySwitches = " << numKeySwitches << "\n";
CryptoContext<DCRTPoly> cc =
CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
ptm, securityLevel, dist, numAdds, numMults, numKeySwitches);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
std::cout << "STEP 2: Key-generation" << "\n";
LPKeyPair<DCRTPoly> kp = cc->KeyGen();
cc->EvalMultKeyGen(kp.secretKey);
std::cout << "STEP 3: Encryption" << "\n";
int num = 1;
Ciphertext<DCRTPoly> ctxt = cc->Encrypt(kp.publicKey,
cc->MakeIntegerPlaintext(num));
std::cout << "STEP 4: Over calculation" << "\n";
Ciphertext<DCRTPoly> validResult = ctxt + ctxt;
Ciphertext<DCRTPoly> invalidResult = validResult;
num += 1;
for (int i = 0; i < 10000 - 2; i++) {
invalidResult += ctxt;
num += 1;
// invalidResult *= invalidResult;
}
std::cout << "FINAL STEP: Decryption" << "\n";
Plaintext result;
cc->Decrypt(kp.secretKey, validResult, &result);
std::cout << " Valid Result\n"
<< " expected: 2, obtained: " << result << "\n";
cc->Decrypt(kp.secretKey, invalidResult, &result);
std::cout << " Invalid Result\n"
<< " expected: " << num << ", obtained: " << result << "\n";
std::cout << "===== DONE =====" << "\n";
return 0;
}