diff --git a/src/algebra/math.rs b/src/algebra/math.rs index 1c535c6..5c1d9cc 100644 --- a/src/algebra/math.rs +++ b/src/algebra/math.rs @@ -27,7 +27,7 @@ use ff::Field; /// /// If the rayon feature is enabled the operations will be parallelized. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct Vector(Box<[F]>); +pub struct Vector(pub(super) Box<[F]>); impl Vector { pub const fn from_boxed_slice(slice: Box<[F]>) -> Self { diff --git a/src/algebra/mod.rs b/src/algebra/mod.rs index 7be3bb3..58a1895 100644 --- a/src/algebra/mod.rs +++ b/src/algebra/mod.rs @@ -2,4 +2,4 @@ pub mod element; pub mod field; pub mod math; pub mod poly; -mod rayon; +pub mod rayon; diff --git a/src/algebra/rayon.rs b/src/algebra/rayon.rs index c6fb876..c5ece82 100644 --- a/src/algebra/rayon.rs +++ b/src/algebra/rayon.rs @@ -2,8 +2,23 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Sub, SubAssign}; use rayon::prelude::*; +use crate::algebra::math; + #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -struct Vector(Box<[F]>); +#[repr(transparent)] +pub struct Vector(math::Vector); + +impl From> for Vector { + fn from(value: math::Vector) -> Self { + Self(value) + } +} + +impl From> for math::Vector { + fn from(val: Vector) -> Self { + val.0 + } +} macro_rules! inherent { ($trait2:ident, $fun2:ident, $trait1:ident, $fun1:ident) => { @@ -13,6 +28,7 @@ macro_rules! inherent { { fn $fun1(&mut self, rhs: &Self) { self.0 + .0 .par_iter_mut() .zip(rhs.0.par_iter()) .for_each(|(a, b)| $trait1::$fun1(a, b)); @@ -71,8 +87,8 @@ inherent!(Sub, sub, SubAssign, sub_assign); inherent!(Div, div, DivAssign, div_assign); impl super::math::Vector { - fn parallelize(self) -> Vector { - Vector(self.into_boxed_slice()) + pub fn parallelize(self) -> Vector { + Vector(self) } } @@ -90,8 +106,8 @@ impl FromParallelIterator for Vector { where I: IntoParallelIterator, { - let boxed = par_iter.into_par_iter().collect(); - Self(boxed) + let boxed: Box<[_]> = par_iter.into_par_iter().collect(); + Self(math::Vector(boxed)) } } @@ -106,7 +122,7 @@ where { fn mul_assign(&mut self, rhs: &B) { let b = rhs; - self.0.par_iter_mut().for_each(|a| *a *= b); + self.0 .0.par_iter_mut().for_each(|a| *a *= b); } } @@ -127,8 +143,8 @@ where fn mul(self, rhs: B) -> Self::Output { let b = rhs; - let internal = self.0.par_iter().map(|a| a * &b).collect(); - Vector(internal) + let internal: Box<[_]> = self.0 .0.par_iter().map(|a| a * &b).collect(); + Vector(math::Vector(internal)) } } diff --git a/src/net/agency.rs b/src/net/agency.rs index 76960db..5c1a608 100644 --- a/src/net/agency.rs +++ b/src/net/agency.rs @@ -29,9 +29,8 @@ use itertools::Itertools; pub trait Broadcast: Send { type BroadcastError: Error + Send + Sync + 'static; - // type Error: Error + 'static; - /// Broadcast a message to all other parties. + /// Broadcast a single message to all other parties. /// /// * `msg`: Message to send /// @@ -41,7 +40,7 @@ pub trait Broadcast: Send { msg: &(impl serde::Serialize + Sync), ) -> impl std::future::Future> + Send; - /// Broadcast a message to all parties and await their messages + /// Broadcast a single message to all parties and await their messages /// Messages are ordered by their index. /// /// This function is symmetric, and as such it is expected that all other parties @@ -100,11 +99,11 @@ impl<'a, B: Broadcast> Broadcast for &'a mut B { } } -// TODO: Possible rename this trait as it's name is confusing. +/// Uni(que) cast to every party pub trait Unicast { type UnicastError: Error + Send + 'static; - /// Unicast messages to each party + /// Send a unique message to each party /// /// Messages are supposed to be in order, meaning message `i` /// will be send to party `i`, skipping your own index. @@ -117,7 +116,7 @@ pub trait Unicast { msgs: &[impl serde::Serialize + Send + Sync], ) -> impl std::future::Future> + Send; - /// Unicast a message to each party and await their messages + /// Send a unique message to each party and await their messages /// Messages are supposed to be in order, meaning message `i` /// will be send to party `i`. /// @@ -129,7 +128,7 @@ pub trait Unicast { where T: serde::Serialize + serde::de::DeserializeOwned + Send + Sync; - /// Receive a message for each party. + /// Receive a message from each party. /// /// Asymmetric, waiting /// diff --git a/src/net/mod.rs b/src/net/mod.rs index 0b41613..b33cbfa 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -3,7 +3,6 @@ use std::{error::Error, future::Future}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use thiserror::Error; use tokio_util::bytes::{Bytes, BytesMut}; -use tracing::instrument; pub mod agency; pub mod connection; diff --git a/src/net/network.rs b/src/net/network.rs index 7e657d8..7c5577e 100644 --- a/src/net/network.rs +++ b/src/net/network.rs @@ -389,7 +389,7 @@ impl Network { impl Unicast for Network { type UnicastError = NetworkError; - #[tracing::instrument(skip(msgs))] + #[tracing::instrument(level = "trace", skip(msgs))] async fn unicast( &mut self, msgs: &[impl serde::Serialize + Sync], @@ -397,7 +397,7 @@ impl Unicast for Network { self.unicast(msgs).await } - #[tracing::instrument(skip(msgs))] + #[tracing::instrument(level = "debug", skip(msgs))] async fn symmetric_unicast(&mut self, msgs: Vec) -> Result, Self::UnicastError> where T: serde::Serialize + serde::de::DeserializeOwned + Sync, @@ -405,7 +405,7 @@ impl Unicast for Network { self.symmetric_unicast(msgs).await } - #[tracing::instrument] + #[tracing::instrument(level = "trace")] async fn receive_all( &mut self, ) -> Result, Self::UnicastError> { @@ -420,7 +420,7 @@ impl Unicast for Network { impl Broadcast for Network { type BroadcastError = NetworkError; - #[tracing::instrument(skip(msg))] + #[tracing::instrument(level = "trace", skip(msg))] async fn broadcast( &mut self, msg: &(impl serde::Serialize + Sync), @@ -428,7 +428,7 @@ impl Broadcast for Network { self.broadcast(msg).await } - #[tracing::instrument(skip(msg))] + #[tracing::instrument(level = "trace", skip(msg))] async fn symmetric_broadcast(&mut self, msg: T) -> Result, Self::BroadcastError> where T: serde::Serialize + serde::de::DeserializeOwned + Sync, @@ -436,7 +436,7 @@ impl Broadcast for Network { self.symmetric_broadcast(msg).await } - #[tracing::instrument] + #[tracing::instrument(level = "trace")] fn recv_from( &mut self, id: Id, @@ -456,7 +456,7 @@ impl Tuneable for Network { self.id() } - #[tracing::instrument] + #[tracing::instrument(level = "trace")] async fn recv_from( &mut self, id: Id, @@ -471,7 +471,7 @@ impl Tuneable for Network { }) } - #[tracing::instrument(skip(msg))] + #[tracing::instrument(level = "trace", skip(msg))] async fn send_to( &mut self, id: Id, @@ -537,6 +537,7 @@ impl InMemoryNetwork { networks } + #[tracing::instrument(level = "trace")] pub async fn shutdown(self) -> Result<(), std::io::Error> { let futs = self .connections @@ -580,6 +581,7 @@ impl TcpNetwork { /// /// * `me`: Socket address to open to /// * `peers`: Socket addresses of other peers + #[tracing::instrument] pub async fn connect(me: SocketAddr, peers: &[SocketAddr]) -> NetResult { let n = peers.len(); @@ -628,6 +630,7 @@ impl TcpNetwork { Ok(network) } + #[tracing::instrument] pub async fn shutdown(self) -> Result<(), std::io::Error> { let futs = self .connections