#[cfg(any(feature = "insecure", feature = "offline"))]
use crate::data::traits::Encryptable;
use crate::data::traits::{BatchEncryptable, Encrypted};
use crate::errors::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()
.enumerate()
.map(|(index, x)| {
x.decrypt(secret_key)
.ok_or(BatchError::DecryptionFailed { index })
})
.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()
.enumerate()
.map(|(index, x)| {
x.decrypt_global(secret_key)
.ok_or(BatchError::DecryptionFailed { index })
})
.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())
}