Skip to content

Commit

Permalink
Merge pull request #27 from encryptogroup/7-improve-aby2-implementation
Browse files Browse the repository at this point in the history
7 improve aby2 implementation
  • Loading branch information
robinhundt authored May 22, 2024
2 parents cbb681f + 360ba23 commit d337ba3
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 101 deletions.
58 changes: 53 additions & 5 deletions crates/seec-channel/src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{multi, sub_channel, tcp, CommunicationError, Receiver, ReceiverT, Sender, SenderT};
use crate::{
channel, multi, sub_channel, tcp, CommunicationError, Receiver, ReceiverT, Sender, SenderT,
};
use async_trait::async_trait;
use futures::future::join;
use futures::stream::FuturesUnordered;
Expand Down Expand Up @@ -54,14 +56,14 @@ pub struct MultiReceiver<T> {
}

impl<T: RemoteSend + Clone> MultiSender<T> {
pub async fn send_to(&self, to: impl IntoIterator<Item = &u32>, msg: T) -> Result<(), Error> {
pub async fn send_to(&self, to: impl IntoIterator<Item = u32>, msg: T) -> Result<(), Error> {
let mut fu = FuturesUnordered::new();
for to in to {
debug!(to, "Sending");
let sender = self
.senders
.get(to)
.ok_or_else(|| Error::UnknownParty(*to))?;
.get(&to)
.ok_or_else(|| Error::UnknownParty(to))?;
fu.push(sender.send(msg.clone()));
}
let mut errors = vec![];
Expand All @@ -81,7 +83,7 @@ impl<T: RemoteSend + Clone> MultiSender<T> {

#[instrument(level = "debug", skip(self, msg), ret)]
pub async fn send_all(&self, msg: T) -> Result<(), Error> {
self.send_to(self.senders.keys(), msg).await
self.send_to(self.senders.keys().copied(), msg).await
}

pub fn sender(&self, to: u32) -> Option<&Sender<T>> {
Expand All @@ -100,6 +102,14 @@ pub struct MsgFrom<T> {
}

impl<T: RemoteSend> MultiReceiver<T> {
pub async fn recv_from_single(&mut self, from: u32) -> Result<T, Error> {
let receiver = self
.receivers
.get_mut(&from)
.ok_or(Error::UnknownParty(from))?;
Ok(map_recv_fut((&from, receiver)).await?.into_msg())
}

pub fn recv_from(
&mut self,
from: &HashSet<u32>,
Expand Down Expand Up @@ -337,6 +347,44 @@ where
Ok(MultiReceiver { receivers })
}

impl<T> MsgFrom<T> {
pub fn into_msg(self) -> T {
self.msg
}
}

pub fn new_local<T: RemoteSend>(parties: usize) -> Vec<(MultiSender<T>, MultiReceiver<T>)> {
let mut res: Vec<(MultiSender<T>, MultiReceiver<T>)> =
(0..parties).map(|_| Default::default()).collect();
for party in 0..parties {
for other in 0..parties {
if party == other {
continue;
}
let (sender, receiver) = channel(128);
res[party].0.senders.insert(other as u32, sender);
res[other].1.receivers.insert(party as u32, receiver);
}
}
res
}

impl<T> Default for MultiSender<T> {
fn default() -> Self {
Self {
senders: Default::default(),
}
}
}

impl<T> Default for MultiReceiver<T> {
fn default() -> Self {
Self {
receivers: Default::default(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
30 changes: 29 additions & 1 deletion crates/seec/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::error::Error;
use std::fmt::Debug;
use std::future::Future;
use std::time::Instant;
use std::{iter, mem};
use std::{iter, mem, vec};

use seec_channel::{Receiver, Sender};
use tracing::{debug, error, info, instrument, trace};
Expand Down Expand Up @@ -42,6 +42,7 @@ pub type DynFDSetup<'c, P, Idx> = Box<
+ 'c,
>;

#[derive(Debug, Clone)]
pub struct GateOutputs<Shares> {
data: Vec<Input<Shares>>,
// Used as a sanity check in debug builds. Stores for which gates we have set the output,
Expand Down Expand Up @@ -625,6 +626,33 @@ impl<Shares: Clone> GateOutputs<Shares> {
}
}

impl<S> Default for GateOutputs<S> {
fn default() -> Self {
Self {
data: vec![],
output_set: Default::default(),
}
}
}

impl<Shares> IntoIterator for GateOutputs<Shares> {
type Item = Output<Shares>;
type IntoIter = vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}

impl<Shares> FromIterator<Input<Shares>> for GateOutputs<Shares> {
fn from_iter<T: IntoIterator<Item = Input<Shares>>>(iter: T) -> Self {
Self {
data: iter.into_iter().collect(),
output_set: Default::default(),
}
}
}

#[cfg(test)]
mod tests {
use crate::circuit::base_circuit::BaseGate;
Expand Down
21 changes: 13 additions & 8 deletions crates/seec/src/private_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,33 @@ use crate::circuit::ExecutableCircuit;
use crate::circuit::{BaseCircuit, BooleanGate, GateIdx};
use crate::common::BitVec;
use crate::executor::{Executor, Input};
use crate::mul_triple::MTProvider;
use crate::mul_triple::{arithmetic, boolean};
use crate::protocols::arithmetic_gmw::{AdditiveSharing, ArithmeticGmw};
use crate::protocols::boolean_gmw::{BooleanGmw, XorSharing};
use crate::protocols::mixed_gmw::{MixedGmw, MixedShareStorage, MixedSharing};
use crate::protocols::{mixed_gmw, Gate, Protocol, Ring, ScalarDim, Share, Sharing};
use crate::protocols::{
mixed_gmw, FunctionDependentSetup, Gate, Protocol, Ring, ScalarDim, Share, Sharing,
};

pub trait ProtocolTestExt: Protocol + Default {
type InsecureSetup: MTProvider<Output = Self::SetupStorage, Error = Infallible>
+ Default
type InsecureSetup<Idx: GateIdx>: FunctionDependentSetup<
Self::ShareStorage,
Self::Gate,
Idx,
Output = Self::SetupStorage,
Error = Infallible,
> + Default
+ Clone
+ Send
+ Sync;
}

impl ProtocolTestExt for BooleanGmw {
type InsecureSetup = boolean::insecure_provider::InsecureMTProvider;
type InsecureSetup<Idx: GateIdx> = boolean::insecure_provider::InsecureMTProvider;
}

impl<R: Ring> ProtocolTestExt for ArithmeticGmw<R> {
type InsecureSetup = arithmetic::insecure_provider::InsecureMTProvider<R>;
type InsecureSetup<Idx: GateIdx> = arithmetic::insecure_provider::InsecureMTProvider<R>;
}

impl<R> ProtocolTestExt for MixedGmw<R>
Expand All @@ -62,7 +68,7 @@ where
Standard: Distribution<R>,
[R; 1]: BitViewSized,
{
type InsecureSetup = mixed_gmw::InsecureMixedSetup<R>;
type InsecureSetup<Idx: GateIdx> = mixed_gmw::InsecureMixedSetup<R>;
}

pub fn create_and_tree(depth: u32) -> BaseCircuit {
Expand Down Expand Up @@ -316,7 +322,6 @@ where
P: ProtocolTestExt<ShareStorage = S::Shared>,
<P::Gate as Gate>::Share: Share<SimdShare = P::ShareStorage>,
Idx: GateIdx,
<P::InsecureSetup as MTProvider>::Error: Debug,
<P as Protocol>::ShareStorage: Send + Sync,
{
let mt_provider = P::InsecureSetup::default();
Expand Down
Loading

0 comments on commit d337ba3

Please sign in to comment.