Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make bincode optional for plexi_core #9

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ license = "Apache-2.0"

[workspace.dependencies]
akd = { version = "0.11", default-features = false }
anyhow = "1.0"
bincode = "2.0.0-rc.3"
anyhow = "1.0"
clap = { version = "4.5", features = ["derive"] }
clap-verbosity-flag = "2.2.0"
colored = "2.1"
Expand Down
9 changes: 5 additions & 4 deletions plexi_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ 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"]
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 = { workspace = true, optional = true }
ed25519-dalek = { workspace = true }
hex = { workspace = true, features = ["serde"] }
prost = { workspace = true }
Expand All @@ -39,4 +40,4 @@ ed25519-dalek = { workspace = true, features = ["rand_core"] }
getrandom = { workspace = true, features = ["js"] }

[build-dependencies]
prost-build = { version = "0.13" }
prost-build = { version = "0.13" }
41 changes: 25 additions & 16 deletions plexi_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -49,6 +45,7 @@ pub enum PlexiError {
#[repr(u32)]
pub enum SignatureVersion {
ProtobufEd25519 = 0x0001,
#[cfg(feature = "bincode")]
BincodeEd25519 = 0x0002,
Unknown(u32),
}
Expand All @@ -57,6 +54,7 @@ impl From<SignatureVersion> for u32 {
fn from(val: SignatureVersion) -> Self {
match val {
SignatureVersion::ProtobufEd25519 => 0x0001,
#[cfg(feature = "bincode")]
SignatureVersion::BincodeEd25519 => 0x0002,
SignatureVersion::Unknown(u) => u,
}
Expand All @@ -67,6 +65,7 @@ impl From<u32> for SignatureVersion {
fn from(u: u32) -> Self {
match u {
0x0001 => Self::ProtobufEd25519,
#[cfg(feature = "bincode")]
0x0002 => Self::BincodeEd25519,
_ => Self::Unknown(u),
}
Expand All @@ -86,13 +85,15 @@ 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",
};
write!(f, "{}", s)
}
}

#[cfg(feature = "bincode")]
impl Encode for SignatureVersion {
fn encode<E: bincode::enc::Encoder>(
&self,
Expand All @@ -103,6 +104,7 @@ impl Encode for SignatureVersion {
}
}

#[cfg(feature = "bincode")]
impl Decode for SignatureVersion {
fn decode<D: bincode::de::Decoder>(
decoder: &mut D,
Expand All @@ -112,6 +114,7 @@ impl Decode for SignatureVersion {
}
}

#[cfg(feature = "bincode")]
impl<'de> BorrowDecode<'de> for SignatureVersion {
fn borrow_decode<B: bincode::de::BorrowDecoder<'de>>(
buffer: &mut B,
Expand All @@ -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);

Expand Down Expand Up @@ -223,8 +227,9 @@ impl Sub<Epoch> 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,
Expand All @@ -243,16 +248,16 @@ impl SignatureMessage {
epoch: &Epoch,
digest: Vec<u8>,
) -> Result<Self, PlexiError> {
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 {
Expand All @@ -275,6 +280,7 @@ impl SignatureMessage {
self.digest.clone()
}

#[cfg(feature = "bincode")]
fn to_vec_bincode(&self) -> Result<Vec<u8>, PlexiError> {
bincode::encode_to_vec(self, bincode::config::legacy())
.map_err(|_e| PlexiError::Serialization)
Expand All @@ -297,6 +303,7 @@ impl SignatureMessage {
pub fn to_vec(&self) -> Result<Vec<u8>, PlexiError> {
match self.version {
SignatureVersion::ProtobufEd25519 => self.to_vec_proto(),
#[cfg(feature = "bincode")]
SignatureVersion::BincodeEd25519 => self.to_vec_bincode(),
_ => Err(PlexiError::Serialization),
}
Expand Down Expand Up @@ -463,6 +470,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(_) => {
Expand Down Expand Up @@ -648,6 +656,7 @@ mod tests {
use super::*;

#[test]
#[cfg(feature = "bincode")]
fn test_vector() {
const TEST_VECTORS: &str = std::include_str!("../tests/test-vectors.json");

Expand Down
Loading