use crate::data::traits::{BatchEncryptable, Encryptable, Encrypted};
use crate::transcryptor::batch::BatchError;
use rand_core::{CryptoRng, Rng};
pub fn encrypt_batch<M, R>(
messages: &[M],
public_key: &M::PublicKeyType,
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, BatchError>
where
M: BatchEncryptable,
R: Rng + CryptoRng,
{
let preprocessed = M::preprocess_batch(messages)?;
Ok(preprocessed
.iter()
.map(|x| x.encrypt(public_key, rng))
.collect())
}
#[cfg(feature = "insecure")]
pub fn encrypt_batch_raw<M, R>(
messages: &[M],
public_key: &M::PublicKeyType,
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, BatchError>
where
M: Encryptable,
R: Rng + CryptoRng,
{
Ok(messages
.iter()
.map(|x| x.encrypt(public_key, rng))
.collect())
}
#[cfg(feature = "offline")]
pub fn encrypt_global_batch<M, R>(
messages: &[M],
public_key: &M::GlobalPublicKeyType,
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, BatchError>
where
M: Encryptable,
R: Rng + CryptoRng,
{
Ok(messages
.iter()
.map(|x| x.encrypt_global(public_key, rng))
.collect())
}
#[cfg(feature = "elgamal3")]
pub fn decrypt_batch<E>(
encrypted: &[E],
secret_key: &E::SecretKeyType,
) -> Result<Vec<E::UnencryptedType>, BatchError>
where
E: Encrypted,
{
encrypted
.iter()
.map(|x| {
x.decrypt(secret_key)
.ok_or_else(|| BatchError::InconsistentStructure {
index: 0,
expected_structure: "valid decryption".to_string(),
actual_structure: "decryption failed".to_string(),
})
})
.collect()
}
#[cfg(not(feature = "elgamal3"))]
pub fn decrypt_batch<E>(
encrypted: &[E],
secret_key: &E::SecretKeyType,
) -> Result<Vec<E::UnencryptedType>, BatchError>
where
E: Encrypted,
{
Ok(encrypted.iter().map(|x| x.decrypt(secret_key)).collect())
}
#[cfg(all(feature = "offline", feature = "insecure", feature = "elgamal3"))]
pub fn decrypt_global_batch<E>(
encrypted: &[E],
secret_key: &E::GlobalSecretKeyType,
) -> Result<Vec<E::UnencryptedType>, BatchError>
where
E: Encrypted,
{
encrypted
.iter()
.map(|x| {
x.decrypt_global(secret_key)
.ok_or_else(|| BatchError::InconsistentStructure {
index: 0,
expected_structure: "valid decryption".to_string(),
actual_structure: "decryption failed".to_string(),
})
})
.collect()
}
#[cfg(all(feature = "offline", feature = "insecure", not(feature = "elgamal3")))]
pub fn decrypt_global_batch<E>(
encrypted: &[E],
secret_key: &E::GlobalSecretKeyType,
) -> Result<Vec<E::UnencryptedType>, BatchError>
where
E: Encrypted,
{
Ok(encrypted
.iter()
.map(|x| x.decrypt_global(secret_key))
.collect())
}