use super::engine_error;
use crate::specification::engines::AbstractEngine;
use crate::prelude::{DecompositionBaseLog, DecompositionLevelCount, Variance};
use crate::specification::entities::{LweKeyswitchKeyEntity, LweSecretKeyEntity};
engine_error! {
LweKeyswitchKeyGenerationError for LweKeyswitchKeyGenerationEngine @
NullDecompositionBaseLog => "The key decomposition base log must be greater than zero.",
NullDecompositionLevelCount => "The key decomposition level count must be greater than zero.",
DecompositionTooLarge => "The decomposition precision (base log * level count) must not exceed \
the precision of the ciphertext."
}
impl<EngineError: std::error::Error> LweKeyswitchKeyGenerationError<EngineError> {
pub fn perform_generic_checks(
decomposition_level_count: DecompositionLevelCount,
decomposition_base_log: DecompositionBaseLog,
ciphertext_modulus_log: usize,
) -> Result<(), Self> {
if decomposition_base_log.0 == 0 {
return Err(Self::NullDecompositionBaseLog);
}
if decomposition_level_count.0 == 0 {
return Err(Self::NullDecompositionLevelCount);
}
if decomposition_level_count.0 * decomposition_base_log.0 > ciphertext_modulus_log {
return Err(Self::DecompositionTooLarge);
}
Ok(())
}
}
pub trait LweKeyswitchKeyGenerationEngine<InputSecretKey, OutputSecretKey, KeyswitchKey>:
AbstractEngine
where
InputSecretKey: LweSecretKeyEntity,
OutputSecretKey: LweSecretKeyEntity,
KeyswitchKey: LweKeyswitchKeyEntity,
{
fn generate_new_lwe_keyswitch_key(
&mut self,
input_key: &InputSecretKey,
output_key: &OutputSecretKey,
decomposition_level_count: DecompositionLevelCount,
decomposition_base_log: DecompositionBaseLog,
noise: Variance,
) -> Result<KeyswitchKey, LweKeyswitchKeyGenerationError<Self::EngineError>>;
unsafe fn generate_new_lwe_keyswitch_key_unchecked(
&mut self,
input_key: &InputSecretKey,
output_key: &OutputSecretKey,
decomposition_level_count: DecompositionLevelCount,
decomposition_base_log: DecompositionBaseLog,
noise: Variance,
) -> KeyswitchKey;
}