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
use crate::key;
use rand_core::{CryptoRng, RngCore};
use std::hash::Hash;
use std::marker::PhantomData;

pub struct SecretKey<R, Algorithm: key::AsymmetricKey> {
    inner: key::SecretKey<Algorithm>,
    marker: PhantomData<R>,
}

pub struct PublicKey<R, Algorithm: key::AsymmetricPublicKey> {
    inner: key::PublicKey<Algorithm>,
    marker: PhantomData<R>,
}

impl<R, Algorithm: key::AsymmetricKey> SecretKey<R, Algorithm> {
    pub fn role(sk: key::SecretKey<Algorithm>) -> Self {
        SecretKey {
            inner: sk,
            marker: PhantomData,
        }
    }

    pub fn unrole(self) -> key::SecretKey<Algorithm> {
        self.inner
    }

    pub fn generate<T: RngCore + CryptoRng>(rng: T) -> Self {
        Self::role(key::SecretKey::generate(rng))
    }

    pub fn to_public(&self) -> PublicKey<R, Algorithm::PubAlg> {
        PublicKey::role(self.inner.to_public())
    }
}

impl<R, Algorithm: key::AsymmetricPublicKey> PublicKey<R, Algorithm> {
    pub fn role(pk: key::PublicKey<Algorithm>) -> Self {
        PublicKey {
            inner: pk,
            marker: PhantomData,
        }
    }

    pub fn unrole(self) -> key::PublicKey<Algorithm> {
        self.inner
    }
}

impl<R, A: key::AsymmetricKey> Clone for SecretKey<R, A> {
    fn clone(&self) -> Self {
        SecretKey {
            inner: self.inner.clone(),
            marker: self.marker,
        }
    }
}

impl<R, A: key::AsymmetricPublicKey> Clone for PublicKey<R, A> {
    fn clone(&self) -> Self {
        PublicKey {
            inner: self.inner.clone(),
            marker: self.marker,
        }
    }
}

impl<R, A: key::AsymmetricPublicKey> std::cmp::PartialEq<Self> for PublicKey<R, A> {
    fn eq(&self, other: &Self) -> bool {
        self.inner.eq(&other.inner)
    }
}

impl<R, A: key::AsymmetricPublicKey> std::cmp::Eq for PublicKey<R, A> {}

impl<R, A: key::AsymmetricPublicKey> std::cmp::PartialOrd<Self> for PublicKey<R, A> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&other.inner)
    }
}

impl<R, A: key::AsymmetricPublicKey> std::cmp::Ord for PublicKey<R, A> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<R, A: key::AsymmetricPublicKey> Hash for PublicKey<R, A> {
    fn hash<H>(&self, state: &mut H)
    where
        H: std::hash::Hasher,
    {
        self.inner.hash(state)
    }
}