bc_components/ec_key/
ec_public_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use anyhow::{bail, Result};
use bc_crypto::ECDSA_SIGNATURE_SIZE;
use bc_ur::prelude::*;

use crate::{ECKeyBase, ECKey, ECPublicKeyBase, tags};

pub const ECDSA_PUBLIC_KEY_SIZE: usize = bc_crypto::ECDSA_PUBLIC_KEY_SIZE;

/// A compressed elliptic curve digital signature algorithm (ECDSA) compressed public key.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ECPublicKey([u8; ECDSA_PUBLIC_KEY_SIZE]);

impl ECPublicKey {
    /// Restores an ECDSA public key from an array of bytes.
    pub const fn from_data(data: [u8; ECDSA_PUBLIC_KEY_SIZE]) -> Self {
        Self(data)
    }

    /// Returns the ECDSA public key as an array of bytes.
    pub fn data(&self) -> &[u8; ECDSA_PUBLIC_KEY_SIZE] {
        &self.0
    }
}

impl ECPublicKey {
    /// Verifies the given ECDSA signature for the given message using this ECDSA public key.
    pub fn verify(&self, signature: &[u8; ECDSA_SIGNATURE_SIZE], message: impl AsRef<[u8]>) -> bool {
        bc_crypto::ecdsa_verify(&self.0, signature, message)
    }
}

impl std::fmt::Display for ECPublicKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.hex())
    }
}

impl std::fmt::Debug for ECPublicKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ECPublicKey({})", self.hex())
    }
}

impl ECKeyBase for ECPublicKey {
    const KEY_SIZE: usize = bc_crypto::ECDSA_PUBLIC_KEY_SIZE;

    fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> where Self: Sized {
        let data = data.as_ref();
        if data.len() != ECDSA_PUBLIC_KEY_SIZE {
            bail!("Invalid ECDSA public key size");
        }
        let mut key = [0u8; ECDSA_PUBLIC_KEY_SIZE];
        key.copy_from_slice(data);
        Ok(Self(key))
    }

    fn data(&self) -> &[u8] {
        self.into()
    }
}

impl ECKey for ECPublicKey {
    fn public_key(&self) -> ECPublicKey {
        self.clone()
    }
}

impl ECPublicKeyBase for ECPublicKey {
    fn uncompressed_public_key(&self) -> crate::ECUncompressedPublicKey {
        bc_crypto::ecdsa_decompress_public_key(&self.0).into()
    }
}

impl<'a> From<&'a ECPublicKey> for &'a [u8; ECPublicKey::KEY_SIZE] {
    fn from(value: &'a ECPublicKey) -> Self {
        &value.0
    }
}

impl From<[u8; ECDSA_PUBLIC_KEY_SIZE]> for ECPublicKey {
    fn from(value: [u8; ECDSA_PUBLIC_KEY_SIZE]) -> Self {
        Self::from_data(value)
    }
}

impl<'a> From<&'a ECPublicKey> for &'a [u8] {
    fn from(value: &'a ECPublicKey) -> Self {
        &value.0
    }
}

impl CBORTagged for ECPublicKey {
    fn cbor_tags() -> Vec<Tag> {
        tags_for_values(&[tags::TAG_EC_KEY, tags::TAG_EC_KEY_V1])
    }
}

impl From<ECPublicKey> for CBOR {
    fn from(value: ECPublicKey) -> Self {
        value.tagged_cbor()
    }
}

impl CBORTaggedEncodable for ECPublicKey {
    fn untagged_cbor(&self) -> CBOR {
        let mut m = Map::new();
        m.insert(3, CBOR::to_byte_string(self.0));
        m.into()
    }
}