use crate::rug::Integer;
pub use cggmp21_keygen::security_level::SecurityLevel as KeygenSecurityLevel;
pub const M: usize = 128;
pub trait SecurityLevel: KeygenSecurityLevel {
const EPSILON: usize;
const ELL: usize;
const ELL_PRIME: usize;
const M: usize;
fn q() -> Integer;
}
pub fn max_exponents_size<L: SecurityLevel>() -> (u32, u32) {
use std::cmp;
let x_bits = cmp::max(
L::ELL as u32 + L::EPSILON as u32 + 4 * L::SECURITY_BITS,
(L::ELL_PRIME + L::EPSILON) as _,
);
let y_bits = (L::ELL + L::EPSILON) as u32 + 8 * L::SECURITY_BITS;
(x_bits, y_bits)
}
#[doc(hidden)]
pub mod _internal {
use hex::FromHex;
pub use crate::rug::Integer;
pub use cggmp21_keygen::security_level::{
define_security_level as define_keygen_security_level, SecurityLevel as KeygenSecurityLevel,
};
#[derive(Clone)]
pub struct Rid<const N: usize>([u8; N]);
impl<const N: usize> AsRef<[u8]> for Rid<N> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl<const N: usize> AsMut<[u8]> for Rid<N> {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl<const N: usize> Default for Rid<N> {
fn default() -> Self {
Self([0u8; N])
}
}
impl<const N: usize> FromHex for Rid<N>
where
[u8; N]: FromHex,
{
type Error = <[u8; N] as FromHex>::Error;
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
FromHex::from_hex(hex).map(Self)
}
}
}
#[macro_export]
macro_rules! define_security_level {
($struct_name:ident {
security_bits = $k:expr,
epsilon = $e:expr,
ell = $ell:expr,
ell_prime = $ell_prime:expr,
m = $m:tt,
q = $q:expr,
}) => {
$crate::define_security_level! {
$struct_name {
epsilon = $e,
ell = $ell,
ell_prime = $ell_prime,
m = $m,
q = $q,
}
}
$crate::security_level::_internal::define_keygen_security_level! {
$struct_name {
security_bits = $k,
}
}
};
($struct_name:ident {
epsilon = $e:expr,
ell = $ell:expr,
ell_prime = $ell_prime:expr,
m = 128,
q = $q:expr,
}) => {
impl $crate::security_level::SecurityLevel for $struct_name {
const EPSILON: usize = $e;
const ELL: usize = $ell;
const ELL_PRIME: usize = $ell_prime;
const M: usize = 128;
fn q() -> $crate::security_level::_internal::Integer {
$q
}
}
};
($struct_name:ident {
epsilon = $e:expr,
ell = $ell:expr,
ell_prime = $ell_prime:expr,
m = $m:tt,
q = $q:expr,
}) => {
compile_error!(concat!("Currently, we can not set security parameter M to anything but 128 (you set m=", stringify!($m), ")"));
};
}
#[doc(inline)]
pub use define_security_level;
#[doc(inline)]
pub use cggmp21_keygen::security_level::SecurityLevel128;
define_security_level!(SecurityLevel128{
epsilon = 230,
ell = 256,
ell_prime = 848,
m = 128,
q = (Integer::ONE << 128_u32).into(),
});
pub(crate) fn validate_public_paillier_key_size<L: SecurityLevel>(N: &Integer) -> bool {
N.significant_bits() >= 8 * L::SECURITY_BITS - 1
}
pub(crate) fn validate_secret_paillier_key_size<L: SecurityLevel>(
p: &Integer,
q: &Integer,
) -> bool {
p.significant_bits() >= 4 * L::SECURITY_BITS && q.significant_bits() >= 4 * L::SECURITY_BITS
}