From 43da1e73d5ea75c9cbd44264b4b5906cd365f79f Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Fri, 4 Oct 2024 14:07:33 -0700 Subject: [PATCH 1/5] make bincode optional --- Cargo.toml | 2 +- plexi_core/Cargo.toml | 7 ++++--- plexi_core/src/lib.rs | 44 +++++++++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fba1d8d..4781658 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ license = "Apache-2.0" [workspace.dependencies] akd = { version = "0.11", default-features = false } anyhow = "1.0" -bincode = "2.0.0-rc.3" +# bincode = "2.0.0-rc.3" clap = { version = "4.5", features = ["derive"] } clap-verbosity-flag = "2.2.0" colored = "2.1" diff --git a/plexi_core/Cargo.toml b/plexi_core/Cargo.toml index e41de4b..9b0b49d 100644 --- a/plexi_core/Cargo.toml +++ b/plexi_core/Cargo.toml @@ -15,13 +15,14 @@ build = "src/build.rs" [features] default = ["openapi"] auditor = ["akd", "akd/parallel_vrf", "akd/parallel_insert", "akd/experimental"] -client = ["auditor", "reqwest"] +client = ["auditor", "reqwest", "bincode"] openapi = ["utoipa"] +bincode = ["dep:bincode"] [dependencies] akd = { workspace = true, features = ["whatsapp_v1", "public_auditing"], optional = true } anyhow = { workspace = true } -bincode = { workspace = true } +bincode = { version = "2.0.0-rc.3", optional = true } ed25519-dalek = { workspace = true } hex = { workspace = true, features = ["serde"] } prost = { workspace = true } @@ -39,4 +40,4 @@ ed25519-dalek = { workspace = true, features = ["rand_core"] } getrandom = { workspace = true, features = ["js"] } [build-dependencies] -prost-build = { version = "0.13" } \ No newline at end of file +prost-build = { version = "0.13" } diff --git a/plexi_core/src/lib.rs b/plexi_core/src/lib.rs index cdab5ae..176d089 100644 --- a/plexi_core/src/lib.rs +++ b/plexi_core/src/lib.rs @@ -7,6 +7,7 @@ use std::{ }; use anyhow::anyhow; +#[cfg(feature = "bincode")] use bincode::{BorrowDecode, Decode, Encode}; use ed25519_dalek::SIGNATURE_LENGTH; use prost::Message; @@ -24,11 +25,6 @@ pub mod crypto; pub mod namespaces; pub mod proto; -const SIGNATURE_VERSIONS: [SignatureVersion; 2] = [ - SignatureVersion::ProtobufEd25519, - SignatureVersion::BincodeEd25519, -]; - #[derive(Error, Debug)] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub enum PlexiError { @@ -49,6 +45,7 @@ pub enum PlexiError { #[repr(u32)] pub enum SignatureVersion { ProtobufEd25519 = 0x0001, + #[cfg(feature = "bincode")] BincodeEd25519 = 0x0002, Unknown(u32), } @@ -57,6 +54,7 @@ impl From for u32 { fn from(val: SignatureVersion) -> Self { match val { SignatureVersion::ProtobufEd25519 => 0x0001, + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => 0x0002, SignatureVersion::Unknown(u) => u, } @@ -67,6 +65,7 @@ impl From for SignatureVersion { fn from(u: u32) -> Self { match u { 0x0001 => Self::ProtobufEd25519, + #[cfg(feature = "bincode")] 0x0002 => Self::BincodeEd25519, _ => Self::Unknown(u), } @@ -86,6 +85,7 @@ impl fmt::Display for SignatureVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { Self::ProtobufEd25519 => "0x0001", + #[cfg(feature = "bincode")] Self::BincodeEd25519 => "0x0002", Self::Unknown(_u) => "unknown", }; @@ -93,6 +93,7 @@ impl fmt::Display for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Encode for SignatureVersion { fn encode( &self, @@ -103,6 +104,7 @@ impl Encode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Decode for SignatureVersion { fn decode( decoder: &mut D, @@ -112,6 +114,7 @@ impl Decode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl<'de> BorrowDecode<'de> for SignatureVersion { fn borrow_decode>( buffer: &mut B, @@ -121,7 +124,8 @@ impl<'de> BorrowDecode<'de> for SignatureVersion { } } -#[derive(Clone, Copy, Debug, Serialize, Deserialize, Encode, Decode)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub struct Epoch(u64); @@ -223,8 +227,10 @@ impl Sub for Epoch { } } -#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)] + +#[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "openapi", derive(ToSchema))] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] pub struct SignatureMessage { version: SignatureVersion, namespace: String, @@ -243,16 +249,17 @@ impl SignatureMessage { epoch: &Epoch, digest: Vec, ) -> Result { - if !SIGNATURE_VERSIONS.contains(version) { - return Err(PlexiError::BadParameter("version".to_string())); + match version { + SignatureVersion::Unknown(_) => Err(PlexiError::BadParameter("version".to_string())), + _ => + Ok(Self { + version: *version, + namespace, + timestamp, + epoch: *epoch, + digest, + }) } - Ok(Self { - version: *version, - namespace, - timestamp, - epoch: *epoch, - digest, - }) } pub fn version(&self) -> &SignatureVersion { @@ -275,6 +282,7 @@ impl SignatureMessage { self.digest.clone() } + #[cfg(feature = "bincode")] fn to_vec_bincode(&self) -> Result, PlexiError> { bincode::encode_to_vec(self, bincode::config::legacy()) .map_err(|_e| PlexiError::Serialization) @@ -297,6 +305,7 @@ impl SignatureMessage { pub fn to_vec(&self) -> Result, PlexiError> { match self.version { SignatureVersion::ProtobufEd25519 => self.to_vec_proto(), + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => self.to_vec_bincode(), _ => Err(PlexiError::Serialization), } @@ -463,6 +472,7 @@ impl SignatureResponse { pub fn verify(&self, verifying_key: &[u8]) -> anyhow::Result<()> { // at the time of writting, all version use ed25519 keys. this simplify parsing of the verifying key match self.version { + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => (), SignatureVersion::ProtobufEd25519 => (), SignatureVersion::Unknown(_) => { @@ -677,6 +687,8 @@ mod tests { let key_id = ed25519_public_key_to_key_id(&verifying_key.to_bytes()); assert_eq!(key_id, tv.key_id); + println!("signature version: {}", tv.signature_version); + let message = SignatureMessage::new( &tv.signature_version, tv.namespace, From c9f183e56cbd66f92d60487979f0e8ccab39666c Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Fri, 4 Oct 2024 14:15:13 -0700 Subject: [PATCH 2/5] make bincode optional for plexi_core --- Cargo.toml | 1 - plexi_core/Cargo.toml | 7 ++++--- plexi_core/src/lib.rs | 43 +++++++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fba1d8d..ed19aba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ license = "Apache-2.0" [workspace.dependencies] akd = { version = "0.11", default-features = false } anyhow = "1.0" -bincode = "2.0.0-rc.3" clap = { version = "4.5", features = ["derive"] } clap-verbosity-flag = "2.2.0" colored = "2.1" diff --git a/plexi_core/Cargo.toml b/plexi_core/Cargo.toml index e41de4b..9b0b49d 100644 --- a/plexi_core/Cargo.toml +++ b/plexi_core/Cargo.toml @@ -15,13 +15,14 @@ build = "src/build.rs" [features] default = ["openapi"] auditor = ["akd", "akd/parallel_vrf", "akd/parallel_insert", "akd/experimental"] -client = ["auditor", "reqwest"] +client = ["auditor", "reqwest", "bincode"] openapi = ["utoipa"] +bincode = ["dep:bincode"] [dependencies] akd = { workspace = true, features = ["whatsapp_v1", "public_auditing"], optional = true } anyhow = { workspace = true } -bincode = { workspace = true } +bincode = { version = "2.0.0-rc.3", optional = true } ed25519-dalek = { workspace = true } hex = { workspace = true, features = ["serde"] } prost = { workspace = true } @@ -39,4 +40,4 @@ ed25519-dalek = { workspace = true, features = ["rand_core"] } getrandom = { workspace = true, features = ["js"] } [build-dependencies] -prost-build = { version = "0.13" } \ No newline at end of file +prost-build = { version = "0.13" } diff --git a/plexi_core/src/lib.rs b/plexi_core/src/lib.rs index cdab5ae..8de90f1 100644 --- a/plexi_core/src/lib.rs +++ b/plexi_core/src/lib.rs @@ -7,6 +7,7 @@ use std::{ }; use anyhow::anyhow; +#[cfg(feature = "bincode")] use bincode::{BorrowDecode, Decode, Encode}; use ed25519_dalek::SIGNATURE_LENGTH; use prost::Message; @@ -24,11 +25,6 @@ pub mod crypto; pub mod namespaces; pub mod proto; -const SIGNATURE_VERSIONS: [SignatureVersion; 2] = [ - SignatureVersion::ProtobufEd25519, - SignatureVersion::BincodeEd25519, -]; - #[derive(Error, Debug)] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub enum PlexiError { @@ -49,6 +45,7 @@ pub enum PlexiError { #[repr(u32)] pub enum SignatureVersion { ProtobufEd25519 = 0x0001, + #[cfg(feature = "bincode")] BincodeEd25519 = 0x0002, Unknown(u32), } @@ -57,6 +54,7 @@ impl From for u32 { fn from(val: SignatureVersion) -> Self { match val { SignatureVersion::ProtobufEd25519 => 0x0001, + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => 0x0002, SignatureVersion::Unknown(u) => u, } @@ -67,6 +65,7 @@ impl From for SignatureVersion { fn from(u: u32) -> Self { match u { 0x0001 => Self::ProtobufEd25519, + #[cfg(feature = "bincode")] 0x0002 => Self::BincodeEd25519, _ => Self::Unknown(u), } @@ -86,6 +85,7 @@ impl fmt::Display for SignatureVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { Self::ProtobufEd25519 => "0x0001", + #[cfg(feature = "bincode")] Self::BincodeEd25519 => "0x0002", Self::Unknown(_u) => "unknown", }; @@ -93,6 +93,7 @@ impl fmt::Display for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Encode for SignatureVersion { fn encode( &self, @@ -103,6 +104,7 @@ impl Encode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Decode for SignatureVersion { fn decode( decoder: &mut D, @@ -112,6 +114,7 @@ impl Decode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl<'de> BorrowDecode<'de> for SignatureVersion { fn borrow_decode>( buffer: &mut B, @@ -121,7 +124,8 @@ impl<'de> BorrowDecode<'de> for SignatureVersion { } } -#[derive(Clone, Copy, Debug, Serialize, Deserialize, Encode, Decode)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub struct Epoch(u64); @@ -223,8 +227,10 @@ impl Sub for Epoch { } } -#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)] + +#[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "openapi", derive(ToSchema))] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] pub struct SignatureMessage { version: SignatureVersion, namespace: String, @@ -243,16 +249,17 @@ impl SignatureMessage { epoch: &Epoch, digest: Vec, ) -> Result { - if !SIGNATURE_VERSIONS.contains(version) { - return Err(PlexiError::BadParameter("version".to_string())); + match version { + SignatureVersion::Unknown(_) => Err(PlexiError::BadParameter("version".to_string())), + _ => + Ok(Self { + version: *version, + namespace, + timestamp, + epoch: *epoch, + digest, + }) } - Ok(Self { - version: *version, - namespace, - timestamp, - epoch: *epoch, - digest, - }) } pub fn version(&self) -> &SignatureVersion { @@ -275,6 +282,7 @@ impl SignatureMessage { self.digest.clone() } + #[cfg(feature = "bincode")] fn to_vec_bincode(&self) -> Result, PlexiError> { bincode::encode_to_vec(self, bincode::config::legacy()) .map_err(|_e| PlexiError::Serialization) @@ -297,6 +305,7 @@ impl SignatureMessage { pub fn to_vec(&self) -> Result, PlexiError> { match self.version { SignatureVersion::ProtobufEd25519 => self.to_vec_proto(), + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => self.to_vec_bincode(), _ => Err(PlexiError::Serialization), } @@ -463,6 +472,7 @@ impl SignatureResponse { pub fn verify(&self, verifying_key: &[u8]) -> anyhow::Result<()> { // at the time of writting, all version use ed25519 keys. this simplify parsing of the verifying key match self.version { + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => (), SignatureVersion::ProtobufEd25519 => (), SignatureVersion::Unknown(_) => { @@ -648,6 +658,7 @@ mod tests { use super::*; #[test] + #[cfg(feature = "bincode")] fn test_vector() { const TEST_VECTORS: &str = std::include_str!("../tests/test-vectors.json"); From 458a9ded79baaa4f624ff96157e9a4dbd5b783f8 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Mon, 7 Oct 2024 09:25:18 -0700 Subject: [PATCH 3/5] cargo fmt --- plexi_core/src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plexi_core/src/lib.rs b/plexi_core/src/lib.rs index 8de90f1..df2ab7d 100644 --- a/plexi_core/src/lib.rs +++ b/plexi_core/src/lib.rs @@ -227,7 +227,6 @@ impl Sub for Epoch { } } - #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "openapi", derive(ToSchema))] #[cfg_attr(feature = "bincode", derive(Encode, Decode))] @@ -251,14 +250,13 @@ impl SignatureMessage { ) -> Result { match version { SignatureVersion::Unknown(_) => Err(PlexiError::BadParameter("version".to_string())), - _ => - Ok(Self { + _ => Ok(Self { version: *version, namespace, timestamp, epoch: *epoch, digest, - }) + }), } } From f81e98164fb3118398d25c52a187f1ecc2f07de7 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Mon, 7 Oct 2024 09:30:26 -0700 Subject: [PATCH 4/5] include bincode in default feature set --- plexi_core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexi_core/Cargo.toml b/plexi_core/Cargo.toml index 9b0b49d..f18627e 100644 --- a/plexi_core/Cargo.toml +++ b/plexi_core/Cargo.toml @@ -13,7 +13,7 @@ categories.workspace = true build = "src/build.rs" [features] -default = ["openapi"] +default = ["openapi", "bincode"] auditor = ["akd", "akd/parallel_vrf", "akd/parallel_insert", "akd/experimental"] client = ["auditor", "reqwest", "bincode"] openapi = ["utoipa"] From d6bf396521bbeb956c217737a0256648d333dfa7 Mon Sep 17 00:00:00 2001 From: Thibault Meunier Date: Thu, 10 Oct 2024 13:18:51 +0200 Subject: [PATCH 5/5] Add bincode as a workspace dependency Dependecnies are managed by the workspace --- Cargo.toml | 1 + plexi_core/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ed19aba..44fcae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ license = "Apache-2.0" [workspace.dependencies] akd = { version = "0.11", default-features = false } +bincode = "2.0.0-rc.3" anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } clap-verbosity-flag = "2.2.0" diff --git a/plexi_core/Cargo.toml b/plexi_core/Cargo.toml index f18627e..2461f36 100644 --- a/plexi_core/Cargo.toml +++ b/plexi_core/Cargo.toml @@ -22,7 +22,7 @@ bincode = ["dep:bincode"] [dependencies] akd = { workspace = true, features = ["whatsapp_v1", "public_auditing"], optional = true } anyhow = { workspace = true } -bincode = { version = "2.0.0-rc.3", optional = true } +bincode = { workspace = true, optional = true } ed25519-dalek = { workspace = true } hex = { workspace = true, features = ["serde"] } prost = { workspace = true }