bc_components/ed25519/
ed25519_private_key.rs1use bc_rand::{RandomNumberGenerator, SecureRandomNumberGenerator};
2
3use crate::{
4 Digest, Ed25519PublicKey, Error, Reference, ReferenceProvider, Result,
5};
6
7pub const ED25519_PRIVATE_KEY_SIZE: usize = bc_crypto::ED25519_PRIVATE_KEY_SIZE;
8
9#[derive(Clone, PartialEq, Eq, Hash)]
27pub struct Ed25519PrivateKey([u8; ED25519_PRIVATE_KEY_SIZE]);
28
29impl Ed25519PrivateKey {
30 pub fn new() -> Self {
32 let mut rng = SecureRandomNumberGenerator;
33 Self::new_using(&mut rng)
34 }
35
36 pub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self {
39 let mut key = [0u8; ED25519_PRIVATE_KEY_SIZE];
40 rng.fill_random_data(&mut key);
41 Self::from_data(key)
42 }
43
44 pub fn data(&self) -> &[u8; ED25519_PRIVATE_KEY_SIZE] { &self.0 }
46
47 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
49
50 pub const fn from_data(data: [u8; ED25519_PRIVATE_KEY_SIZE]) -> Self {
52 Self(data)
53 }
54
55 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
57 let data = data.as_ref();
58 if data.len() != ED25519_PRIVATE_KEY_SIZE {
59 return Err(Error::invalid_size(
60 "Ed25519 private key",
61 ED25519_PRIVATE_KEY_SIZE,
62 data.len(),
63 ));
64 }
65 let mut arr = [0u8; ED25519_PRIVATE_KEY_SIZE];
66 arr.copy_from_slice(data);
67 Ok(Self::from_data(arr))
68 }
69
70 pub fn derive_from_key_material(key_material: impl AsRef<[u8]>) -> Self {
72 Self::from_data(bc_crypto::derive_signing_private_key(key_material))
73 }
74
75 pub fn hex(&self) -> String { hex::encode(self.data()) }
76
77 pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> {
78 let data = hex::decode(hex.as_ref())?;
79 Self::from_data_ref(data)
80 }
81}
82
83impl Ed25519PrivateKey {
84 pub fn public_key(&self) -> Ed25519PublicKey {
86 bc_crypto::ed25519_public_key_from_private_key(self.into()).into()
87 }
88
89 pub fn sign(
90 &self,
91 message: impl AsRef<[u8]>,
92 ) -> [u8; bc_crypto::ED25519_SIGNATURE_SIZE] {
93 bc_crypto::ed25519_sign(&self.0, message.as_ref())
94 }
95}
96
97impl From<[u8; ED25519_PRIVATE_KEY_SIZE]> for Ed25519PrivateKey {
99 fn from(data: [u8; ED25519_PRIVATE_KEY_SIZE]) -> Self {
100 Self::from_data(data)
101 }
102}
103
104impl AsRef<[u8]> for Ed25519PrivateKey {
107 fn as_ref(&self) -> &[u8] { &self.0 }
108}
109
110impl std::fmt::Debug for Ed25519PrivateKey {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 write!(f, "Ed25519PrivateKey({})", self.hex())
114 }
115}
116
117impl Default for Ed25519PrivateKey {
119 fn default() -> Self { Self::new() }
120}
121
122impl<'a> From<&'a Ed25519PrivateKey> for &'a [u8; ED25519_PRIVATE_KEY_SIZE] {
125 fn from(value: &'a Ed25519PrivateKey) -> Self { &value.0 }
126}
127
128impl<'a> From<&'a Ed25519PrivateKey> for &'a [u8] {
130 fn from(value: &'a Ed25519PrivateKey) -> Self { &value.0 }
131}
132
133impl ReferenceProvider for Ed25519PrivateKey {
134 fn reference(&self) -> Reference {
135 Reference::from_digest(Digest::from_image(self.data()))
136 }
137}
138
139impl std::fmt::Display for Ed25519PrivateKey {
140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141 write!(f, "Ed25519PrivateKey({})", self.ref_hex_short())
142 }
143}