forked from RasoulAM/CompressLWE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
145 lines (116 loc) · 4.35 KB
/
tests.cpp
File metadata and controls
145 lines (116 loc) · 4.35 KB
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
#include <iostream>
#include <cassert>
#include "library.h"
#include "chrono"
using namespace std::chrono;
void test_single_lwe_compress() {
uint64_t n = 630;
uint64_t log_q = 64;
uint64_t p = 32;
// Generate a random vector<uint64_t> of size n+1 with values in [0, q) and call it the secret key
std::vector<uint64_t> lwe_key(n);
for (int i = 0; i < n; i++) {
lwe_key[i] = sample(log_q);
}
// Generate a ciphertext
std::vector<uint64_t> lwe_ct(n + 1);
for (int i = 0; i < n + 1; i++) {
lwe_ct[i] = sample(log_q);
}
// Call the generate Keys function with the lwe_key
auto key = generateKeys(lwe_key);
// Call the compressSingle function with the compression key, the ciphertext, and the LWEParams
LWEParams params(n, log_q, p);
auto compressed_ct = compressSingle(key.compKey, lwe_ct, params);
// Call the decryptCompressedSingle function with the compressed ciphertext, the paillier private key, and the LWEParams
auto decrypted = decryptCompressedSingle(compressed_ct, key.paiKeys.priv_key, params);
uint64_t lwe_decrypted = decryptLWE(lwe_ct, lwe_key, params);
assert(decrypted == lwe_decrypted);
// print success message
std::cout << "Single Test Passed!" << std::endl;
}
void test_batched_lwe_compress() {
uint64_t n = 630;
uint64_t log_q = 64;
uint64_t p = 32;
assert(log_q <= 64);
// Get starting timepoint
auto start = high_resolution_clock::now();
// Generate a lwe key of size n with values in [0, q)
std::vector<uint64_t> lwe_key(n);
for (int i = 0; i < n; i++) {
lwe_key[i] = sample(log_q);
}
// Get ending timepoint
auto stop = high_resolution_clock::now();
// Get duration. Substart timepoints to
auto d_lwe_key = duration_cast<microseconds>(stop - start);
start=stop;
uint64_t num_cts = 784;
// Generate a batch of lwe ciphertexts
std::vector<std::vector<uint64_t>> lwe_cts(num_cts);
for (int i = 0; i < num_cts; i++) {
lwe_cts[i] = std::vector<uint64_t>(n + 1);
for (int j = 0; j < n + 1; j++) {
lwe_cts[i][j] = sample(log_q);
}
}
// Get ending timepoint
stop = high_resolution_clock::now();
// Get duration. Substart timepoints to
auto d_lwe_cts = duration_cast<microseconds>(stop - start);
start = stop;
auto key = generateKeys(lwe_key);
// Get ending timepoint
stop = high_resolution_clock::now();
// Get duration. Substart timepoints to
auto d_keygen = duration_cast<microseconds>(stop - start);
start=stop;
LWEParams params(n, log_q, p);
CompressedCiphertext compressed_ct = compressBatched(
key.compKey,
lwe_cts,
params
);
// Get ending timepoint
stop = high_resolution_clock::now();
// Get duration. Substart timepoints to
auto d_compress = duration_cast<microseconds>(stop - start);
start = stop;
std::vector<uint64_t> decrypted = decryptCompressedBatched(
compressed_ct,
key.paiKeys.priv_key,
params,
num_cts
);
// Get ending timepoint
stop = high_resolution_clock::now();
// Get duration. Substart timepoints to
auto d_decrypt = duration_cast<microseconds>(stop - start);
for (int i = 0; i < num_cts; i++) {
uint64_t lwe_decrypted = decryptLWE(lwe_cts[i], lwe_key, params);
assert(decrypted[i] == lwe_decrypted);
}
std::cout << "Batched Test Passed!" << std::endl;
std::cout << "Time taken by LWE key: " << d_lwe_key.count() << " microseconds" << std::endl;
std::cout << "Time taken by LWE cts: " << d_lwe_cts.count() << " microseconds" << std::endl;
std::cout << "Time taken by keygen: " << d_keygen.count() << " microseconds" << std::endl;
std::cout << "Time taken by compress: " << d_compress.count() << " microseconds" << std::endl;
std::cout << "Time taken by decrypt: " << d_decrypt.count() << " microseconds" << std::endl;
}
void test_conversion() {
for (uint64_t i = 0; i < 10; i++) {
uint64_t x = sample(64);
BigNumber x_bn = from64(x);
uint64_t y = to64(x_bn);
assert(x == y);
}
std::cout << "Conversion Test Passed!" << std::endl;
}
int main() {
srand(time(nullptr));
test_conversion();
test_single_lwe_compress();
test_batched_lwe_compress();
return 0;
}