cardano_serialization_lib/chain_crypto/algorithms/
ed25519_derive.rs

1use crate::chain_crypto::key::{
2    AsymmetricKey, AsymmetricPublicKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic,
3};
4use crate::chain_crypto::sign::{
5    SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm,
6};
7
8use ed25519_bip32 as i;
9use ed25519_bip32::{XPrv, XPub, XPRV_SIZE, XPUB_SIZE};
10use rand_os::rand_core::{CryptoRng, RngCore};
11
12/// Ed25519 BIP32 Signature algorithm
13pub struct Ed25519Bip32;
14
15impl From<i::PrivateKeyError> for SecretKeyError {
16    fn from(v: i::PrivateKeyError) -> Self {
17        match v {
18            i::PrivateKeyError::HighestBitsInvalid => SecretKeyError::StructureInvalid,
19            i::PrivateKeyError::LowestBitsInvalid => SecretKeyError::StructureInvalid,
20            i::PrivateKeyError::LengthInvalid(_) => SecretKeyError::SizeInvalid,
21        }
22    }
23}
24
25impl From<i::PublicKeyError> for PublicKeyError {
26    fn from(v: i::PublicKeyError) -> Self {
27        match v {
28            i::PublicKeyError::LengthInvalid(_) => PublicKeyError::SizeInvalid,
29        }
30    }
31}
32
33impl AsymmetricPublicKey for Ed25519Bip32 {
34    type Public = XPub;
35    const PUBLIC_BECH32_HRP: &'static str = "xpub";
36    const PUBLIC_KEY_SIZE: usize = XPUB_SIZE;
37    fn public_from_binary(data: &[u8]) -> Result<Self::Public, PublicKeyError> {
38        let xpub = XPub::from_slice(data)?;
39        Ok(xpub)
40    }
41}
42
43impl AsymmetricKey for Ed25519Bip32 {
44    type Secret = XPrv;
45    type PubAlg = Ed25519Bip32;
46
47    const SECRET_BECH32_HRP: &'static str = "xprv";
48
49    fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Self::Secret {
50        let mut priv_bytes = [0u8; XPRV_SIZE];
51        rng.fill_bytes(&mut priv_bytes);
52        XPrv::normalize_bytes_force3rd(priv_bytes)
53    }
54
55    fn compute_public(key: &Self::Secret) -> <Self as AsymmetricPublicKey>::Public {
56        key.public()
57    }
58
59    fn secret_from_binary(data: &[u8]) -> Result<Self::Secret, SecretKeyError> {
60        let xprv = XPrv::from_slice_verified(data)?;
61        Ok(xprv)
62    }
63}
64
65impl SecretKeySizeStatic for Ed25519Bip32 {
66    const SECRET_KEY_SIZE: usize = XPRV_SIZE;
67}
68
69impl From<i::SignatureError> for SignatureError {
70    fn from(v: i::SignatureError) -> Self {
71        match v {
72            i::SignatureError::InvalidLength(got) => SignatureError::SizeInvalid {
73                expected: ed25519_bip32::SIGNATURE_SIZE,
74                got: got,
75            },
76        }
77    }
78}
79
80type XSig = ed25519_bip32::Signature<u8>;
81
82impl VerificationAlgorithm for Ed25519Bip32 {
83    type Signature = XSig;
84
85    const SIGNATURE_SIZE: usize = ed25519_bip32::SIGNATURE_SIZE;
86    const SIGNATURE_BECH32_HRP: &'static str = "xsig";
87
88    fn signature_from_bytes(data: &[u8]) -> Result<Self::Signature, SignatureError> {
89        let xsig = XSig::from_slice(data)?;
90        Ok(xsig)
91    }
92
93    fn verify_bytes(
94        pubkey: &Self::Public,
95        signature: &Self::Signature,
96        msg: &[u8],
97    ) -> Verification {
98        pubkey.verify(msg, signature).into()
99    }
100}
101
102impl SigningAlgorithm for Ed25519Bip32 {
103    fn sign(key: &Self::Secret, msg: &[u8]) -> XSig {
104        key.sign(msg)
105    }
106}
107
108#[cfg(test)]
109mod test {
110    use super::*;
111
112    use crate::chain_crypto::key::KeyPair;
113    use crate::chain_crypto::sign::test::{keypair_signing_ko, keypair_signing_ok};
114
115    #[quickcheck]
116    fn sign_ok(input: (KeyPair<Ed25519Bip32>, Vec<u8>)) -> bool {
117        keypair_signing_ok(input)
118    }
119    #[quickcheck]
120    fn sign_ko(input: (KeyPair<Ed25519Bip32>, KeyPair<Ed25519Bip32>, Vec<u8>)) -> bool {
121        keypair_signing_ko(input)
122    }
123}