From 111223065edd0dfcd47085a6acdc407ca233fbdd Mon Sep 17 00:00:00 2001 From: Frederic Kettelhoit Date: Mon, 9 Sep 2024 20:37:07 +0100 Subject: [PATCH] Fix clippy warnings --- benches/join.rs | 2 +- src/channel.rs | 18 ++++++++++-------- src/faand.rs | 42 ++++++++++++++++++++++++------------------ src/fpre.rs | 22 +++++++++++----------- src/protocol.rs | 2 +- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/benches/join.rs b/benches/join.rs index 125a79c..1813986 100644 --- a/benches/join.rs +++ b/benches/join.rs @@ -35,7 +35,7 @@ fn join_benchmark(c: &mut Criterion) { )]), ), ]); - let prg = compile_with_constants(&code, consts).unwrap(); + let prg = compile_with_constants(code, consts).unwrap(); println!("{}", prg.circuit.report_gates()); let elapsed = now.elapsed(); println!( diff --git a/src/channel.rs b/src/channel.rs index ed166b9..4cf1ec7 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -30,6 +30,8 @@ pub enum ErrorKind { InvalidLength, } +type ChunkAndRemaining = (Vec, usize); + /// A communication channel used to send/receive messages to/from another party. pub trait Channel { /// The error that can occur sending messages over the channel. @@ -53,7 +55,7 @@ pub trait Channel { party: usize, phase: &str, i: usize, - ) -> impl Future, usize), Self::RecvError>> + Send; + ) -> impl Future> + Send; } /// A wrapper around [`Channel`] that takes care of (de-)serializing messages. @@ -142,8 +144,8 @@ impl MsgChannel { /// A simple synchronous channel using [`Sender`] and [`Receiver`]. #[derive(Debug)] pub struct SimpleChannel { - s: Vec, usize)>>>, - r: Vec, usize)>>>, + s: Vec>>, + r: Vec>>, } impl SimpleChannel { @@ -187,7 +189,7 @@ pub enum AsyncRecvError { } impl Channel for SimpleChannel { - type SendError = SendError<(Vec, usize)>; + type SendError = SendError; type RecvError = AsyncRecvError; async fn send_bytes_to( @@ -197,7 +199,7 @@ impl Channel for SimpleChannel { i: usize, remaining: usize, msg: Vec, - ) -> Result<(), SendError<(Vec, usize)>> { + ) -> Result<(), SendError> { let mb = msg.len() as f64 / 1024.0 / 1024.0; let i = i + 1; let total = i + remaining; @@ -218,15 +220,15 @@ impl Channel for SimpleChannel { p: usize, _phase: &str, _i: usize, - ) -> Result<(Vec, usize), AsyncRecvError> { + ) -> Result { let chunk = self.r[p] .as_mut() .unwrap_or_else(|| panic!("No receiver for party {p}")) .recv(); match timeout(Duration::from_secs(10 * 60), chunk).await { Ok(Some((bytes, remaining))) => Ok((bytes, remaining)), - Ok(None) => return Err(AsyncRecvError::Closed), - Err(_) => return Err(AsyncRecvError::TimeoutElapsed), + Ok(None) => Err(AsyncRecvError::Closed), + Err(_) => Err(AsyncRecvError::TimeoutElapsed), } } } diff --git a/src/faand.rs b/src/faand.rs index 1a5101d..bd4f14a 100644 --- a/src/faand.rs +++ b/src/faand.rs @@ -88,7 +88,7 @@ pub(crate) async fn shared_rng( .recv_from(p, "RNG") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; commitments[p] = commitment; bufs[p] = buffer; } @@ -249,7 +249,7 @@ pub(crate) async fn fabitn( for p in (0..p_max).filter(|p| *p != p_own) { fabitn_msg.iter_mut().enumerate().for_each(|(i, msg)| { msg.1 = macint[p][i]; - }); + }); channel.send_to(p, "fabitn", &fabitn_msg).await?; } @@ -355,10 +355,11 @@ pub(crate) async fn fashare( .send_to(p, "fashare commitverify", &own_commitments) .await?; } - let mut commitments: Vec)>> = - vec![vec![]; p_max]; + let mut commitments = vec![vec![]; p_max]; for p in (0..p_max).filter(|p| *p != p_own) { - commitments[p] = channel.recv_from(p, "fashare commitverify").await?; + commitments[p] = channel + .recv_from::<(Commitment, Commitment, Commitment, Vec)>(p, "fashare commitverify") + .await?; } commitments[p_own] = own_commitments; @@ -770,18 +771,22 @@ pub(crate) async fn faand( } channel.send_to(p, "faand", &ef_with_macs).await?; } - let mut faand_vec: Vec, Option)>> = - vec![vec![(false, false, None, None); vectriples.len()]; p_max]; + let mut faand_vec = vec![vec![(false, false, None, None); vectriples.len()]; p_max]; for p in (0..p_max).filter(|p| *p != p_own) { - faand_vec[p] = channel.recv_from(p, "faand").await?; + faand_vec[p] = channel + .recv_from::<(bool, bool, Option, Option)>(p, "faand") + .await?; } - ef_with_macs.iter_mut().enumerate().for_each(|(i, (e, f, _, _))| { - for p in (0..p_max).filter(|&p| p != p_own) { - let (fa_e, fa_f, _, _) = faand_vec[p][i]; - *e ^= fa_e; - *f ^= fa_f; - } - }); + ef_with_macs + .iter_mut() + .enumerate() + .for_each(|(i, (e, f, _, _))| { + for p in (0..p_max).filter(|&p| p != p_own) { + let (fa_e, fa_f, _, _) = faand_vec[p][i]; + *e ^= fa_e; + *f ^= fa_f; + } + }); let mut result = vec![Share(false, Auth(vec![])); vectriples.len()]; for i in 0..vectriples.len() { @@ -857,10 +862,11 @@ pub(crate) async fn check_dvalue( channel.send_to(p, "dvalue", &dvalue_msg).await?; } - let mut dvalue_msg_p: Vec, Vec>)>> = - vec![vec![(vec![], vec![]); buckets.len()]; p_max]; + let mut dvalue_msg_p = vec![vec![(vec![], vec![]); buckets.len()]; p_max]; for p in (0..p_max).filter(|p| *p != p_own) { - dvalue_msg_p[p] = channel.recv_from(p, "dvalue").await?; + dvalue_msg_p[p] = channel + .recv_from::<(Vec, Vec>)>(p, "dvalue") + .await?; } for p in (0..p_max).filter(|p| *p != p_own) { diff --git a/src/fpre.rs b/src/fpre.rs index a29883d..20ada81 100644 --- a/src/fpre.rs +++ b/src/fpre.rs @@ -64,7 +64,7 @@ where .recv_from(p, "random shares (fpre)") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; if num_shares > 0 && num_shares != r { let e = Error::RandomSharesMismatch(num_shares, r); for p in 0..parties { @@ -352,12 +352,12 @@ mod tests { .recv_from(fpre_party, "delta") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; let delta_b: Delta = b .recv_from(fpre_party, "delta") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; // random r1, r2, s1, s2: a.send_to(fpre_party, "random shares", &[2_u32]).await?; @@ -416,12 +416,12 @@ mod tests { .recv_from(fpre_party, "delta") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; let delta_b: Delta = b .recv_from(fpre_party, "delta") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; // random r1, r2, s1, s2: a.send_to(fpre_party, "random shares", &[2_u32]).await?; @@ -445,9 +445,9 @@ mod tests { if i == 0 { // uncorrupted authenticated (r1 XOR s1) AND (r2 XOR s2): - a.send_to(fpre_party, "AND shares", &vec![(auth_r1, auth_r2)]) + a.send_to(fpre_party, "AND shares", &[(auth_r1, auth_r2)]) .await?; - b.send_to(fpre_party, "AND shares", &vec![(auth_s1, auth_s2)]) + b.send_to(fpre_party, "AND shares", &[(auth_s1, auth_s2)]) .await?; let Share(r3, Auth(mac_r3_key_s3)) = a .recv_from::(fpre_party, "AND shares") @@ -470,10 +470,10 @@ mod tests { a.send_to( fpre_party, "AND shares", - &vec![(auth_r1_corrupted, auth_r2)], + &[(auth_r1_corrupted, auth_r2)], ) .await?; - b.send_to(fpre_party, "AND shares", &vec![(auth_s1, auth_s2)]) + b.send_to(fpre_party, "AND shares", &[(auth_s1, auth_s2)]) .await?; assert_eq!( a.recv_from::(fpre_party, "AND shares").await?, @@ -491,10 +491,10 @@ mod tests { a.send_to( fpre_party, "AND shares", - &vec![(auth_r1_corrupted, auth_r2)], + &[(auth_r1_corrupted, auth_r2)], ) .await?; - b.send_to(fpre_party, "AND shares", &vec![(auth_s1, auth_s2)]) + b.send_to(fpre_party, "AND shares", &[(auth_s1, auth_s2)]) .await?; assert_eq!( a.recv_from::(fpre_party, "AND shares").await?.len(), diff --git a/src/protocol.rs b/src/protocol.rs index 09e5ac2..a21f185 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -343,7 +343,7 @@ pub async fn mpc( .recv_from(p_fpre, "delta") .await? .pop() - .ok_or_else(|| Error::EmptyMsg)?; + .ok_or(Error::EmptyMsg)?; } else { for p in (0..p_max).filter(|p| *p != p_own) { let (d, u, b, w) = generate_ots(secret_bits_ot + 3 * faand_len).await.unwrap();