forked from iotaledger/crypto.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed25519.rs
134 lines (108 loc) · 3.15 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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use core::{
cmp::Ordering,
convert::TryFrom,
hash::{Hash, Hasher},
};
pub const SECRET_KEY_LENGTH: usize = 32;
pub const PUBLIC_KEY_LENGTH: usize = 32;
pub const SIGNATURE_LENGTH: usize = 64;
pub struct SecretKey(ed25519_zebra::SigningKey);
impl SecretKey {
#[cfg(feature = "random")]
#[cfg_attr(docsrs, doc(cfg(feature = "random")))]
pub fn generate() -> crate::Result<Self> {
let mut bs = [0u8; SECRET_KEY_LENGTH];
crate::utils::rand::fill(&mut bs)?;
Ok(Self::from_bytes(bs))
}
#[cfg(feature = "rand")]
pub fn generate_with<R: rand::CryptoRng + rand::RngCore>(rng: &mut R) -> Self {
let mut bs = [0_u8; SECRET_KEY_LENGTH];
rng.fill_bytes(&mut bs);
Self::from_bytes(bs)
}
pub fn public_key(&self) -> PublicKey {
PublicKey(ed25519_zebra::VerificationKey::from(&self.0))
}
pub fn to_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
self.0.into()
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_ref()
}
pub fn from_bytes(bytes: [u8; SECRET_KEY_LENGTH]) -> Self {
Self(bytes.into())
}
pub fn sign(&self, msg: &[u8]) -> Signature {
Signature(self.0.sign(msg))
}
}
#[derive(Copy, Clone, Debug)]
pub struct PublicKey(ed25519_zebra::VerificationKey);
impl PublicKey {
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; PUBLIC_KEY_LENGTH] {
self.0.into()
}
pub fn try_from_bytes(bytes: [u8; PUBLIC_KEY_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; PUBLIC_KEY_LENGTH]> for PublicKey {
type Error = crate::Error;
fn try_from(bytes: [u8; PUBLIC_KEY_LENGTH]) -> crate::Result<Self> {
Self::try_from_bytes(bytes)
}
}
impl From<PublicKey> for [u8; PUBLIC_KEY_LENGTH] {
fn from(pk: PublicKey) -> Self {
pk.to_bytes()
}
}
impl PartialEq for PublicKey {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl Eq for PublicKey {}
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.as_ref().partial_cmp(other.as_ref())
}
}
impl Ord for PublicKey {
fn cmp(&self, other: &Self) -> Ordering {
self.as_ref().cmp(other.as_ref())
}
}
impl Hash for PublicKey {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.as_slice()).hash(state);
}
}
pub struct Signature(ed25519_zebra::Signature);
impl Signature {
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))
}
}