From 63570e31fd4300835706f2069dba69397ebd24b4 Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Wed, 16 Oct 2024 14:19:11 -0700 Subject: [PATCH] Use serde-encoded-bytes instead of the vendored module --- manul/Cargo.toml | 1 + manul/src/lib.rs | 1 - manul/src/protocol/round.rs | 10 ++++----- manul/src/serde_bytes.rs | 42 ------------------------------------ manul/src/session/session.rs | 12 +++++------ 5 files changed, 10 insertions(+), 56 deletions(-) delete mode 100644 manul/src/serde_bytes.rs diff --git a/manul/Cargo.toml b/manul/Cargo.toml index ae0a23a..adef1de 100644 --- a/manul/Cargo.toml +++ b/manul/Cargo.toml @@ -12,6 +12,7 @@ categories = ["cryptography", "no-std"] [dependencies] serde = { version = "1", default-features = false, features = ["alloc", "serde_derive"] } +serde-encoded-bytes = { version = "0.1", default-features = false, features = ["base64"] } digest = { version = "0.10", default-features = false } signature = { version = "2", default-features = false, features = ["digest", "rand_core"] } rand_core = { version = "0.6.4", default-features = false } diff --git a/manul/src/lib.rs b/manul/src/lib.rs index 8f5c507..1e1c79a 100644 --- a/manul/src/lib.rs +++ b/manul/src/lib.rs @@ -16,6 +16,5 @@ extern crate alloc; pub mod protocol; -mod serde_bytes; pub mod session; pub mod testing; diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index 04d1153..ba7b118 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -10,15 +10,13 @@ use core::{any::Any, fmt::Debug}; use digest::Digest; use rand_core::CryptoRngCore; use serde::{Deserialize, Serialize}; +use serde_encoded_bytes::{Base64, SliceLike}; use super::{ error::{LocalError, RemoteError}, object_safe::{ObjectSafeRound, ObjectSafeRoundWrapper}, }; -use crate::{ - serde_bytes, - session::{EchoRoundError, SessionId}, -}; +use crate::session::{EchoRoundError, SessionId}; /// An error that can be returned from [`Round::receive_message`]. #[derive(Debug)] @@ -383,7 +381,7 @@ pub struct EchoBroadcastError(DeserializationError); /// A serialized direct message. #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DirectMessage(#[serde(with = "serde_bytes")] Box<[u8]>); +pub struct DirectMessage(#[serde(with = "SliceLike::")] Box<[u8]>); impl DirectMessage { /// Creates a new serialized direct message. @@ -412,7 +410,7 @@ impl DirectMessage { /// A serialized echo broadcast. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct EchoBroadcast(#[serde(with = "serde_bytes")] Box<[u8]>); +pub struct EchoBroadcast(#[serde(with = "SliceLike::")] Box<[u8]>); impl EchoBroadcast { /// Creates a new serialized echo broadcast. diff --git a/manul/src/serde_bytes.rs b/manul/src/serde_bytes.rs deleted file mode 100644 index cdc0302..0000000 --- a/manul/src/serde_bytes.rs +++ /dev/null @@ -1,42 +0,0 @@ -//! A simple helper to support serialization of `Box<[u8]>` in `serde`. - -use alloc::boxed::Box; -use core::fmt; - -use serde::{de, Deserializer, Serializer}; - -struct BoxVisitor; - -impl<'de> de::Visitor<'de> for BoxVisitor { - type Value = Box<[u8]>; - - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "a bytestring") - } - - fn visit_bytes(self, v: &[u8]) -> Result - where - E: de::Error, - { - Ok(v.into()) - } -} - -/// A helper function that will serialize a byte array efficiently -/// depending on whether the target format is text or binary based. -pub(crate) fn serialize(obj: &T, serializer: S) -> Result -where - T: AsRef<[u8]>, - S: Serializer, -{ - serializer.serialize_bytes(obj.as_ref()) -} - -/// A helper function that will deserialize from a byte array, -/// matching the format used by [`serde_serialize`]. -pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - deserializer.deserialize_bytes(BoxVisitor) -} diff --git a/manul/src/session/session.rs b/manul/src/session/session.rs index adc8d5c..2ce1c51 100644 --- a/manul/src/session/session.rs +++ b/manul/src/session/session.rs @@ -8,6 +8,7 @@ use core::fmt::Debug; use rand_core::CryptoRngCore; use serde::{Deserialize, Serialize}; +use serde_encoded_bytes::{Base64, SliceLike}; use signature::{DigestVerifier, Keypair, RandomizedDigestSigner}; use tracing::debug; @@ -18,17 +19,14 @@ use super::{ transcript::{SessionOutcome, SessionReport, Transcript}, LocalError, RemoteError, }; -use crate::{ - protocol::{ - Artifact, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, FirstRound, ObjectSafeRound, - ObjectSafeRoundWrapper, Payload, Protocol, ReceiveError, ReceiveErrorType, Round, RoundId, - }, - serde_bytes, +use crate::protocol::{ + Artifact, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, FirstRound, ObjectSafeRound, + ObjectSafeRoundWrapper, Payload, Protocol, ReceiveError, ReceiveErrorType, Round, RoundId, }; /// A session identifier shared between the parties. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash)] -pub struct SessionId(#[serde(with = "serde_bytes")] Box<[u8]>); +pub struct SessionId(#[serde(with = "SliceLike::")] Box<[u8]>); /// A session ID. ///