Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed May 29, 2024
1 parent 33d4b3d commit 3b35279
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 144 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![deny(unsafe_code)]
#![allow(refining_impl_trait)]
#![allow(dead_code)]

#![feature(async_fn_traits)]

mod algebra;
Expand All @@ -11,6 +10,6 @@ mod protocols;
pub mod schemes;

mod help;
pub mod marker;
#[cfg(test)]
mod testing;
pub mod marker;
35 changes: 26 additions & 9 deletions src/marker/exptree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use std::{collections::BTreeMap, ops::AsyncFn, sync::{atomic::{AtomicU32, Ordering::SeqCst}, Mutex}};
use crate::net::Communicate;
use std::{
collections::BTreeMap,
ops::AsyncFn,
sync::{
atomic::{AtomicU32, Ordering::SeqCst},
Mutex,
},
};
#[derive(Clone)]
pub enum Status<T> {
Verified,
Expand All @@ -23,7 +30,10 @@ impl<T: Clone> ExpTree<T> {

pub fn add_dependent(&self, a: u32, b: u32) -> u32 {
let new = self.issue();
let status : Status<T> = Status::Unverified { parents: (a, b), data: None };
let status: Status<T> = Status::Unverified {
parents: (a, b),
data: None,
};
{
let mut tree = self.tree.lock().unwrap();
tree.insert(new, status);
Expand All @@ -34,7 +44,10 @@ impl<T: Clone> ExpTree<T> {
pub fn add_dependent_with(&self, a: u32, b: u32, data: T) -> u32 {
let new = self.issue();
let data = Some(data);
let status = Status::Unverified { parents: (a, b), data };
let status = Status::Unverified {
parents: (a, b),
data,
};
{
let mut tree = self.tree.lock().unwrap();
tree.insert(new, status);
Expand All @@ -53,7 +66,9 @@ impl<T: Clone> ExpTree<T> {
}

pub async fn verify<F, C>(&mut self, id: u32, verify_fn: F, coms: C) -> Option<bool>
where F: AsyncFn(Vec<T>, C) -> Option<bool>, C: Communicate
where
F: AsyncFn(Vec<T>, C) -> Option<bool>,
C: Communicate,
{
let mut to_check = vec![];
let mut datas: Vec<T> = vec![];
Expand All @@ -63,11 +78,11 @@ impl<T: Clone> ExpTree<T> {
let tree = self.tree.get_mut().unwrap();

while let Some(id) = checking.pop() {
let t = tree.get(&id)?;
let t = tree.get(&id)?;
match t {
Status::Failure => todo!("Taint dependent values -or- crash and burn"),
Status::Verified => (),
Status::Unverified{parents, data} => {
Status::Unverified { parents, data } => {
checking.push(parents.0);
checking.push(parents.1);
if let Some(data) = data {
Expand All @@ -79,14 +94,16 @@ impl<T: Clone> ExpTree<T> {
}

let res = verify_fn.async_call((datas, coms)).await?;
let status = if res { Status::Verified } else { Status::Failure };
let status = if res {
Status::Verified
} else {
Status::Failure
};

for id in to_check {
tree.insert(id, status.clone());
}

Some(res)

}

}
64 changes: 46 additions & 18 deletions src/marker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod exptree;
use rand::RngCore;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{net::Communicate, schemes::{interactive::InteractiveShared, Shared, Verify}};

use crate::{
net::Communicate,
schemes::{interactive::InteractiveShared, Shared, Verify},
};

#[repr(transparent)]
#[derive(Serialize, Debug, Clone)]
Expand All @@ -23,7 +25,6 @@ impl<'de, S: Shared + DeserializeOwned> Deserialize<'de> for Unverified<S> {
}
}


impl<S: Verify> Unverified<S> {
pub async fn verify(self, coms: impl Communicate, args: S::Args) -> Option<Verified<S>> {
if self.0.verify(coms, args).await {
Expand All @@ -40,26 +41,42 @@ impl<S> Unverified<S> {
}
}


impl<S: InteractiveShared> Verified<S> {
pub async fn open(self, ctx: &S::Context, coms: impl Communicate) -> Result<S::Value, S::Error> {
pub async fn open(
self,
ctx: &S::Context,
coms: impl Communicate,
) -> Result<S::Value, S::Error> {
S::recombine(ctx, self.0, coms).await
}

pub async fn share(val: S::Value, ctx: &S::Context, rng: impl RngCore + Send, coms: impl Communicate) -> Result<Self, S::Error> {
pub async fn share(
val: S::Value,
ctx: &S::Context,
rng: impl RngCore + Send,
coms: impl Communicate,
) -> Result<Self, S::Error> {
let s = S::share(ctx, val, rng, coms).await?;
Ok(Self(s))
}

}

impl<S: InteractiveShared> Unverified<S> {
pub async fn share_symmetric(val: S::Value, ctx: &S::Context, rng: impl RngCore + Send, coms: impl Communicate) -> Result<Vec<Self>, S::Error> {
pub async fn share_symmetric(
val: S::Value,
ctx: &S::Context,
rng: impl RngCore + Send,
coms: impl Communicate,
) -> Result<Vec<Self>, S::Error> {
let s = S::symmetric_share(ctx, val, rng, coms).await?;
Ok(s.into_iter().map(Self).collect())
}

pub async fn receive_share(ctx: &S::Context, coms: impl Communicate, from: usize) -> Result<Self, S::Error> {
pub async fn receive_share(
ctx: &S::Context,
coms: impl Communicate,
from: usize,
) -> Result<Self, S::Error> {
let s = S::receive_share(ctx, coms, from).await?;
Ok(Self(s))
}
Expand All @@ -77,22 +94,27 @@ impl<T> From<Verified<Vec<T>>> for Vec<Verified<T>> {
}
}


impl<S: Verify> Unverified<Vec<S>> {
pub async fn verify_all(self, coms: impl Communicate, args: S::Args) -> Verified<Vec<Option<S>>> {
pub async fn verify_all(
self,
coms: impl Communicate,
args: S::Args,
) -> Verified<Vec<Option<S>>> {
let res = S::verify_many(&self.0, coms, args).await;
let res = res.into_iter().zip(self.0).map(|(verified, t)| {
verified.then_some(t)
}).collect();
let res = res
.into_iter()
.zip(self.0)
.map(|(verified, t)| verified.then_some(t))
.collect();
Verified(res)
}
}

// Pure boring manual operator implementations
// Could be done with some macros instead.
mod ops {
use std::ops::{Add, Mul, Sub};
use crate::schemes::Shared;
use std::ops::{Add, Mul, Sub};

use super::*;

Expand Down Expand Up @@ -144,15 +166,21 @@ mod ops {
}
}

impl<S: Shared> Mul<S::Value> for Verified<S> where S: Mul<S::Value, Output=S> {
impl<S: Shared> Mul<S::Value> for Verified<S>
where
S: Mul<S::Value, Output = S>,
{
type Output = Self;

fn mul(self, rhs: S::Value) -> Self::Output {
Self(self.0 * rhs)
}
}

impl<S: Shared> Mul<S::Value> for Unverified<S> where S: Mul<S::Value, Output=S> {
impl<S: Shared> Mul<S::Value> for Unverified<S>
where
S: Mul<S::Value, Output = S>,
{
type Output = Self;

fn mul(self, rhs: S::Value) -> Self::Output {
Expand All @@ -179,7 +207,7 @@ mod test {
me: 0,
};
let mut rng = rngs::mock::StepRng::new(0, 0);
let s = <mock::Share<Mod11> as Shared>::share(&ctx, Mod11(3), &mut rng);
let s = <mock::Share<Mod11> as Shared>::share(&ctx, Mod11(3), &mut rng);
let s = Verified(s[0]);
let s0 = s.clone();
let s = s0 + s;
Expand Down
8 changes: 5 additions & 3 deletions src/net/agency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ impl<'a, B: Broadcast> Broadcast for &'a mut B {
msg: T,
) -> impl Future<Output = Result<Vec<T>, Self::BroadcastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync {
(**self).symmetric_broadcast(msg)
T: serde::Serialize + serde::de::DeserializeOwned + Sync,
{
(**self).symmetric_broadcast(msg)
}

fn recv_from<T: serde::de::DeserializeOwned>(
Expand Down Expand Up @@ -166,7 +167,8 @@ impl<'a, U: Unicast> Unicast for &'a mut U {
msgs: Vec<T>,
) -> impl Future<Output = Result<Vec<T>, Self::UnicastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync {
T: serde::Serialize + serde::de::DeserializeOwned + Sync,
{
(**self).symmetric_unicast(msgs)
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/net/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,25 @@ impl<R: AsyncRead, W: AsyncWrite> Connection<R, W> {
}
}


pub struct Sending<W: AsyncWrite>(FramedWrite<W, LengthDelimitedCodec>);
pub struct Receiving<R: AsyncRead>(FramedRead<R, LengthDelimitedCodec>);


impl<W: AsyncWrite + Unpin + Send> SendBytes for Sending<W> {
type SendError = ConnectionError;

async fn send_bytes(&mut self, bytes: Bytes) -> Result<(), Self::SendError> {
SinkExt::<_>::send(&mut self.0, bytes).await.map_err(|_| ConnectionError::Closed)
SinkExt::<_>::send(&mut self.0, bytes)
.await
.map_err(|_| ConnectionError::Closed)
}
}

impl<R: AsyncRead + Unpin + Send> RecvBytes for Receiving<R> {
type RecvError = ConnectionError;

async fn recv_bytes(&mut self) -> Result<BytesMut, Self::RecvError> {
self.0.next()
self.0
.next()
.await
.ok_or(ConnectionError::Closed)?
.map_err(|e| ConnectionError::Unknown(Box::new(e)))
Expand All @@ -117,7 +118,6 @@ impl<R: AsyncRead + Unpin + Send, W: AsyncWrite + Unpin + Send> Connection<R, W>
pub async fn recv<T: serde::de::DeserializeOwned>(&mut self) -> Result<T, ConnectionError> {
self.receiver.recv().await
}

}

impl<
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<
}
}

impl<R: AsyncRead + Unpin + Send, W: AsyncWrite + Unpin + Send> Channel for Connection<R,W> {
impl<R: AsyncRead + Unpin + Send, W: AsyncWrite + Unpin + Send> Channel for Connection<R, W> {
type Error = ConnectionError;
}

Expand Down
15 changes: 3 additions & 12 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod connection;
pub mod mux;
pub mod network;


pub trait SendBytes: Send {
type SendError: Error + Send + Sync + 'static;

Expand Down Expand Up @@ -39,7 +38,6 @@ impl<S: SendBytes> SendBytes for &mut S {
}
}


pub trait RecvBytes: Send {
type RecvError: Error + Send + Sync + 'static;
fn recv_bytes(
Expand All @@ -64,7 +62,6 @@ impl<R: RecvBytes> RecvBytes for &mut R {
) -> impl std::future::Future<Output = Result<BytesMut, Self::RecvError>> + Send {
(**self).recv_bytes()
}

}

/// A communication medium between you and another party.
Expand All @@ -77,17 +74,14 @@ impl<C: Channel> Channel for &mut C {
type Error = C::Error;
}



/// A [Channel] which can be split into a sender and receiver.
pub trait SplitChannel: Channel + Send {
type Sender: SendBytes<SendError = Self::SendError> + Send;
type Receiver: RecvBytes<RecvError = Self::RecvError> + Send;
fn split(&mut self) -> (&mut Self::Sender, &mut Self::Receiver);

}

impl<'a, C: SplitChannel> SplitChannel for &'a mut C {
impl<'a, C: SplitChannel> SplitChannel for &'a mut C {
type Sender = C::Sender;
type Receiver = C::Receiver;

Expand All @@ -96,7 +90,6 @@ impl<'a, C: SplitChannel> SplitChannel for &'a mut C {
}
}


/// Tune to a specific channel
pub trait Tuneable {
type TuningError: Error + Send + 'static;
Expand Down Expand Up @@ -138,8 +131,6 @@ impl<'a, R: Tuneable + ?Sized> Tuneable for &'a mut R {
}
}


/// General communication with support for most network functionality.
pub trait Communicate : agency::Broadcast + agency::Unicast + Tuneable + Send {}
impl<'a, C: Communicate> Communicate for &'a mut C { }

pub trait Communicate: agency::Broadcast + agency::Unicast + Tuneable + Send {}
impl<'a, C: Communicate> Communicate for &'a mut C {}
4 changes: 1 addition & 3 deletions src/net/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use tokio::join;

use crate::{
help,
net::{
network::Network, Channel, RecvBytes, SendBytes, SplitChannel
},
net::{network::Network, Channel, RecvBytes, SendBytes, SplitChannel},
};

#[derive(Debug, Error)]
Expand Down
Loading

0 comments on commit 3b35279

Please sign in to comment.