-
Notifications
You must be signed in to change notification settings - Fork 2
/
boostrapping_tests.rs
156 lines (129 loc) · 3.98 KB
/
boostrapping_tests.rs
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
use tfhe::bootstrapping::{
boots_and, boots_andny, boots_andyn, boots_constant, boots_nand, boots_nor, boots_not, boots_or,
boots_orny, boots_oryn, boots_xnor, boots_xor,
};
use tfhe::bootstrapping::{bootstrapping_parameters, decrypt, encrypt, generate_keys};
#[test]
fn test_encrypt_decrypt_true_is_true() {
let message = true;
let security = 128;
let params = bootstrapping_parameters(security);
let (secret_key, _cloud_key) = generate_keys(¶ms);
let encrypted = encrypt(message, &secret_key);
let decrypted = decrypt(&encrypted, &secret_key);
assert_eq!(message, decrypted);
}
#[test]
fn test_encrypt_decrypt_false_is_false() {
let message = false;
let security = 128;
let params = bootstrapping_parameters(security);
let (secret_key, _cloud_key) = generate_keys(¶ms);
let encrypted = encrypt(message, &secret_key);
let decrypted = decrypt(&encrypted, &secret_key);
assert_eq!(message, decrypted);
}
#[test]
fn test_bootstrapping_constant() {
let security = 128;
let params = bootstrapping_parameters(security);
let (secret_key, cloud_key) = generate_keys(¶ms);
let every_combo = vec![true, false];
for x in every_combo {
let bootstrapped = boots_constant(x, &cloud_key);
let decrypted = decrypt(&bootstrapped, &secret_key);
assert_eq!(x, decrypted);
}
}
macro_rules! test_binary_gate {
($test_name: ident, $binary_gate:ident, $encrypted_gate:ident) => {
#[test]
fn $test_name() {
let security = 128;
let params = bootstrapping_parameters(security);
let (secret_key, cloud_key) = generate_keys(¶ms);
let enc_true = encrypt(true, &secret_key);
let enc_false = encrypt(false, &secret_key);
let every_combo = vec![(true, true), (true, false), (false, true), (false, false)];
for (a, b) in every_combo {
let enc_a = match a {
true => &enc_true,
false => &enc_false,
};
let enc_b = match b {
true => &enc_true,
false => &enc_false,
};
let encrypted = $encrypted_gate(enc_a, enc_b, &cloud_key);
let decrypted = decrypt(&encrypted, &secret_key);
assert_eq!(decrypted, $binary_gate(a, b));
}
}
};
}
macro_rules! test_unary_gate {
($test_name: ident, $unary_gate:ident, $encrypted_gate:ident) => {
#[test]
fn $test_name() {
let security = 128;
let params = bootstrapping_parameters(security);
let (secret_key, cloud_key) = generate_keys(¶ms);
let enc_true = encrypt(true, &secret_key);
let enc_false = encrypt(false, &secret_key);
let every_combo = vec![true, false];
for x in every_combo {
let encrypted = match x {
true => &enc_true,
false => &enc_false,
};
let result = $encrypted_gate(encrypted, &cloud_key);
let decrypted = decrypt(&result, &secret_key);
assert_eq!(decrypted, $unary_gate(x));
}
}
};
}
fn and(a: bool, b: bool) -> bool {
a && b
}
fn or(a: bool, b: bool) -> bool {
a || b
}
fn nand(a: bool, b: bool) -> bool {
!(a && b)
}
fn not(x: bool) -> bool {
!x
}
fn nor(a: bool, b: bool) -> bool {
!(a || b)
}
fn xor(a: bool, b: bool) -> bool {
a ^ b
}
fn xnor(a: bool, b: bool) -> bool {
a == b
}
fn andny(a: bool, b: bool) -> bool {
(!a) && b
}
fn andyn(a: bool, b: bool) -> bool {
a && (!b)
}
fn orny(a: bool, b: bool) -> bool {
(!a) || b
}
fn oryn(a: bool, b: bool) -> bool {
a || (!b)
}
test_binary_gate!(test_and_gate, and, boots_and);
test_binary_gate!(test_or_gate, or, boots_or);
test_binary_gate!(test_nand_gate, nand, boots_nand);
test_binary_gate!(test_nor_gate, nor, boots_nor);
test_binary_gate!(test_xor_gate, xor, boots_xor);
test_binary_gate!(test_xnor_gate, xnor, boots_xnor);
test_binary_gate!(test_andny_gate, andny, boots_andny);
test_binary_gate!(test_andyn_gate, andyn, boots_andyn);
test_binary_gate!(test_orny_gate, orny, boots_orny);
test_binary_gate!(test_oryn_gate, oryn, boots_oryn);
test_unary_gate!(test_not_gate, not, boots_not);