chore: split up more xof key gen function

This commit is contained in:
Thomas Montaigu
2025-09-23 20:27:59 +02:00
committed by tmontaigu
parent 736185bb31
commit 8b5d7321fb
8 changed files with 605 additions and 513 deletions

View File

@@ -15,6 +15,8 @@ rust-version = "1.72"
aes = "0.8.2"
rayon = { workspace = true, optional = true }
getrandom = { workspace = true }
serde = "1.0.226"
tfhe-versionable = { version = "0.6.2", path = "../utils/tfhe-versionable" }
[target.'cfg(target_os = "macos")'.dependencies]
libc = "0.2.133"

View File

@@ -0,0 +1,8 @@
use tfhe_versionable::VersionsDispatch;
use crate::seeders::XofSeed;
#[derive(VersionsDispatch)]
pub enum XofSeedVersions {
V0(XofSeed),
}

View File

@@ -9,14 +9,15 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Seed(pub u128);
/// A Seed as described in the [NIST document]
/// A Seed as described in the [Threshold (Fully) Homomorphic Encryption]
///
/// This seed contains 2 information:
/// * The domain separator bytes (ASCII string)
/// * The seed bytes
///
/// [NIST document]: https://eprint.iacr.org/2025/699
#[derive(Debug, Clone, PartialEq, Eq)]
/// [Threshold (Fully) Homomorphic Encryption]: https://eprint.iacr.org/2025/699
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Versionize)]
#[versionize(XofSeedVersions)]
pub struct XofSeed {
// We store the domain separator concatenated with the seed bytes (str||seed)
// as it makes it easier to create the iterator of u128 blocks
@@ -97,7 +98,8 @@ pub enum SeedKind {
/// Initializes the Aes-Ctr with a counter starting at 0
/// and uses the seed as the Aes key.
Ctr(Seed),
/// Seed that initialized the Aes-Ctr following the NIST document (see [XofSeed]).
/// Seed that initialized the Aes-Ctr following the Threshold (Fully) Homomorphic Encryption
/// document (see [XofSeed]).
///
/// An Aes-Key and starting counter will be derived from the XofSeed, to
/// then initialize the Aes-Ctr random generator
@@ -128,11 +130,15 @@ pub trait Seeder {
Self: Sized;
}
pub mod backward_compatibility;
mod implem;
// This import statement can be empty if seeder features are disabled, rustc's behavior changed to
// warn of empty modules, we know this can happen, so allow it.
#[allow(unused_imports)]
pub use implem::*;
use tfhe_versionable::Versionize;
use crate::seeders::backward_compatibility::XofSeedVersions;
#[cfg(test)]
mod generic_tests {

View File

@@ -534,7 +534,7 @@ pub fn allocate_and_generate_lwe_bootstrapping_key_with_pre_seeded_generator<
decomp_level_count: DecompositionLevelCount,
noise_distribution: DynamicDistribution<GlweCont::Element>,
ciphertext_modulus: CiphertextModulus<GlweCont::Element>,
noise_generator: &mut EncryptionRandomGenerator<ByteGen>,
generator: &mut EncryptionRandomGenerator<ByteGen>,
) -> SeededLweBootstrapKeyOwned<GlweCont::Element>
where
LweCont: Container,
@@ -560,7 +560,7 @@ where
output_glwe_secret_key,
&mut lwe_bootstrapping_key,
noise_distribution,
noise_generator,
generator,
);
lwe_bootstrapping_key

View File

@@ -563,6 +563,7 @@ where
pub fn allocate_and_generate_lwe_key_switching_key_with_pre_seeded_generator<
InputLweCont,
OutputLweCont,
Gen,
>(
input_lwe_secret_key: &LweSecretKey<InputLweCont>,
output_lwe_secret_key: &LweSecretKey<OutputLweCont>,
@@ -570,13 +571,14 @@ pub fn allocate_and_generate_lwe_key_switching_key_with_pre_seeded_generator<
decomp_level_count: DecompositionLevelCount,
noise_distribution: DynamicDistribution<InputLweCont::Element>,
ciphertext_modulus: CiphertextModulus<InputLweCont::Element>,
noise_generator: &mut EncryptionRandomGenerator<DefaultRandomGenerator>,
generator: &mut EncryptionRandomGenerator<Gen>,
) -> SeededLweKeyswitchKeyOwned<InputLweCont::Element>
where
InputLweCont: Container,
InputLweCont::Element:
UnsignedInteger + Encryptable<Uniform, DynamicDistribution<InputLweCont::Element>>,
OutputLweCont: Container<Element = InputLweCont::Element>,
Gen: ByteRandomGenerator,
{
let mut key_switching_key = SeededLweKeyswitchKeyOwned::new(
InputLweCont::Element::ZERO,
@@ -592,7 +594,7 @@ where
output_lwe_secret_key,
&mut key_switching_key,
noise_distribution,
noise_generator,
generator,
);
key_switching_key

View File

@@ -19,11 +19,8 @@ pub use tfhe_csprng::seeders::{Seed, Seeder, XofSeed};
/// Module to proxy the serialization for `tfhe-csprng::Seed` to avoid adding serde as a
/// dependency to `tfhe-csprng`
pub mod serialization_proxy {
// use crate::core_crypto::backward_compatibility::commons::math::random::{
// XofSeedSerdeDefVersioned, XofSeedSerdeDefVersionedOwned,
// };
pub(crate) use serde::{Deserialize, Serialize};
pub(crate) use tfhe_csprng::seeders::{Seed, XofSeed};
pub(crate) use tfhe_csprng::seeders::Seed;
// See https://serde.rs/remote-derive.html
// Serde calls this the definition of the remote type. It is just a copy of the remote data
// structure. The `remote` attribute gives the path to the actual type we intend to derive code
@@ -31,16 +28,6 @@ pub mod serialization_proxy {
#[derive(Serialize, Deserialize)]
#[serde(remote = "Seed")]
pub(crate) struct SeedSerdeDef(pub u128);
#[derive(Serialize, Deserialize)]
#[serde(remote = "XofSeed")]
pub(crate) struct XofSeedSerdeDef(#[serde(getter = "XofSeed::bytes")] Vec<u8>);
impl From<XofSeedSerdeDef> for XofSeed {
fn from(value: XofSeedSerdeDef) -> Self {
Self::from_bytes(value.0)
}
}
}
pub(crate) use serialization_proxy::*;

View File

@@ -1,15 +1,9 @@
use serde::{Deserialize, Serialize};
use tfhe_versionable::VersionsDispatch;
use crate::high_level_api::xof_key_set::{CompressedXofKeySet, XofKeySet};
#[derive(Serialize)]
pub enum CompressedXofKeySetVersioned<'vers> {
V0(&'vers CompressedXofKeySet),
}
#[derive(Serialize, Deserialize)]
pub enum CompressedXofKeySetVersionedOwned {
#[derive(VersionsDispatch)]
pub enum CompressedXofKeySetVersions {
V0(CompressedXofKeySet),
}

File diff suppressed because it is too large Load Diff