Skip to main content

phoenix_core/keys/
secret.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use dusk_bytes::{DeserializableSlice, Error, Serializable};
8use dusk_jubjub::{GENERATOR_EXTENDED, JubJubScalar};
9use ff::Field;
10use jubjub_schnorr::SecretKey as NoteSecretKey;
11use rand::{CryptoRng, RngCore};
12#[cfg(feature = "rkyv-impl")]
13use rkyv::{Archive, Deserialize, Serialize};
14use subtle::{Choice, ConstantTimeEq};
15use zeroize::Zeroize;
16
17use crate::StealthAddress;
18use crate::keys::hash;
19
20/// Secret pair of `a` and `b` defining a [`SecretKey`]
21///
22/// ## Safety
23///
24/// To ensure that no secret information lingers in memory after the variable
25/// goes out of scope, we advice calling `zeroize` before the variable goes out
26/// of scope.
27///
28/// ## Examples
29///
30/// Generate a random `SecretKey`:
31/// ```
32/// use phoenix_core::SecretKey;
33/// use rand::rngs::StdRng;
34/// use rand::SeedableRng;
35/// use zeroize::Zeroize;
36///
37/// let mut rng = StdRng::seed_from_u64(12345);
38/// let mut sk = SecretKey::random(&mut rng);
39///
40/// // do something with the sk
41///
42/// sk.zeroize();
43/// ```
44///
45/// # Note
46/// Implementing `ZeroizeOnDrop` seems like an excellent way to lift the burden
47/// of manually zeroizing after use off the user, but unfortunately it doesn't
48/// delete the memory reliably. See #244
49#[derive(Clone, Eq, Debug, Zeroize)]
50#[cfg_attr(
51    feature = "rkyv-impl",
52    derive(Archive, Serialize, Deserialize),
53    archive_attr(derive(bytecheck::CheckBytes))
54)]
55pub struct SecretKey {
56    a: JubJubScalar,
57    b: JubJubScalar,
58}
59
60impl SecretKey {
61    /// This method is used to construct a new `SecretKey` from the given
62    /// secret pair of `a` and `b`.
63    pub fn new(a: JubJubScalar, b: JubJubScalar) -> Self {
64        Self { a, b }
65    }
66
67    /// Gets `a`
68    pub fn a(&self) -> &JubJubScalar {
69        &self.a
70    }
71
72    /// Gets `b`
73    pub fn b(&self) -> &JubJubScalar {
74        &self.b
75    }
76
77    /// Deterministically create a new [`SecretKey`] from a random number
78    /// generator
79    pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
80        let a = JubJubScalar::random(&mut *rng);
81        let b = JubJubScalar::random(&mut *rng);
82
83        SecretKey::new(a, b)
84    }
85
86    /// Generates a [`NoteSecretKey`] using the `R` of the given
87    /// [`StealthAddress`]. With the formula: `note_sk = H(a · R) + b`
88    pub fn gen_note_sk(&self, stealth: &StealthAddress) -> NoteSecretKey {
89        let aR = stealth.R() * self.a;
90
91        NoteSecretKey::from(hash(&aR) + self.b)
92    }
93
94    /// Checks if `note_pk ?= (H(R · a) + b) · G`
95    pub fn owns(&self, stealth_address: &StealthAddress) -> bool {
96        let note_sk = self.gen_note_sk(stealth_address);
97
98        let note_pk = GENERATOR_EXTENDED * note_sk.as_ref();
99
100        stealth_address.note_pk().as_ref() == &note_pk
101    }
102}
103
104impl ConstantTimeEq for SecretKey {
105    fn ct_eq(&self, other: &Self) -> Choice {
106        self.a.ct_eq(&other.a) & self.b.ct_eq(&other.b)
107    }
108}
109
110impl PartialEq for SecretKey {
111    fn eq(&self, other: &Self) -> bool {
112        self.ct_eq(other).into()
113    }
114}
115
116impl Serializable<64> for SecretKey {
117    type Error = Error;
118
119    fn to_bytes(&self) -> [u8; 64] {
120        let mut bytes = [0u8; 64];
121        bytes[..32].copy_from_slice(&self.a.to_bytes());
122        bytes[32..].copy_from_slice(&self.b.to_bytes());
123        bytes
124    }
125
126    fn from_bytes(buf: &[u8; 64]) -> Result<Self, Self::Error> {
127        let a = JubJubScalar::from_slice(&buf[..32])?;
128        let b = JubJubScalar::from_slice(&buf[32..])?;
129
130        Ok(Self { a, b })
131    }
132}