use libcrux_secrets::U8;
pub trait EcdhArrayref<const RAND_LEN: usize, const SECRET_LEN: usize, const PUBLIC_LEN: usize> {
fn generate_secret(
secret: &mut [U8; SECRET_LEN],
rand: &[U8; RAND_LEN],
) -> Result<(), GenerateSecretError>;
fn secret_to_public(
public: &mut [u8; PUBLIC_LEN],
secret: &[U8; SECRET_LEN],
) -> Result<(), SecretToPublicError>;
fn generate_pair(
public: &mut [u8; PUBLIC_LEN],
secret: &mut [U8; SECRET_LEN],
rand: &[U8; RAND_LEN],
) -> Result<(), GenerateSecretError> {
Self::generate_secret(secret, rand)?;
Self::secret_to_public(public, secret).map_err(|_| GenerateSecretError::Unknown)
}
fn derive_ecdh(
derived: &mut [U8; PUBLIC_LEN],
public: &[u8; PUBLIC_LEN],
secret: &[U8; SECRET_LEN],
) -> Result<(), DeriveError>;
fn validate_secret(secret: &[U8; SECRET_LEN]) -> Result<(), ValidateSecretError>;
}
#[derive(Debug)]
pub enum GenerateSecretError {
InvalidRandomness,
Unknown,
}
#[derive(Debug)]
pub enum SecretToPublicError {
InvalidSecret,
Unknown,
}
#[derive(Debug)]
pub enum DeriveError {
InvalidPublic,
InvalidSecret,
Unknown,
}
#[derive(Debug)]
pub enum ValidateSecretError {
InvalidSecret,
Unknown,
}
impl core::fmt::Display for GenerateSecretError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
GenerateSecretError::InvalidRandomness => {
"error generating secret value with provided randomness"
}
GenerateSecretError::Unknown => "an unknown error occured",
};
f.write_str(text)
}
}
impl core::fmt::Display for SecretToPublicError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
SecretToPublicError::InvalidSecret => "secret value is invalid",
SecretToPublicError::Unknown => "an unknown error occured",
};
f.write_str(text)
}
}
impl core::fmt::Display for DeriveError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
DeriveError::InvalidPublic => "public value is invalid",
DeriveError::InvalidSecret => "secret value is invalid",
DeriveError::Unknown => "an unknown error occured",
};
f.write_str(text)
}
}
impl core::fmt::Display for ValidateSecretError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
ValidateSecretError::InvalidSecret => "secret value is invalid",
ValidateSecretError::Unknown => "an unknown error occured",
};
f.write_str(text)
}
}
#[cfg(feature = "error-in-core")]
mod error_in_core {
impl core::error::Error for super::GenerateSecretError {}
impl core::error::Error for super::SecretToPublicError {}
impl core::error::Error for super::DeriveError {}
impl core::error::Error for super::ValidateSecretError {}
}