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
use crate::{permutation, JubJubScalar, SecretKey, ViewKey};
use super::public::PublicSpendKey;
use super::stealth::StealthAddress;
#[cfg(feature = "canon")]
use canonical_derive::Canon;
use dusk_bytes::{DeserializableSlice, Error, HexDebug, Serializable};
use dusk_jubjub::GENERATOR_EXTENDED;
use rand_core::{CryptoRng, RngCore};
use subtle::{Choice, ConstantTimeEq};
#[derive(Clone, Copy, Eq, HexDebug)]
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct SecretSpendKey {
a: JubJubScalar,
b: JubJubScalar,
}
impl SecretSpendKey {
pub fn new(a: JubJubScalar, b: JubJubScalar) -> Self {
Self { a, b }
}
pub fn a(&self) -> &JubJubScalar {
&self.a
}
pub fn b(&self) -> &JubJubScalar {
&self.b
}
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let a = JubJubScalar::random(rng);
let b = JubJubScalar::random(rng);
SecretSpendKey::new(a, b)
}
pub fn sk_r(&self, sa: &StealthAddress) -> SecretKey {
let aR = sa.R() * self.a;
let aR = permutation::hash(&aR);
SecretKey(aR + self.b)
}
pub fn public_spend_key(&self) -> PublicSpendKey {
let A = GENERATOR_EXTENDED * self.a;
let B = GENERATOR_EXTENDED * self.b;
PublicSpendKey::new(A, B)
}
pub fn view_key(&self) -> ViewKey {
let B = GENERATOR_EXTENDED * self.b;
ViewKey::new(self.a, B)
}
}
impl ConstantTimeEq for SecretSpendKey {
fn ct_eq(&self, other: &Self) -> Choice {
self.a.ct_eq(&other.a) & self.b.ct_eq(&other.b)
}
}
impl PartialEq for SecretSpendKey {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}
impl Serializable<64> for SecretSpendKey {
type Error = Error;
fn to_bytes(&self) -> [u8; 64] {
let mut bytes = [0u8; 64];
bytes[..32].copy_from_slice(&self.a.to_bytes());
bytes[32..].copy_from_slice(&self.b.to_bytes());
bytes
}
fn from_bytes(buf: &[u8; 64]) -> Result<Self, Self::Error> {
let a = JubJubScalar::from_slice(&buf[..32])?;
let b = JubJubScalar::from_slice(&buf[32..])?;
Ok(Self { a, b })
}
}