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
use super::secret::SecretKey;
use crate::{JubJubAffine, JubJubExtended};
use dusk_bytes::{Error, HexDebug, Serializable};
use dusk_jubjub::GENERATOR_EXTENDED;
#[cfg(feature = "canon")]
use canonical_derive::Canon;
#[derive(Copy, Clone, PartialEq, HexDebug)]
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct PublicKey(pub(crate) JubJubExtended);
impl From<&SecretKey> for PublicKey {
fn from(sk: &SecretKey) -> Self {
let public_key = GENERATOR_EXTENDED * sk.0;
PublicKey(public_key)
}
}
impl From<JubJubExtended> for PublicKey {
fn from(p: JubJubExtended) -> PublicKey {
PublicKey(p)
}
}
impl From<&JubJubExtended> for PublicKey {
fn from(p: &JubJubExtended) -> PublicKey {
PublicKey(*p)
}
}
impl AsRef<JubJubExtended> for PublicKey {
fn as_ref(&self) -> &JubJubExtended {
&self.0
}
}
impl Serializable<32> for PublicKey {
type Error = Error;
fn to_bytes(&self) -> [u8; 32] {
JubJubAffine::from(self.0).to_bytes()
}
fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
Ok(Self(JubJubAffine::from_bytes(bytes)?.into()))
}
}