Skip to main content

crypto_dispatch/algorithms/
ed25519.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)]
6
7use crate::traits::SignatureAlgorithm;
8use crate::AlgorithmError;
9use crypto_core::Algorithm;
10use zeroize::Zeroizing;
11
12/// Ed25519 signature adapter.
13pub struct Ed25519Algo;
14
15impl SignatureAlgorithm for Ed25519Algo {
16    const ALG: Algorithm = Algorithm::Ed25519;
17
18    fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
19        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
20        {
21            return crate::algorithms::KeypairResultExt::into_algorithm_keypair(
22                crypto_ed25519::generate_ed25519_keypair(),
23                Self::ALG,
24            );
25        }
26
27        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
28        {
29            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
30        }
31    }
32
33    fn sign(secret: &[u8], msg: &[u8]) -> Result<Vec<u8>, AlgorithmError> {
34        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
35        {
36            return crypto_ed25519::sign_ed25519(secret, msg).map_err(AlgorithmError::from);
37        }
38
39        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
40        {
41            let _ = (secret, msg);
42            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
43        }
44    }
45
46    fn verify(public: &[u8], msg: &[u8], sig: &[u8]) -> Result<(), AlgorithmError> {
47        #[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
48        {
49            return crypto_ed25519::verify_ed25519(public, msg, sig)
50                .map_err(|error| crate::algorithms::map_verify_error(Self::ALG, error));
51        }
52
53        #[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
54        {
55            let _ = (public, msg, sig);
56            Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
57        }
58    }
59}