-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathed25519.rs
259 lines (209 loc) · 6.33 KB
/
ed25519.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use core::{
cmp::Ordering,
convert::TryFrom,
hash::{Hash, Hasher},
};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
#[deprecated = "Use associated const SecretKey::LENGTH"]
pub const SECRET_KEY_LENGTH: usize = 32;
#[deprecated = "Use associated const PublicKey::LENGTH"]
pub const PUBLIC_KEY_LENGTH: usize = 32;
#[deprecated = "Use associated const Signature::LENGTH"]
pub const SIGNATURE_LENGTH: usize = 64;
#[derive(Zeroize, ZeroizeOnDrop, Clone)]
pub struct SecretKey(ed25519_zebra::SigningKey);
impl SecretKey {
pub const LENGTH: usize = 32;
#[cfg(feature = "random")]
#[cfg_attr(docsrs, doc(cfg(feature = "random")))]
pub fn generate() -> crate::Result<Self> {
let mut bs = [0u8; SecretKey::LENGTH];
crate::utils::rand::fill(&mut bs)?;
let sk = Self::from_bytes(&bs);
bs.zeroize();
Ok(sk)
}
#[cfg(feature = "rand")]
pub fn generate_with<R: rand::CryptoRng + rand::RngCore>(rng: &mut R) -> Self {
let mut bs = [0_u8; SecretKey::LENGTH];
rng.fill_bytes(&mut bs);
let sk = Self::from_bytes(&bs);
bs.zeroize();
sk
}
pub fn public_key(&self) -> PublicKey {
PublicKey(ed25519_zebra::VerificationKey::from(&self.0))
}
pub fn to_bytes(&self) -> Zeroizing<[u8; SecretKey::LENGTH]> {
Zeroizing::new(self.0.into())
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_ref()
}
pub fn from_bytes(bytes: &[u8; SecretKey::LENGTH]) -> Self {
Self((*bytes).into())
}
pub fn sign(&self, msg: &[u8]) -> Signature {
Signature(self.0.sign(msg))
}
}
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PublicKey(ed25519_zebra::VerificationKey);
impl PublicKey {
pub const LENGTH: usize = 32;
pub fn verify(&self, sig: &Signature, msg: &[u8]) -> bool {
self.0.verify(&sig.0, msg).is_ok()
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_ref()
}
pub fn to_bytes(self) -> [u8; PublicKey::LENGTH] {
self.0.into()
}
pub fn try_from_bytes(bytes: [u8; PublicKey::LENGTH]) -> crate::Result<Self> {
ed25519_zebra::VerificationKey::try_from(bytes)
.map(Self)
.map_err(|_| crate::Error::ConvertError {
from: "compressed bytes",
to: "Ed25519 public key",
})
}
}
impl AsRef<[u8]> for PublicKey {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl TryFrom<[u8; PublicKey::LENGTH]> for PublicKey {
type Error = crate::Error;
fn try_from(bytes: [u8; PublicKey::LENGTH]) -> crate::Result<Self> {
Self::try_from_bytes(bytes)
}
}
impl From<PublicKey> for [u8; PublicKey::LENGTH] {
fn from(pk: PublicKey) -> Self {
pk.to_bytes()
}
}
impl PartialEq for PublicKey {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for PublicKey {}
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PublicKey {
fn cmp(&self, other: &Self) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl Hash for PublicKey {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.as_slice()).hash(state);
}
}
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PublicKeyBytes(ed25519_zebra::VerificationKeyBytes);
impl PublicKeyBytes {
pub const LENGTH: usize = PublicKey::LENGTH;
pub fn verify(&self, sig: &Signature, msg: &[u8]) -> crate::Result<bool> {
Ok(self.into_public_key()?.verify(sig, msg))
}
pub fn into_public_key(self) -> crate::Result<PublicKey> {
self.0
.try_into()
.map_err(|_| crate::Error::ConvertError {
from: "Ed25519 public key bytes",
to: "Ed25519 public key",
})
.map(PublicKey)
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_ref()
}
pub fn to_bytes(self) -> [u8; PublicKey::LENGTH] {
self.0.into()
}
pub fn from_bytes(bytes: [u8; PublicKey::LENGTH]) -> Self {
Self(ed25519_zebra::VerificationKeyBytes::from(bytes))
}
}
impl TryFrom<PublicKeyBytes> for PublicKey {
type Error = crate::Error;
fn try_from(value: PublicKeyBytes) -> Result<Self, Self::Error> {
value.into_public_key()
}
}
impl AsRef<[u8]> for PublicKeyBytes {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl From<[u8; PublicKey::LENGTH]> for PublicKeyBytes {
fn from(bytes: [u8; PublicKey::LENGTH]) -> Self {
Self::from_bytes(bytes)
}
}
impl From<PublicKeyBytes> for [u8; PublicKey::LENGTH] {
fn from(pk: PublicKeyBytes) -> Self {
pk.to_bytes()
}
}
impl PartialEq for PublicKeyBytes {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for PublicKeyBytes {}
impl PartialOrd for PublicKeyBytes {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PublicKeyBytes {
fn cmp(&self, other: &Self) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl Hash for PublicKeyBytes {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.as_slice()).hash(state);
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Signature(ed25519_zebra::Signature);
impl Signature {
pub const LENGTH: usize = 64;
pub fn to_bytes(&self) -> [u8; Signature::LENGTH] {
self.0.into()
}
pub fn from_bytes(bs: [u8; Signature::LENGTH]) -> Self {
Self(ed25519_zebra::Signature::from(bs))
}
}
impl PartialOrd for Signature {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Signature {
fn cmp(&self, other: &Self) -> Ordering {
let r_cmp = self.0.r_bytes().cmp(other.0.r_bytes());
let s_cmp = self.0.s_bytes().cmp(other.0.s_bytes());
r_cmp.then(s_cmp)
}
}
impl Hash for Signature {
fn hash<H: Hasher>(&self, state: &mut H) {
<[u8; 64]>::from(self.0).hash(state);
}
}