#![allow(clippy::needless_return)]
use crate::traits::SignatureAlgorithm;
use crate::AlgorithmError;
use crypto_core::Algorithm;
use zeroize::Zeroizing;
pub struct MlDsa65Algo;
impl SignatureAlgorithm for MlDsa65Algo {
const ALG: Algorithm = Algorithm::MlDsa65;
fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crate::algorithms::KeypairResultExt::into_algorithm_keypair(
crypto_ml_dsa_65::generate_ml_dsa_65_keypair(),
Self::ALG,
);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
fn sign(secret: &[u8], msg: &[u8]) -> Result<Vec<u8>, AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_ml_dsa_65::sign_ml_dsa_65(secret, msg).map_err(AlgorithmError::from);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (secret, msg);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
fn verify(public: &[u8], msg: &[u8], sig: &[u8]) -> Result<(), AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_ml_dsa_65::verify_ml_dsa_65(public, msg, sig)
.map_err(|error| crate::algorithms::map_verify_error(Self::ALG, error));
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (public, msg, sig);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
}