Skip to main content

crypto_dispatch/algorithms/
ml_dsa_44.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#![allow(clippy::needless_return)]
6use crate::traits::SignatureAlgorithm;
7use crate::AlgorithmError;
8use crypto_core::Algorithm;
9use zeroize::Zeroizing;
10
11/// ML-DSA-44 signature adapter.
12pub struct MlDsa44Algo;
13
14impl SignatureAlgorithm for MlDsa44Algo {
15    const ALG: Algorithm = Algorithm::MlDsa44;
16
17    fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
18        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
19        {
20            return crate::algorithms::KeypairResultExt::into_algorithm_keypair(
21                crypto_ml_dsa_44::generate_ml_dsa_44_keypair(),
22                Self::ALG,
23            );
24        }
25
26        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
27        {
28            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
29        }
30    }
31
32    fn sign(secret: &[u8], msg: &[u8]) -> Result<Vec<u8>, AlgorithmError> {
33        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
34        {
35            return crypto_ml_dsa_44::sign_ml_dsa_44(secret, msg).map_err(AlgorithmError::from);
36        }
37
38        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
39        {
40            let _ = (secret, msg);
41            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
42        }
43    }
44
45    fn verify(public: &[u8], msg: &[u8], sig: &[u8]) -> Result<(), AlgorithmError> {
46        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
47        {
48            return crypto_ml_dsa_44::verify_ml_dsa_44(public, msg, sig)
49                .map_err(|error| crate::algorithms::map_verify_error(Self::ALG, error));
50        }
51
52        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
53        {
54            let _ = (public, msg, sig);
55            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
56        }
57    }
58}