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
use crate::JubJubScalar;
use dusk_bytes::{Error, HexDebug, Serializable};
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "canon")]
use canonical_derive::Canon;
#[allow(non_snake_case)]
#[cfg_attr(feature = "canon", derive(Canon))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, HexDebug)]
pub struct SecretKey(pub(crate) JubJubScalar);
impl From<JubJubScalar> for SecretKey {
fn from(s: JubJubScalar) -> SecretKey {
SecretKey(s)
}
}
impl From<&JubJubScalar> for SecretKey {
fn from(s: &JubJubScalar) -> SecretKey {
SecretKey(*s)
}
}
impl AsRef<JubJubScalar> for SecretKey {
fn as_ref(&self) -> &JubJubScalar {
&self.0
}
}
impl SecretKey {
pub fn random<T>(rand: &mut T) -> SecretKey
where
T: RngCore + CryptoRng,
{
let fr = JubJubScalar::random(rand);
SecretKey(fr)
}
}
impl Serializable<32> for SecretKey {
type Error = Error;
fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
Ok(Self(JubJubScalar::from_bytes(bytes)?))
}
}