blueprint_crypto_ed25519/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
2
3pub mod error;
4use error::{Ed25519Error, Result};
5
6#[cfg(test)]
7mod tests;
8
9use blueprint_crypto_core::BytesEncoding;
10use blueprint_crypto_core::{KeyType, KeyTypeId};
11use blueprint_std::{
12 hash::Hash,
13 string::{String, ToString},
14 vec::Vec,
15};
16use serde::{Deserialize, Serialize};
17
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
20pub struct Ed25519Zebra;
21
22macro_rules! impl_zebra_serde {
23 ($name:ident, $inner:ty) => {
24 #[derive(Clone)]
25 pub struct $name(pub $inner);
26
27 impl PartialEq for $name {
28 fn eq(&self, other: &Self) -> bool {
29 self.to_bytes() == other.to_bytes()
30 }
31 }
32
33 impl Eq for $name {}
34
35 impl PartialOrd for $name {
36 fn partial_cmp(&self, other: &Self) -> Option<blueprint_std::cmp::Ordering> {
37 Some(self.cmp(other))
38 }
39 }
40
41 impl Hash for $name {
42 fn hash<H: blueprint_std::hash::Hasher>(&self, state: &mut H) {
43 self.to_bytes().hash(state);
44 }
45 }
46
47 impl Ord for $name {
48 fn cmp(&self, other: &Self) -> blueprint_std::cmp::Ordering {
49 self.to_bytes().cmp(&other.to_bytes())
50 }
51 }
52
53 impl blueprint_std::fmt::Debug for $name {
54 fn fmt(&self, f: &mut blueprint_std::fmt::Formatter<'_>) -> blueprint_std::fmt::Result {
55 write!(f, "{:?}", self.to_bytes())
56 }
57 }
58
59 impl BytesEncoding for $name {
60 fn to_bytes(&self) -> Vec<u8> {
61 self.to_bytes_impl()
62 }
63
64 fn from_bytes(bytes: &[u8]) -> core::result::Result<Self, serde::de::value::Error> {
65 Self::from_bytes_impl(bytes).map_err(|e| serde::de::Error::custom(e.to_string()))
66 }
67 }
68
69 impl serde::Serialize for $name {
70 fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
71 where
72 S: serde::Serializer,
73 {
74 let bytes = self.to_bytes();
76 Vec::serialize(&bytes, serializer)
77 }
78 }
79
80 impl<'de> serde::Deserialize<'de> for $name {
81 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
82 where
83 D: serde::Deserializer<'de>,
84 {
85 let bytes = <Vec<u8>>::deserialize(deserializer)?;
87
88 let inner = <$inner>::try_from(bytes.as_slice())
90 .map_err(|e| serde::de::Error::custom(e.to_string()))?;
91
92 Ok($name(inner))
93 }
94 }
95 };
96}
97
98impl_zebra_serde!(Ed25519SigningKey, ed25519_zebra::SigningKey);
99
100impl Ed25519SigningKey {
101 fn to_bytes_impl(&self) -> Vec<u8> {
102 self.0.as_ref().to_vec()
103 }
104
105 pub fn from_bytes_impl(bytes: &[u8]) -> Result<Self> {
106 let inner = ed25519_zebra::SigningKey::try_from(bytes)
107 .map_err(|e| Ed25519Error::InvalidSigner(e.to_string()))?;
108 Ok(Ed25519SigningKey(inner))
109 }
110}
111
112impl_zebra_serde!(Ed25519VerificationKey, ed25519_zebra::VerificationKey);
113
114impl Ed25519VerificationKey {
115 fn to_bytes_impl(&self) -> Vec<u8> {
116 self.0.as_ref().to_vec()
117 }
118
119 pub fn from_bytes_impl(bytes: &[u8]) -> Result<Self> {
120 let inner = ed25519_zebra::VerificationKey::try_from(bytes)
121 .map_err(|e| Ed25519Error::InvalidVerifyingKey(e.to_string()))?;
122 Ok(Ed25519VerificationKey(inner))
123 }
124}
125
126impl_zebra_serde!(Ed25519Signature, ed25519_zebra::Signature);
127
128impl Ed25519Signature {
129 fn to_bytes_impl(&self) -> Vec<u8> {
130 self.0.to_bytes().to_vec()
131 }
132
133 fn from_bytes_impl(bytes: &[u8]) -> Result<Self> {
134 let inner = ed25519_zebra::Signature::try_from(bytes)
135 .map_err(|e| Ed25519Error::InvalidSignature(e.to_string()))?;
136 Ok(Ed25519Signature(inner))
137 }
138}
139
140impl KeyType for Ed25519Zebra {
141 type Public = Ed25519VerificationKey;
142 type Secret = Ed25519SigningKey;
143 type Signature = Ed25519Signature;
144 type Error = Ed25519Error;
145
146 fn key_type_id() -> KeyTypeId {
147 KeyTypeId::Ed25519
148 }
149
150 fn generate_with_seed(seed: Option<&[u8]>) -> Result<Self::Secret> {
151 if let Some(seed) = seed {
152 let mut seed_bytes = [0u8; 32];
153 let len = seed.len().min(32);
154 seed_bytes[..len].copy_from_slice(&seed[..len]);
155 let seed = seed_bytes;
156 Ok(Ed25519SigningKey(ed25519_zebra::SigningKey::from(seed)))
157 } else {
158 let mut rng = Self::get_rng();
159 Ok(Ed25519SigningKey(ed25519_zebra::SigningKey::new(&mut rng)))
160 }
161 }
162
163 fn generate_with_string(secret: String) -> Result<Self::Secret> {
164 let hex_encoded = hex::decode(secret)?;
165 let secret = ed25519_zebra::SigningKey::try_from(hex_encoded.as_slice())
166 .map_err(|e| Ed25519Error::InvalidSeed(e.to_string()))?;
167 Ok(Ed25519SigningKey(secret))
168 }
169
170 fn public_from_secret(secret: &Self::Secret) -> Self::Public {
171 Ed25519VerificationKey((&secret.0).into())
172 }
173
174 fn sign_with_secret(secret: &mut Self::Secret, msg: &[u8]) -> Result<Self::Signature> {
175 Ok(Ed25519Signature(secret.0.sign(msg)))
176 }
177
178 fn sign_with_secret_pre_hashed(
179 secret: &mut Self::Secret,
180 msg: &[u8; 32],
181 ) -> Result<Self::Signature> {
182 Ok(Ed25519Signature(secret.0.sign(msg)))
183 }
184
185 fn verify(public: &Self::Public, msg: &[u8], signature: &Self::Signature) -> bool {
186 public.0.verify(&signature.0, msg).is_ok()
187 }
188}