Skip to content

Commit bb1e13b

Browse files
committed
add reproducibility_test
1 parent 7bf2ec6 commit bb1e13b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exclude = [
2121
"utils/tfhe-backward-compat-data",
2222
"utils/tfhe-lints",
2323
"apps/trivium",
24+
"reproductibility_test",
2425
]
2526
[workspace.dependencies]
2627
aligned-vec = { version = "0.6", default-features = false }

reproductibility_test/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "reproductibility_test"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
tfhe = { path = "../tfhe", features = [
8+
"shortint",
9+
"experimental-force_fft_algo_dif4",
10+
] }
11+
sha2 = "0.10"
12+
bincode = "1.3"
13+
rayon = "1.10"

reproductibility_test/src/main.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use rayon::prelude::*;
2+
use sha2::{Digest, Sha256};
3+
use tfhe::core_crypto::commons::generators::DeterministicSeeder;
4+
use tfhe::core_crypto::commons::math::random::Seed;
5+
use tfhe::core_crypto::prelude::DefaultRandomGenerator;
6+
use tfhe::shortint::engine::ShortintEngine;
7+
use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
8+
use tfhe::shortint::server_key::{generate_lookup_table, LookupTableOwned, LookupTableSize};
9+
use tfhe::shortint::{gen_keys, ClientKey, ServerKey, ShortintParameterSet};
10+
11+
fn hash_vec_u8(data: &[u8]) -> Vec<u8> {
12+
let mut hasher = Sha256::new();
13+
hasher.update(data);
14+
hasher.finalize().to_vec()
15+
}
16+
17+
fn hash_for_keyset(
18+
num_ct: usize,
19+
seed: Seed,
20+
cks: &ClientKey,
21+
sks: &ServerKey,
22+
lut: &LookupTableOwned,
23+
) -> Vec<u8> {
24+
let mut seeder = DeterministicSeeder::<DefaultRandomGenerator>::new(seed);
25+
26+
let shortint_engine = ShortintEngine::new_from_seeder(&mut seeder);
27+
ShortintEngine::with_thread_local_mut(|local_engine| {
28+
let _ = std::mem::replace(local_engine, shortint_engine);
29+
});
30+
31+
let msg = 0;
32+
33+
let inputs: Vec<_> = (0..num_ct).map(|_| cks.encrypt(msg)).collect();
34+
35+
let hashes: Vec<_> = inputs
36+
.par_iter()
37+
.map(|ct| {
38+
let ct_res = sks.apply_lookup_table(ct, lut);
39+
40+
let res = bincode::serialize(&ct_res).unwrap();
41+
42+
hash_vec_u8(&res)
43+
})
44+
.collect();
45+
46+
let hashes_concatenated: Vec<u8> = hashes.iter().flatten().copied().collect();
47+
48+
hash_vec_u8(&hashes_concatenated)
49+
}
50+
51+
fn full_hash(params: ShortintParameterSet, num_keyset: usize, num_ct_per_keyset: usize) -> Vec<u8> {
52+
let modulus = params.message_modulus().0;
53+
54+
let lut = generate_lookup_table(
55+
LookupTableSize::new(
56+
params.glwe_dimension().to_glwe_size(),
57+
params.polynomial_size(),
58+
),
59+
params.ciphertext_modulus(),
60+
params.message_modulus(),
61+
params.carry_modulus(),
62+
|x| x % modulus,
63+
);
64+
65+
let mut hashes_concatenated = vec![];
66+
67+
for i in 0..num_keyset {
68+
let mut seeder = DeterministicSeeder::<DefaultRandomGenerator>::new(Seed(i as u128));
69+
70+
let shortint_engine = ShortintEngine::new_from_seeder(&mut seeder);
71+
ShortintEngine::with_thread_local_mut(|local_engine| {
72+
let _ = std::mem::replace(local_engine, shortint_engine);
73+
});
74+
75+
let (cks, sks) = gen_keys(params);
76+
77+
hashes_concatenated.extend_from_slice(&hash_for_keyset(
78+
num_ct_per_keyset,
79+
Seed(i as u128),
80+
&cks,
81+
&sks,
82+
&lut,
83+
));
84+
85+
println!("Done {}/{num_keyset}", i + 1);
86+
}
87+
88+
hash_vec_u8(&hashes_concatenated)
89+
}
90+
91+
fn main() {
92+
let full_hash = full_hash(PARAM_MESSAGE_2_CARRY_2_KS_PBS.into(), 60, 30_000);
93+
94+
println!("{:?}", &full_hash);
95+
}

0 commit comments

Comments
 (0)