bc_components/ed25519/
ed25519_public_key.rs

1use anyhow::{bail, 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 corresponding
8/// 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            bail!("Invalid Ed25519 public key size");
31        }
32        let mut key = [0u8; ED25519_PUBLIC_KEY_SIZE];
33        key.copy_from_slice(data);
34        Ok(Self(key))
35    }
36
37    /// Returns the Ed25519 public key as an array of bytes.
38    pub fn data(&self) -> &[u8; ED25519_PUBLIC_KEY_SIZE] {
39        &self.0
40    }
41
42    fn hex(&self) -> String {
43        hex::encode(self.data())
44    }
45
46    pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> {
47        let data = hex::decode(hex.as_ref())?;
48        Self::from_data_ref(data)
49    }
50}
51
52impl Ed25519PublicKey {
53    /// Verifies the given Ed25519 signature for the given message using this Ed25519 public key.
54    pub fn verify(&self, signature: &[u8; bc_crypto::ED25519_SIGNATURE_SIZE], message: impl AsRef<[u8]>) -> bool {
55        bc_crypto::ed25519_verify(&self.0, message.as_ref(), signature)
56    }
57}
58
59/// Implements Display to output the key as a hex string.
60impl std::fmt::Display for Ed25519PublicKey {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{}", self.hex())
63    }
64}
65
66/// Implements Debug to output the key with a type label.
67impl std::fmt::Debug for Ed25519PublicKey {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(f, "Ed25519PublicKey({})", self.hex())
70    }
71}
72
73/// Implements conversion from an Ed25519PublicKey reference to a byte array reference.
74impl<'a> From<&'a Ed25519PublicKey> for &'a [u8; ED25519_PUBLIC_KEY_SIZE] {
75    fn from(value: &'a Ed25519PublicKey) -> Self {
76        &value.0
77    }
78}
79
80/// Implements conversion from a byte array to an Ed25519PublicKey.
81impl From<[u8; ED25519_PUBLIC_KEY_SIZE]> for Ed25519PublicKey {
82    fn from(value: [u8; ED25519_PUBLIC_KEY_SIZE]) -> Self {
83        Self::from_data(value)
84    }
85}
86
87/// Implements conversion from an Ed25519PublicKey reference to a byte slice.
88impl<'a> From<&'a Ed25519PublicKey> for &'a [u8] {
89    fn from(value: &'a Ed25519PublicKey) -> Self {
90        &value.0
91    }
92}