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
use crate::NoiseError;
use curve25519_dalek::{
constants::{X25519_BASEPOINT, ED25519_BASEPOINT_POINT},
edwards::CompressedEdwardsY,
montgomery::MontgomeryPoint,
scalar::Scalar
};
#[derive(Debug, Clone)]
pub enum Curve25519 {}
#[derive(Debug, Clone)]
pub enum Ed25519 {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublicKey<T> {
bytes: [u8; 32],
_marker: std::marker::PhantomData<T>
}
impl<T> PublicKey<T> {
pub(crate) fn new(bytes: [u8; 32]) -> Self {
PublicKey { bytes, _marker: std::marker::PhantomData }
}
}
impl PublicKey<Ed25519> {
pub fn from_core(key: &libp2p_core::PublicKey) -> Result<PublicKey<Ed25519>, NoiseError> {
if let libp2p_core::PublicKey::Ed25519(k) = key {
if k.len() == 32 {
let cp = CompressedEdwardsY::from_slice(k);
cp.decompress().ok_or(NoiseError::InvalidKey)?;
return Ok(PublicKey::new(cp.0))
}
}
Err(NoiseError::InvalidKey)
}
pub fn into_curve_25519(self) -> PublicKey<Curve25519> {
let m = CompressedEdwardsY(self.bytes)
.decompress()
.expect("Constructing a PublicKey<Ed25519> ensures this is a valid y-coordinate.")
.to_montgomery();
PublicKey::new(m.0)
}
}
impl<T> AsRef<[u8]> for PublicKey<T> {
fn as_ref(&self) -> &[u8] {
&self.bytes
}
}
#[derive(Clone)]
pub struct SecretKey {
scalar: Scalar
}
impl SecretKey {
pub(crate) fn new(bytes: [u8; 32]) -> Self {
SecretKey {
scalar: Scalar::from_bytes_mod_order(bytes)
}
}
}
impl SecretKey {
pub fn public(&self) -> PublicKey<Curve25519> {
PublicKey::new(self.x25519(&X25519_BASEPOINT).0)
}
pub fn ecdh(&self, pk: &PublicKey<Curve25519>) -> [u8; 32] {
self.x25519(&MontgomeryPoint(pk.bytes)).0
}
fn x25519(&self, p: &MontgomeryPoint) -> MontgomeryPoint {
let mut s = self.scalar.to_bytes();
s[0] &= 248;
s[31] &= 127;
s[31] |= 64;
Scalar::from_bits(s) * p
}
}
impl AsRef<[u8]> for SecretKey {
fn as_ref(&self) -> &[u8] {
self.scalar.as_bytes()
}
}
#[derive(Clone)]
pub struct Keypair<T> {
secret: SecretKey,
public: PublicKey<T>
}
impl<T> Keypair<T> {
pub fn new(s: SecretKey, p: PublicKey<T>) -> Self {
Keypair { secret: s, public: p }
}
pub fn secret(&self) -> &SecretKey {
&self.secret
}
pub fn public(&self) -> &PublicKey<T> {
&self.public
}
}
impl<T> Into<(SecretKey, PublicKey<T>)> for Keypair<T> {
fn into(self) -> (SecretKey, PublicKey<T>) {
(self.secret, self.public)
}
}
impl Keypair<Curve25519> {
pub fn gen_curve25519() -> Self {
let secret = SecretKey {
scalar: Scalar::random(&mut rand::thread_rng())
};
let public = secret.public();
Keypair { secret, public }
}
}
impl Keypair<Ed25519> {
pub fn gen_ed25519() -> Self {
let scalar = Scalar::random(&mut rand::thread_rng());
let public = PublicKey::new((scalar * ED25519_BASEPOINT_POINT).compress().0);
let secret = SecretKey { scalar };
Keypair { secret, public }
}
}