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
use crate::traits::Uniform;
use bincode::serialize;
use serde::Serialize;
pub const TEST_SEED: [u8; 32] = [0u8; 32];
#[cfg_attr(test, derive(Clone))]
pub struct KeyPair<S, P>
where
for<'a> P: From<&'a S>,
{
pub private_key: S,
pub public_key: P,
}
impl<S, P> From<S> for KeyPair<S, P>
where
for<'a> P: From<&'a S>,
{
fn from(private_key: S) -> Self {
KeyPair {
public_key: (&private_key).into(),
private_key,
}
}
}
impl<S, P> Uniform for KeyPair<S, P>
where
S: Uniform,
for<'a> P: From<&'a S>,
{
fn generate_for_testing<R>(rng: &mut R) -> Self
where
R: ::rand::SeedableRng + ::rand::RngCore + ::rand::CryptoRng,
{
let private_key = S::generate_for_testing(rng);
private_key.into()
}
}
impl<S, P> Uniform for (S, P)
where
S: Uniform,
for<'a> P: From<&'a S>,
{
fn generate_for_testing<R>(rng: &mut R) -> Self
where
R: ::rand::SeedableRng + ::rand::RngCore + ::rand::CryptoRng,
{
let private_key = S::generate_for_testing(rng);
let public_key = (&private_key).into();
(private_key, public_key)
}
}
impl<Priv, Pub> std::fmt::Debug for KeyPair<Priv, Pub>
where
Priv: Serialize,
Pub: Serialize + for<'a> From<&'a Priv>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut v = serialize(&self.private_key).unwrap();
v.extend(&serialize(&self.public_key).unwrap());
write!(f, "{}", hex::encode(&v[..]))
}
}