Skip to content

Commit

Permalink
Use dynamic serializers/deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Oct 28, 2024
1 parent 8f91c69 commit 355c5da
Show file tree
Hide file tree
Showing 20 changed files with 483 additions and 204 deletions.
145 changes: 134 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"

[dependencies]
manul = { path = "../manul" }
bincode = { version = "2.0.0-rc.3", default-features = false, features = ["alloc", "serde"] }
postcard = { version = "1", features = ["alloc"] }
serde = "1"
sha3 = "0.10"
rand_core = "0.6"
Expand Down
23 changes: 12 additions & 11 deletions examples/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use manul::{
protocol::{DeserializationError, LocalError},
session::Format,
};
use serde::{Deserialize, Serialize};
use manul::{protocol::LocalError, session::Format};
use serde::Serialize;

#[derive(Debug)]
pub struct Bincode;
pub struct Binary;

impl Format for Bincode {
impl Format for Binary {
fn serialize<T: Serialize>(value: T) -> Result<Box<[u8]>, LocalError> {
bincode::serde::encode_to_vec(value, bincode::config::standard())
postcard::to_allocvec(&value)
.map(|vec| vec.into())
.map_err(|err| LocalError::new(err.to_string()))
}

fn deserialize<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, DeserializationError> {
bincode::serde::decode_borrowed_from_slice(bytes, bincode::config::standard())
.map_err(|err| DeserializationError::new(err.to_string()))
type Des<'de> = postcard::Deserializer<'de, postcard::de_flavors::Slice<'de>>;

fn deserializer<'de>(bytes: &'de [u8]) -> Self::Des<'de>
where
for<'a> &'a mut Self::Des<'de>: serde::Deserializer<'de>,
{
postcard::Deserializer::from_flavor(postcard::de_flavors::Slice::new(bytes))
}
}
2 changes: 1 addition & 1 deletion examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub mod simple;
#[cfg(test)]
mod simple_malicious;

pub use format::Bincode;
pub use format::Binary;
4 changes: 2 additions & 2 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ mod tests {
use tracing_subscriber::EnvFilter;

use super::{Inputs, Round1};
use crate::Bincode;
use crate::Binary;

#[test]
fn round() {
Expand All @@ -422,7 +422,7 @@ mod tests {
.with_env_filter(EnvFilter::from_default_env())
.finish();
let reports = tracing::subscriber::with_default(my_subscriber, || {
run_sync::<Round1<TestVerifier>, TestSessionParams<Bincode>>(&mut OsRng, inputs).unwrap()
run_sync::<Round1<TestVerifier>, TestSessionParams<Binary>>(&mut OsRng, inputs).unwrap()
});

for (_id, report) in reports {
Expand Down
12 changes: 6 additions & 6 deletions examples/src/simple_malicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing_subscriber::EnvFilter;

use crate::{
simple::{Inputs, Round1, Round1Message, Round2, Round2Message},
Bincode,
Binary,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundOverride<Id> for Mali
my_position: self.round.context.ids_to_positions[&self.round.context.id],
your_position: self.round.context.ids_to_positions[&self.round.context.id],
};
DirectMessage::new(serializer, &message)
DirectMessage::new(serializer, message)
} else {
self.inner_round_ref().make_direct_message(rng, serializer, destination)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundOverride<Id> for Mali
my_position: self.round.context.ids_to_positions[&self.round.context.id],
your_position: self.round.context.ids_to_positions[&self.round.context.id],
};
DirectMessage::new(serializer, &message)
DirectMessage::new(serializer, message)
} else {
self.inner_round_ref().make_direct_message(rng, serializer, destination)
}
Expand Down Expand Up @@ -177,7 +177,7 @@ fn serialized_garbage() {
.with_env_filter(EnvFilter::from_default_env())
.finish();
let mut reports = tracing::subscriber::with_default(my_subscriber, || {
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Bincode>>(&mut OsRng, run_inputs).unwrap()
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Binary>>(&mut OsRng, run_inputs).unwrap()
});

let v0 = signers[0].verifying_key();
Expand Down Expand Up @@ -223,7 +223,7 @@ fn attributable_failure() {
.with_env_filter(EnvFilter::from_default_env())
.finish();
let mut reports = tracing::subscriber::with_default(my_subscriber, || {
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Bincode>>(&mut OsRng, run_inputs).unwrap()
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Binary>>(&mut OsRng, run_inputs).unwrap()
});

let v0 = signers[0].verifying_key();
Expand Down Expand Up @@ -269,7 +269,7 @@ fn attributable_failure_round2() {
.with_env_filter(EnvFilter::from_default_env())
.finish();
let mut reports = tracing::subscriber::with_default(my_subscriber, || {
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Bincode>>(&mut OsRng, run_inputs).unwrap()
run_sync::<MaliciousRound1<TestVerifier>, TestSessionParams<Binary>>(&mut OsRng, run_inputs).unwrap()
});

let v0 = signers[0].verifying_key();
Expand Down
Loading

0 comments on commit 355c5da

Please sign in to comment.