Skip to content

Commit 4ffc11f

Browse files
committed
feat(core): chunked lwe_bsk generation
1 parent 6f7c9d7 commit 4ffc11f

File tree

8 files changed

+751
-11
lines changed

8 files changed

+751
-11
lines changed

tfhe/src/core_crypto/algorithms/lwe_bootstrap_key_generation.rs

+408
Large diffs are not rendered by default.

tfhe/src/core_crypto/algorithms/test/lwe_bootstrap_key_generation.rs

+57-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::core_crypto::commons::math::random::{
66
};
77
use crate::core_crypto::commons::math::torus::UnsignedTorus;
88
use crate::core_crypto::commons::parameters::{
9-
CiphertextModulus, DecompositionBaseLog, DecompositionLevelCount, GlweDimension, LweDimension,
10-
PolynomialSize,
9+
ChunkSize, CiphertextModulus, DecompositionBaseLog, DecompositionLevelCount, GlweDimension,
10+
LweDimension, PolynomialSize,
1111
};
1212
use crate::core_crypto::commons::test_tools::new_secret_random_generator;
1313
use crate::core_crypto::entities::*;
@@ -17,7 +17,7 @@ const NB_TESTS: usize = 10;
1717
#[cfg(tarpaulin)]
1818
const NB_TESTS: usize = 1;
1919

20-
fn test_parallel_and_seeded_bsk_gen_equivalence<T: UnsignedTorus + Sync + Send>(
20+
fn test_parallel_and_seeded_and_chunked_bsk_gen_equivalence<T: UnsignedTorus + Sync + Send>(
2121
ciphertext_modulus: CiphertextModulus<T>,
2222
) {
2323
for _ in 0..NB_TESTS {
@@ -144,29 +144,75 @@ fn test_parallel_and_seeded_bsk_gen_equivalence<T: UnsignedTorus + Sync + Send>(
144144
let par_decompressed_bsk = parallel_seeded_bsk.par_decompress_into_lwe_bootstrap_key();
145145

146146
assert_eq!(ser_decompressed_bsk, par_decompressed_bsk);
147+
148+
let encryption_generator = EncryptionRandomGenerator::<DefaultRandomGenerator>::new(
149+
mask_seed,
150+
&mut DeterministicSeeder::<DefaultRandomGenerator>::new(deterministic_seeder_seed),
151+
);
152+
153+
let chunk_generator = LweBootstrapKeyChunkGenerator::new(
154+
encryption_generator,
155+
ChunkSize(crate::core_crypto::commons::test_tools::random_usize_between(1..5)),
156+
lwe_dim,
157+
glwe_dim.to_glwe_size(),
158+
poly_size,
159+
base_log,
160+
level,
161+
ciphertext_modulus,
162+
lwe_sk.clone(),
163+
glwe_sk.clone(),
164+
noise_distribution,
165+
false,
166+
);
167+
let chunks = chunk_generator.collect::<Vec<_>>();
168+
let assembled_bsk = allocate_and_assemble_lwe_bootstrap_key_from_chunks(chunks);
169+
assert_eq!(assembled_bsk, sequential_bsk);
170+
171+
let encryption_generator = EncryptionRandomGenerator::<DefaultRandomGenerator>::new(
172+
mask_seed,
173+
&mut DeterministicSeeder::<DefaultRandomGenerator>::new(deterministic_seeder_seed),
174+
);
175+
176+
let par_chunk_generator = LweBootstrapKeyChunkGenerator::new(
177+
encryption_generator,
178+
ChunkSize(crate::core_crypto::commons::test_tools::random_usize_between(1..5)),
179+
lwe_dim,
180+
glwe_dim.to_glwe_size(),
181+
poly_size,
182+
base_log,
183+
level,
184+
ciphertext_modulus,
185+
lwe_sk,
186+
glwe_sk,
187+
noise_distribution,
188+
true,
189+
);
190+
let chunks = par_chunk_generator.collect::<Vec<_>>();
191+
let assembled_bsk = allocate_and_assemble_lwe_bootstrap_key_from_chunks(chunks);
192+
assert_eq!(assembled_bsk, sequential_bsk);
147193
}
148194
}
149195

150196
#[test]
151-
fn test_parallel_and_seeded_bsk_gen_equivalence_u32_native_mod() {
152-
test_parallel_and_seeded_bsk_gen_equivalence::<u32>(CiphertextModulus::new_native());
197+
fn test_parallel_and_seeded_and_chunked_bsk_gen_equivalence_u32_native_mod() {
198+
test_parallel_and_seeded_and_chunked_bsk_gen_equivalence::<u32>(CiphertextModulus::new_native());
153199
}
154200

155201
#[test]
156-
fn test_parallel_and_seeded_bsk_gen_equivalence_u32_custom_mod() {
157-
test_parallel_and_seeded_bsk_gen_equivalence::<u32>(
202+
fn test_parallel_and_seeded_and_chunked_bsk_gen_equivalence_u32_custom_mod() {
203+
test_parallel_and_seeded_and_chunked_bsk_gen_equivalence::<u32>(
158204
CiphertextModulus::try_new_power_of_2(31).unwrap(),
159205
);
160206
}
161207

162208
#[test]
163-
fn test_parallel_and_seeded_bsk_gen_equivalence_u64_native_mod() {
164-
test_parallel_and_seeded_bsk_gen_equivalence::<u64>(CiphertextModulus::new_native());
209+
fn test_parallel_and_seeded_and_chunked_bsk_gen_equivalence_u64_native_mod() {
210+
test_parallel_and_seeded_and_chunked_bsk_gen_equivalence::<u64>(CiphertextModulus::new_native());
165211
}
166212

167213
#[test]
168-
fn test_parallel_and_seeded_bsk_gen_equivalence_u64_custom_mod() {
169-
test_parallel_and_seeded_bsk_gen_equivalence::<u64>(
214+
fn test_parallel_and_seeded_and_chunked_bsk_gen_equivalence_u64_custom_mod() {
215+
test_parallel_and_seeded_and_chunked_bsk_gen_equivalence::<u64>(
170216
CiphertextModulus::try_new_power_of_2(63).unwrap(),
171217
);
172218
}

tfhe/src/core_crypto/backward_compatibility/commons/parameters.rs

+5
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,8 @@ pub enum RSigmaFactorVersions {
182182
pub enum NoiseEstimationMeasureBoundVersions {
183183
V0(NoiseEstimationMeasureBound),
184184
}
185+
186+
#[derive(VersionsDispatch)]
187+
pub enum ChunkSizeVersions {
188+
V0(ChunkSize),
189+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use tfhe_versionable::deprecation::Deprecable;
2+
use tfhe_versionable::VersionsDispatch;
3+
4+
use crate::core_crypto::prelude::{Container, LweBootstrapKeyChunk, UnsignedInteger};
5+
6+
impl<C: Container> Deprecable for LweBootstrapKeyChunk<C>
7+
where
8+
C::Element: UnsignedInteger,
9+
{
10+
const TYPE_NAME: &'static str = "LweBootstrapKeyChunk";
11+
// TODO
12+
const MIN_SUPPORTED_APP_VERSION: &'static str = "TFHE-rs v0.10";
13+
}
14+
15+
#[derive(VersionsDispatch)]
16+
pub enum LweBootstrapKeyChunkVersions<C: Container>
17+
where
18+
C::Element: UnsignedInteger,
19+
{
20+
V0(LweBootstrapKeyChunk<C>),
21+
}

tfhe/src/core_crypto/backward_compatibility/entities/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod glwe_ciphertext_list;
99
pub mod glwe_secret_key;
1010
pub mod gsw_ciphertext;
1111
pub mod lwe_bootstrap_key;
12+
pub mod lwe_bootstrap_key_chunk;
1213
pub mod lwe_ciphertext;
1314
pub mod lwe_ciphertext_list;
1415
pub mod lwe_compact_ciphertext_list;

tfhe/src/core_crypto/commons/parameters.rs

+5
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,8 @@ pub struct RSigmaFactor(pub f64);
388388
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Versionize)]
389389
#[versionize(NoiseEstimationMeasureBoundVersions)]
390390
pub struct NoiseEstimationMeasureBound(pub f64);
391+
392+
/// The size of a chunk in a chunked key.
393+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Versionize)]
394+
#[versionize(ChunkSizeVersions)]
395+
pub struct ChunkSize(pub usize);

0 commit comments

Comments
 (0)