Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed Jan 31, 2024
1 parent 66d9d81 commit 7987433
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 143 deletions.
28 changes: 13 additions & 15 deletions src/algebra/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! values.
//!

use ff::{Field};
use ff::Field;
use rayon::prelude::*;

// TODO: Consider smallvec or tinyvec
Expand Down Expand Up @@ -101,7 +101,6 @@ impl<F: Send + Sync> FromParallelIterator<F> for Vector<F> {
}
}


impl<T: Send + Sync> AsRef<[T]> for Vector<T> {
fn as_ref(&self) -> &[T] {
&self.0
Expand Down Expand Up @@ -279,7 +278,6 @@ where
}
}


/// Generic implementation of row-wise multiplication
///
/// Vector<A> * B -> Vector<B>
Expand Down Expand Up @@ -310,7 +308,7 @@ where
///
/// Vector<A> * B -> Vector<A>
/// if A *= B exists
///
///
impl<A: Send + Sync, B: Send + Sync> std::ops::Mul<B> for Vector<A>
where
for<'b> A: std::ops::MulAssign<&'b B>,
Expand All @@ -323,7 +321,6 @@ where
}
}


/// Generic implemetantion of sum
///
/// Sum(...Vector<A>) -> Vector<A>
Expand All @@ -342,23 +339,24 @@ where
}
}


impl<F: Field> Vector<F> {
pub fn inner_product(&self, other: &Self) -> F {
self.par_iter().zip(other).map(|(&a, &b)| a*b).sum()
self.par_iter().zip(other).map(|(&a, &b)| a * b).sum()
}
}

pub fn lagrange_coefficients<F: Field>(xs: &[F], x: F) -> Vec<F> {
xs.iter().map(|&i| {
let mut prod = F::ONE;
for &m in xs.iter() {
if m != i {
prod *= x - m * (i - m).invert().unwrap_or(F::ZERO);
xs.iter()
.map(|&i| {
let mut prod = F::ONE;
for &m in xs.iter() {
if m != i {
prod *= x - m * (i - m).invert().unwrap_or(F::ZERO);
}
}
}
prod
}).collect()
prod
})
.collect()
}

#[cfg(test)]
Expand Down
13 changes: 6 additions & 7 deletions src/extra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ use std::error::Error;
use crate::net::{agency::Broadcast, Channel};
use rand::Rng;

pub struct CoinToss<Rng: rand::RngCore>{
pub struct CoinToss<Rng: rand::RngCore> {
rng: Rng,
// hashing_alg: D,
}


impl<Rng: rand::RngCore> CoinToss<Rng> {

pub fn new(rng: Rng) -> Self {
Self { rng }
}

/// NOT COMPLETE
pub async fn toss<const N: usize>(&mut self, cx: &mut impl Broadcast) -> Result<[u8; N], Box<dyn Error>> {
pub async fn toss<const N: usize>(
&mut self,
cx: &mut impl Broadcast,
) -> Result<[u8; N], Box<dyn Error>> {
let mut my_seed = [0u8; N];
self.rng.fill_bytes(&mut my_seed);
// TODO: Commitment protocol
let my_seed : Box<[u8]> = Box::new(my_seed);
let my_seed: Box<[u8]> = Box::new(my_seed);
let seeds = cx.symmetric_broadcast(my_seed).await.unwrap();

fn xor(acc: &mut [u8], ins: &[u8]) {
Expand All @@ -32,5 +33,3 @@ impl<Rng: rand::RngCore> CoinToss<Rng> {
Ok(acc)
}
}


4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
mod algebra;
mod extra;
pub mod net;
pub mod ot;
pub mod poly;
pub mod schemes;
pub mod theater;
mod testing;
pub mod ot;
pub mod theater;
5 changes: 2 additions & 3 deletions src/net/agency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ use itertools::Itertools;
// One could still be for the broadcast/unicast operations to have a `size` function
// which gives the current network size. However I am not sure if this will be relevant?


pub trait Broadcast {
type Error : Error;
type Error: Error;

fn broadcast(&mut self, msg: &impl serde::Serialize);

Expand Down Expand Up @@ -82,7 +81,7 @@ use digest::Digest;
// There is also the question if we should overload the existing methods or provide
// new methods prefixed with 'verified' or something.

trait VerifiedBroadcast<D: Digest>: Broadcast {
pub trait VerifiedBroadcast<D: Digest>: Broadcast {
/// Ensure that a received broadcast is the same across all parties.
async fn symmetric_broadcast<T: AsRef<[u8]>>(
&mut self,
Expand Down
20 changes: 12 additions & 8 deletions src/net/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ pub struct Connection<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> {
did_flush: tokio::sync::mpsc::Receiver<()>,
}


impl<R: AsyncRead + Unpin + Send + 'static, W: AsyncWrite + Unpin + Send + 'static> Connection<R, W> {
impl<R: AsyncRead + Unpin + Send + 'static, W: AsyncWrite + Unpin + Send + 'static>
Connection<R, W>
{
/// Construct a new connection from a reader and writer
/// Messages are serialized with bincode and length delimated.
///
Expand Down Expand Up @@ -89,7 +90,6 @@ impl<R: AsyncRead + Unpin + Send + 'static, W: AsyncWrite + Unpin + Send + 'stat
}

impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> Connection<R, W> {

/// Destroy the connection, returning the internal reader and writer.
pub async fn destroy(self) -> Result<(R, W), ConnectionError> {
let Self {
Expand All @@ -110,7 +110,10 @@ impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> Connection<R, W> {
}

pub async fn flush(&mut self) -> Result<(), ConnectionError> {
self.flush.send(()).await.map_err(|_| ConnectionError::Closed)?;
self.flush
.send(())
.await
.map_err(|_| ConnectionError::Closed)?;
self.did_flush.recv().await;
Ok(())
}
Expand Down Expand Up @@ -142,12 +145,15 @@ impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> Connection<R, W> {
/// Receive a message waiting for arrival
pub async fn recv<T: serde::de::DeserializeOwned>(&mut self) -> Result<T, ConnectionError> {
// TODO: Handle timeouts?
let buf = self.reader.next().await.ok_or(ConnectionError::Closed)?
let buf = self
.reader
.next()
.await
.ok_or(ConnectionError::Closed)?
.map_err(|e| ConnectionError::Unknown(Box::new(e)))?;
let buf = std::io::Cursor::new(buf);
bincode::deserialize_from(buf).map_err(ConnectionError::BadSerialization)
}

}

pub type TcpConnection = Connection<OwnedReadHalf, OwnedWriteHalf>;
Expand All @@ -168,7 +174,6 @@ impl TcpConnection {
// But just don't do that.
Ok(r.reunite(w).expect("TCP Streams didn't match"))
}

}

pub type TlsConnection = Connection<
Expand All @@ -183,7 +188,6 @@ impl TlsConnection {
let (reader, writer) = tokio::io::split(stream);
Self::new(reader, writer)
}

}

/// Connection to a in-memory data stream.
Expand Down
36 changes: 24 additions & 12 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,61 @@ use std::ops::Index;

use futures::Future;

use crate::net::{agency::{Broadcast, Unicast}, connection::{ConnectionError, Connection}, network::Network};
use crate::net::{
agency::{Broadcast, Unicast},
connection::{Connection, ConnectionError},
network::Network,
};

pub mod agency;
pub mod connection;
pub mod network;



// TODO: Serde trait bounds on `T`
// TODO: Properly use this trait for other things (Connection/Agency etc.)
pub trait Channel {
type Error;

fn send<T: serde::Serialize>(&self, msg: &T) -> impl Future<Output = Result<(), Self::Error>>;

fn recv<T: serde::de::DeserializeOwned>(&mut self) -> impl Future<Output = Result<T, Self::Error>>;
fn recv<T: serde::de::DeserializeOwned>(
&mut self,
) -> impl Future<Output = Result<T, Self::Error>>;
}


impl<R: tokio::io::AsyncRead + std::marker::Unpin, W: tokio::io::AsyncWrite + std::marker::Unpin> Channel for Connection<R,W> {
impl<
R: tokio::io::AsyncRead + std::marker::Unpin,
W: tokio::io::AsyncWrite + std::marker::Unpin,
> Channel for Connection<R, W>
{
type Error = ConnectionError;

fn send<T: serde::Serialize>(&self, msg: &T) -> impl Future<Output = Result<(), Self::Error>> {
async {todo!()}
async { todo!() }
}

fn recv<T: serde::de::DeserializeOwned>(&mut self) -> impl Future<Output = Result<T, Self::Error>> {
fn recv<T: serde::de::DeserializeOwned>(
&mut self,
) -> impl Future<Output = Result<T, Self::Error>> {
Connection::recv(self)
}
}


pub trait ChannelStation : Broadcast + Unicast {
pub trait ChannelStation: Broadcast + Unicast {
type Error;
type SubChannel : Channel;
type SubChannel: Channel;
type Idx;

fn tune_mut(&mut self, idx: Self::Idx) -> &mut Self::SubChannel;

fn tune(&self, idx: Self::Idx) -> &Self::SubChannel;
}

impl<R: tokio::io::AsyncRead + std::marker::Unpin, W: tokio::io::AsyncWrite + std::marker::Unpin> ChannelStation for Network<R, W> {
impl<
R: tokio::io::AsyncRead + std::marker::Unpin,
W: tokio::io::AsyncWrite + std::marker::Unpin,
> ChannelStation for Network<R, W>
{
type Error = ConnectionError;
type SubChannel = Connection<R, W>;
type Idx = usize;
Expand Down
Loading

0 comments on commit 7987433

Please sign in to comment.