From 83817ce0ce1714cb692d1f005270ad1b93e1fa7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= <lasse.moeldrup@gmail.com> Date: Wed, 29 Nov 2023 14:50:27 +0100 Subject: [PATCH 1/4] Add FromStr and Display impls for PRF keys --- .../src/dodis_yampolskiy_prf/secret.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs b/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs index 888c1a779..25d55f37f 100644 --- a/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs +++ b/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs @@ -74,6 +74,30 @@ impl<C: Curve> SecretKey<C> { } } +impl<C: Curve> std::fmt::Display for SecretKey<C> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", base16_encode_string(self)) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum SecretKeyFromStrError { + #[error("Invalid secret key: {0}")] + InvalidKey(#[from] anyhow::Error), + #[error("Invalid hex string: {0}")] + InvalidHex(#[from] hex::FromHexError), +} + +impl<C: Curve> std::str::FromStr for SecretKey<C> { + type Err = SecretKeyFromStrError; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + let mut bytes = std::io::Cursor::new(hex::decode(s)?); + let key = from_bytes(&mut bytes)?; + Ok(key) + } +} + #[cfg(test)] mod tests { use super::*; From 7e0fa06cca969f7c50d9b5359fc70fcf1a3e1433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= <lasse.moeldrup@gmail.com> Date: Wed, 29 Nov 2023 14:53:07 +0100 Subject: [PATCH 2/4] Update CHANGELOG.md --- rust-src/concordium_base/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-src/concordium_base/CHANGELOG.md b/rust-src/concordium_base/CHANGELOG.md index 4e7a061d1..dcac97480 100644 --- a/rust-src/concordium_base/CHANGELOG.md +++ b/rust-src/concordium_base/CHANGELOG.md @@ -4,6 +4,7 @@ - Support `P7` protocol version. - The `Debug` implementation for `ContractEvent` displays the value in `hex`. The alternate formatter (using `#`) displays it as a list of bytes. +- Add `FromStr` and `Display` instances to `dodis_yampolskiy_prf::SecretKey`. ## 3.2.0 (2023-11-22) @@ -30,7 +31,6 @@ The representable timestamps are between `-262144-01-01T00:00:00Z` and `+262143-12-31T23:59:59.999Z` - ## 3.0.0 (2023-08-21) - Remove the constant `MAX_ALLOWED_INVOKE_ENERGY` since it was no longer From afe6326456c787181077983c79b3c6320fd644f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= <lasse.moeldrup@gmail.com> Date: Wed, 29 Nov 2023 14:56:30 +0100 Subject: [PATCH 3/4] Undo unnecessary format change --- rust-src/concordium_base/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/rust-src/concordium_base/CHANGELOG.md b/rust-src/concordium_base/CHANGELOG.md index dcac97480..c8268d6ce 100644 --- a/rust-src/concordium_base/CHANGELOG.md +++ b/rust-src/concordium_base/CHANGELOG.md @@ -31,6 +31,7 @@ The representable timestamps are between `-262144-01-01T00:00:00Z` and `+262143-12-31T23:59:59.999Z` + ## 3.0.0 (2023-08-21) - Remove the constant `MAX_ALLOWED_INVOKE_ENERGY` since it was no longer From f3bead279853a647dfa5ab18a9f49a42cce255ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Bizjak?= <ab@concordium.com> Date: Tue, 2 Jan 2024 09:59:06 +0100 Subject: [PATCH 4/4] Omit emitting secret key in debug and display. --- rust-src/concordium_base/CHANGELOG.md | 1 + .../concordium_base/src/dodis_yampolskiy_prf/secret.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rust-src/concordium_base/CHANGELOG.md b/rust-src/concordium_base/CHANGELOG.md index c8268d6ce..ab3342b2b 100644 --- a/rust-src/concordium_base/CHANGELOG.md +++ b/rust-src/concordium_base/CHANGELOG.md @@ -5,6 +5,7 @@ - The `Debug` implementation for `ContractEvent` displays the value in `hex`. The alternate formatter (using `#`) displays it as a list of bytes. - Add `FromStr` and `Display` instances to `dodis_yampolskiy_prf::SecretKey`. +- Change `Debug` instance of `dodis_yampolskiy_prf::SecretKey` to hide the value. ## 3.2.0 (2023-11-22) diff --git a/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs b/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs index 25d55f37f..9d137c6dd 100644 --- a/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs +++ b/rust-src/concordium_base/src/dodis_yampolskiy_prf/secret.rs @@ -10,7 +10,7 @@ use rand::*; use std::rc::Rc; /// A PRF key. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, SerdeBase16Serialize)] +#[derive(Clone, PartialEq, Eq, Serialize, SerdeBase16Serialize)] pub struct SecretKey<C: Curve>(Rc<Secret<C::Scalar>>); /// This trait allows automatic conversion of `&SecretKey<C>` to `&C::Scalar`. @@ -76,7 +76,13 @@ impl<C: Curve> SecretKey<C> { impl<C: Curve> std::fmt::Display for SecretKey<C> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", base16_encode_string(self)) + write!(f, "<Dodis Yampolskiy SecretKey>") + } +} + +impl<C: Curve> std::fmt::Debug for SecretKey<C> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "<Dodis Yampolskiy SecretKey>") } }