bc_components/ed25519/
ed25519_public_key.rs

1use crate::{Digest, Error, Reference, ReferenceProvider, Result};
2
3pub const ED25519_PUBLIC_KEY_SIZE: usize = bc_crypto::ED25519_PUBLIC_KEY_SIZE;
4
5/// An Ed25519 public key for verifying digital signatures.
6///
7/// Ed25519 public keys are used to verify signatures created with the
8/// corresponding private key. The Ed25519 signature system provides:
9///
10/// - Fast signature verification
11/// - Small public keys (32 bytes)
12/// - High security with resistance to various attacks
13///
14/// This implementation allows:
15/// - Creating Ed25519 public keys from raw data
16/// - Verifying signatures against messages
17/// - Converting between various formats
18#[derive(Clone, PartialEq, Eq, Hash)]
19pub struct Ed25519PublicKey([u8; ED25519_PUBLIC_KEY_SIZE]);
20
21impl Ed25519PublicKey {
22    /// Restores an Ed25519 public key from an array of bytes.
23    pub const fn from_data(data: [u8; ED25519_PUBLIC_KEY_SIZE]) -> Self {
24        Self(data)
25    }
26
27    pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
28        let data = data.as_ref();
29        if data.len() != ED25519_PUBLIC_KEY_SIZE {
30            return Err(Error::invalid_size(
31                "Ed25519 public key",
32                ED25519_PUBLIC_KEY_SIZE,
33                data.len(),
34            ));
35        }
36        let mut key = [0u8; ED25519_PUBLIC_KEY_SIZE];
37        key.copy_from_slice(data);
38        Ok(Self(key))
39    }
40
41    /// Returns the Ed25519 public key as an array of bytes.
42    pub fn data(&self) -> &[u8; ED25519_PUBLIC_KEY_SIZE] { &self.0 }
43
44    /// Get the Ed25519 public key as a byte slice.
45    pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
46
47    fn hex(&self) -> String { hex::encode(self.data()) }
48
49    pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> {
50        let data = hex::decode(hex.as_ref())?;
51        Self::from_data_ref(data)
52    }
53}
54
55impl Ed25519PublicKey {
56    /// Verifies the given Ed25519 signature for the given message using this
57    /// Ed25519 public key.
58    pub fn verify(
59        &self,
60        signature: &[u8; bc_crypto::ED25519_SIGNATURE_SIZE],
61        message: impl AsRef<[u8]>,
62    ) -> bool {
63        bc_crypto::ed25519_verify(&self.0, message.as_ref(), signature)
64    }
65}
66
67impl AsRef<[u8]> for Ed25519PublicKey {
68    fn as_ref(&self) -> &[u8] { &self.0 }
69}
70
71/// Implements Debug to output the key with a type label.
72impl std::fmt::Debug for Ed25519PublicKey {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        write!(f, "Ed25519PublicKey({})", self.hex())
75    }
76}
77
78/// Implements conversion from an Ed25519PublicKey reference to a byte array
79/// reference.
80impl<'a> From<&'a Ed25519PublicKey> for &'a [u8; ED25519_PUBLIC_KEY_SIZE] {
81    fn from(value: &'a Ed25519PublicKey) -> Self { &value.0 }
82}
83
84/// Implements conversion from a byte array to an Ed25519PublicKey.
85impl From<[u8; ED25519_PUBLIC_KEY_SIZE]> for Ed25519PublicKey {
86    fn from(value: [u8; ED25519_PUBLIC_KEY_SIZE]) -> Self {
87        Self::from_data(value)
88    }
89}
90
91/// Implements conversion from an Ed25519PublicKey reference to a byte slice.
92impl<'a> From<&'a Ed25519PublicKey> for &'a [u8] {
93    fn from(value: &'a Ed25519PublicKey) -> Self { &value.0 }
94}
95
96impl ReferenceProvider for Ed25519PublicKey {
97    fn reference(&self) -> Reference {
98        Reference::from_digest(Digest::from_image(self.data()))
99    }
100}
101
102impl std::fmt::Display for Ed25519PublicKey {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        write!(f, "Ed25519PublicKey({})", self.ref_hex_short())
105    }
106}