crypto_dispatch/algorithms/
secp256k1.rs1#![allow(clippy::needless_return)]
6use crate::traits::SignatureAlgorithm;
7use crate::AlgorithmError;
8use crypto_core::Algorithm;
9use zeroize::Zeroizing;
10
11pub struct Secp256k1Algo;
13
14impl SignatureAlgorithm for Secp256k1Algo {
15 const ALG: Algorithm = Algorithm::Secp256k1;
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_secp256k1::generate_secp256k1_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_secp256k1::sign_secp256k1(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_secp256k1::verify_secp256k1(sig, msg, public)
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}