Skip to content

Commit

Permalink
Use umbral_pre::serde_bytes for serialization/deserialization of Sess…
Browse files Browse the repository at this point in the history
…ionStaticKey.
  • Loading branch information
derekpierre committed Jun 21, 2023
1 parent f708092 commit a57aca3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion nucypher-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ zeroize = { version="1.6.0", features = ["derive"] }
rand_core = "0.6.4"
rand_chacha = "0.3.1"
rand = "0.8.5"
serde_bytes = "0.11.9"
21 changes: 12 additions & 9 deletions nucypher-core/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ pub mod session {
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use rand_core::{CryptoRng, OsRng, RngCore};
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes;
use umbral_pre::serde_bytes;
use x25519_dalek::{PublicKey, SharedSecret, StaticSecret};

use crate::secret_box::{kdf, SecretBox};
Expand Down Expand Up @@ -194,12 +193,21 @@ pub mod session {
write!(f, "SessionStaticKey: {}", hex::encode(&self.as_ref()[..8]))
}
}

impl Serialize for SessionStaticKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_bytes::serialize(&self.as_ref(), serializer)
serde_bytes::as_hex::serialize(self.0.as_bytes(), serializer)
}
}

impl serde_bytes::TryFromBytes for SessionStaticKey {
type Error = core::array::TryFromSliceError;
fn try_from_bytes(bytes: &[u8]) -> Result<Self, Self::Error> {
let array: [u8; 32] = bytes.try_into()?;
Ok(SessionStaticKey(PublicKey::from(array)))
}
}

Expand All @@ -208,12 +216,7 @@ pub mod session {
where
D: Deserializer<'a>,
{
let slice: &[u8] = serde_bytes::deserialize(deserializer)?;
let array: [u8; 32] = slice.try_into().map_err(|_| {
let expected = "[u8; 32]";
Error::invalid_length(slice.len(), &expected)
})?;
Ok(Self(PublicKey::from(array)))
serde_bytes::as_hex::deserialize(deserializer)
}
}

Expand Down

0 comments on commit a57aca3

Please sign in to comment.