diff --git a/CHANGELOG.md b/CHANGELOG.md index c203760..e74c543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `SessionId::new()` renamed to `from_seed()`. ([#41]) - `FirstRound::new()` takes a `&[u8]` instead of a `SessionId` object. ([#41]) - The signatures of `Round::make_echo_broadcast()`, `Round::make_direct_message()`, and `Round::receive_message()`, take messages without `Option`s. ([#46]) -- `Round::make_direct_message_with_artifact()` is the method returning an artifact now; `Round::make_direct_message()` is a shortcut for cases where no artifact is returned. ([#46]) - `Artifact::empty()` removed, the user should return `None` instead. ([#46]) - `EchoBroadcast` and `DirectMessage` now use `ProtocolMessagePart` trait for their methods. ([#47]) - Added normal broadcasts support in addition to echo ones; signatures of `Round` methods changed accordingly; added `Round::make_normal_broadcast()`. ([#47]) diff --git a/examples/src/simple.rs b/examples/src/simple.rs index b947fac..bbf4756 100644 --- a/examples/src/simple.rs +++ b/examples/src/simple.rs @@ -228,14 +228,15 @@ impl Round for Round1 { _rng: &mut impl CryptoRngCore, serializer: &Serializer, destination: &Id, - ) -> Result { + ) -> Result<(DirectMessage, Option), LocalError> { debug!("{:?}: making direct message for {:?}", self.context.id, destination); let message = Round1Message { my_position: self.context.ids_to_positions[&self.context.id], your_position: self.context.ids_to_positions[destination], }; - DirectMessage::new(serializer, message) + let dm = DirectMessage::new(serializer, message)?; + Ok((dm, None)) } fn receive_message( @@ -325,14 +326,15 @@ impl Round for Round2 { _rng: &mut impl CryptoRngCore, serializer: &Serializer, destination: &Id, - ) -> Result { + ) -> Result<(DirectMessage, Option), LocalError> { debug!("{:?}: making direct message for {:?}", self.context.id, destination); let message = Round2Message { my_position: self.context.ids_to_positions[&self.context.id], your_position: self.context.ids_to_positions[destination], }; - DirectMessage::new(serializer, message) + let dm = DirectMessage::new(serializer, message)?; + Ok((dm, None)) } fn receive_message( diff --git a/examples/src/simple_malicious.rs b/examples/src/simple_malicious.rs index 1ed749d..950c9f2 100644 --- a/examples/src/simple_malicious.rs +++ b/examples/src/simple_malicious.rs @@ -68,15 +68,15 @@ impl RoundOverride for MaliciousRound1 { rng: &mut impl CryptoRngCore, serializer: &Serializer, destination: &Id, - ) -> Result { + ) -> Result<(DirectMessage, Option), LocalError> { if matches!(self.behavior, Behavior::SerializedGarbage) { - DirectMessage::new(serializer, [99u8]) + Ok((DirectMessage::new(serializer, [99u8])?, None)) } else if matches!(self.behavior, Behavior::AttributableFailure) { let message = Round1Message { 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) + Ok((DirectMessage::new(serializer, message)?, None)) } else { self.inner_round_ref().make_direct_message(rng, serializer, destination) } @@ -131,13 +131,13 @@ impl RoundOverride for MaliciousRound2 { rng: &mut impl CryptoRngCore, serializer: &Serializer, destination: &Id, - ) -> Result { + ) -> Result<(DirectMessage, Option), LocalError> { if matches!(self.behavior, Behavior::AttributableFailureRound2) { let message = Round2Message { 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) + Ok((DirectMessage::new(serializer, message)?, None)) } else { self.inner_round_ref().make_direct_message(rng, serializer, destination) } diff --git a/manul/benches/empty_rounds.rs b/manul/benches/empty_rounds.rs index 6ce657b..4420243 100644 --- a/manul/benches/empty_rounds.rs +++ b/manul/benches/empty_rounds.rs @@ -119,7 +119,7 @@ impl Round for EmptyRound { } } - fn make_direct_message_with_artifact( + fn make_direct_message( &self, _rng: &mut impl CryptoRngCore, serializer: &Serializer, diff --git a/manul/src/protocol/object_safe.rs b/manul/src/protocol/object_safe.rs index fd65f95..6d282be 100644 --- a/manul/src/protocol/object_safe.rs +++ b/manul/src/protocol/object_safe.rs @@ -48,7 +48,7 @@ pub(crate) trait ObjectSafeRound: 'static + Debug + Send + Sync { fn message_destinations(&self) -> &BTreeSet; - fn make_direct_message_with_artifact( + fn make_direct_message( &self, rng: &mut dyn CryptoRngCore, serializer: &Serializer, @@ -130,15 +130,14 @@ where self.round.message_destinations() } - fn make_direct_message_with_artifact( + fn make_direct_message( &self, rng: &mut dyn CryptoRngCore, serializer: &Serializer, destination: &Id, ) -> Result<(DirectMessage, Option), LocalError> { let mut boxed_rng = BoxedRng(rng); - self.round - .make_direct_message_with_artifact(&mut boxed_rng, serializer, destination) + self.round.make_direct_message(&mut boxed_rng, serializer, destination) } fn make_echo_broadcast( diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index e2f113b..56c1aa1 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -358,35 +358,16 @@ pub trait Round: 'static + Debug + Send + Sync { /// /// Return [`DirectMessage::none`] if this round does not send direct messages. /// - /// Falls back to [`make_direct_message`](`Self::make_direct_message`) if not implemented. - /// This is the method that will be called by the upper layer when creating direct messages. - /// /// In some protocols, when a message to another node is created, there is some associated information /// that needs to be retained for later (randomness, proofs of knowledge, and so on). /// These should be put in an [`Artifact`] and will be available at the time of [`finalize`](`Self::finalize`). - fn make_direct_message_with_artifact( - &self, - rng: &mut impl CryptoRngCore, - serializer: &Serializer, - destination: &Id, - ) -> Result<(DirectMessage, Option), LocalError> { - Ok((self.make_direct_message(rng, serializer, destination)?, None)) - } - - /// Returns the direct message to the given destination. - /// - /// This method will not be called by the upper layer directly, - /// only via [`make_direct_message_with_artifact`](`Self::make_direct_message_with_artifact`). - /// - /// Return [`DirectMessage::none`] if this round does not send direct messages. - /// This is also the blanket implementation. fn make_direct_message( &self, #[allow(unused_variables)] rng: &mut impl CryptoRngCore, #[allow(unused_variables)] serializer: &Serializer, #[allow(unused_variables)] destination: &Id, - ) -> Result { - Ok(DirectMessage::none()) + ) -> Result<(DirectMessage, Option), LocalError> { + Ok((DirectMessage::none(), None)) } /// Returns the echo broadcast for this round. @@ -438,7 +419,7 @@ pub trait Round: 'static + Debug + Send + Sync { /// /// `payloads` here are the ones previously generated by [`receive_message`](`Self::receive_message`), /// and `artifacts` are the ones previously generated by - /// [`make_direct_message_with_artifact`](`Self::make_direct_message_with_artifact`). + /// [`make_direct_message`](`Self::make_direct_message`). fn finalize( self, rng: &mut impl CryptoRngCore, diff --git a/manul/src/session/session.rs b/manul/src/session/session.rs index 5c1845e..6daec53 100644 --- a/manul/src/session/session.rs +++ b/manul/src/session/session.rs @@ -226,9 +226,7 @@ where rng: &mut impl CryptoRngCore, destination: &SP::Verifier, ) -> Result<(Message, ProcessedArtifact), LocalError> { - let (direct_message, artifact) = - self.round - .make_direct_message_with_artifact(rng, &self.serializer, destination)?; + let (direct_message, artifact) = self.round.make_direct_message(rng, &self.serializer, destination)?; let message = Message::new::( rng, diff --git a/manul/src/testing/macros.rs b/manul/src/testing/macros.rs index d9c5d70..ac8fb57 100644 --- a/manul/src/testing/macros.rs +++ b/manul/src/testing/macros.rs @@ -25,24 +25,13 @@ pub trait RoundWrapper: 'static + Sized + Send + Sync { /// /// The blanket implementations delegate to the methods of the wrapped round. pub trait RoundOverride: RoundWrapper { - /// An override for [`Round::make_direct_message_with_artifact`]. - fn make_direct_message_with_artifact( - &self, - rng: &mut impl CryptoRngCore, - serializer: &Serializer, - destination: &Id, - ) -> Result<(DirectMessage, Option), LocalError> { - let dm = self.make_direct_message(rng, serializer, destination)?; - Ok((dm, None)) - } - /// An override for [`Round::make_direct_message`]. fn make_direct_message( &self, rng: &mut impl CryptoRngCore, serializer: &Serializer, destination: &Id, - ) -> Result { + ) -> Result<(DirectMessage, Option), LocalError> { self.inner_round_ref().make_direct_message(rng, serializer, destination) } @@ -113,17 +102,8 @@ macro_rules! round_override { rng: &mut impl CryptoRngCore, serializer: &$crate::protocol::Serializer, destination: &Id, - ) -> Result<$crate::protocol::DirectMessage, $crate::protocol::LocalError> { - >::make_direct_message(self, rng, serializer, destination) - } - - fn make_direct_message_with_artifact( - &self, - rng: &mut impl CryptoRngCore, - serializer: &$crate::protocol::Serializer, - destination: &Id, ) -> Result<($crate::protocol::DirectMessage, Option<$crate::protocol::Artifact>), $crate::protocol::LocalError> { - >::make_direct_message_with_artifact(self, rng, serializer, destination) + >::make_direct_message(self, rng, serializer, destination) } fn make_echo_broadcast(