Skip to content

Commit

Permalink
Draft of Chain combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Nov 2, 2024
1 parent 00f468c commit 98a5c2a
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate alloc;

pub mod simple;
mod simple_chain;

#[cfg(test)]
mod simple_malicious;
86 changes: 86 additions & 0 deletions examples/src/simple_chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use core::fmt::Debug;
use core::marker::PhantomData;

use manul::{combinators::*, protocol::PartyId};

use super::simple::{Inputs, Round1, SimpleProtocol};

pub struct ChainedSimple<Id>(PhantomData<Id>);

#[derive(Debug)]
pub struct NewInputs<Id>(Inputs<Id>);

impl<'a, Id: PartyId> From<&'a NewInputs<Id>> for Inputs<Id> {
fn from(source: &'a NewInputs<Id>) -> Self {
source.0.clone()
}
}

impl<Id: PartyId> From<(NewInputs<Id>, u8)> for Inputs<Id> {
fn from(source: (NewInputs<Id>, u8)) -> Self {
source.0 .0
}
}

impl<Id: PartyId> Chained<Id> for ChainedSimple<Id> {
type Protocol1 = SimpleProtocol;
type Protocol2 = SimpleProtocol;
type CorrectnessProof = ();
type Inputs = NewInputs<Id>;
type EntryPoint1 = Round1<Id>;
type EntryPoint2 = Round1<Id>;
}

#[cfg(test)]
mod tests {
use alloc::collections::BTreeSet;

use manul::{
combinators::Chain,
session::{signature::Keypair, SessionOutcome},
testing::{run_sync, BinaryFormat, TestSessionParams, TestSigner, TestVerifier},
};
use rand_core::OsRng;
use tracing_subscriber::EnvFilter;

use super::{ChainedSimple, NewInputs};
use crate::simple::Inputs;

#[test]
fn round() {
let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>();
let all_ids = signers
.iter()
.map(|signer| signer.verifying_key())
.collect::<BTreeSet<_>>();
let inputs = signers
.into_iter()
.map(|signer| {
(
signer,
NewInputs(Inputs {
all_ids: all_ids.clone(),
}),
)
})
.collect::<Vec<_>>();

let my_subscriber = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.finish();
let reports = tracing::subscriber::with_default(my_subscriber, || {
run_sync::<Chain<TestVerifier, ChainedSimple<TestVerifier>>, TestSessionParams<BinaryFormat>>(
&mut OsRng, inputs,
)
.unwrap()
});

for (_id, report) in reports {
if let SessionOutcome::Result(result) = report.outcome {
assert_eq!(result, 3); // 0 + 1 + 2
} else {
panic!("Session did not finish successfully");
}
}
}
}
2 changes: 2 additions & 0 deletions manul/src/combinators.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod chain;
mod misbehave;

pub use chain::{Chain, Chained, ChainedProtocol};
pub use misbehave::{Misbehaving, MisbehavingInputs, MisbehavingRound};
Loading

0 comments on commit 98a5c2a

Please sign in to comment.