Skip to content
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
16 changes: 8 additions & 8 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
use alloc::collections::{BTreeMap, BTreeSet};
use core::fmt::Debug;

use manul::protocol::{
BoxedRound, CommunicationInfo, EntryPoint, EvidenceError, EvidenceMessages, FinalizeOutcome, LocalError, NoMessage,
PartyId, Protocol, ProtocolError, ProtocolMessage, ReceiveError, RequiredMessageParts, RequiredMessages, Round,
RoundId, RoundInfo, TransitionInfo,
use manul::{
protocol::{
BoxedRound, CommunicationInfo, EntryPoint, EvidenceError, EvidenceMessages, FinalizeOutcome, LocalError,
NoMessage, PartyId, Protocol, ProtocolError, ProtocolMessage, ReceiveError, RequiredMessageParts,
RequiredMessages, Round, RoundId, RoundInfo, TransitionInfo,
},
utils::Without,
};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -162,13 +165,10 @@ impl<Id: PartyId> EntryPoint<Id> for SimpleProtocolEntryPoint<Id> {
.map(|(idx, id)| (id.clone(), idx as u8))
.collect::<BTreeMap<_, _>>();

let mut ids = self.all_ids;
ids.remove(id);

Ok(BoxedRound::new(Round1 {
context: Context {
id: id.clone(),
other_ids: ids,
other_ids: self.all_ids.clone().without(id),
ids_to_positions,
},
}))
Expand Down
173 changes: 66 additions & 107 deletions examples/src/simple_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::collections::BTreeSet;

use manul::{
dev::{run_sync, BinaryFormat, ExtendableEntryPoint, RoundExtension, TestSessionParams, TestSigner},
dev::{check_evidence_with_extension, BinaryFormat, RoundExtension, TestSessionParams, TestSigner, TestVerifier},
protocol::{LocalError, PartyId},
signature::Keypair,
};
Expand All @@ -10,128 +10,87 @@ use test_log::test;

use crate::simple::{Round1, Round1Message, Round2, Round2Message, SimpleProtocolEntryPoint};

#[derive(Debug, Clone)]
struct Round1InvalidDirectMessage;
type Id = TestVerifier;
type SP = TestSessionParams<BinaryFormat>;
type EP = SimpleProtocolEntryPoint<Id>;

impl<Id> RoundExtension<Id> for Round1InvalidDirectMessage
where
Id: PartyId,
{
type Round = Round1<Id>;

fn make_direct_message(
&self,
_rng: &mut impl CryptoRngCore,
round: &Self::Round,
_destination: &Id,
) -> Result<(Round1Message, ()), LocalError> {
Ok((
Round1Message {
my_position: round.context.ids_to_positions[&round.context.id],
your_position: round.context.ids_to_positions[&round.context.id],
},
(),
))
}
}

#[test]
fn round1_attributable_failure() {
fn make_entry_points() -> (Vec<(TestSigner, EP)>, ()) {
let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>();
let all_ids = signers
.iter()
.map(|signer| signer.verifying_key())
.collect::<BTreeSet<_>>();

let entry_points = signers
.iter()
.enumerate()
.map(|(idx, signer)| {
let entry_point = SimpleProtocolEntryPoint::new(all_ids.clone());
let mut entry_point = ExtendableEntryPoint::new(entry_point);
if idx == 0 {
entry_point.extend(Round1InvalidDirectMessage);
}
.into_iter()
.map(|signer| (signer, SimpleProtocolEntryPoint::new(all_ids.clone())))
.collect();

(*signer, entry_point)
})
.collect::<Vec<_>>();

let mut reports = run_sync::<_, TestSessionParams<BinaryFormat>>(&mut OsRng, entry_points)
.unwrap()
.reports;

let v0 = signers[0].verifying_key();
let v1 = signers[1].verifying_key();
let v2 = signers[2].verifying_key();

let _report0 = reports.remove(&v0).unwrap();
let report1 = reports.remove(&v1).unwrap();
let report2 = reports.remove(&v2).unwrap();

assert!(report1.provable_errors[&v0].verify(&()).is_ok());
assert!(report2.provable_errors[&v0].verify(&()).is_ok());
(entry_points, ())
}

#[derive(Debug, Clone)]
struct Round2InvalidDirectMessage;

impl<Id> RoundExtension<Id> for Round2InvalidDirectMessage
where
Id: PartyId,
{
type Round = Round2<Id>;

fn make_direct_message(
&self,
_rng: &mut impl CryptoRngCore,
round: &Self::Round,
_destination: &Id,
) -> Result<(Round2Message, ()), LocalError> {
Ok((
Round2Message {
#[test]
fn round1_attributable_failure() -> Result<(), LocalError> {
#[derive(Debug, Clone)]
struct Round1InvalidDirectMessage;

impl<Id> RoundExtension<Id> for Round1InvalidDirectMessage
where
Id: PartyId,
{
type Round = Round1<Id>;

fn make_direct_message(
&self,
_rng: &mut impl CryptoRngCore,
round: &Self::Round,
_destination: &Id,
) -> Result<(Round1Message, ()), LocalError> {
let message = Round1Message {
my_position: round.context.ids_to_positions[&round.context.id],
your_position: round.context.ids_to_positions[&round.context.id],
},
(),
))
};
Ok((message, ()))
}
}

check_evidence_with_extension::<SP, _>(
&mut OsRng,
make_entry_points(),
Round1InvalidDirectMessage,
"(Round 1): Invalid position",
)
}

#[test]
fn round2_attributable_failure() {
let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>();
let all_ids = signers
.iter()
.map(|signer| signer.verifying_key())
.collect::<BTreeSet<_>>();

let entry_points = signers
.iter()
.enumerate()
.map(|(idx, signer)| {
let entry_point = SimpleProtocolEntryPoint::new(all_ids.clone());
let mut entry_point = ExtendableEntryPoint::new(entry_point);
if idx == 0 {
entry_point.extend(Round2InvalidDirectMessage);
}

(*signer, entry_point)
})
.collect::<Vec<_>>();

let mut reports = run_sync::<_, TestSessionParams<BinaryFormat>>(&mut OsRng, entry_points)
.unwrap()
.reports;

let v0 = signers[0].verifying_key();
let v1 = signers[1].verifying_key();
let v2 = signers[2].verifying_key();

let _report0 = reports.remove(&v0).unwrap();
let report1 = reports.remove(&v1).unwrap();
let report2 = reports.remove(&v2).unwrap();
fn round2_attributable_failure() -> Result<(), LocalError> {
#[derive(Debug, Clone)]
struct Round2InvalidDirectMessage;

impl<Id> RoundExtension<Id> for Round2InvalidDirectMessage
where
Id: PartyId,
{
type Round = Round2<Id>;

fn make_direct_message(
&self,
_rng: &mut impl CryptoRngCore,
round: &Self::Round,
_destination: &Id,
) -> Result<(Round2Message, ()), LocalError> {
let message = Round2Message {
my_position: round.context.ids_to_positions[&round.context.id],
your_position: round.context.ids_to_positions[&round.context.id],
};
Ok((message, ()))
}
}

assert!(report1.provable_errors[&v0].verify(&()).is_ok());
assert!(report2.provable_errors[&v0].verify(&()).is_ok());
check_evidence_with_extension::<SP, _>(
&mut OsRng,
make_entry_points(),
Round2InvalidDirectMessage,
"(Round 2): Invalid position",
)
}
5 changes: 2 additions & 3 deletions manul/benches/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use manul::{
Protocol, ProtocolMessage, ReceiveError, Round, RoundId, RoundInfo, TransitionInfo,
},
signature::Keypair,
utils::Without,
};
use rand_core::{CryptoRngCore, OsRng};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -241,11 +242,9 @@ fn bench_async_session(c: &mut Criterion) {
let entry_points = signers
.into_iter()
.map(|signer| {
let mut other_ids = all_ids.clone();
other_ids.remove(&signer.verifying_key());
let entry_point = Inputs {
rounds_num,
other_ids,
other_ids: all_ids.clone().without(&signer.verifying_key()),
echo: false,
};
(signer, entry_point)
Expand Down
9 changes: 3 additions & 6 deletions manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use manul::{
Protocol, ProtocolMessage, ReceiveError, Round, RoundId, RoundInfo, TransitionInfo,
},
signature::Keypair,
utils::Without,
};
use rand_core::{CryptoRngCore, OsRng};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -218,13 +219,11 @@ fn bench_empty_rounds(c: &mut Criterion) {
.iter()
.cloned()
.map(|signer| {
let mut other_ids = all_ids.clone();
other_ids.remove(&signer.verifying_key());
(
signer,
Inputs {
rounds_num,
other_ids,
other_ids: all_ids.clone().without(&signer.verifying_key()),
echo: false,
},
)
Expand All @@ -246,13 +245,11 @@ fn bench_empty_rounds(c: &mut Criterion) {
.iter()
.cloned()
.map(|signer| {
let mut other_ids = all_ids.clone();
other_ids.remove(&signer.verifying_key());
(
signer,
Inputs {
rounds_num,
other_ids,
other_ids: all_ids.clone().without(&signer.verifying_key()),
echo: true,
},
)
Expand Down
2 changes: 2 additions & 0 deletions manul/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The [`run_sync()`] method is helpful to execute a protocol synchronously and col
*/

mod extend;
mod misbehave;
mod run_sync;
mod session_parameters;
mod wire_format;
Expand All @@ -17,6 +18,7 @@ mod wire_format;
pub mod tokio;

pub use extend::{ExtendableEntryPoint, RoundExtension};
pub use misbehave::{check_evidence, check_evidence_with_extension, check_evidence_with_extensions, extend_one};
pub use run_sync::{run_sync, ExecutionResult};
pub use session_parameters::{TestHasher, TestSessionParams, TestSignature, TestSigner, TestVerifier};
pub use wire_format::{BinaryFormat, HumanReadableFormat};
Loading
Loading