-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathtests.rs
More file actions
87 lines (73 loc) · 3.23 KB
/
Copy pathtests.rs
File metadata and controls
87 lines (73 loc) · 3.23 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
use crate::shortint::keycache::KEY_CACHE;
use crate::shortint::noise_squashing::{
CompressedNoiseSquashingKey, NoiseSquashingKey, NoiseSquashingPrivateKey,
};
use crate::shortint::parameters::test_params::*;
use crate::shortint::parameters::*;
use rand::prelude::*;
use rand::thread_rng;
#[test]
fn test_classic_noise_squashing_ci_run_filter() {
test_noise_squashing(
TEST_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
TEST_PARAM_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
}
#[test]
fn test_multi_bit_noise_squashing_ci_run_filter() {
test_noise_squashing(
TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
NOISE_SQUASHING_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
}
#[test]
fn test_ks32_noise_squashing_ci_run_filter() {
test_noise_squashing(
TEST_PARAM_MESSAGE_2_CARRY_2_KS32_PBS_TUNIFORM_2M128,
TEST_PARAM_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
}
fn test_noise_squashing(
classic_params: impl Into<AtomicPatternParameters>,
noise_squashing_params: NoiseSquashingParameters,
) {
let keycache_entry = KEY_CACHE.get_from_param(classic_params);
let (cks, sks) = (keycache_entry.client_key(), keycache_entry.server_key());
let noise_squashing_private_key = NoiseSquashingPrivateKey::new(noise_squashing_params);
let decompressed_noise_squashing_key = {
let compressed_noise_squashing_key =
CompressedNoiseSquashingKey::new(cks, &noise_squashing_private_key);
compressed_noise_squashing_key.decompress()
};
let noise_squashing_key = NoiseSquashingKey::new(cks, &noise_squashing_private_key);
let mut rng = thread_rng();
let id_lut = sks.generate_lookup_table(|x| x);
for _ in 0..50 {
let msg_1 = rng.gen::<u64>() % cks.parameters().message_modulus().0;
let msg_2 = rng.gen::<u64>() % cks.parameters().message_modulus().0;
let mut ct_1 = cks.encrypt(msg_1);
let mut ct_2 = cks.encrypt(msg_2);
// Set ciphertext noise level to nominal
rayon::join(
|| sks.apply_lookup_table_assign(&mut ct_1, &id_lut),
|| sks.apply_lookup_table_assign(&mut ct_2, &id_lut),
);
// Pack
let packed = sks.unchecked_add(
&sks.unchecked_scalar_mul(&ct_1, sks.message_modulus.0.try_into().unwrap()),
&ct_2,
);
let squashed_noise_ct_from_compressed =
decompressed_noise_squashing_key.squash_ciphertext_noise(&packed, sks);
let squashed_noise_ct = noise_squashing_key.squash_ciphertext_noise(&packed, sks);
assert_eq!(squashed_noise_ct.degree(), packed.degree);
assert_eq!(squashed_noise_ct_from_compressed.degree(), packed.degree);
let recovered_from_compressed = noise_squashing_private_key
.decrypt_squashed_noise_ciphertext(&squashed_noise_ct_from_compressed);
let recovered =
noise_squashing_private_key.decrypt_squashed_noise_ciphertext(&squashed_noise_ct);
let expected_u128: u128 = (msg_1 * sks.message_modulus.0 + msg_2).into();
assert_eq!(recovered_from_compressed, expected_u128);
assert_eq!(recovered, expected_u128);
}
}