Skip to content

Commit

Permalink
Introduce PartyId to shorten the list of bounds we need to repeat eve…
Browse files Browse the repository at this point in the history
…ry time
  • Loading branch information
fjarri committed Oct 30, 2024
1 parent 1e2080e commit 5e6d446
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `DirectMessage::assert_is_none()` and `verify_is_some()`, same for `EchoBroadcast`. Users can now check that a part of the round message (echo or direct) is `None` as expected, and make a verifiable evidence if it is not. ([#46])
- Re-export `digest` from the `session` module. ([#56])
- Added `Message::destination()`. ([#56])
- `PartyId` trait alias for the combination of bounds needed for a party identifier. ([#59])


[#32]: https://github.com/entropyxyz/manul/pull/32
Expand All @@ -43,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#56]: https://github.com/entropyxyz/manul/pull/56
[#57]: https://github.com/entropyxyz/manul/pull/57
[#58]: https://github.com/entropyxyz/manul/pull/58
[#59]: https://github.com/entropyxyz/manul/pull/59


## [0.0.1] - 2024-10-12
Expand Down
6 changes: 3 additions & 3 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct Round1Payload {
x: u8,
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for Round1<Id> {
impl<Id: PartyId> FirstRound<Id> for Round1<Id> {
type Inputs = Inputs<Id>;
fn new(
_rng: &mut impl CryptoRngCore,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for Round1<
}
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> Round<Id> for Round1<Id> {
impl<Id: PartyId> Round<Id> for Round1<Id> {
type Protocol = SimpleProtocol;

fn id(&self) -> RoundId {
Expand Down Expand Up @@ -305,7 +305,7 @@ pub(crate) struct Round2Message {
pub(crate) your_position: u8,
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> Round<Id> for Round2<Id> {
impl<Id: PartyId> Round<Id> for Round2<Id> {
type Protocol = SimpleProtocol;

fn id(&self) -> RoundId {
Expand Down
14 changes: 7 additions & 7 deletions examples/src/simple_malicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::fmt::Debug;

use manul::{
protocol::{
Artifact, DirectMessage, FinalizeError, FinalizeOutcome, FirstRound, LocalError, Payload, ProtocolMessagePart,
Round, Serializer,
Artifact, DirectMessage, FinalizeError, FinalizeOutcome, FirstRound, LocalError, PartyId, Payload,
ProtocolMessagePart, Round, Serializer,
},
session::signature::Keypair,
testing::{
Expand Down Expand Up @@ -36,7 +36,7 @@ struct MaliciousRound1<Id> {
behavior: Behavior,
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundWrapper<Id> for MaliciousRound1<Id> {
impl<Id: PartyId> RoundWrapper<Id> for MaliciousRound1<Id> {
type InnerRound = Round1<Id>;
fn inner_round_ref(&self) -> &Self::InnerRound {
&self.round
Expand All @@ -46,7 +46,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundWrapper<Id> for Malic
}
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for MaliciousRound1<Id> {
impl<Id: PartyId> FirstRound<Id> for MaliciousRound1<Id> {
type Inputs = MaliciousInputs<Id>;
fn new(
rng: &mut impl CryptoRngCore,
Expand All @@ -62,7 +62,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for Malicio
}
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundOverride<Id> for MaliciousRound1<Id> {
impl<Id: PartyId> RoundOverride<Id> for MaliciousRound1<Id> {
fn make_direct_message(
&self,
rng: &mut impl CryptoRngCore,
Expand Down Expand Up @@ -115,7 +115,7 @@ struct MaliciousRound2<Id> {
behavior: Behavior,
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundWrapper<Id> for MaliciousRound2<Id> {
impl<Id: PartyId> RoundWrapper<Id> for MaliciousRound2<Id> {
type InnerRound = Round2<Id>;
fn inner_round_ref(&self) -> &Self::InnerRound {
&self.round
Expand All @@ -125,7 +125,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundWrapper<Id> for Malic
}
}

impl<Id: 'static + Debug + Clone + Ord + Send + Sync> RoundOverride<Id> for MaliciousRound2<Id> {
impl<Id: PartyId> RoundOverride<Id> for MaliciousRound2<Id> {
fn make_direct_message(
&self,
rng: &mut impl CryptoRngCore,
Expand Down
2 changes: 1 addition & 1 deletion manul/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use errors::{
};
pub use message::{DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessagePart};
pub use round::{
AnotherRound, Artifact, FinalizeOutcome, FirstRound, Payload, Protocol, ProtocolError, Round, RoundId,
AnotherRound, Artifact, FinalizeOutcome, FirstRound, PartyId, Payload, Protocol, ProtocolError, Round, RoundId,
};
pub use serialization::{Deserializer, Serializer};

Expand Down
14 changes: 9 additions & 5 deletions manul/src/protocol/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand_core::{CryptoRng, CryptoRngCore, RngCore};
use super::{
errors::{FinalizeError, LocalError, ReceiveError},
message::{DirectMessage, EchoBroadcast, NormalBroadcast},
round::{Artifact, FinalizeOutcome, Payload, Protocol, Round, RoundId},
round::{Artifact, FinalizeOutcome, PartyId, Payload, Protocol, Round, RoundId},
serialization::{Deserializer, Serializer},
};

Expand Down Expand Up @@ -39,7 +39,7 @@ impl RngCore for BoxedRng<'_> {
// Since we want `Round` methods to take `&mut impl CryptoRngCore` arguments
// (which is what all cryptographic libraries generally take), it cannot be object-safe.
// Thus we have to add this crate-private object-safe layer on top of `Round`.
pub(crate) trait ObjectSafeRound<Id>: 'static + Debug + Send + Sync {
pub(crate) trait ObjectSafeRound<Id: PartyId>: 'static + Debug + Send + Sync {
type Protocol: Protocol;

fn id(&self) -> RoundId;
Expand Down Expand Up @@ -98,7 +98,11 @@ pub(crate) struct ObjectSafeRoundWrapper<Id, R> {
phantom: PhantomData<fn(Id) -> Id>,
}

impl<Id: 'static, R: Round<Id>> ObjectSafeRoundWrapper<Id, R> {
impl<Id, R> ObjectSafeRoundWrapper<Id, R>
where
Id: PartyId,
R: Round<Id>,
{
pub fn new(round: R) -> Self {
Self {
round,
Expand All @@ -109,7 +113,7 @@ impl<Id: 'static, R: Round<Id>> ObjectSafeRoundWrapper<Id, R> {

impl<Id, R> ObjectSafeRound<Id> for ObjectSafeRoundWrapper<Id, R>
where
Id: 'static + Debug,
Id: PartyId,
R: Round<Id>,
{
type Protocol = <R as Round<Id>>::Protocol;
Expand Down Expand Up @@ -203,7 +207,7 @@ where
// so we have to provide this workaround.
impl<Id, P> dyn ObjectSafeRound<Id, Protocol = P>
where
Id: 'static,
Id: PartyId,
P: Protocol,
{
pub fn try_downcast<T: Round<Id>>(self: Box<Self>) -> Result<T, Box<Self>> {
Expand Down
13 changes: 9 additions & 4 deletions manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum FinalizeOutcome<Id, P: Protocol> {

impl<Id, P> FinalizeOutcome<Id, P>
where
Id: 'static + Debug,
Id: PartyId,
P: Protocol,
{
/// A helper method to create an [`AnotherRound`](`Self::AnotherRound`) variant.
Expand All @@ -44,7 +44,7 @@ pub struct AnotherRound<Id, P: Protocol>(Box<dyn ObjectSafeRound<Id, Protocol =

impl<Id, P> AnotherRound<Id, P>
where
Id: 'static + Debug,
Id: PartyId,
P: Protocol,
{
/// Wraps an object implementing [`Round`].
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Artifact {
///
/// This is a round that can be created directly;
/// all the others are only reachable throud [`Round::finalize`] by the execution layer.
pub trait FirstRound<Id>: Round<Id> + Sized {
pub trait FirstRound<Id: PartyId>: Round<Id> + Sized {
/// Additional inputs for the protocol (besides the mandatory ones in [`new`](`Self::new`)).
type Inputs;

Expand All @@ -317,6 +317,11 @@ pub trait FirstRound<Id>: Round<Id> + Sized {
) -> Result<Self, LocalError>;
}

/// A trait alias for the combination of traits needed for a party identifier.
pub trait PartyId: 'static + Debug + Clone + Ord + Send + Sync {}

impl<T> PartyId for T where T: 'static + Debug + Clone + Ord + Send + Sync {}

/**
A type representing a single round of a protocol.
Expand All @@ -326,7 +331,7 @@ The way a round will be used by an external caller:
- process received messages from other nodes (by calling [`receive_message`](`Self::receive_message`));
- attempt to finalize (by calling [`finalize`](`Self::finalize`)) to produce the next round, or return a result.
*/
pub trait Round<Id>: 'static + Debug + Send + Sync {
pub trait Round<Id: PartyId>: 'static + Debug + Send + Sync {
/// The protocol this round is a part of.
type Protocol: Protocol;

Expand Down
13 changes: 3 additions & 10 deletions manul/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use super::{
};
use crate::protocol::{
Artifact, Deserializer, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, FirstRound, NormalBroadcast,
ObjectSafeRound, ObjectSafeRoundWrapper, Payload, Protocol, ProtocolMessagePart, ReceiveError, ReceiveErrorType,
Round, RoundId, Serializer,
ObjectSafeRound, ObjectSafeRoundWrapper, PartyId, Payload, Protocol, ProtocolMessagePart, ReceiveError,
ReceiveErrorType, Round, RoundId, Serializer,
};

/// A set of types needed to execute a session.
Expand All @@ -40,14 +40,7 @@ pub trait SessionParameters: 'static {
type Digest: Digest;

/// The verifier type, which will also serve as a node identifier.
type Verifier: Debug
+ Clone
+ Ord
+ DigestVerifier<Self::Digest, Self::Signature>
+ Serialize
+ for<'de> Deserialize<'de>
+ Send
+ Sync;
type Verifier: PartyId + DigestVerifier<Self::Digest, Self::Signature> + Serialize + for<'de> Deserialize<'de>;

/// The signature type corresponding to [`Signer`](`Self::Signer`) and [`Verifier`](`Self::Verifier`).
type Signature: Serialize + for<'de> Deserialize<'de>;
Expand Down
12 changes: 6 additions & 6 deletions manul/src/testing/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use alloc::collections::BTreeMap;
use rand_core::CryptoRngCore;

use crate::protocol::{
Artifact, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, LocalError, NormalBroadcast, Payload,
Round, Serializer,
Artifact, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, LocalError, NormalBroadcast, PartyId,
Payload, Round, Serializer,
};

/// A trait defining a wrapper around an existing type implementing [`Round`].
pub trait RoundWrapper<Id>: 'static + Sized + Send + Sync {
pub trait RoundWrapper<Id: PartyId>: 'static + Sized + Send + Sync {
/// The inner round type.
type InnerRound: Round<Id>;

Expand All @@ -24,7 +24,7 @@ pub trait RoundWrapper<Id>: 'static + Sized + Send + Sync {
/// Intended to be used with the [`round_override`] macro to generate the [`Round`] implementation.
///
/// The blanket implementations delegate to the methods of the wrapped round.
pub trait RoundOverride<Id>: RoundWrapper<Id> {
pub trait RoundOverride<Id: PartyId>: RoundWrapper<Id> {
/// An override for [`Round::make_direct_message_with_artifact`].
fn make_direct_message_with_artifact(
&self,
Expand Down Expand Up @@ -90,8 +90,8 @@ macro_rules! round_override {
($round: ident) => {
impl<Id> Round<Id> for $round<Id>
where
Id: Debug,
$round<Id>: RoundOverride<Id>,
Id: $crate::protocol::PartyId,
$round<Id>: $crate::testing::RoundOverride<Id>,
{
type Protocol =
<<$round<Id> as $crate::testing::RoundWrapper<Id>>::InnerRound as $crate::protocol::Round<Id>>::Protocol;
Expand Down
8 changes: 4 additions & 4 deletions manul/src/testing/run_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn propagate<P, SP>(
accum: RoundAccumulator<P, SP>,
) -> Result<(State<P, SP>, Vec<RoundMessage<SP>>), LocalError>
where
P: 'static + Protocol,
SP: 'static + SessionParameters,
P: Protocol,
SP: SessionParameters,
{
let mut messages = Vec::new();

Expand Down Expand Up @@ -94,8 +94,8 @@ pub fn run_sync<R, SP>(
inputs: Vec<(SP::Signer, R::Inputs)>,
) -> Result<BTreeMap<SP::Verifier, SessionReport<R::Protocol, SP>>, LocalError>
where
R: 'static + FirstRound<SP::Verifier>,
SP: 'static + SessionParameters,
R: FirstRound<SP::Verifier>,
SP: SessionParameters,
{
let session_id = SessionId::random::<SP>(rng);

Expand Down

0 comments on commit 5e6d446

Please sign in to comment.