Skip to content

Commit

Permalink
fix: don't print a secret key by default
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed Jul 6, 2022
1 parent 75f9d2c commit a85000c
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/ristretto/ristretto_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
borrow::Borrow,
cmp::Ordering,
fmt,
fmt::Debug,
hash::{Hash, Hasher},
ops::{Add, Mul, Sub},
};
Expand Down Expand Up @@ -50,7 +49,7 @@ use crate::{
/// let _k2 = RistrettoSecretKey::from_hex(&"100000002000000030000000040000000");
/// let _k3 = RistrettoSecretKey::random(&mut rng);
/// ```
#[derive(Eq, Clone, Debug, Default)]
#[derive(Eq, Clone, Default)]
pub struct RistrettoSecretKey(pub(crate) Scalar);

const SCALAR_LENGTH: usize = 32;
Expand Down Expand Up @@ -111,6 +110,37 @@ impl PartialEq for RistrettoSecretKey {
}
}

//---------------------------------- RistrettoSecretKey Debug --------------------------------------------//
impl fmt::Debug for RistrettoSecretKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RistrettoSecretKey(***)")
}
}

pub struct RevealedSecretKey<'a> {
secret: &'a RistrettoSecretKey,
}

impl RistrettoSecretKey {
pub fn reveal(&self) -> RevealedSecretKey<'_> {
RevealedSecretKey { secret: self }
}
}

impl<'a> fmt::Display for RevealedSecretKey<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.secret.to_hex())
}
}

impl<'a> fmt::Debug for RevealedSecretKey<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("RistrettoSecretKey")
.field(&self.secret.to_hex())
.finish()
}
}

//---------------------------------- RistrettoSecretKey Mul / Add / Sub --------------------------------------------//

impl<'a, 'b> Mul<&'b RistrettoPublicKey> for &'a RistrettoSecretKey {
Expand Down Expand Up @@ -790,4 +820,14 @@ mod test {
"3ae035e2663d9c561300cca67743ccdb56ea07ca7dacd8394356c4354b030e0c"
);
}

#[test]
fn visibility_test() {
let key =
RistrettoSecretKey::from_hex("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c").unwrap();
let invisible = format!("{:?}", key);
assert!(!invisible.contains("b5bb"));
let visible = format!("{:?}", key.reveal());
assert!(visible.contains("016cc60cab13b0517dab425b1cf01179df1352f23cd32812f4850b878ae4940c"));
}
}

0 comments on commit a85000c

Please sign in to comment.