Skip to content

Commit

Permalink
reduces passing of inputs in protocols #290
Browse files Browse the repository at this point in the history
  • Loading branch information
marsella committed Jun 15, 2023
1 parent c69143d commit 9b7666b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 68 deletions.
9 changes: 2 additions & 7 deletions src/auxinfo/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ impl ProtocolParticipant for AuxInfoParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Self::Input,
) -> Result<ProcessOutcome<Self::Output>> {
info!("Processing auxinfo message.");

Expand All @@ -212,7 +211,7 @@ impl ProtocolParticipant for AuxInfoParticipant {
let broadcast_outcome = self.handle_broadcast(rng, message)?;

// Handle the broadcasted message if all parties have agreed on it
broadcast_outcome.convert(self, Self::handle_round_one_msg, rng, input)
broadcast_outcome.convert(self, Self::handle_round_one_msg, rng)
}
MessageType::Auxinfo(AuxinfoMessageType::R2Decommit) => {
self.handle_round_two_msg(rng, message)
Expand Down Expand Up @@ -333,7 +332,6 @@ impl AuxInfoParticipant {
&mut self,
rng: &mut R,
broadcast_message: BroadcastOutput,
_input: &(),
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Handling round one auxinfo message.");

Expand Down Expand Up @@ -720,10 +718,7 @@ mod tests {
&message.message_type(),
&message.from(),
);
Some((
index,
participant.process_message(rng, &message, &()).unwrap(),
))
Some((index, participant.process_message(rng, &message).unwrap()))
}

#[cfg_attr(feature = "flame_it", flame)]
Expand Down
1 change: 0 additions & 1 deletion src/broadcast/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl ProtocolParticipant for BroadcastParticipant {
&mut self,
rng: &mut R,
message: &Message,
_: &Self::Input,
) -> Result<ProcessOutcome<Self::Output>> {
info!("Processing broadcast message.");

Expand Down
9 changes: 2 additions & 7 deletions src/keygen/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ impl ProtocolParticipant for KeygenParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Self::Input,
) -> Result<ProcessOutcome<Self::Output>> {
info!("Processing keygen message.");

Expand All @@ -264,7 +263,7 @@ impl ProtocolParticipant for KeygenParticipant {
let broadcast_outcome = self.handle_broadcast(rng, message)?;

// Handle the broadcasted message if all parties have agreed on it
broadcast_outcome.convert(self, Self::handle_round_one_msg, rng, input)
broadcast_outcome.convert(self, Self::handle_round_one_msg, rng)
}
MessageType::Keygen(KeygenMessageType::R2Decommit) => {
self.handle_round_two_msg(message)
Expand Down Expand Up @@ -392,7 +391,6 @@ impl KeygenParticipant {
&mut self,
rng: &mut R,
broadcast_message: BroadcastOutput,
_input: &(),
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Handling round one keygen message.");

Expand Down Expand Up @@ -748,10 +746,7 @@ mod tests {
&message.message_type(),
&message.from(),
);
Some((
index,
participant.process_message(rng, &message, &()).unwrap(),
))
Some((index, participant.process_message(rng, &message).unwrap()))
}

#[cfg_attr(feature = "flame_it", flame)]
Expand Down
8 changes: 3 additions & 5 deletions src/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ where
participant: &mut P,
mut handle_output: F,
rng: &mut R,
storage: &P::Input,
) -> Result<ProcessOutcome<P::Output>>
where
P: InnerProtocolParticipant,
F: FnMut(&mut P, &mut R, O, &P::Input) -> Result<ProcessOutcome<P::Output>>,
F: FnMut(&mut P, &mut R, O) -> Result<ProcessOutcome<P::Output>>,
R: CryptoRng + RngCore,
{
let (output, messages) = self.into_parts();
let outcome = match output {
Some(o) => handle_output(participant, rng, o, storage)?,
Some(o) => handle_output(participant, rng, o)?,
None => ProcessOutcome::Incomplete,
};
Ok(outcome.with_messages(messages))
Expand Down Expand Up @@ -241,7 +240,6 @@ pub trait ProtocolParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Self::Input,
) -> Result<ProcessOutcome<Self::Output>>;

/// The status of the protocol execution.
Expand Down Expand Up @@ -413,7 +411,7 @@ pub(crate) trait Broadcast {

let outcome = self
.broadcast_participant()
.process_message(rng, &broadcast_input, &())?;
.process_message(rng, &broadcast_input)?;

// ...and then re-wrap the output messages.
let (output, mut messages) = outcome.into_parts();
Expand Down
72 changes: 27 additions & 45 deletions src/presign/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ impl ProtocolParticipant for PresignParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Self::Input,
) -> Result<ProcessOutcome<Self::Output>> {
info!("Processing presign message.");

Expand All @@ -434,23 +433,21 @@ impl ProtocolParticipant for PresignParticipant {
}

match message.message_type() {
MessageType::Presign(PresignMessageType::Ready) => {
self.handle_ready_msg(rng, message, input)
}
MessageType::Presign(PresignMessageType::Ready) => self.handle_ready_msg(rng, message),
MessageType::Presign(PresignMessageType::RoundOneBroadcast) => {
let broadcast_outcome = self.handle_broadcast(rng, message)?;

// Handle the broadcasted message if all parties have agreed on it
broadcast_outcome.convert(self, Self::handle_round_one_broadcast_msg, rng, input)
broadcast_outcome.convert(self, Self::handle_round_one_broadcast_msg, rng)
}
MessageType::Presign(PresignMessageType::RoundOne) => {
self.handle_round_one_msg(rng, message, input)
self.handle_round_one_msg(rng, message)
}
MessageType::Presign(PresignMessageType::RoundTwo) => {
self.handle_round_two_msg(rng, message, input)
self.handle_round_two_msg(rng, message)
}
MessageType::Presign(PresignMessageType::RoundThree) => {
self.handle_round_three_msg(message, input)
self.handle_round_three_msg(message)
}
message_type => {
error!(
Expand Down Expand Up @@ -500,15 +497,13 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Input,
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Handling ready presign message.");

let (ready_outcome, is_ready) = self.process_ready_message::<storage::Ready>(message)?;

if is_ready {
let round_one_messages =
run_only_once!(self.gen_round_one_msgs(rng, message.id(), input))?;
let round_one_messages = run_only_once!(self.gen_round_one_msgs(rng, message.id()))?;
Ok(ready_outcome.with_messages(round_one_messages))
} else {
Ok(ready_outcome)
Expand All @@ -528,12 +523,11 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
sid: Identifier,
input: &Input,
) -> Result<Vec<Message>> {
info!("Generating round one presign messages.");

let info = PresignKeyShareAndInfo::new(self.id, input)?;
let other_public_auxinfo = input.all_but_one_auxinfo_public(self.id);
let info = PresignKeyShareAndInfo::new(self.id, self.input())?;
let other_public_auxinfo = self.input().all_but_one_auxinfo_public(self.id);

// Run round one.
let (private, r1_publics, r1_public_broadcast) =
Expand Down Expand Up @@ -578,7 +572,6 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
broadcast_message: BroadcastOutput,
input: &Input,
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Presign: Handling round one broadcast message.");

Expand All @@ -603,7 +596,7 @@ impl PresignParticipant {
return Err(InternalError::ProtocolError);
}
match retrieved_messages.get(0) {
Some(message) => self.handle_round_one_msg(rng, message, input),
Some(message) => self.handle_round_one_msg(rng, message),
None => Ok(ProcessOutcome::Incomplete),
}
}
Expand All @@ -618,7 +611,6 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Input,
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
use crate::round_one::Public as RoundOnePublic;

Expand All @@ -642,8 +634,8 @@ impl PresignParticipant {
.local_storage
.retrieve::<storage::RoundOnePublicBroadcast>(message.from())?;

let info = PresignKeyShareAndInfo::new(self.id, input)?;
let auxinfo_public = input.find_auxinfo_public(message.from())?;
let info = PresignKeyShareAndInfo::new(self.id, self.input())?;
let auxinfo_public = self.input().find_auxinfo_public(message.from())?;
let round_one_public = RoundOnePublic::try_from(message)?;
round_one_public.verify(
&self.retrieve_context(),
Expand All @@ -665,13 +657,12 @@ impl PresignParticipant {
{
info!("Presign: Round one complete. Generating round two messages.");
// Finish round one by generating messages for round two.
let round_two_messages =
run_only_once!(self.gen_round_two_msgs(rng, message.id(), input))?;
let round_two_messages = run_only_once!(self.gen_round_two_msgs(rng, message.id()))?;
// Process any round two messages we may have received early.
let round_two_outcomes = self
.fetch_messages(MessageType::Presign(PresignMessageType::RoundTwo))?
.iter()
.map(|msg| self.handle_round_two_msg(rng, msg, input))
.map(|msg| self.handle_round_two_msg(rng, msg))
.collect::<Result<Vec<_>>>()?;
ProcessOutcome::collect_with_messages(round_two_outcomes, round_two_messages)
} else {
Expand All @@ -689,7 +680,6 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
sid: Identifier,
input: &Input,
) -> Result<Vec<Message>> {
info!("Presign: Generating round two messages.");

Expand All @@ -701,11 +691,11 @@ impl PresignParticipant {
.local_storage
.contains::<storage::RoundOnePrivate>(self.id)
{
let more_messages = run_only_once!(self.gen_round_one_msgs(rng, sid, input))?;
let more_messages = run_only_once!(self.gen_round_one_msgs(rng, sid))?;
messages.extend_from_slice(&more_messages);
}

let info = PresignKeyShareAndInfo::new(self.id, input)?;
let info = PresignKeyShareAndInfo::new(self.id, self.input())?;
// We need this clone as the map below uses a mutable `self`.
let pids = self.other_participant_ids.clone();
let more_messages: Vec<Message> = pids
Expand All @@ -717,7 +707,7 @@ impl PresignParticipant {
let r1_public_broadcast = self
.local_storage
.retrieve::<storage::RoundOnePublicBroadcast>(pid)?;
let sender_auxinfo_public = input.find_auxinfo_public(pid)?;
let sender_auxinfo_public = self.input().find_auxinfo_public(pid)?;
let (r2_priv, r2_pub) = info.round_two(
rng,
&self.retrieve_context(),
Expand Down Expand Up @@ -750,7 +740,6 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
message: &Message,
input: &Input,
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Presign: Handling round two message.");

Expand All @@ -765,7 +754,7 @@ impl PresignParticipant {
return Ok(ProcessOutcome::Incomplete);
}

self.validate_and_store_round_two_public(input, message)?;
self.validate_and_store_round_two_public(message)?;

// Check if storage has all of the other participants' round two values
// (both private and public), and start generating the messages for
Expand All @@ -779,13 +768,13 @@ impl PresignParticipant {
if all_privates_received && all_publics_received {
info!("Presign: Round two complete. Generating round three messages.");
// Generate messages for round three...
let messages = run_only_once!(self.gen_round_three_msgs(rng, message.id(), input))?;
let messages = run_only_once!(self.gen_round_three_msgs(rng, message.id()))?;
// ... and handle any messages that other participants have sent for round
// three.
let outcomes = self
.fetch_messages(MessageType::Presign(PresignMessageType::RoundThree))?
.iter()
.map(|msg| self.handle_round_three_msg(msg, input))
.map(|msg| self.handle_round_three_msg(msg))
.collect::<Result<Vec<_>>>()?;
ProcessOutcome::collect_with_messages(outcomes, messages)
} else {
Expand All @@ -806,16 +795,15 @@ impl PresignParticipant {
&mut self,
rng: &mut R,
sid: Identifier,
input: &Input,
) -> Result<Vec<Message>> {
info!("Generating round three presign messages.");

let info = PresignKeyShareAndInfo::new(self.id, input)?;
let info = PresignKeyShareAndInfo::new(self.id, self.input())?;
// Collect the other participant's values from storage needed for round
// three.
let mut hashmap = HashMap::new();
for pid in self.other_participant_ids.clone() {
let auxinfo_public = input.find_auxinfo_public(pid)?;
let auxinfo_public = self.input().find_auxinfo_public(pid)?;
let r2_private = self
.local_storage
.retrieve::<storage::RoundTwoPrivate>(pid)?;
Expand Down Expand Up @@ -863,7 +851,6 @@ impl PresignParticipant {
fn handle_round_three_msg(
&mut self,
message: &Message,
input: &Input,
) -> Result<ProcessOutcome<<Self as ProtocolParticipant>::Output>> {
info!("Handling round three presign message.");

Expand All @@ -878,7 +865,7 @@ impl PresignParticipant {
return Ok(ProcessOutcome::Incomplete);
}

self.validate_and_store_round_three_public(input, message)?;
self.validate_and_store_round_three_public(message)?;

// If we have round three public values from all other participants, we
// are done with the protocol! All we have left to do is create the
Expand Down Expand Up @@ -939,11 +926,9 @@ impl PresignParticipant {
}

#[cfg_attr(feature = "flame_it", flame("presign"))]
fn validate_and_store_round_two_public(
&mut self,
input: &Input,
message: &Message,
) -> Result<()> {
fn validate_and_store_round_two_public(&mut self, message: &Message) -> Result<()> {
let input = self.input();

let receiver_auxinfo_public = input.find_auxinfo_public(message.to())?;
let sender_auxinfo_public = input.find_auxinfo_public(message.from())?;
let sender_keyshare_public = input.find_keyshare_public(message.from())?;
Expand Down Expand Up @@ -971,11 +956,8 @@ impl PresignParticipant {
}

#[cfg_attr(feature = "flame_it", flame("presign"))]
fn validate_and_store_round_three_public(
&mut self,
input: &Input,
message: &Message,
) -> Result<()> {
fn validate_and_store_round_three_public(&mut self, message: &Message) -> Result<()> {
let input = self.input();
let receiver_auxinfo_public = input.find_auxinfo_public(message.to())?;
let sender_auxinfo_public = input.find_auxinfo_public(message.from())?;
let sender_r1_public_broadcast = self
Expand Down
4 changes: 1 addition & 3 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ impl<P: ProtocolParticipant> Participant<P> {
}

// Handle it!
let outcome =
self.participant
.process_message(rng, message, &self.participant.input().clone())?;
let outcome = self.participant.process_message(rng, message)?;
let (output, messages) = outcome.into_parts();
Ok((output, messages))
}
Expand Down

0 comments on commit 9b7666b

Please sign in to comment.